From 1aa13f240872e9ab5d89d5eed0cdfda336730c89 Mon Sep 17 00:00:00 2001 From: makeworld Date: Tue, 28 Dec 2021 16:39:42 -0500 Subject: [PATCH] Tabs have labels of the URL domain instead of numbers Fixes #202 --- CHANGELOG.md | 1 + display/display.go | 4 ++-- display/private.go | 2 +- display/tab.go | 37 +++++++++++++++++++++++++++++++++++-- display/util.go | 6 ------ 5 files changed, 39 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1b32332..0cde56a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Center text automatically, removing `left_margin` from the config (#233) - `max_width` defaults to 80 columns instead of 100 (#233) +- Tabs have the domain of the current page instead of numbers (#202) ### Fixed - Modal can't be closed when opening non-gemini text URLs from the commandline (#283, #284) diff --git a/display/display.go b/display/display.go index 16a5379..d89cbc9 100644 --- a/display/display.go +++ b/display/display.go @@ -88,7 +88,7 @@ func Init(version, commit, builtBy string) { // Overwrite all tabs with a new, differently sized, left margin browser.AddTab( strconv.Itoa(i), - makeTabLabel(strconv.Itoa(i+1)), + tabs[i].label(), makeContentLayout(tabs[i].view, leftMargin()), ) if tabs[i] == t { @@ -438,7 +438,7 @@ func NewTabWithURL(url string) { browser.AddTab( strconv.Itoa(curTab), - makeTabLabel(strconv.Itoa(curTab+1)), + tabs[curTab].label(), makeContentLayout(tabs[curTab].view, leftMargin()), ) browser.SetCurrentTab(strconv.Itoa(curTab)) diff --git a/display/private.go b/display/private.go index 0405971..f5e138d 100644 --- a/display/private.go +++ b/display/private.go @@ -118,7 +118,7 @@ func setPage(t *tab, p *structs.Page) { tabNum := tabNumber(t) browser.AddTab( strconv.Itoa(tabNum), - makeTabLabel(strconv.Itoa(tabNum+1)), + t.label(), makeContentLayout(t.view, leftMargin()), ) App.Draw() diff --git a/display/tab.go b/display/tab.go index ba20106..ec321f8 100644 --- a/display/tab.go +++ b/display/tab.go @@ -3,6 +3,7 @@ package display import ( "fmt" "net/url" + "path" "strconv" "strings" @@ -389,7 +390,7 @@ func (t *tab) applyHorizontalScroll() { // Scrolled to the right far enough that no left margin is needed browser.AddTab( strconv.Itoa(i), - makeTabLabel(strconv.Itoa(i+1)), + t.label(), makeContentLayout(t.view, 0), ) t.view.ScrollTo(t.page.Row, t.page.Column-leftMargin()) @@ -397,7 +398,7 @@ func (t *tab) applyHorizontalScroll() { // Left margin is still needed, but is not necessarily at the right size by default browser.AddTab( strconv.Itoa(i), - makeTabLabel(strconv.Itoa(i+1)), + t.label(), makeContentLayout(t.view, leftMargin()-t.page.Column), ) } @@ -508,3 +509,35 @@ func (t *tab) highlightedURL() string { } return "" } + +// label returns the label to use for the tab name +func (t *tab) label() string { + tn := tabNumber(t) + if tn < 0 { + // Invalid tab, shouldn't happen + return "" + } + + // Increment so there's no tab 0 in the label + tn++ + + if t.page.URL == "" || t.page.URL == "about:newtab" { + // Just use tab number + // Spaces around to keep original Amfora look + return fmt.Sprintf(" %d ", tn) + } + if strings.HasPrefix(t.page.URL, "about:") { + // Don't look for domain, put the whole URL except query strings + return strings.SplitN(t.page.URL, "?", 2)[0] + } + if strings.HasPrefix(t.page.URL, "file://") { + // File URL, use file or folder as tab name + return path.Base(t.page.URL[7:]) + } + // Otherwise, it's a Gemini URL + pu, err := url.Parse(t.page.URL) + if err != nil { + return fmt.Sprintf(" %d ", tn) + } + return pu.Host +} diff --git a/display/util.go b/display/util.go index 74f0b81..550cdff 100644 --- a/display/util.go +++ b/display/util.go @@ -32,12 +32,6 @@ func makeContentLayout(tv *cview.TextView, leftMargin int) *cview.Flex { return vert } -// makeTabLabel takes a string and adds spacing to it, making it -// suitable for display as a tab label. -func makeTabLabel(s string) string { - return " " + s + " " -} - // tabNumber gets the index of the tab in the tabs slice. It returns -1 // if the tab is not in that slice. func tabNumber(t *tab) int {