Skip to content

chore: 新增多命令分割 #42

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config-example.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
"UI": {
"AmbiguousWidth": "auto",
"HistoryLines": 100000,
"RTTVHeight": 10
"RTTVHeight": 10,
"Split":false,
"Separator":";"
},
"Mud": {
"Host": "mud.pkuxkx.net",
Expand Down
2 changes: 2 additions & 0 deletions config-example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ UI:
AmbiguousWidth: auto
HistoryLines: 100000
RTTVHeight: 10
Split: true
Sparator: ;
MUD:
Host: mud.pkuxkx.net
Port: 8080
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ LOOP:

func (c *Client) DoCmd(cmd string) {
switch cmd {
case "exit", "quit":
case "/exit":
c.quit <- true
return
case "/version":
Expand Down
20 changes: 20 additions & 0 deletions ui/readline.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ type Readline struct {

repeat bool
autoTrim bool

cmds []string
split bool
separator string
}

func NewReadline() *Readline {
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand Down
15 changes: 14 additions & 1 deletion ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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
}

Expand Down