-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from mudclient/feat/improve-input-line
feat: 增强命令行,上下箭头翻找历史,回车重复上一条
- Loading branch information
Showing
2 changed files
with
107 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters