]> git.lizzy.rs Git - micro.git/commitdiff
Add automatic settings to ~/.micro/settings.json
authorZachary Yedidia <zyedidia@gmail.com>
Tue, 29 Mar 2016 01:10:10 +0000 (21:10 -0400)
committerZachary Yedidia <zyedidia@gmail.com>
Tue, 29 Mar 2016 01:10:10 +0000 (21:10 -0400)
runtime/syntax/go.micro
src/colorscheme.go
src/cursor.go
src/highlighter.go
src/micro.go
src/option.go [deleted file]
src/settings.go [new file with mode: 0644]
src/util.go
src/view.go

index 1feff1305911d244ec28174352db9531221ccf7d..21fa52d8c82da998c38fe8f7d0cae17bc755e806 100644 (file)
@@ -1,6 +1,5 @@
 syntax "Go" "\.go$"
 
-color identifier "[A-Za-z_][A-Za-z0-9_]*[[:space:]]*[()]"
 color statement "\b(append|cap|close|complex|copy|delete|imag|len)\b"
 color statement "\b(make|new|panic|print|println|protect|real|recover)\b"
 color type     "\b(u?int(8|16|32|64)?|float(32|64)|complex(64|128))\b"
index 6b41d6694c9c24257f0b34da3271c27ad6f31181..721e243f1a51a213e7e770cbb703d1204ab713be 100644 (file)
@@ -28,7 +28,7 @@ func LoadDefaultColorscheme() {
                TermMessage("Error finding your home directory\nCan't load runtime files")
                return
        }
-       LoadColorscheme(options["colorscheme"].(string), dir+"/.micro/colorschemes")
+       LoadColorscheme(settings.Colorscheme, dir+"/.micro/colorschemes")
 }
 
 // LoadColorscheme loads the given colorscheme from a directory
