From 8635c70fda20b91f97c42f4e23e97bc01a14a89d Mon Sep 17 00:00:00 2001 From: Galen Abell Date: Tue, 23 Jul 2019 12:52:33 -0400 Subject: Add command history and cycling Aerc will keep track of the previous 1000 commands, which the user can cycle through using the arrow keys while in the ex-line. Pressing up will move backwards in history while pressing down will move forward. --- widgets/exline.go | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'widgets/exline.go') diff --git a/widgets/exline.go b/widgets/exline.go index e984ee1..b7b4e3d 100644 --- a/widgets/exline.go +++ b/widgets/exline.go @@ -3,6 +3,7 @@ package widgets import ( "github.com/gdamore/tcell" + "git.sr.ht/~sircmpwn/aerc/lib" "git.sr.ht/~sircmpwn/aerc/lib/ui" ) @@ -11,17 +12,20 @@ type ExLine struct { cancel func() commit func(cmd string) tabcomplete func(cmd string) []string + cmdHistory lib.History input *ui.TextInput } func NewExLine(commit func(cmd string), cancel func(), - tabcomplete func(cmd string) []string) *ExLine { + tabcomplete func(cmd string) []string, + cmdHistory lib.History) *ExLine { input := ui.NewTextInput("").Prompt(":") exline := &ExLine{ cancel: cancel, commit: commit, tabcomplete: tabcomplete, + cmdHistory: cmdHistory, input: input, } input.OnInvalidate(func(d ui.Drawable) { @@ -47,10 +51,18 @@ func (ex *ExLine) Event(event tcell.Event) bool { case *tcell.EventKey: switch event.Key() { case tcell.KeyEnter: + cmd := ex.input.String() ex.input.Focus(false) - ex.commit(ex.input.String()) + ex.commit(cmd) + case tcell.KeyUp: + ex.input.Set(ex.cmdHistory.Prev()) + ex.Invalidate() + case tcell.KeyDown: + ex.input.Set(ex.cmdHistory.Next()) + ex.Invalidate() case tcell.KeyEsc, tcell.KeyCtrlC: ex.input.Focus(false) + ex.cmdHistory.Reset() ex.cancel() case tcell.KeyTab: complete := ex.tabcomplete(ex.input.StringLeft()) -- cgit v1.2.3