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:
parent
c1ecbf71b9
commit
32175a662c
3
NOTES.md
3
NOTES.md
@ -1,7 +1,8 @@
|
||||
# Notes
|
||||
|
||||
## 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
|
||||
- Also handle non-network pages like `about:` pages, where `Raw` already exists and just needs to be rendered
|
||||
|
||||
|
@ -136,7 +136,7 @@ func Bookmarks(t *tab) {
|
||||
TermWidth: termW,
|
||||
Mediatype: structs.TextGemini,
|
||||
}
|
||||
setPage(t, &page)
|
||||
t.setPage(&page)
|
||||
t.applyBottomBar()
|
||||
}
|
||||
|
||||
|
@ -78,7 +78,7 @@ func Init(version, commit, builtBy string) {
|
||||
)
|
||||
if tabs[i] == t {
|
||||
// Reformat page ASAP, in the middle of loop
|
||||
reformatPageAndSetView(t, t.page)
|
||||
t.reformatPageAndSetView()
|
||||
}
|
||||
}
|
||||
App.Draw()
|
||||
@ -230,7 +230,7 @@ func Init(version, commit, builtBy string) {
|
||||
}
|
||||
if i <= len(tabs[tab].page.Links) && i > 0 {
|
||||
// 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
|
||||
}
|
||||
// Invalid link number, don't do anything
|
||||
@ -321,7 +321,7 @@ func Init(version, commit, builtBy string) {
|
||||
switch cmd {
|
||||
case config.CmdNewTab:
|
||||
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 {
|
||||
Error("URL Error", err.Error())
|
||||
return nil
|
||||
@ -392,7 +392,7 @@ func NewTab() {
|
||||
|
||||
tabs = append(tabs, makeNewTab())
|
||||
temp := newTabPage // Copy
|
||||
setPage(tabs[curTab], &temp)
|
||||
tabs[curTab].setPage(&temp)
|
||||
tabs[curTab].addToHistory("about:newtab")
|
||||
tabs[curTab].history.pos = 0 // Manually set as first page
|
||||
|
||||
@ -468,7 +468,7 @@ func SwitchTab(tab int) {
|
||||
curTab = tab % NumTabs()
|
||||
|
||||
// Display tab
|
||||
reformatPageAndSetView(tabs[curTab], tabs[curTab].page)
|
||||
tabs[curTab].reformatPageAndSetView()
|
||||
browser.SetCurrentTab(strconv.Itoa(curTab))
|
||||
tabs[curTab].applyAll()
|
||||
|
||||
@ -490,7 +490,7 @@ func Reload() {
|
||||
Mediatype: structs.TextGemini,
|
||||
}
|
||||
temp := newTabPage // Copy
|
||||
setPage(tabs[curTab], &temp)
|
||||
tabs[curTab].setPage(&temp)
|
||||
return
|
||||
}
|
||||
|
||||
@ -500,7 +500,7 @@ func Reload() {
|
||||
|
||||
go func(t *tab) {
|
||||
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] {
|
||||
// Display the bottomBar state that handleURL set
|
||||
t.applyBottomBar()
|
||||
@ -513,13 +513,13 @@ func Reload() {
|
||||
func URL(u string) {
|
||||
t := tabs[curTab]
|
||||
if strings.HasPrefix(u, "about:") {
|
||||
if final, ok := handleAbout(t, u); ok {
|
||||
if final, ok := t.handleAbout(u); ok {
|
||||
t.addToHistory(final)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
go goURL(t, fixUserURL(u))
|
||||
go t.goURL(fixUserURL(u))
|
||||
}
|
||||
|
||||
func NumTabs() int {
|
||||
|
@ -95,7 +95,7 @@ func handleOther(u string) {
|
||||
// It returns the URL displayed, and a bool indicating if the provided
|
||||
// URL could be handled. The string returned will always be empty
|
||||
// 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:") {
|
||||
return "", false
|
||||
}
|
||||
@ -106,27 +106,27 @@ func handleAbout(t *tab, u string) (string, bool) {
|
||||
return u, true
|
||||
case "about:newtab":
|
||||
temp := newTabPage // Copy
|
||||
setPage(t, &temp)
|
||||
t.setPage(&temp)
|
||||
t.applyBottomBar()
|
||||
return u, true
|
||||
case "about:version":
|
||||
temp := versionPage
|
||||
setPage(t, &temp)
|
||||
t.setPage(&temp)
|
||||
t.applyBottomBar()
|
||||
return u, true
|
||||
case "about:license":
|
||||
temp := licensePage
|
||||
setPage(t, &temp)
|
||||
t.setPage(&temp)
|
||||
t.applyBottomBar()
|
||||
return u, true
|
||||
case "about:thanks":
|
||||
temp := thanksPage
|
||||
setPage(t, &temp)
|
||||
t.setPage(&temp)
|
||||
t.applyBottomBar()
|
||||
return u, true
|
||||
case "about:about":
|
||||
temp := aboutPage
|
||||
setPage(t, &temp)
|
||||
t.setPage(&temp)
|
||||
t.applyBottomBar()
|
||||
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.
|
||||
// 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
|
||||
|
||||
// Save for resetting on error
|
||||
@ -201,7 +201,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
|
||||
App.SetFocus(t.view)
|
||||
|
||||
if strings.HasPrefix(u, "about:") {
|
||||
return ret(handleAbout(t, u))
|
||||
return ret(t.handleAbout(u))
|
||||
}
|
||||
|
||||
u = normalizeURL(u)
|
||||
@ -237,7 +237,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
|
||||
if !ok {
|
||||
return ret("", false)
|
||||
}
|
||||
setPage(t, page)
|
||||
t.setPage(page)
|
||||
return ret(u, true)
|
||||
}
|
||||
|
||||
@ -258,7 +258,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
|
||||
if numRedirects == 0 {
|
||||
page, ok := cache.GetPage(u)
|
||||
if ok {
|
||||
setPage(t, page)
|
||||
t.setPage(page)
|
||||
return ret(u, true)
|
||||
}
|
||||
}
|
||||
@ -344,7 +344,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
|
||||
go cache.AddPage(page)
|
||||
}
|
||||
|
||||
setPage(t, page)
|
||||
t.setPage(page)
|
||||
return ret(u, true)
|
||||
}
|
||||
// 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.")
|
||||
return ret("", false)
|
||||
}
|
||||
return ret(handleURL(t, parsed.String(), 0))
|
||||
return ret(t.handleURL(parsed.String(), 0))
|
||||
}
|
||||
return ret("", false)
|
||||
case 30, 31:
|
||||
@ -395,7 +395,7 @@ func handleURL(t *tab, u string, numRedirects int) (string, bool) {
|
||||
if res.Status == gemini.StatusRedirectPermanent {
|
||||
go cache.AddRedir(u, redir)
|
||||
}
|
||||
return ret(handleURL(t, redir, numRedirects+1))
|
||||
return ret(t.handleURL(redir, numRedirects+1))
|
||||
}
|
||||
return ret("", false)
|
||||
case 40:
|
||||
|
@ -2,11 +2,11 @@ package display
|
||||
|
||||
// applyHist is a history.go internal function, to load a URL in the history.
|
||||
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()
|
||||
}
|
||||
|
||||
func histForward(t *tab) {
|
||||
func (t *tab) histForward() {
|
||||
if t.history.pos >= len(t.history.urls)-1 {
|
||||
// Already on the most recent URL in the history
|
||||
return
|
||||
@ -15,7 +15,7 @@ func histForward(t *tab) {
|
||||
go applyHist(t)
|
||||
}
|
||||
|
||||
func histBack(t *tab) {
|
||||
func (t *tab) histBack() {
|
||||
if t.history.pos <= 0 {
|
||||
// First tab in history
|
||||
return
|
||||
|
@ -15,21 +15,21 @@ import (
|
||||
// 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.
|
||||
// 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 final, ok := handleAbout(t, next); ok {
|
||||
if final, ok := t.handleAbout(next); ok {
|
||||
t.addToHistory(final)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
if t.hasContent() && !t.isAnAboutPage() {
|
||||
nextURL, err := resolveRelLink(t, prev, next)
|
||||
nextURL, err := t.resolveRelLink(next)
|
||||
if err != nil {
|
||||
Error("URL Error", err.Error())
|
||||
return
|
||||
}
|
||||
go goURL(t, nextURL)
|
||||
go t.goURL(nextURL)
|
||||
return
|
||||
}
|
||||
// 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")
|
||||
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.
|
||||
@ -79,13 +79,13 @@ func reformatPage(p *structs.Page) {
|
||||
|
||||
// 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.
|
||||
func reformatPageAndSetView(t *tab, p *structs.Page) {
|
||||
if p.TermWidth == termW {
|
||||
func (t *tab) reformatPageAndSetView() {
|
||||
if t.page.TermWidth == termW {
|
||||
// No changes to make
|
||||
return
|
||||
}
|
||||
reformatPage(p)
|
||||
t.view.SetText(p.Content)
|
||||
reformatPage(t.page)
|
||||
t.view.SetText(t.page.Content)
|
||||
t.applyScroll() // Go back to where you were, roughly
|
||||
|
||||
App.Draw()
|
||||
@ -93,7 +93,7 @@ func reformatPageAndSetView(t *tab, p *structs.Page) {
|
||||
|
||||
// setPage displays a Page on the passed tab number.
|
||||
// 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) {
|
||||
// Don't waste time reformatting an invalid tab
|
||||
return
|
||||
@ -130,8 +130,8 @@ func setPage(t *tab, p *structs.Page) {
|
||||
// It has no return values to be processed.
|
||||
//
|
||||
// It should be called in a goroutine.
|
||||
func goURL(t *tab, u string) {
|
||||
final, displayed := handleURL(t, u, 0)
|
||||
func (t *tab) goURL(u string) {
|
||||
final, displayed := t.handleURL(u, 0)
|
||||
if displayed {
|
||||
t.addToHistory(final)
|
||||
}
|
||||
|
@ -65,7 +65,7 @@ func Subscriptions(t *tab, u string) string {
|
||||
// Retrieve cached version if there hasn't been any updates
|
||||
p, ok := cache.GetPage(u)
|
||||
if subscriptionPageUpdated[pageN].After(subscriptions.LastUpdated) && ok {
|
||||
setPage(t, p)
|
||||
t.setPage(p)
|
||||
t.applyBottomBar()
|
||||
return u
|
||||
}
|
||||
@ -155,7 +155,7 @@ func Subscriptions(t *tab, u string) string {
|
||||
Mediatype: structs.TextGemini,
|
||||
}
|
||||
go cache.AddPage(&page)
|
||||
setPage(t, &page)
|
||||
t.setPage(&page)
|
||||
t.applyBottomBar()
|
||||
|
||||
subscriptionPageUpdated[pageN] = time.Now()
|
||||
@ -194,7 +194,7 @@ func ManageSubscriptions(t *tab, u string) {
|
||||
Mediatype: structs.TextGemini,
|
||||
}
|
||||
go cache.AddPage(&page)
|
||||
setPage(t, &page)
|
||||
t.setPage(&page)
|
||||
t.applyBottomBar()
|
||||
}
|
||||
|
||||
|
@ -86,7 +86,7 @@ func makeNewTab() *tab {
|
||||
linkN, _ := strconv.Atoi(currentSelection[0])
|
||||
tabs[tab].page.Selected = tabs[tab].page.Links[linkN]
|
||||
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
|
||||
}
|
||||
if len(currentSelection) == 0 && (key == tcell.KeyEnter || key == tcell.KeyTab) {
|
||||
@ -165,10 +165,10 @@ func makeNewTab() *tab {
|
||||
}
|
||||
return nil
|
||||
case config.CmdBack:
|
||||
histBack(&t)
|
||||
t.histBack()
|
||||
return nil
|
||||
case config.CmdForward:
|
||||
histForward(&t)
|
||||
t.histForward()
|
||||
return nil
|
||||
case config.CmdSub:
|
||||
Subscriptions(&t, "about:subscriptions")
|
||||
@ -209,7 +209,7 @@ func makeNewTab() *tab {
|
||||
if cmd >= config.CmdLink1 && cmd <= config.CmdLink0 {
|
||||
if int(cmd) <= len(t.page.Links) {
|
||||
// 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
|
||||
}
|
||||
}
|
||||
|
@ -88,15 +88,15 @@ func textWidth() int {
|
||||
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
|
||||
// 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() {
|
||||
return next, nil
|
||||
}
|
||||
|
||||
prevParsed, _ := url.Parse(prev)
|
||||
prevParsed, _ := url.Parse(t.page.URL)
|
||||
nextParsed, err := url.Parse(next)
|
||||
if err != nil {
|
||||
return "", errors.New("link URL could not be parsed") //nolint:goerr113
|
||||
|
Loading…
Reference in New Issue
Block a user