From e42b95a617399b2736df96ae6469309e6b306d71 Mon Sep 17 00:00:00 2001 From: Jeffas Date: Fri, 19 Jul 2019 18:12:57 +0100 Subject: Add change tab command This command allows the user to change tab by giving the tab name. This can be tab completed too. The previous tab is stored in the tabs module so that when a new tab is created it is still possible to go to the previous one. Normal invocation is :ct folder Previous tab is :ct - --- lib/ui/tab.go | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) (limited to 'lib') diff --git a/lib/ui/tab.go b/lib/ui/tab.go index 0061472..7808db4 100644 --- a/lib/ui/tab.go +++ b/lib/ui/tab.go @@ -29,7 +29,7 @@ func NewTabs() *Tabs { tabs := &Tabs{} tabs.TabStrip = (*TabStrip)(tabs) tabs.TabContent = (*TabContent)(tabs) - tabs.history = []int{0} + tabs.history = []int{} return tabs } @@ -64,7 +64,10 @@ func (tabs *Tabs) Remove(content Drawable) { break } } - tabs.Select(tabs.popHistory()) + index, ok := tabs.popHistory() + if ok { + tabs.Select(index) + } tabs.TabStrip.Invalidate() } @@ -90,22 +93,34 @@ func (tabs *Tabs) Select(index int) { } if tabs.Selected != index { + tabs.pushHistory(tabs.Selected) tabs.Selected = index - tabs.pushHistory(index) tabs.TabStrip.Invalidate() tabs.TabContent.Invalidate() } } +func (tabs *Tabs) SelectPrevious() bool { + index, ok := tabs.popHistory() + if !ok { + return false + } + tabs.Select(index) + return true +} + func (tabs *Tabs) pushHistory(index int) { tabs.history = append(tabs.history, index) } -func (tabs *Tabs) popHistory() int { +func (tabs *Tabs) popHistory() (int, bool) { lastIdx := len(tabs.history) - 1 + if lastIdx < 0 { + return 0, false + } item := tabs.history[lastIdx] tabs.history = tabs.history[:lastIdx] - return item + return item, true } func (tabs *Tabs) removeHistory(index int) { -- cgit 1.4.1-2-gfad0