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/aerc.go | 13 +++++++++++-- widgets/compose.go | 9 +++++++-- widgets/exline.go | 16 ++++++++++++++-- 3 files changed, 32 insertions(+), 6 deletions(-) (limited to 'widgets') diff --git a/widgets/aerc.go b/widgets/aerc.go index 050ba77..458c2f9 100644 --- a/widgets/aerc.go +++ b/widgets/aerc.go @@ -11,6 +11,7 @@ import ( "github.com/google/shlex" "git.sr.ht/~sircmpwn/aerc/config" + "git.sr.ht/~sircmpwn/aerc/lib" "git.sr.ht/~sircmpwn/aerc/lib/ui" libui "git.sr.ht/~sircmpwn/aerc/lib/ui" ) @@ -18,6 +19,7 @@ import ( type Aerc struct { accounts map[string]*AccountView cmd func(cmd []string) error + cmdHistory lib.History complete func(cmd string) []string conf *config.AercConfig focused libui.Interactive @@ -31,7 +33,8 @@ type Aerc struct { } func NewAerc(conf *config.AercConfig, logger *log.Logger, - cmd func(cmd []string) error, complete func(cmd string) []string) *Aerc { + cmd func(cmd []string) error, complete func(cmd string) []string, + cmdHistory lib.History) *Aerc { tabs := libui.NewTabs() @@ -54,6 +57,7 @@ func NewAerc(conf *config.AercConfig, logger *log.Logger, accounts: make(map[string]*AccountView), conf: conf, cmd: cmd, + cmdHistory: cmdHistory, complete: complete, grid: grid, logger: logger, @@ -323,6 +327,11 @@ func (aerc *Aerc) BeginExCommand() { aerc.PushStatus(" "+err.Error(), 10*time.Second). Color(tcell.ColorDefault, tcell.ColorRed) } + // only add to history if this is an unsimulated command, + // ie one not executed from a keybinding + if aerc.simulating == 0 { + aerc.cmdHistory.Add(cmd) + } aerc.statusbar.Pop() aerc.focus(previous) }, func() { @@ -330,7 +339,7 @@ func (aerc *Aerc) BeginExCommand() { aerc.focus(previous) }, func(cmd string) []string { return aerc.complete(cmd) - }) + }, aerc.cmdHistory) aerc.statusbar.Push(exline) aerc.focus(exline) } diff --git a/widgets/compose.go b/widgets/compose.go index b45892f..4f6f7a1 100644 --- a/widgets/compose.go +++ b/widgets/compose.go @@ -51,7 +51,8 @@ func NewComposer(conf *config.AercConfig, defaults["From"] = acct.From } - layout, editors, focusable := buildComposeHeader(conf.Compose.HeaderLayout, defaults) + layout, editors, focusable := buildComposeHeader( + conf.Compose.HeaderLayout, defaults) header, headerHeight := layout.grid( func(header string) ui.Drawable { return editors[header] }, @@ -90,7 +91,11 @@ func NewComposer(conf *config.AercConfig, return c } -func buildComposeHeader(layout HeaderLayout, defaults map[string]string) (newLayout HeaderLayout, editors map[string]*headerEditor, focusable []ui.DrawableInteractive) { +func buildComposeHeader(layout HeaderLayout, defaults map[string]string) ( + newLayout HeaderLayout, + editors map[string]*headerEditor, + focusable []ui.DrawableInteractive, +) { editors = make(map[string]*headerEditor) focusable = make([]ui.DrawableInteractive, 0) 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