From 36c22067857819d5de475d6f2ca8f3c017d9ca86 Mon Sep 17 00:00:00 2001 From: makeworld Date: Fri, 3 Jul 2020 12:22:44 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Wrapping=20done=20-=20fixes=20#33?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- amfora.go | 2 ++ display/display.go | 25 ++++++++++++++++++------- display/private.go | 6 +++--- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/amfora.go b/amfora.go index b5937d5..d0b7d1a 100644 --- a/amfora.go +++ b/amfora.go @@ -35,6 +35,8 @@ func main() { display.Init() display.NewTab() + display.NewTab() // Open extra tab and close it to fully initialize the app and wrapping + display.CloseTab() if len(os.Args[1:]) > 0 { display.URL(os.Args[1]) } diff --git a/display/display.go b/display/display.go index 3bba092..1bf75e7 100644 --- a/display/display.go +++ b/display/display.go @@ -6,6 +6,7 @@ import ( "path" "strconv" "strings" + "sync" "github.com/gdamore/tcell" "github.com/makeworld-the-better-one/amfora/cache" @@ -71,7 +72,9 @@ var layout = cview.NewFlex(). var renderedNewTabContent string var newTabLinks []string -var newTabPage structs.Page +var newTabPage *structs.Page + +var reformatMuts = make(map[int]*sync.Mutex) // Mutex for each tab var App = cview.NewApplication(). EnableMouse(false). @@ -82,7 +85,12 @@ var App = cview.NewApplication(). termH = height // Make sure the current tab content is reformatted when the terminal size changes - reformatAndDisplayPage(tabMap[curTab]) + go func(tab int) { + reformatMuts[tab].Lock() // Only one reformat job per tab + defer reformatMuts[tab].Unlock() + // Use the current tab, but don't affect other tabs if the user switches tabs + reformatAndDisplayPage(tab, tabMap[tab]) + }(curTab) }) func Init() { @@ -194,12 +202,12 @@ func Init() { // Render the default new tab content ONCE and store it for later renderedNewTabContent, newTabLinks = renderer.RenderGemini(newTabContent, textWidth(), leftMargin()) - newTabPage = structs.Page{ + newTabPage = &structs.Page{ Raw: newTabContent, Content: renderedNewTabContent, Links: newTabLinks, Url: "about:newtab", - Width: termW, + Width: -1, // Force reformatting on first display } modalInit() @@ -358,7 +366,9 @@ func NewTab() { selectedLink = "" curTab = NumTabs() - tabMap[curTab] = &newTabPage + reformatPage(newTabPage) + tabMap[curTab] = newTabPage + reformatMuts[curTab] = &sync.Mutex{} tabViews[curTab] = cview.NewTextView(). SetDynamicColors(true). SetRegions(true). @@ -462,6 +472,7 @@ func CloseTab() { delete(tabMap, curTab) tabPages.RemovePage(strconv.Itoa(curTab)) delete(tabViews, curTab) + delete(reformatMuts, curTab) delete(tabHist, curTab) delete(tabHistPos, curTab) @@ -505,7 +516,7 @@ func SwitchTab(tab int) { } curTab = tab % NumTabs() - reformatAndDisplayPage(tabMap[curTab]) + reformatAndDisplayPage(curTab, tabMap[curTab]) tabPages.SwitchToPage(strconv.Itoa(curTab)) tabRow.Highlight(strconv.Itoa(curTab)).ScrollToHighlight() @@ -536,7 +547,7 @@ func URL(u string) { return } if u == "about:newtab" { - setPage(&newTabPage) + setPage(newTabPage) return } if strings.HasPrefix(u, "about:") { diff --git a/display/private.go b/display/private.go index 849bd6b..97f8a15 100644 --- a/display/private.go +++ b/display/private.go @@ -176,10 +176,10 @@ func reformatPage(p *structs.Page) { // reformatAndDisplayPage is for reformatting a page that is already being displayed. // setPage should be used when a page is being loaded for the first time. -func reformatAndDisplayPage(p *structs.Page) { +func reformatAndDisplayPage(tab int, p *structs.Page) { saveScroll() - reformatPage(tabMap[curTab]) - tabViews[curTab].SetText(tabMap[curTab].Content) + reformatPage(p) + tabViews[tab].SetText(p.Content) applyScroll() // Go back to where you were, roughly }