mirror of
https://github.com/zyedidia/micro.git
synced 2026-03-16 22:07:09 +09:00
Add simple commands
This commit is contained in:
14
src/command.go
Normal file
14
src/command.go
Normal file
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user