Make setting options global

This commit is contained in:
Zachary Yedidia
2016-08-17 20:00:38 -07:00
parent 7474fdc499
commit e4b6a931de
3 changed files with 56 additions and 47 deletions

View File

@@ -165,12 +165,14 @@ func NewTab(args []string) {
// Set sets an option
func Set(args []string) {
// Set an option and we have to set it for every view
for _, tab := range tabs {
for _, view := range tab.views {
SetOption(view, args)
}
if len(args) < 2 {
return
}
option := strings.TrimSpace(args[0])
value := strings.TrimSpace(args[1])
SetOption(option, value)
}
// Bind creates a new keybinding

View File

@@ -262,6 +262,7 @@ func main() {
L.SetGlobal("messenger", luar.New(L, messenger))
L.SetGlobal("GetOption", luar.New(L, GetOption))
L.SetGlobal("AddOption", luar.New(L, AddOption))
L.SetGlobal("SetOption", luar.New(L, SetOption))
L.SetGlobal("BindKey", luar.New(L, BindKey))
L.SetGlobal("MakeCommand", luar.New(L, MakeCommand))
L.SetGlobal("CurView", luar.New(L, CurView))

View File

@@ -6,7 +6,6 @@ import (
"os"
"reflect"
"strconv"
"strings"
)
// The options that the user can set
@@ -90,51 +89,58 @@ func DefaultSettings() map[string]interface{} {
}
// SetOption prompts the user to set an option and checks that the response is valid
func SetOption(view *View, args []string) {
func SetOption(option, value string) {
filename := configDir + "/settings.json"
if len(args) == 2 {
option := strings.TrimSpace(args[0])
value := strings.TrimSpace(args[1])
if _, ok := settings[option]; !ok {
messenger.Error(option + " is not a valid option")
return
}
if _, ok := settings[option]; !ok {
messenger.Error(option + " is not a valid option")
return
}
kind := reflect.TypeOf(settings[option]).Kind()
if kind == reflect.Bool {
b, err := ParseBool(value)
if err != nil {
messenger.Error("Invalid value for " + option)
return
}
settings[option] = b
} else if kind == reflect.String {
settings[option] = value
} else if kind == reflect.Float64 {
i, err := strconv.Atoi(value)
if err != nil {
messenger.Error("Invalid value for " + option)
return
}
settings[option] = float64(i)
}
if option == "colorscheme" {
LoadSyntaxFiles()
view.Buf.UpdateRules()
}
if option == "statusline" {
view.ToggleStatusLine()
}
err := WriteSettings(filename)
kind := reflect.TypeOf(settings[option]).Kind()
if kind == reflect.Bool {
b, err := ParseBool(value)
if err != nil {
messenger.Error("Error writing to settings.json: " + err.Error())
messenger.Error("Invalid value for " + option)
return
}
} else {
messenger.Error("No value given")
settings[option] = b
} else if kind == reflect.String {
settings[option] = value
} else if kind == reflect.Float64 {
i, err := strconv.Atoi(value)
if err != nil {
messenger.Error("Invalid value for " + option)
return
}
settings[option] = float64(i)
}
if option == "colorscheme" {
LoadSyntaxFiles()
for _, tab := range tabs {
for _, view := range tab.views {
view.Buf.UpdateRules()
if settings["syntax"].(bool) {
view.matches = Match(view)
}
}
}
}
if option == "statusline" {
for _, tab := range tabs {
for _, view := range tab.views {
view.ToggleStatusLine()
if settings["syntax"].(bool) {
view.matches = Match(view)
}
}
}
}
err := WriteSettings(filename)
if err != nil {
messenger.Error("Error writing to settings.json: " + err.Error())
return
}
}