From 936d519a67301bd78a4d737dc47234e3769e639f Mon Sep 17 00:00:00 2001 From: delitako Date: Thu, 25 Jan 2024 22:50:14 -0600 Subject: tabs: optimize switching by offsets MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I imagine no sane user requires aerc to correctly handle commands like `:next-tab 1000000000`, but I tried anyway and it froze aerc while also eating up many GBs of system memory. This behavior is not ideal, so I improved it. This commit adds functions for selecting a tab at an offset from the currently-selected tab and changes the next-tab, prev-tab, and change-tab commands to use these functions instead of looping. Signed-off-by: delitako Tested-by: Thomas Böhler Reviewed-by: Thomas Böhler Acked-by: Robin Jarry --- commands/ct.go | 16 ++++------------ commands/next-tab.go | 15 +++++++++------ 2 files changed, 13 insertions(+), 18 deletions(-) (limited to 'commands') diff --git a/commands/ct.go b/commands/ct.go index 1f99c6a5..4f908fcf 100644 --- a/commands/ct.go +++ b/commands/ct.go @@ -37,20 +37,12 @@ func (c ChangeTab) Execute(args []string) error { } else { n, err := strconv.Atoi(c.Tab) if err == nil { - switch { - case strings.HasPrefix(c.Tab, "+"): - for ; n > 0; n-- { - app.NextTab() - } - case strings.HasPrefix(c.Tab, "-"): - for ; n < 0; n++ { - app.PrevTab() - } - default: + if strings.HasPrefix(c.Tab, "+") || strings.HasPrefix(c.Tab, "-") { + app.SelectTabAtOffset(n) + } else { ok := app.SelectTabIndex(n) if !ok { - return errors.New( - "No tab with that index") + return errors.New("No tab with that index") } } } else { diff --git a/commands/next-tab.go b/commands/next-tab.go index adab95ae..12f659c9 100644 --- a/commands/next-tab.go +++ b/commands/next-tab.go @@ -21,13 +21,16 @@ func (NextPrevTab) Aliases() []string { } func (np NextPrevTab) Execute(args []string) error { - for n := 0; n < np.Offset; n++ { - if args[0] == "prev-tab" { - app.PrevTab() - } else { - app.NextTab() - } + if np.Offset <= 0 { + return nil } + + offset := np.Offset + if args[0] == "prev-tab" { + offset *= -1 + } + + app.SelectTabAtOffset(offset) app.UpdateStatus() return nil } -- cgit