index 70b7442e039708b81d2fe1b1315c727ae1d45710..8b0f56fb9fa67ddd9abe997f0badc687743e4bee 100644 (file)
@@ -257,7 +257,7 @@ func (c *Cursor) Start() {
 // GetCharPosInLine gets the char position of a visual x y coordinate (this is necessary because tabs are 1 char but 4 visual spaces)
 func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
        // Get the tab size
-       tabSize := options["tabsize"].(int)
+       tabSize := settings.TabSize
        // This is the visual line -- every \t replaced with the correct number of spaces
        visualLine := strings.Replace(c.v.buf.lines[lineNum], "\t", "\t"+Spaces(tabSize-1), -1)
        if visualPos > Count(visualLine) {
@@ -273,7 +273,7 @@ func (c *Cursor) GetCharPosInLine(lineNum, visualPos int) int {
 // GetVisualX returns the x value of the cursor in visual spaces
 func (c *Cursor) GetVisualX() int {
        runes := []rune(c.v.buf.lines[c.y])
-       tabSize := options["tabsize"].(int)
+       tabSize := settings.TabSize
        return c.x + NumOccurences(string(runes[:c.x]), '\t')*(tabSize-1)
 }
 
index e383c1aa16751010f1278ba27ba50729647cc564..f4483448247290cdfe7d8f0bcf710e5e9623e618 100644 (file)
@@ -31,12 +31,12 @@ var syntaxFiles map[[2]*regexp.Regexp]FileTypeRules
 
 // LoadSyntaxFiles loads the syntax files from the default directory ~/.micro
 func LoadSyntaxFiles() {
-       dir, err := homedir.Dir()
+       home, err := homedir.Dir()
        if err != nil {
-               TermMessage("Error finding your home directory\nCan't load runtime files")
+               TermMessage("Error finding your home directory\nCan't load syntax files")
                return
        }
-       LoadSyntaxFilesFromDir(dir + "/.micro/syntax")
+       LoadSyntaxFilesFromDir(home + "/.micro/syntax")
 }
 
 // JoinRule takes a syntax rule (which can be multiple regular expressions)
index 884272d1706f61afb8889d9cea47c30bbf725b10..ee038597d1aa773a9722d5c54e9f7ebdefa65b6d 100644 (file)
@@ -64,7 +64,7 @@ func main() {
                os.Exit(1)
        }
 
-       InitOptions()
+       InitSettings()
 
        // Should we enable true color?
        truecolor := os.Getenv("MICRO_TRUECOLOR") == "1"
diff --git a/src/option.go b/src/option.go
deleted file mode 100644 (file)
index e4ffa88..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-package main
-
-import (
-       "strconv"
-       "strings"
-)
-
-// The options that the user can set
-var options map[string]interface{}
-
-// InitOptions initializes the options map and sets all options to their default values
-func InitOptions() {
-       options = make(map[string]interface{})
-       options["tabsize"] = 4
-       options["colorscheme"] = "default"
-}
-
-// SetOption prompts the user to set an option and checks that the response is valid
-func SetOption(view *View) {
-       choice, canceled := messenger.Prompt("Option: ")
-       if !canceled {
-               split := strings.Split(choice, "=")
-               if len(split) == 2 {
-                       option := strings.TrimSpace(split[0])
-                       value := strings.TrimSpace(split[1])
-                       if _, exists := options[option]; exists {
-                               if option == "tabsize" {
-                                       tsize, err := strconv.Atoi(value)
-                                       if err != nil {
-                                               messenger.Error("Invalid value for " + option)
-                                               return
-                                       }
-                                       options[option] = tsize
-                               }
-                               if option == "colorscheme" {
-                                       options[option] = value
-                                       LoadSyntaxFiles()
-                                       view.buf.UpdateRules()
-                               }
-                       } else {
-                               messenger.Error("Option " + option + " does not exist")
-                       }
-               } else {
-                       messenger.Error("Invalid option, please use option = value")
-               }
-       }
-}
diff --git a/src/settings.go b/src/settings.go
new file mode 100644 (file)
index 0000000..e010383
--- /dev/null
@@ -0,0 +1,110 @@
+package main
+
+import (
+       "encoding/json"
+       "github.com/mitchellh/go-homedir"
+       "io/ioutil"
+       "os"
+       "strconv"
+       "strings"
+)
+
+// The options that the user can set
+var settings Settings
+
+// All the possible settings
+var possibleSettings = []string{"colorscheme", "tabsize", "autoindent"}
+
+// The Settings struct contains the settings for micro
+type Settings struct {
+       Colorscheme string `json:"colorscheme"`
+       TabSize     int    `json:"tabsize"`
+       AutoIndent  bool   `json:"autoindent"`
+}
+
+// InitSettings initializes the options map and sets all options to their default values
+func InitSettings() {
+       home, err := homedir.Dir()
+       if err != nil {
+               TermMessage("Error finding your home directory\nCan't load settings file")
+               return
+       }
+
+       filename := home + "/.micro/settings.json"
+       if _, e := os.Stat(filename); e == nil {
+               input, err := ioutil.ReadFile(filename)
+               if err != nil {
+                       TermMessage("Error reading settings.json file: " + err.Error())
+                       return
+               }
+
+               json.Unmarshal(input, &settings)
+       } else {
+               settings = DefaultSettings()
+               err := WriteSettings(filename)
+               if err != nil {
+                       TermMessage("Error writing settings.json file: " + err.Error())
+               }
+       }
+}
+
+// WriteSettings writes the settings to the specified filename as JSON
+func WriteSettings(filename string) error {
+       txt, _ := json.MarshalIndent(settings, "", "    ")
+       err := ioutil.WriteFile(filename, txt, 0644)
+       return err
+}
+
+// DefaultSettings returns the default settings for micro
+func DefaultSettings() Settings {
+       return Settings{
+               Colorscheme: "default",
+               TabSize:     4,
+               AutoIndent:  true,
+       }
+}
+
+// SetOption prompts the user to set an option and checks that the response is valid
+func SetOption(view *View) {
+       choice, canceled := messenger.Prompt("Option: ")
+
+       home, err := homedir.Dir()
+       if err != nil {
+               messenger.Error("Error finding your home directory\nCan't load settings file")
+               return
+       }
+
+       filename := home + "/.micro/settings.json"
+
+       if !canceled {
+               split := strings.Split(choice, " ")
+               if len(split) == 2 {
+                       option := strings.TrimSpace(split[0])
+                       value := strings.TrimSpace(split[1])
+
+                       if Contains(possibleSettings, option) {
+                               if option == "tabsize" {
+                                       tsize, err := strconv.Atoi(value)
+                                       if err != nil {
+                                               messenger.Error("Invalid value for " + option)
+                                               return
+                                       }
+                                       settings.TabSize = tsize
+                               } else if option == "colorscheme" {
+                                       settings.Colorscheme = value
+                                       LoadSyntaxFiles()
+                                       view.buf.UpdateRules()
+                               }
+                               err := WriteSettings(filename)
+                               if err != nil {
+                                       messenger.Error("Error writing to settings.json: " + err.Error())
+                                       return
+                               }
+                       } else {
+                               messenger.Error("Option " + option + " does not exist")
+                       }
+               } else {
+                       messenger.Error("Invalid option, please use option value")
+               }
+       }
+}
index ca2ffb05797a4852c07b675da474ddde8dbed62d..2ad5538730f69951e64db4c88a812090f1962256 100644 (file)
@@ -60,3 +60,13 @@ func IsWordChar(str string) bool {
        c := str[0]
        return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c == '_')
 }
+
+// Contains returns whether or not a string array contains a given string
+func Contains(list []string, a string) bool {
+       for _, b := range list {
+               if b == a {
+                       return true
+               }
+       }
+       return false
+}
index 55c566da8c630c028ce7b9333729c2bb3d3d41a2..6b6f76c759fa40bd6f9efcaf1b5a762a00bd1330 100644 (file)
@@ -626,7 +626,7 @@ func (v *View) DisplayView() {
 
                        if ch == '\t' {
                                screen.SetContent(x+tabchars, lineN, ' ', nil, lineStyle)
-                               tabSize := options["tabsize"].(int)
+                               tabSize := settings.TabSize
                                for i := 0; i < tabSize-1; i++ {
                                        tabchars++
                                        screen.SetContent(x+tabchars, lineN, ' ', nil, lineStyle)