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

143 lines
3.8 KiB
Go
Raw Normal View History

2020-06-18 16:54:48 -04:00
package display
import (
"net/url"
2020-08-05 13:31:59 -04:00
"strconv"
2020-06-18 16:54:48 -04:00
"strings"
2021-03-01 15:35:47 -05:00
"github.com/makeworld-the-better-one/amfora/render"
2020-06-18 16:54:48 -04:00
"github.com/makeworld-the-better-one/amfora/structs"
)
// This file contains the functions that aren't part of the public API.
// The funcs are for network and displaying.
2020-07-01 13:39:13 -04:00
2020-06-18 16:54:48 -04:00
// 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) {
if strings.HasPrefix(next, "about:") {
2020-12-06 20:57:57 -05:00
if final, ok := handleAbout(t, next); ok {
t.addToHistory(final)
}
return
}
if t.hasContent() && !t.isAnAboutPage() {
nextURL, err := resolveRelLink(t, prev, next)
if err != nil {
2020-07-01 13:39:13 -04:00
Error("URL Error", err.Error())
return
}
go goURL(t, nextURL)
return
}
// No content on current tab, so the "prev" URL is not valid.
// An example is the about:newtab page
_, err := url.Parse(next)
2020-06-18 16:54:48 -04:00
if err != nil {
2020-06-19 14:05:05 -04:00
Error("URL Error", "Link URL could not be parsed")
2020-06-18 16:54:48 -04:00
return
}
go goURL(t, next)
2020-06-18 16:54:48 -04:00
}
// reformatPage will take the raw page content and reformat it according to the current terminal dimensions.
// It should be called when the terminal size changes.
// It will not waste resources if the passed page is already fitted to the current terminal width, and can be
// called safely even when the page might be already formatted properly.
func reformatPage(p *structs.Page) {
2021-02-27 18:17:49 -05:00
if p.TermWidth == termW {
// No changes to make
return
}
2021-03-01 15:35:47 -05:00
// TODO: Setup a render.RenderFromMediatype func so this isn't needed
var rendered string
switch p.Mediatype {
case structs.TextGemini:
// Links are not recorded because they won't change
proxied := true
if strings.HasPrefix(p.URL, "gemini") ||
strings.HasPrefix(p.URL, "about") ||
strings.HasPrefix(p.URL, "file") {
proxied = false
}
2021-03-01 15:35:47 -05:00
rendered, _ = render.RenderGemini(p.Raw, textWidth(), proxied)
case structs.TextPlain:
2021-03-01 15:35:47 -05:00
rendered = render.RenderPlainText(p.Raw)
case structs.TextAnsi:
2021-03-01 15:35:47 -05:00
rendered = render.RenderANSI(p.Raw)
default:
// Rendering this type is not implemented
return
}
p.Content = rendered
2021-02-27 18:17:49 -05:00
p.TermWidth = termW
}
2020-07-04 11:32:11 -04:00
// 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) {
2021-02-27 18:17:49 -05:00
if p.TermWidth == termW {
// No changes to make
return
}
2020-07-03 12:22:44 -04:00
reformatPage(p)
t.view.SetText(p.Content)
t.applyScroll() // Go back to where you were, roughly
App.Draw()
}
// 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) {
if !isValidTab(t) {
// Don't waste time reformatting an invalid tab
return
}
// Make sure the page content is fitted to the terminal every time it's displayed
reformatPage(p)
t.page = p
2020-08-05 13:31:59 -04:00
// Change page on screen
t.view.SetText(p.Content)
t.view.Highlight("") // Turn off highlights, other funcs may restore if necessary
t.view.ScrollToBeginning()
2021-02-27 18:17:49 -05:00
// Reset page left margin
tabNum := tabNumber(t)
2021-02-27 18:17:49 -05:00
browser.AddTab(
strconv.Itoa(tabNum),
makeTabLabel(strconv.Itoa(tabNum+1)),
makeContentLayout(t.view, leftMargin()),
)
App.Draw()
2020-06-18 16:54:48 -04:00
// Setup display
App.SetFocus(t.view)
// Save bottom bar for the tab - other funcs will apply/display it
t.barLabel = ""
t.barText = p.URL
}
// goURL is like handleURL, but takes care of history and the bottomBar.
// It should be preferred over handleURL in most cases.
// 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)
if displayed {
t.addToHistory(final)
}
if t == tabs[curTab] {
// Display the bottomBar state that handleURL set
t.applyBottomBar()
}
2020-06-18 16:54:48 -04:00
}