Skip to content

Commit bb0bfca

Browse files
committed
chore: 新增多命令分割
1 parent 270967c commit bb0bfca

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

config-example.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"UI": {
33
"AmbiguousWidth": "auto",
44
"HistoryLines": 100000,
5-
"RTTVHeight": 10
5+
"RTTVHeight": 10,
6+
"Split":false,
7+
"Separator":";"
68
},
79
"Mud": {
810
"Host": "mud.pkuxkx.net",

config-example.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ UI:
22
AmbiguousWidth: auto
33
HistoryLines: 100000
44
RTTVHeight: 10
5+
Split: true
6+
Sparator: ;
57
MUD:
68
Host: mud.pkuxkx.net
79
Port: 8080

main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ LOOP:
107107

108108
func (c *Client) DoCmd(cmd string) {
109109
switch cmd {
110-
case "exit", "quit":
110+
case "/exit":
111111
c.quit <- true
112112
return
113113
case "/version":

ui/readline.go

+20
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ type Readline struct {
1818

1919
repeat bool
2020
autoTrim bool
21+
22+
cmds []string
23+
split bool
24+
separator string
2125
}
2226

2327
func NewReadline() *Readline {
@@ -29,6 +33,12 @@ func NewReadline() *Readline {
2933
}
3034
}
3135

36+
func (r *Readline) SetSeparator(s string) *Readline {
37+
r.split = true
38+
r.separator = s
39+
return r
40+
}
41+
3242
func (r *Readline) SetRepeat(b bool) *Readline {
3343
r.repeat = b
3444
return r
@@ -69,14 +79,24 @@ func (r *Readline) InputCapture(event *tcell.EventKey) *tcell.EventKey {
6979
func (r *Readline) Enter() string {
7080
text := r.InputField.GetText()
7181

82+
r.cmds = nil
83+
7284
if text != "" && r.autoTrim {
7385
text = strings.TrimSpace(text)
7486
// 如果 trim 之后变成了空串,则至少保留一个空格,以免用户发不出空格
7587
if text == "" {
7688
text = " "
89+
}else if r.split && "" != r.separator {
90+
//命令分割
91+
r.cmds = strings.Split(text,r.separator)
92+
if len(r.cmds) < 2 {
93+
r.cmds = nil
94+
}
7795
}
7896
}
7997

98+
99+
80100
last := ""
81101
if len(r.history) > 0 {
82102
last = r.history[len(r.history)-1]

ui/ui.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ type Config struct {
1616
AmbiguousWidth string `flag:"|auto|二义性字符宽度,可选值: auto/single/double/space"`
1717
HistoryLines int `flag:"|100000|历史记录保留行数"`
1818
RTTVHeight int `flag:"|10|历史查看模式下实时文本区域高度"`
19+
Split bool `flag:"|false|分割多个命令"`
20+
Separator string `flag:"|;|自定义分隔符"`
1921
}
2022

2123
type UI struct {
@@ -74,6 +76,11 @@ func (ui *UI) Create(title string) {
7476
ui.ansiWriter = tview.ANSIWriter(ui.realtimeTV)
7577

7678
ui.cmdLine = NewReadline()
79+
80+
if ui.config.Split {
81+
ui.cmdLine.SetSeparator(ui.config.Separator)
82+
}
83+
7784
ui.cmdLine.SetRepeat(true).
7885
SetAutoTrim(true).
7986
SetFieldBackgroundColor(tcell.ColorBlack).
@@ -132,7 +139,13 @@ func (ui *UI) InputCapture(event *tcell.EventKey) *tcell.EventKey {
132139

133140
if key == tcell.KeyEnter {
134141
cmd := ui.cmdLine.Enter()
135-
ui.input <- cmd
142+
if ui.config.Split && nil != ui.cmdLine.cmds {
143+
for _,cmd := range ui.cmdLine.cmds{
144+
ui.input <- cmd
145+
}
146+
}else{
147+
ui.input <- cmd
148+
}
136149
return nil
137150
}
138151

0 commit comments

Comments
 (0)