Skip to content

Commit

Permalink
Merge pull request #35 from mudclient/feat/improve-input-line
Browse files Browse the repository at this point in the history
feat: 增强命令行,上下箭头翻找历史,回车重复上一条
  • Loading branch information
dzpao authored Oct 16, 2020
2 parents 358d2a5 + 1225d55 commit c80bc8d
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 21 deletions.
98 changes: 98 additions & 0 deletions ui/readline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package ui

import (
"strings"

"github.com/gdamore/tcell"
"github.com/rivo/tview"
)

type Readline struct {
*tview.InputField

history []string
curSel int
historySize int

repeat bool
autoTrim bool
}

func NewReadline() *Readline {
return &Readline{
InputField: tview.NewInputField(),
history: make([]string, 0, 32),
curSel: 0,
historySize: 10000,
}
}

func (r *Readline) SetRepeat(b bool) *Readline {
r.repeat = b

return r
}

func (r *Readline) SetAutoTrim(b bool) *Readline {
r.autoTrim = b

return r
}

func (r *Readline) InputCapture(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyCtrlC:
r.InputField.SetText("")
return nil
case tcell.KeyUp:
if r.curSel > 0 {
r.curSel--
r.InputField.SetText(r.history[r.curSel])
}
return nil
case tcell.KeyDown:
if r.curSel == len(r.history)-1 {
r.curSel++
r.InputField.SetText("")
}
if r.curSel < len(r.history)-1 {
r.curSel++
r.InputField.SetText(r.history[r.curSel])
}
return nil
default:
}

return event
}

func (r *Readline) Enter() string {
text := r.InputField.GetText()

if text != "" && r.autoTrim {
text = strings.TrimSpace(text)
// 如果 trim 之后变成了空串,则至少保留一个空格,以免用户发不出空格
if text == "" {
text = " "
}
}

last := ""
if len(r.history) > 0 {
last = r.history[len(r.history)-1]
}

if text == "" && r.repeat && last != "" {
text = last
} else if text != " " && text != last {
if len(r.history) >= r.historySize {
r.history = r.history[1 : len(r.history)-1]
}
r.history = append(r.history, text)
r.curSel = len(r.history)
}

r.InputField.SetText("")

return text
}
30 changes: 9 additions & 21 deletions ui/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type UI struct {
historyTV *tview.TextView
sepLine *tview.TextView
realtimeTV *tview.TextView
cmdLine *tview.InputField
cmdLine *Readline

buffer []string
unformed bool
Expand Down Expand Up @@ -73,7 +73,9 @@ func (ui *UI) Create(title string) {

ui.ansiWriter = tview.ANSIWriter(ui.realtimeTV)

ui.cmdLine = tview.NewInputField().
ui.cmdLine = NewReadline()
ui.cmdLine.SetRepeat(true).
SetAutoTrim(true).
SetFieldBackgroundColor(tcell.ColorBlack).
SetLabelColor(tcell.ColorWhite).
SetLabel("命令: ")
Expand Down Expand Up @@ -128,28 +130,13 @@ func (ui *UI) InputCapture(event *tcell.EventKey) *tcell.EventKey {
return nil
}

return ui.cmdLineInputCapture(event)
}

func (ui *UI) cmdLineInputCapture(event *tcell.EventKey) *tcell.EventKey {
key := event.Key()

if key == tcell.KeyCtrlC {
ui.cmdLine.SetText("")
return nil
} else if key == tcell.KeyEnter {
cmd := ui.cmdLine.GetText()
if cmd != "" {
ui.input <- cmd
ui.cmdLine.SetText("")
}
if ui.isScrolling() {
ui.pageEnd()
}
if key == tcell.KeyEnter {
cmd := ui.cmdLine.Enter()
ui.input <- cmd
return nil
}

return event
return ui.cmdLine.InputCapture(event)
}

func (ui *UI) cmdLineTextChanged(text string) {
Expand Down Expand Up @@ -198,6 +185,7 @@ func (ui *UI) historyInputCapture(event *tcell.EventKey) *tcell.EventKey {
case 'G':
ui.pageEnd()
}
default:
}

return nil
Expand Down

0 comments on commit c80bc8d

Please sign in to comment.