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

View File

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

View File

@@ -6,7 +6,6 @@ import (
"os" "os"
"reflect" "reflect"
"strconv" "strconv"
"strings"
) )
// The options that the user can set // 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 // 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" filename := configDir + "/settings.json"
if len(args) == 2 { if _, ok := settings[option]; !ok {
option := strings.TrimSpace(args[0]) messenger.Error(option + " is not a valid option")
value := strings.TrimSpace(args[1]) return
}
if _, ok := settings[option]; !ok { kind := reflect.TypeOf(settings[option]).Kind()
messenger.Error(option + " is not a valid option") if kind == reflect.Bool {
return b, err := ParseBool(value)
}
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)
if err != nil { if err != nil {
messenger.Error("Error writing to settings.json: " + err.Error()) messenger.Error("Invalid value for " + option)
return return
} }
} else { settings[option] = b
messenger.Error("No value given") } 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
} }
} }