From 6f73172865d90877ac09cfbee2436e94d228ae38 Mon Sep 17 00:00:00 2001 From: Zachary Yedidia Date: Tue, 29 Mar 2016 20:44:33 -0400 Subject: [PATCH] Add simple commands --- src/command.go | 14 ++++++++++++++ src/messenger.go | 18 ++++++++++++------ src/micro.go | 5 ++++- src/settings.go | 49 +++++++++++++++++++++--------------------------- 4 files changed, 51 insertions(+), 35 deletions(-) create mode 100644 src/command.go diff --git a/src/command.go b/src/command.go new file mode 100644 index 00000000..0c72209e --- /dev/null +++ b/src/command.go @@ -0,0 +1,14 @@ +package main + +import ( + "strings" +) + +// HandleCommand handles input from the user +func HandleCommand(input string, view *View) { + cmd := strings.Split(input, " ")[0] + args := strings.Split(input, " ")[1:] + if cmd == "set" { + SetOption(view, args) + } +} diff --git a/src/messenger.go b/src/messenger.go index 0a90ca74..ca80b1b4 100644 --- a/src/messenger.go +++ b/src/messenger.go @@ -104,6 +104,11 @@ func (m *Messenger) Prompt(prompt string) (string, bool) { } m.HandleEvent(event) + + if m.cursorx < 0 { + // Cancel + m.hasPrompt = false + } } m.Reset() @@ -116,14 +121,18 @@ func (m *Messenger) HandleEvent(event tcell.Event) { case *tcell.EventKey: switch e.Key() { case tcell.KeyLeft: - m.cursorx-- + if m.cursorx > 0 { + m.cursorx-- + } case tcell.KeyRight: - m.cursorx++ + if m.cursorx < Count(m.response) { + m.cursorx++ + } case tcell.KeyBackspace2: if m.cursorx > 0 { m.response = string([]rune(m.response)[:Count(m.response)-1]) - m.cursorx-- } + m.cursorx-- case tcell.KeySpace: m.response += " " m.cursorx++ @@ -132,9 +141,6 @@ func (m *Messenger) HandleEvent(event tcell.Event) { m.cursorx++ } } - if m.cursorx < 0 { - m.cursorx = 0 - } } // Reset resets the messenger's cursor, message and response diff --git a/src/micro.go b/src/micro.go index ee038597..8f155fd6 100644 --- a/src/micro.go +++ b/src/micro.go @@ -146,7 +146,10 @@ func main() { os.Exit(0) } case tcell.KeyCtrlE: - SetOption(view) + input, canceled := messenger.Prompt("> ") + if !canceled { + HandleCommand(input, view) + } case tcell.KeyCtrlH: DisplayHelp() // Make sure to resize the view if the user resized the terminal while looking at the help text diff --git a/src/settings.go b/src/settings.go index e0103836..b4755e67 100644 --- a/src/settings.go +++ b/src/settings.go @@ -65,46 +65,39 @@ func DefaultSettings() Settings { } // SetOption prompts the user to set an option and checks that the response is valid -func SetOption(view *View) { - choice, canceled := messenger.Prompt("Option: ") - +func SetOption(view *View, args []string) { 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 len(args) == 2 { + option := strings.TrimSpace(args[0]) + value := strings.TrimSpace(args[1]) - 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 Contains(possibleSettings, option) { + if option == "tabsize" { + tsize, err := strconv.Atoi(value) if err != nil { - messenger.Error("Error writing to settings.json: " + err.Error()) + messenger.Error("Invalid value for " + option) return } - } else { - messenger.Error("Option " + option + " does not exist") + 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("Invalid option, please use option value") + messenger.Error("Option " + option + " does not exist") } + } else { + messenger.Error("Invalid option, please use option value") } }