1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-12-04 14:46:29 -05:00

Refactor many funcs into methods of tab

This commit is contained in:
makeworld 2021-06-25 19:03:41 -04:00
parent c1ecbf71b9
commit 32175a662c
9 changed files with 50 additions and 49 deletions

View File

@ -1,7 +1,8 @@
# Notes # Notes
## Stream (#9) ## Stream (#9)
- Then make handlers and stuff part of `tab` - Change tab funcs that rely on that tab being the "current tab"
- Fix compile errors
- Go through process of loading a page from the very beginning and line up all the parts - Go through process of loading a page from the very beginning and line up all the parts
- Also handle non-network pages like `about:` pages, where `Raw` already exists and just needs to be rendered - Also handle non-network pages like `about:` pages, where `Raw` already exists and just needs to be rendered

View File

@ -136,7 +136,7 @@ func Bookmarks(t *tab) {
TermWidth: termW, TermWidth: termW,
Mediatype: structs.TextGemini, Mediatype: structs.TextGemini,
} }
setPage(t, &page) t.setPage(&page)
t.applyBottomBar() t.applyBottomBar()
} }

View File

@ -78,7 +78,7 @@ func Init(version, commit, builtBy string) {
) )
if tabs[i] == t { if tabs[i] == t {
// Reformat page ASAP, in the middle of loop // Reformat page ASAP, in the middle of loop
reformatPageAndSetView(t, t.page) t.reformatPageAndSetView()
} }
} }
App.Draw() App.Draw()
@ -230,7 +230,7 @@ func Init(version, commit, builtBy string) {
} }
if i <= len(tabs[tab].page.Links) && i > 0 { if i <= len(tabs[tab].page.Links) && i > 0 {
// It's a valid link number // It's a valid link number
followLink(tabs[tab], tabs[tab].page.URL, tabs[tab].page.Links[i-1]) tabs[tab].followLink(tabs[tab].page.Links[i-1])
return return
} }
// Invalid link number, don't do anything // Invalid link number, don't do anything
@ -321,7 +321,7 @@ func Init(version, commit, builtBy string) {
switch cmd { switch cmd {
case config.CmdNewTab: case config.CmdNewTab:
if tabs[curTab].page.Mode == structs.ModeLinkSelect { if tabs[curTab].page.Mode == structs.ModeLinkSelect {
next, err := resolveRelLink(tabs[curTab], tabs[curTab].page.URL, tabs[curTab].page.Selected) next, err := tabs[curTab].resolveRelLink(tabs[curTab].page.Selected)
if err != nil { if err != nil {
Error("URL Error", err.Error()) Error("URL Error", err.Error())
return nil return nil
@ -392,7 +392,7 @@ func NewTab() {
tabs = append(tabs, makeNewTab()) tabs = append(tabs, makeNewTab())
temp := newTabPage // Copy temp := newTabPage // Copy
setPage(tabs[curTab], &temp) tabs[curTab].setPage(&temp)
tabs[curTab].addToHistory("about:newtab") tabs[curTab].addToHistory("about:newtab")
tabs[curTab].history.pos = 0 // Manually set as first page tabs[curTab].history.pos = 0 // Manually set as first page
@ -468,7 +468,7 @@ func SwitchTab(tab int) {
curTab = tab % NumTabs() curTab = tab % NumTabs()
// Display tab // Display tab
reformatPageAndSetView(tabs[curTab], tabs[curTab].page) tabs[curTab].reformatPageAndSetView()
browser.SetCurrentTab(strconv.Itoa(curTab)) browser.SetCurrentTab(strconv.Itoa(curTab))
tabs[curTab].applyAll() tabs[curTab].applyAll()
@ -490,7 +490,7 @@ func Reload() {
Mediatype: structs.TextGemini, Mediatype: structs.TextGemini,
} }
temp := newTabPage // Copy temp := newTabPage // Copy
setPage(tabs[curTab], &temp) tabs[curTab].setPage(&temp)
return return
} }
@ -500,7 +500,7 @@ func Reload() {
go func(t *tab) { go func(t *tab) {
cache.RemovePage(tabs[curTab].page.URL) cache.RemovePage(tabs[curTab].page.URL)
handleURL(t, t.page.URL, 0) // goURL is not used bc history shouldn't be added to t.handleURL(t.page.URL, 0) // goURL is not used bc history shouldn't be added to
if t == tabs[curTab] { if t == tabs[curTab] {
// Display the bottomBar state that handleURL set // Display the bottomBar state that handleURL set
t.applyBottomBar() t.applyBottomBar()
@ -513,13 +513,13 @@ func Reload() {
func URL(u string) { func URL(u string) {
t := tabs[curTab] t := tabs[curTab]
if strings.HasPrefix(u, "about:") { if strings.HasPrefix(u, "about:") {
if final, ok := handleAbout(t, u); ok { if final, ok := t.handleAbout(u); ok {
t.addToHistory(final) t.addToHistory(final)
} }
return return
} }
go goURL(t, fixUserURL(u)) go t.goURL(fixUserURL(u))
} }
func NumTabs() int { func NumTabs() int {

View File

@ -95,7 +95,7 @@ func handleOther(u string) {
// It returns the URL displayed, and a bool indicating if the provided // It returns the URL displayed, and a bool indicating if the provided
// URL could be handled. The string returned will always be empty // URL could be handled. The string returned will always be empty
// if the bool is false. // if the bool is false.
func handleAbout(t *tab, u string) (string, bool) { func (t *tab) handleAbout(u string) (string, bool) {
if !strings.HasPrefix(u, "about:") { if !strings.HasPrefix(u, "about:") {
return "", false return "", false
} }
@ -106,27 +106,27 @@ func handleAbout(t *tab, u string) (string, bool) {
return u, true return u, true
case "about:newtab": case "about:newtab":
temp := newTabPage // Copy temp := newTabPage // Copy
setPage(t, &temp) t.setPage(&temp)
t.applyBottomBar() t.applyBottomBar()
return u, true return u, true
case "about:version": case "about:version":
temp := versionPage temp := versionPage
setPage(t, &temp) t.setPage(&temp)
t.applyBottomBar() t.applyBottomBar()
return u, true return u, true
case "about:license": case "about:license":
temp := licensePage temp := licensePage
setPage(t, &temp) t.setPage(&temp)
t.applyBottomBar() t.applyBottomBar()
return u, true return u, true
case "about:thanks": case "about:thanks":
temp := thanksPage temp := thanksPage
setPage(t, &temp) t.setPage(&temp)
t.applyBottomBar() t.applyBottomBar()
return u, true return u, true
case "about:about": case "about:about":
temp := aboutPage temp := aboutPage
setPage(t, &temp) t.setPage(&temp)
t.applyBottomBar() t.applyBottomBar()
return u, true return u, true
} }
@ -163,7 +163,7 @@ func handleAbout(t *tab, u string) (string, bool) {
// //
// numRedirects is the number of redirects that resulted in the provided URL. // numRedirects is the number of redirects that resulted in the provided URL.
// It should typically be 0. // It should typically be 0.
func handleURL(t *tab, u string, numRedirects int) (string, bool) { func (t *tab) handleURL(u string, numRedirects int) (string, bool) {
defer App.Draw() // Just in case defer App.Draw() // Just in case
// Save for resetting on error // Save for resetting on error
@ -201,7 +201,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
App.SetFocus(t.view) App.SetFocus(t.view)
if strings.HasPrefix(u, "about:") { if strings.HasPrefix(u, "about:") {
return ret(handleAbout(t, u)) return ret(t.handleAbout(u))
} }
u = normalizeURL(u) u = normalizeURL(u)
@ -237,7 +237,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
if !ok { if !ok {
return ret("", false) return ret("", false)
} }
setPage(t, page) t.setPage(page)
return ret(u, true) return ret(u, true)
} }
@ -258,7 +258,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
if numRedirects == 0 { if numRedirects == 0 {
page, ok := cache.GetPage(u) page, ok := cache.GetPage(u)
if ok { if ok {
setPage(t, page) t.setPage(page)
return ret(u, true) return ret(u, true)
} }
} }
@ -344,7 +344,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
go cache.AddPage(page) go cache.AddPage(page)
} }
setPage(t, page) t.setPage(page)
return ret(u, true) return ret(u, true)
} }
// Not displayable // Not displayable
@ -370,7 +370,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
Error("Input Error", "URL for that input would be too long.") Error("Input Error", "URL for that input would be too long.")
return ret("", false) return ret("", false)
} }
return ret(handleURL(t, parsed.String(), 0)) return ret(t.handleURL(parsed.String(), 0))
} }
return ret("", false) return ret("", false)
case 30, 31: case 30, 31:
@ -395,7 +395,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
if res.Status == gemini.StatusRedirectPermanent { if res.Status == gemini.StatusRedirectPermanent {
go cache.AddRedir(u, redir) go cache.AddRedir(u, redir)
} }
return ret(handleURL(t, redir, numRedirects+1)) return ret(t.handleURL(redir, numRedirects+1))
} }
return ret("", false) return ret("", false)
case 40: case 40:

View File

@ -2,11 +2,11 @@ package display
// applyHist is a history.go internal function, to load a URL in the history. // applyHist is a history.go internal function, to load a URL in the history.
func applyHist(t *tab) { func applyHist(t *tab) {
handleURL(t, t.history.urls[t.history.pos], 0) // Load that position in history t.handleURL(t.history.urls[t.history.pos], 0) // Load that position in history
t.applyAll() t.applyAll()
} }
func histForward(t *tab) { func (t *tab) histForward() {
if t.history.pos >= len(t.history.urls)-1 { if t.history.pos >= len(t.history.urls)-1 {
// Already on the most recent URL in the history // Already on the most recent URL in the history
return return
@ -15,7 +15,7 @@ func histForward(t *tab) {
go applyHist(t) go applyHist(t)
} }
func histBack(t *tab) { func (t *tab) histBack() {
if t.history.pos <= 0 { if t.history.pos <= 0 {
// First tab in history // First tab in history
return return

View File

@ -15,21 +15,21 @@ import (
// followLink should be used when the user "clicks" a link on a page. // followLink should be used when the user "clicks" a link on a page.
// Not when a URL is opened on a new tab for the first time. // Not when a URL is opened on a new tab for the first time.
// It will handle setting the bottomBar. // It will handle setting the bottomBar.
func followLink(t *tab, prev, next string) { func (t *tab) followLink(next string) {
if strings.HasPrefix(next, "about:") { if strings.HasPrefix(next, "about:") {
if final, ok := handleAbout(t, next); ok { if final, ok := t.handleAbout(next); ok {
t.addToHistory(final) t.addToHistory(final)
} }
return return
} }
if t.hasContent() && !t.isAnAboutPage() { if t.hasContent() && !t.isAnAboutPage() {
nextURL, err := resolveRelLink(t, prev, next) nextURL, err := t.resolveRelLink(next)
if err != nil { if err != nil {
Error("URL Error", err.Error()) Error("URL Error", err.Error())
return return
} }
go goURL(t, nextURL) go t.goURL(nextURL)
return return
} }
// No content on current tab, so the "prev" URL is not valid. // No content on current tab, so the "prev" URL is not valid.
@ -39,7 +39,7 @@ func followLink(t *tab, prev, next string) {
Error("URL Error", "Link URL could not be parsed") Error("URL Error", "Link URL could not be parsed")
return return
} }
go goURL(t, next) go t.goURL(next)
} }
// reformatPage will take the raw page content and reformat it according to the current terminal dimensions. // reformatPage will take the raw page content and reformat it according to the current terminal dimensions.
@ -79,13 +79,13 @@ func reformatPage(p *structs.Page) {
// reformatPageAndSetView is for reformatting a page that is already being displayed. // reformatPageAndSetView is for reformatting a page that is already being displayed.
// setPage should be used when a page is being loaded for the first time. // setPage should be used when a page is being loaded for the first time.
func reformatPageAndSetView(t *tab, p *structs.Page) { func (t *tab) reformatPageAndSetView() {
if p.TermWidth == termW { if t.page.TermWidth == termW {
// No changes to make // No changes to make
return return
} }
reformatPage(p) reformatPage(t.page)
t.view.SetText(p.Content) t.view.SetText(t.page.Content)
t.applyScroll() // Go back to where you were, roughly t.applyScroll() // Go back to where you were, roughly
App.Draw() App.Draw()
@ -93,7 +93,7 @@ func reformatPageAndSetView(t *tab, p *structs.Page) {
// setPage displays a Page on the passed tab number. // setPage displays a Page on the passed tab number.
// The bottomBar is not actually changed in this func // The bottomBar is not actually changed in this func
func setPage(t *tab, p *structs.Page) { func (t *tab) setPage(p *structs.Page) {
if !isValidTab(t) { if !isValidTab(t) {
// Don't waste time reformatting an invalid tab // Don't waste time reformatting an invalid tab
return return
@ -130,8 +130,8 @@ func setPage(t *tab, p *structs.Page) {
// It has no return values to be processed. // It has no return values to be processed.
// //
// It should be called in a goroutine. // It should be called in a goroutine.
func goURL(t *tab, u string) { func (t *tab) goURL(u string) {
final, displayed := handleURL(t, u, 0) final, displayed := t.handleURL(u, 0)
if displayed { if displayed {
t.addToHistory(final) t.addToHistory(final)
} }

View File

@ -65,7 +65,7 @@ func Subscriptions(t *tab, u string) string {
// Retrieve cached version if there hasn't been any updates // Retrieve cached version if there hasn't been any updates
p, ok := cache.GetPage(u) p, ok := cache.GetPage(u)
if subscriptionPageUpdated[pageN].After(subscriptions.LastUpdated) && ok { if subscriptionPageUpdated[pageN].After(subscriptions.LastUpdated) && ok {
setPage(t, p) t.setPage(p)
t.applyBottomBar() t.applyBottomBar()
return u return u
} }
@ -155,7 +155,7 @@ func Subscriptions(t *tab, u string) string {
Mediatype: structs.TextGemini, Mediatype: structs.TextGemini,
} }
go cache.AddPage(&page) go cache.AddPage(&page)
setPage(t, &page) t.setPage(&page)
t.applyBottomBar() t.applyBottomBar()
subscriptionPageUpdated[pageN] = time.Now() subscriptionPageUpdated[pageN] = time.Now()
@ -194,7 +194,7 @@ func ManageSubscriptions(t *tab, u string) {
Mediatype: structs.TextGemini, Mediatype: structs.TextGemini,
} }
go cache.AddPage(&page) go cache.AddPage(&page)
setPage(t, &page) t.setPage(&page)
t.applyBottomBar() t.applyBottomBar()
} }

View File

@ -86,7 +86,7 @@ func makeNewTab() *tab {
linkN, _ := strconv.Atoi(currentSelection[0]) linkN, _ := strconv.Atoi(currentSelection[0])
tabs[tab].page.Selected = tabs[tab].page.Links[linkN] tabs[tab].page.Selected = tabs[tab].page.Links[linkN]
tabs[tab].page.SelectedID = currentSelection[0] tabs[tab].page.SelectedID = currentSelection[0]
followLink(tabs[tab], tabs[tab].page.URL, tabs[tab].page.Links[linkN]) tabs[tab].followLink(tabs[tab].page.Links[linkN])
return return
} }
if len(currentSelection) == 0 && (key == tcell.KeyEnter || key == tcell.KeyTab) { if len(currentSelection) == 0 && (key == tcell.KeyEnter || key == tcell.KeyTab) {
@ -165,10 +165,10 @@ func makeNewTab() *tab {
} }
return nil return nil
case config.CmdBack: case config.CmdBack:
histBack(&t) t.histBack()
return nil return nil
case config.CmdForward: case config.CmdForward:
histForward(&t) t.histForward()
return nil return nil
case config.CmdSub: case config.CmdSub:
Subscriptions(&t, "about:subscriptions") Subscriptions(&t, "about:subscriptions")
@ -209,7 +209,7 @@ func makeNewTab() *tab {
if cmd >= config.CmdLink1 && cmd <= config.CmdLink0 { if cmd >= config.CmdLink1 && cmd <= config.CmdLink0 {
if int(cmd) <= len(t.page.Links) { if int(cmd) <= len(t.page.Links) {
// It's a valid link number // It's a valid link number
followLink(&t, t.page.URL, t.page.Links[cmd-1]) t.followLink(t.page.Links[cmd-1])
return nil return nil
} }
} }

View File

@ -88,15 +88,15 @@ func textWidth() int {
return viper.GetInt("a-general.max_width") return viper.GetInt("a-general.max_width")
} }
// resolveRelLink returns an absolute link for the given absolute link and relative one. // resolveRelLink returns an absolute link for the given relative link.
// It also returns an error if it could not resolve the links, which should be displayed // It also returns an error if it could not resolve the links, which should be displayed
// to the user. // to the user.
func resolveRelLink(t *tab, prev, next string) (string, error) { func (t *tab) resolveRelLink(next string) (string, error) {
if !t.hasContent() || t.isAnAboutPage() { if !t.hasContent() || t.isAnAboutPage() {
return next, nil return next, nil
} }
prevParsed, _ := url.Parse(prev) prevParsed, _ := url.Parse(t.page.URL)
nextParsed, err := url.Parse(next) nextParsed, err := url.Parse(next)
if err != nil { if err != nil {
return "", errors.New("link URL could not be parsed") //nolint:goerr113 return "", errors.New("link URL could not be parsed") //nolint:goerr113