diff --git a/config-example.json b/config-example.json index 5337088..ee7da2c 100644 --- a/config-example.json +++ b/config-example.json @@ -2,7 +2,9 @@ "UI": { "AmbiguousWidth": "auto", "HistoryLines": 100000, - "RTTVHeight": 10 + "RTTVHeight": 10, + "Split":false, + "Separator":";" }, "Mud": { "Host": "mud.pkuxkx.net", diff --git a/config-example.yaml b/config-example.yaml index 969e30c..4fd3966 100644 --- a/config-example.yaml +++ b/config-example.yaml @@ -2,6 +2,8 @@ UI: AmbiguousWidth: auto HistoryLines: 100000 RTTVHeight: 10 + Split: true + Sparator: ; MUD: Host: mud.pkuxkx.net Port: 8080 diff --git a/main.go b/main.go index 245a4e9..0861d0b 100644 --- a/main.go +++ b/main.go @@ -107,7 +107,7 @@ LOOP: func (c *Client) DoCmd(cmd string) { switch cmd { - case "exit", "quit": + case "/exit": c.quit <- true return case "/version": diff --git a/ui/readline.go b/ui/readline.go index 7b0aa29..83c18c4 100644 --- a/ui/readline.go +++ b/ui/readline.go @@ -18,6 +18,10 @@ type Readline struct { repeat bool autoTrim bool + + cmds []string + split bool + separator string } func NewReadline() *Readline { @@ -29,6 +33,12 @@ func NewReadline() *Readline { } } +func (r *Readline) SetSeparator(s string) *Readline { + r.split = true + r.separator = s + return r +} + func (r *Readline) SetRepeat(b bool) *Readline { r.repeat = b return r @@ -69,14 +79,24 @@ func (r *Readline) InputCapture(event *tcell.EventKey) *tcell.EventKey { func (r *Readline) Enter() string { text := r.InputField.GetText() + r.cmds = nil + if text != "" && r.autoTrim { text = strings.TrimSpace(text) // 如果 trim 之后变成了空串,则至少保留一个空格,以免用户发不出空格 if text == "" { text = " " + }else if r.split && "" != r.separator { + //命令分割 + r.cmds = strings.Split(text,r.separator) + if len(r.cmds) < 2 { + r.cmds = nil + } } } + + last := "" if len(r.history) > 0 { last = r.history[len(r.history)-1] diff --git a/ui/ui.go b/ui/ui.go index 1014224..43bb56b 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -16,6 +16,8 @@ type Config struct { AmbiguousWidth string `flag:"|auto|二义性字符宽度,可选值: auto/single/double/space"` HistoryLines int `flag:"|100000|历史记录保留行数"` RTTVHeight int `flag:"|10|历史查看模式下实时文本区域高度"` + Split bool `flag:"|false|分割多个命令"` + Separator string `flag:"|;|自定义分隔符"` } type UI struct { @@ -74,6 +76,11 @@ func (ui *UI) Create(title string) { ui.ansiWriter = tview.ANSIWriter(ui.realtimeTV) ui.cmdLine = NewReadline() + + if ui.config.Split { + ui.cmdLine.SetSeparator(ui.config.Separator) + } + ui.cmdLine.SetRepeat(true). SetAutoTrim(true). SetFieldBackgroundColor(tcell.ColorBlack). @@ -132,7 +139,13 @@ func (ui *UI) InputCapture(event *tcell.EventKey) *tcell.EventKey { if key == tcell.KeyEnter { cmd := ui.cmdLine.Enter() - ui.input <- cmd + if ui.config.Split && nil != ui.cmdLine.cmds { + for _,cmd := range ui.cmdLine.cmds{ + ui.input <- cmd + } + }else{ + ui.input <- cmd + } return nil }