2020-06-18 23:54:48 +03:00
|
|
|
package display
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-06-29 20:56:27 +03:00
|
|
|
"net/url"
|
2020-12-20 23:13:26 +03:00
|
|
|
"regexp"
|
2020-06-18 23:54:48 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2021-04-07 19:56:32 +03:00
|
|
|
"code.rocketnine.space/tslocum/cview"
|
2021-02-17 22:17:13 +03:00
|
|
|
"github.com/gdamore/tcell/v2"
|
2020-06-18 23:54:48 +03:00
|
|
|
"github.com/makeworld-the-better-one/amfora/cache"
|
2021-12-23 08:23:06 +03:00
|
|
|
"github.com/makeworld-the-better-one/amfora/client"
|
2020-07-28 23:58:32 +03:00
|
|
|
"github.com/makeworld-the-better-one/amfora/config"
|
2020-06-24 03:07:25 +03:00
|
|
|
"github.com/makeworld-the-better-one/amfora/renderer"
|
2020-06-18 23:54:48 +03:00
|
|
|
"github.com/makeworld-the-better-one/amfora/structs"
|
2020-08-28 03:04:02 +03:00
|
|
|
"github.com/makeworld-the-better-one/go-gemini"
|
2021-12-11 07:09:31 +03:00
|
|
|
"github.com/muesli/termenv"
|
2020-06-24 03:07:25 +03:00
|
|
|
"github.com/spf13/viper"
|
2020-06-18 23:54:48 +03:00
|
|
|
)
|
|
|
|
|
2020-07-08 04:13:45 +03:00
|
|
|
var tabs []*tab // Slice of all the current browser tabs
|
|
|
|
var curTab = -1 // What tab is currently visible - index for the tabs slice (-1 means there are no tabs)
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2020-06-29 22:01:41 +03:00
|
|
|
// Terminal dimensions
|
2020-06-21 23:53:12 +03:00
|
|
|
var termW int
|
2020-06-29 22:01:41 +03:00
|
|
|
var termH int
|
2020-06-21 23:53:12 +03:00
|
|
|
|
2020-06-18 23:54:48 +03:00
|
|
|
// The user input and URL display bar at the bottom
|
2020-07-28 23:58:32 +03:00
|
|
|
var bottomBar = cview.NewInputField()
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2020-12-20 23:13:26 +03:00
|
|
|
// When the bottom bar string has a space, this regex decides whether it's
|
|
|
|
// a non-encoded URL or a search string.
|
|
|
|
// See this comment for details:
|
|
|
|
// https://github.com/makeworld-the-better-one/amfora/issues/138#issuecomment-740961292
|
|
|
|
var hasSpaceisURL = regexp.MustCompile(`[^ ]+\.[^ ].*/.`)
|
|
|
|
|
2020-06-18 23:54:48 +03:00
|
|
|
// Viewer for the tab primitives
|
|
|
|
// Pages are named as strings of tab numbers - so the textview for the first tab
|
|
|
|
// is held in the page named "0".
|
2020-06-24 03:07:25 +03:00
|
|
|
// The only pages that don't confine to this scheme are those named after modals,
|
|
|
|
// which are used to draw modals on top the current tab.
|
2020-06-18 23:54:48 +03:00
|
|
|
// Ex: "info", "error", "input", "yesno"
|
2021-02-17 22:17:13 +03:00
|
|
|
var panels = cview.NewPanels()
|
|
|
|
|
|
|
|
// Tabbed viewer for primitives
|
|
|
|
// Panels are named as strings of tab numbers - so the textview for the first tab
|
|
|
|
// is held in the page named "0".
|
|
|
|
var browser = cview.NewTabbedPanels()
|
2020-06-18 23:54:48 +03:00
|
|
|
|
|
|
|
// Root layout
|
2021-02-17 22:17:13 +03:00
|
|
|
var layout = cview.NewFlex()
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2020-07-08 04:13:45 +03:00
|
|
|
var newTabPage structs.Page
|
2020-07-02 03:37:34 +03:00
|
|
|
|
2021-02-17 22:17:13 +03:00
|
|
|
var App = cview.NewApplication()
|
|
|
|
|
|
|
|
func Init(version, commit, builtBy string) {
|
|
|
|
aboutInit(version, commit, builtBy)
|
|
|
|
|
2021-12-11 07:09:31 +03:00
|
|
|
// Detect terminal colors for syntax highlighting
|
|
|
|
switch termenv.ColorProfile() {
|
|
|
|
case termenv.TrueColor:
|
|
|
|
renderer.TermColor = "terminal16m"
|
|
|
|
case termenv.ANSI256:
|
|
|
|
renderer.TermColor = "terminal256"
|
|
|
|
case termenv.ANSI:
|
|
|
|
renderer.TermColor = "terminal16"
|
|
|
|
case termenv.Ascii:
|
|
|
|
renderer.TermColor = ""
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:17:13 +03:00
|
|
|
App.EnableMouse(false)
|
|
|
|
App.SetRoot(layout, true)
|
|
|
|
App.SetAfterResizeFunc(func(width int, height int) {
|
2020-06-22 06:49:43 +03:00
|
|
|
// Store for calculations
|
2020-06-21 23:53:12 +03:00
|
|
|
termW = width
|
2020-06-29 22:01:41 +03:00
|
|
|
termH = height
|
2020-06-22 00:15:21 +03:00
|
|
|
|
2020-07-03 06:55:24 +03:00
|
|
|
// Make sure the current tab content is reformatted when the terminal size changes
|
2022-04-26 03:34:04 +03:00
|
|
|
for i := range tabs {
|
|
|
|
// Overwrite all tabs with a new, differently sized, left margin
|
|
|
|
browser.AddTab(
|
|
|
|
strconv.Itoa(i),
|
|
|
|
tabs[i].label(),
|
|
|
|
makeContentLayout(tabs[i].view, leftMargin()),
|
|
|
|
)
|
|
|
|
if tabs[i] == tabs[curTab] {
|
|
|
|
// Reformat page ASAP, in the middle of loop
|
|
|
|
reformatPageAndSetView(tabs[curTab], tabs[curTab].page)
|
2021-02-17 22:17:13 +03:00
|
|
|
}
|
2022-04-26 03:34:04 +03:00
|
|
|
}
|
2020-06-21 23:53:12 +03:00
|
|
|
})
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2021-12-08 04:18:08 +03:00
|
|
|
panels.AddPanel(PanelBrowser, browser, true, true)
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2020-07-01 20:39:13 +03:00
|
|
|
helpInit()
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2021-02-17 22:17:13 +03:00
|
|
|
layout.SetDirection(cview.FlexRow)
|
|
|
|
layout.AddItem(panels, 0, 1, true)
|
|
|
|
layout.AddItem(bottomBar, 1, 1, false)
|
2020-07-28 23:58:32 +03:00
|
|
|
|
2020-06-24 18:37:32 +03:00
|
|
|
if viper.GetBool("a-general.color") {
|
2020-07-28 23:58:32 +03:00
|
|
|
bottomBar.SetBackgroundColor(config.GetColor("bottombar_bg"))
|
2021-02-17 22:17:13 +03:00
|
|
|
bottomBar.SetLabelColor(config.GetColor("bottombar_label"))
|
|
|
|
bottomBar.SetFieldBackgroundColor(config.GetColor("bottombar_bg"))
|
|
|
|
bottomBar.SetFieldTextColor(config.GetColor("bottombar_text"))
|
|
|
|
|
|
|
|
browser.SetTabBackgroundColor(config.GetColor("bg"))
|
|
|
|
browser.SetTabBackgroundColorFocused(config.GetColor("tab_num"))
|
|
|
|
browser.SetTabTextColor(config.GetColor("tab_num"))
|
2021-12-04 01:58:16 +03:00
|
|
|
browser.SetTabTextColorFocused(config.GetColor("ColorBg"))
|
2021-02-17 22:17:13 +03:00
|
|
|
browser.SetTabSwitcherDivider(
|
|
|
|
"",
|
|
|
|
fmt.Sprintf("[%s:%s]|[-]", config.GetColorString("tab_divider"), config.GetColorString("bg")),
|
|
|
|
fmt.Sprintf("[%s:%s]|[-]", config.GetColorString("tab_divider"), config.GetColorString("bg")),
|
|
|
|
)
|
|
|
|
browser.Switcher.SetBackgroundColor(config.GetColor("bg"))
|
2020-06-24 18:37:32 +03:00
|
|
|
} else {
|
2020-07-28 23:58:32 +03:00
|
|
|
bottomBar.SetBackgroundColor(tcell.ColorWhite)
|
2021-02-17 22:17:13 +03:00
|
|
|
bottomBar.SetLabelColor(tcell.ColorBlack)
|
|
|
|
bottomBar.SetFieldBackgroundColor(tcell.ColorWhite)
|
|
|
|
bottomBar.SetFieldTextColor(tcell.ColorBlack)
|
|
|
|
|
|
|
|
browser.SetTabBackgroundColor(tcell.ColorBlack)
|
|
|
|
browser.SetTabBackgroundColorFocused(tcell.ColorWhite)
|
|
|
|
browser.SetTabTextColor(tcell.ColorWhite)
|
|
|
|
browser.SetTabTextColorFocused(tcell.ColorBlack)
|
|
|
|
browser.SetTabSwitcherDivider(
|
|
|
|
"",
|
|
|
|
"[#ffffff:#000000]|[-]",
|
|
|
|
"[#ffffff:#000000]|[-]",
|
|
|
|
)
|
2020-06-24 18:37:32 +03:00
|
|
|
}
|
2021-02-17 22:17:13 +03:00
|
|
|
|
2020-06-18 23:54:48 +03:00
|
|
|
bottomBar.SetDoneFunc(func(key tcell.Key) {
|
2020-07-08 04:13:45 +03:00
|
|
|
tab := curTab
|
|
|
|
|
|
|
|
// Reset func to set the bottomBar back to what it was before
|
|
|
|
// Use for errors.
|
|
|
|
reset := func() {
|
|
|
|
bottomBar.SetLabel("")
|
|
|
|
tabs[tab].applyAll()
|
|
|
|
App.SetFocus(tabs[tab].view)
|
|
|
|
}
|
2020-06-29 20:56:27 +03:00
|
|
|
|
2020-08-27 18:47:57 +03:00
|
|
|
//nolint:exhaustive
|
2020-06-18 23:54:48 +03:00
|
|
|
switch key {
|
|
|
|
case tcell.KeyEnter:
|
2020-06-21 23:53:12 +03:00
|
|
|
// Figure out whether it's a URL, link number, or search
|
|
|
|
// And send out a request
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2020-06-21 00:17:34 +03:00
|
|
|
query := bottomBar.GetText()
|
|
|
|
|
|
|
|
if strings.TrimSpace(query) == "" {
|
2020-06-18 23:54:48 +03:00
|
|
|
// Ignore
|
2020-07-08 04:13:45 +03:00
|
|
|
reset()
|
2020-06-18 23:54:48 +03:00
|
|
|
return
|
|
|
|
}
|
2021-05-13 23:38:53 +03:00
|
|
|
if query[0] == '.' && tabs[tab].hasContent() && !tabs[tab].isAnAboutPage() {
|
2020-08-19 02:48:10 +03:00
|
|
|
// Relative url
|
2020-08-26 04:03:21 +03:00
|
|
|
current, err := url.Parse(tabs[tab].page.URL)
|
2020-06-29 20:56:27 +03:00
|
|
|
if err != nil {
|
|
|
|
// This shouldn't occur
|
|
|
|
return
|
|
|
|
}
|
2020-11-05 04:41:09 +03:00
|
|
|
|
|
|
|
if query == ".." && tabs[tab].page.URL[len(tabs[tab].page.URL)-1] != '/' {
|
|
|
|
// Support what ".." used to work like
|
|
|
|
// If on /dir/doc.gmi, got to /dir/
|
|
|
|
query = "./"
|
|
|
|
}
|
|
|
|
|
2020-08-19 00:32:53 +03:00
|
|
|
target, err := current.Parse(query)
|
|
|
|
if err != nil {
|
2020-08-19 02:48:10 +03:00
|
|
|
// Invalid relative url
|
2020-06-29 20:56:27 +03:00
|
|
|
return
|
|
|
|
}
|
2020-08-19 00:32:53 +03:00
|
|
|
URL(target.String())
|
2020-06-29 20:56:27 +03:00
|
|
|
return
|
|
|
|
}
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2020-06-21 00:17:34 +03:00
|
|
|
i, err := strconv.Atoi(query)
|
2020-06-18 23:54:48 +03:00
|
|
|
if err != nil {
|
2020-07-01 20:39:13 +03:00
|
|
|
if strings.HasPrefix(query, "new:") && len(query) > 4 {
|
|
|
|
// They're trying to open a link number in a new tab
|
|
|
|
i, err = strconv.Atoi(query[4:])
|
|
|
|
if err != nil {
|
2020-07-08 04:13:45 +03:00
|
|
|
reset()
|
2020-07-01 20:39:13 +03:00
|
|
|
return
|
|
|
|
}
|
2020-07-08 04:13:45 +03:00
|
|
|
if i <= len(tabs[tab].page.Links) && i > 0 {
|
2020-07-01 20:39:13 +03:00
|
|
|
// Open new tab and load link
|
2020-07-08 04:13:45 +03:00
|
|
|
oldTab := tab
|
2020-07-01 20:39:13 +03:00
|
|
|
// Resolve and follow link manually
|
2020-07-08 04:13:45 +03:00
|
|
|
nextParsed, err := url.Parse(tabs[oldTab].page.Links[i-1])
|
2020-07-01 20:39:13 +03:00
|
|
|
if err != nil {
|
|
|
|
Error("URL Error", "link URL could not be parsed")
|
2020-07-08 04:13:45 +03:00
|
|
|
reset()
|
2020-07-01 20:39:13 +03:00
|
|
|
return
|
|
|
|
}
|
2022-04-15 18:33:32 +03:00
|
|
|
if tabs[oldTab].hasContent() && !tabs[oldTab].isAnAboutPage() {
|
|
|
|
prevParsed, _ := url.Parse(tabs[oldTab].page.URL)
|
|
|
|
NewTabWithURL(prevParsed.ResolveReference(nextParsed).String())
|
|
|
|
} else {
|
|
|
|
NewTabWithURL(nextParsed.String())
|
|
|
|
}
|
2020-07-01 20:39:13 +03:00
|
|
|
return
|
|
|
|
}
|
2020-06-21 00:17:34 +03:00
|
|
|
} else {
|
2020-07-01 20:39:13 +03:00
|
|
|
// It's a full URL or search term
|
|
|
|
// Detect if it's a search or URL
|
2021-02-08 05:52:53 +03:00
|
|
|
|
|
|
|
// Remove whitespace from the string.
|
|
|
|
// We don't want to convert legitimate
|
|
|
|
// :// links to search terms.
|
|
|
|
query := strings.TrimSpace(query)
|
2023-05-20 03:44:23 +03:00
|
|
|
if ((strings.Contains(query, " ") && !hasSpaceisURL.MatchString(query)) ||
|
2020-12-21 06:58:25 +03:00
|
|
|
(!strings.HasPrefix(query, "//") && !strings.Contains(query, "://") &&
|
2023-05-20 03:44:23 +03:00
|
|
|
!strings.Contains(query, ".")) && !strings.HasPrefix(query, "about:")) &&
|
|
|
|
!(query == "localhost" || strings.HasPrefix(query, "localhost/") || strings.HasPrefix(query, "localhost:")) {
|
2020-12-21 06:58:25 +03:00
|
|
|
// Has a space and follows regex, OR
|
|
|
|
// doesn't start with "//", contain "://", and doesn't have a dot either.
|
|
|
|
// Then it's a search
|
|
|
|
|
2020-08-28 03:04:02 +03:00
|
|
|
u := viper.GetString("a-general.search") + "?" + gemini.QueryEscape(query)
|
2020-12-25 00:25:39 +03:00
|
|
|
// Don't use the cached version of the search
|
2021-12-23 08:23:06 +03:00
|
|
|
cache.RemovePage(client.NormalizeURL(u))
|
2020-07-02 18:55:41 +03:00
|
|
|
URL(u)
|
2020-07-01 20:39:13 +03:00
|
|
|
} else {
|
|
|
|
// Full URL
|
2020-12-25 00:25:39 +03:00
|
|
|
// Don't use cached version for manually entered URL
|
2021-12-23 08:23:06 +03:00
|
|
|
cache.RemovePage(client.NormalizeURL(client.FixUserURL(query)))
|
2020-07-01 20:39:13 +03:00
|
|
|
URL(query)
|
|
|
|
}
|
|
|
|
return
|
2020-06-21 00:17:34 +03:00
|
|
|
}
|
2020-06-18 23:54:48 +03:00
|
|
|
}
|
2020-07-08 04:13:45 +03:00
|
|
|
if i <= len(tabs[tab].page.Links) && i > 0 {
|
2020-06-21 00:17:34 +03:00
|
|
|
// It's a valid link number
|
2021-12-23 01:09:47 +03:00
|
|
|
go followLink(tabs[tab], tabs[tab].page.URL, tabs[tab].page.Links[i-1])
|
2020-06-18 23:54:48 +03:00
|
|
|
return
|
|
|
|
}
|
2020-07-01 20:39:13 +03:00
|
|
|
// Invalid link number, don't do anything
|
2020-07-08 04:13:45 +03:00
|
|
|
reset()
|
|
|
|
return
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2020-07-29 02:06:00 +03:00
|
|
|
case tcell.KeyEsc:
|
2020-06-18 23:54:48 +03:00
|
|
|
// Set back to what it was
|
2020-07-08 04:13:45 +03:00
|
|
|
reset()
|
|
|
|
return
|
2020-06-18 23:54:48 +03:00
|
|
|
}
|
|
|
|
// Other potential keys are Tab and Backtab, they are ignored
|
|
|
|
})
|
|
|
|
|
|
|
|
// Render the default new tab content ONCE and store it for later
|
2020-11-18 04:56:15 +03:00
|
|
|
// This code is repeated in Reload()
|
2020-09-01 20:55:09 +03:00
|
|
|
newTabContent := getNewTabContent()
|
2021-02-28 05:18:03 +03:00
|
|
|
renderedNewTabContent, newTabLinks := renderer.RenderGemini(newTabContent, textWidth(), false)
|
2020-07-08 04:13:45 +03:00
|
|
|
newTabPage = structs.Page{
|
2021-02-28 05:18:03 +03:00
|
|
|
Raw: newTabContent,
|
|
|
|
Content: renderedNewTabContent,
|
|
|
|
Links: newTabLinks,
|
|
|
|
URL: "about:newtab",
|
|
|
|
TermWidth: -1, // Force reformatting on first display
|
|
|
|
Mediatype: structs.TextGemini,
|
2020-07-03 06:55:24 +03:00
|
|
|
}
|
2020-06-18 23:54:48 +03:00
|
|
|
|
|
|
|
modalInit()
|
|
|
|
|
|
|
|
// Setup map of keys to functions here
|
|
|
|
// Changing tabs, new tab, etc
|
|
|
|
App.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
|
|
|
|
_, ok := App.GetFocus().(*cview.Button)
|
|
|
|
if ok {
|
|
|
|
// It's focused on a modal right now, nothing should interrupt
|
|
|
|
return event
|
|
|
|
}
|
|
|
|
_, ok = App.GetFocus().(*cview.InputField)
|
|
|
|
if ok {
|
|
|
|
// An InputField is in focus, nothing should interrupt
|
|
|
|
return event
|
|
|
|
}
|
2020-07-10 21:37:18 +03:00
|
|
|
_, ok = App.GetFocus().(*cview.Modal)
|
|
|
|
if ok {
|
|
|
|
// It's focused on a modal right now, nothing should interrupt
|
|
|
|
return event
|
|
|
|
}
|
2021-12-08 04:18:08 +03:00
|
|
|
frontPanelName, _ := panels.GetFrontPanel()
|
|
|
|
if frontPanelName == PanelHelp {
|
2020-08-29 18:52:25 +03:00
|
|
|
// It's focused on help right now
|
2021-12-08 04:18:08 +03:00
|
|
|
if config.TranslateKeyEvent(event) == config.CmdQuit {
|
|
|
|
// Allow quit key to work, but nothing else
|
|
|
|
Stop()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Pass everything else directly, inhibiting other keybindings
|
|
|
|
// like for editing the URL
|
2020-08-29 18:52:25 +03:00
|
|
|
return event
|
|
|
|
}
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2020-12-25 00:13:38 +03:00
|
|
|
// To add a configurable global key command, you'll need to update one of
|
|
|
|
// the two switch statements here. You'll also need to add an enum entry in
|
|
|
|
// config/keybindings.go, update KeyInit() in config/keybindings.go, add a default
|
|
|
|
// keybinding in config/config.go and update the help panel in display/help.go
|
|
|
|
|
|
|
|
cmd := config.TranslateKeyEvent(event)
|
2020-07-08 04:13:45 +03:00
|
|
|
if tabs[curTab].mode == tabModeDone {
|
|
|
|
// All the keys and operations that can only work while NOT loading
|
2020-08-27 18:47:57 +03:00
|
|
|
//nolint:exhaustive
|
2020-12-25 00:13:38 +03:00
|
|
|
switch cmd {
|
|
|
|
case config.CmdReload:
|
2020-07-08 04:13:45 +03:00
|
|
|
Reload()
|
|
|
|
return nil
|
2020-12-25 00:13:38 +03:00
|
|
|
case config.CmdHome:
|
2020-07-08 04:13:45 +03:00
|
|
|
URL(viper.GetString("a-general.home"))
|
2020-06-29 04:06:58 +03:00
|
|
|
return nil
|
2020-12-25 00:13:38 +03:00
|
|
|
case config.CmdBottom:
|
|
|
|
// Space starts typing, like Bombadillo
|
|
|
|
bottomBar.SetLabel("[::b]URL/Num./Search: [::-]")
|
|
|
|
bottomBar.SetText("")
|
|
|
|
// Don't save bottom bar, so that whenever you switch tabs, it's not in that mode
|
|
|
|
App.SetFocus(bottomBar)
|
|
|
|
return nil
|
|
|
|
case config.CmdEdit:
|
|
|
|
// Letter e allows to edit current URL
|
|
|
|
bottomBar.SetLabel("[::b]Edit URL: [::-]")
|
|
|
|
bottomBar.SetText(tabs[curTab].page.URL)
|
|
|
|
App.SetFocus(bottomBar)
|
|
|
|
return nil
|
|
|
|
case config.CmdAddSub:
|
2020-11-28 01:01:29 +03:00
|
|
|
go addSubscription()
|
2020-11-18 04:56:15 +03:00
|
|
|
return nil
|
2020-06-29 04:06:58 +03:00
|
|
|
}
|
|
|
|
}
|
2020-08-27 18:47:57 +03:00
|
|
|
|
2020-07-08 04:13:45 +03:00
|
|
|
// All the keys and operations that can work while a tab IS loading
|
2020-08-27 18:47:57 +03:00
|
|
|
//nolint:exhaustive
|
2020-12-25 00:13:38 +03:00
|
|
|
switch cmd {
|
|
|
|
case config.CmdNewTab:
|
2020-07-29 00:16:57 +03:00
|
|
|
if tabs[curTab].page.Mode == structs.ModeLinkSelect {
|
2020-08-26 04:03:21 +03:00
|
|
|
next, err := resolveRelLink(tabs[curTab], tabs[curTab].page.URL, tabs[curTab].page.Selected)
|
2020-07-29 00:16:57 +03:00
|
|
|
if err != nil {
|
|
|
|
Error("URL Error", err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
2021-12-08 03:54:54 +03:00
|
|
|
NewTabWithURL(next)
|
2020-07-29 00:16:57 +03:00
|
|
|
} else {
|
|
|
|
NewTab()
|
|
|
|
}
|
|
|
|
return nil
|
2020-12-25 00:13:38 +03:00
|
|
|
case config.CmdCloseTab:
|
2020-06-18 23:54:48 +03:00
|
|
|
CloseTab()
|
|
|
|
return nil
|
2020-12-25 00:13:38 +03:00
|
|
|
case config.CmdQuit:
|
2020-07-26 18:21:33 +03:00
|
|
|
Stop()
|
|
|
|
return nil
|
2020-12-25 00:13:38 +03:00
|
|
|
case config.CmdPrevTab:
|
2020-08-06 22:08:19 +03:00
|
|
|
// Wrap around, allow for modulo with negative numbers
|
|
|
|
n := NumTabs()
|
|
|
|
SwitchTab((((curTab - 1) % n) + n) % n)
|
|
|
|
return nil
|
2020-12-25 00:13:38 +03:00
|
|
|
case config.CmdNextTab:
|
2020-08-06 22:08:19 +03:00
|
|
|
SwitchTab((curTab + 1) % NumTabs())
|
|
|
|
return nil
|
2020-12-25 00:13:38 +03:00
|
|
|
case config.CmdHelp:
|
|
|
|
Help()
|
|
|
|
return nil
|
|
|
|
}
|
2020-08-06 20:55:43 +03:00
|
|
|
|
2020-12-25 00:13:38 +03:00
|
|
|
if cmd >= config.CmdTab1 && cmd <= config.CmdTab0 {
|
|
|
|
if cmd == config.CmdTab0 {
|
|
|
|
// Zero key goes to the last tab
|
|
|
|
SwitchTab(NumTabs() - 1)
|
|
|
|
} else {
|
|
|
|
SwitchTab(int(cmd - config.CmdTab1))
|
2020-06-18 23:54:48 +03:00
|
|
|
}
|
2020-12-25 00:13:38 +03:00
|
|
|
return nil
|
2020-06-18 23:54:48 +03:00
|
|
|
}
|
2020-07-08 04:13:45 +03:00
|
|
|
|
|
|
|
// Let another element handle the event, it's not a special global key
|
2020-06-18 23:54:48 +03:00
|
|
|
return event
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop stops the app gracefully.
|
|
|
|
// In the future it will handle things like ongoing downloads, etc
|
|
|
|
func Stop() {
|
|
|
|
App.Stop()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTab opens a new tab and switches to it, displaying the
|
|
|
|
// the default empty content because there's no URL.
|
|
|
|
func NewTab() {
|
2021-12-08 03:54:54 +03:00
|
|
|
NewTabWithURL("about:newtab")
|
|
|
|
|
|
|
|
bottomBar.SetLabel("")
|
|
|
|
bottomBar.SetText("")
|
|
|
|
tabs[NumTabs()-1].saveBottomBar()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTabWithURL opens a new tab and switches to it, displaying the
|
|
|
|
// the URL provided.
|
|
|
|
func NewTabWithURL(url string) {
|
2020-07-08 04:13:45 +03:00
|
|
|
// Create TextView and change curTab
|
|
|
|
// Set the TextView options, and the changed func to App.Draw()
|
2020-06-18 23:54:48 +03:00
|
|
|
// SetDoneFunc to do link highlighting
|
|
|
|
// Add view to pages and switch to it
|
|
|
|
|
2020-07-08 04:13:45 +03:00
|
|
|
// Process current tab before making a new one
|
2020-07-02 03:44:16 +03:00
|
|
|
if curTab > -1 {
|
2020-07-08 04:13:45 +03:00
|
|
|
// Turn off link selecting mode in the current tab
|
|
|
|
tabs[curTab].view.Highlight("")
|
|
|
|
// Save bottomBar state
|
|
|
|
tabs[curTab].saveBottomBar()
|
2020-07-02 03:44:16 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 23:54:48 +03:00
|
|
|
curTab = NumTabs()
|
2020-06-29 22:20:24 +03:00
|
|
|
|
2020-07-08 04:13:45 +03:00
|
|
|
tabs = append(tabs, makeNewTab())
|
2021-12-08 03:54:54 +03:00
|
|
|
|
|
|
|
var interstitial string
|
|
|
|
if !strings.HasPrefix(url, "about:") {
|
|
|
|
interstitial = "Loading " + url + "..."
|
|
|
|
}
|
|
|
|
|
|
|
|
setPage(tabs[curTab], renderPageFromString(interstitial))
|
|
|
|
|
|
|
|
// Regardless of the starting URL, about:newtab will
|
|
|
|
// be the history root.
|
2020-12-20 04:19:47 +03:00
|
|
|
tabs[curTab].addToHistory("about:newtab")
|
|
|
|
tabs[curTab].history.pos = 0 // Manually set as first page
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2021-02-28 02:17:49 +03:00
|
|
|
browser.AddTab(
|
|
|
|
strconv.Itoa(curTab),
|
2021-12-29 00:39:42 +03:00
|
|
|
tabs[curTab].label(),
|
2021-02-28 02:17:49 +03:00
|
|
|
makeContentLayout(tabs[curTab].view, leftMargin()),
|
|
|
|
)
|
2021-02-17 22:17:13 +03:00
|
|
|
browser.SetCurrentTab(strconv.Itoa(curTab))
|
2020-07-08 04:13:45 +03:00
|
|
|
App.SetFocus(tabs[curTab].view)
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2021-12-08 03:54:54 +03:00
|
|
|
URL(url)
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2020-06-22 06:39:33 +03:00
|
|
|
// Draw just in case
|
2020-06-18 23:54:48 +03:00
|
|
|
App.Draw()
|
|
|
|
}
|
|
|
|
|
|
|
|
// CloseTab closes the current tab and switches to the one to its left.
|
|
|
|
func CloseTab() {
|
|
|
|
// Basically the NewTab() func inverted
|
|
|
|
|
|
|
|
// TODO: Support closing middle tabs, by renumbering all the maps
|
|
|
|
// So that tabs to the right of the closed tabs point to the right places
|
|
|
|
// For now you can only close the right-most tab
|
|
|
|
if curTab != NumTabs()-1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if NumTabs() <= 1 {
|
|
|
|
// There's only one tab open, close the app instead
|
|
|
|
Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-08 04:13:45 +03:00
|
|
|
tabs = tabs[:len(tabs)-1]
|
2021-02-17 22:17:13 +03:00
|
|
|
browser.RemoveTab(strconv.Itoa(curTab))
|
2020-06-18 23:54:48 +03:00
|
|
|
|
|
|
|
if curTab <= 0 {
|
|
|
|
curTab = NumTabs() - 1
|
|
|
|
} else {
|
|
|
|
curTab--
|
|
|
|
}
|
|
|
|
|
2021-02-17 22:17:13 +03:00
|
|
|
browser.SetCurrentTab(strconv.Itoa(curTab)) // Go to previous page
|
2020-07-08 04:13:45 +03:00
|
|
|
// Restore previous tab's state
|
|
|
|
tabs[curTab].applyAll()
|
|
|
|
|
|
|
|
App.SetFocus(tabs[curTab].view)
|
2020-06-18 23:54:48 +03:00
|
|
|
|
|
|
|
// Just in case
|
|
|
|
App.Draw()
|
|
|
|
}
|
|
|
|
|
|
|
|
// SwitchTab switches to a specific tab, using its number, 0-indexed.
|
|
|
|
// The tab numbers are clamped to the end, so for example numbers like -5 and 1000 are still valid.
|
|
|
|
// This means that calling something like SwitchTab(curTab - 1) will never cause an error.
|
|
|
|
func SwitchTab(tab int) {
|
|
|
|
if tab < 0 {
|
|
|
|
tab = 0
|
|
|
|
}
|
|
|
|
if tab > NumTabs()-1 {
|
|
|
|
tab = NumTabs() - 1
|
|
|
|
}
|
|
|
|
|
2020-07-08 04:13:45 +03:00
|
|
|
// Save current tab attributes
|
|
|
|
if curTab > -1 {
|
|
|
|
// Save bottomBar state
|
|
|
|
tabs[curTab].saveBottomBar()
|
|
|
|
}
|
|
|
|
|
2020-06-18 23:54:48 +03:00
|
|
|
curTab = tab % NumTabs()
|
2020-07-08 04:13:45 +03:00
|
|
|
|
|
|
|
// Display tab
|
|
|
|
reformatPageAndSetView(tabs[curTab], tabs[curTab].page)
|
2021-02-17 22:17:13 +03:00
|
|
|
browser.SetCurrentTab(strconv.Itoa(curTab))
|
2020-07-08 04:13:45 +03:00
|
|
|
tabs[curTab].applyAll()
|
2020-06-18 23:54:48 +03:00
|
|
|
|
2020-07-08 04:13:45 +03:00
|
|
|
App.SetFocus(tabs[curTab].view)
|
2020-06-18 23:54:48 +03:00
|
|
|
|
|
|
|
// Just in case
|
|
|
|
App.Draw()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Reload() {
|
2020-09-01 20:55:09 +03:00
|
|
|
if tabs[curTab].page.URL == "about:newtab" && config.CustomNewTab {
|
|
|
|
// Re-render new tab, similar to Init()
|
|
|
|
newTabContent := getNewTabContent()
|
|
|
|
tmpTermW := termW
|
2021-02-28 05:18:03 +03:00
|
|
|
renderedNewTabContent, newTabLinks := renderer.RenderGemini(newTabContent, textWidth(), false)
|
2020-09-01 20:55:09 +03:00
|
|
|
newTabPage = structs.Page{
|
2021-02-28 05:18:03 +03:00
|
|
|
Raw: newTabContent,
|
|
|
|
Content: renderedNewTabContent,
|
|
|
|
Links: newTabLinks,
|
|
|
|
URL: "about:newtab",
|
|
|
|
TermWidth: tmpTermW,
|
|
|
|
Mediatype: structs.TextGemini,
|
2020-09-01 20:55:09 +03:00
|
|
|
}
|
|
|
|
temp := newTabPage // Copy
|
|
|
|
setPage(tabs[curTab], &temp)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-08 04:13:45 +03:00
|
|
|
if !tabs[curTab].hasContent() {
|
2020-07-01 20:39:13 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-08 04:13:45 +03:00
|
|
|
go func(t *tab) {
|
2020-08-26 04:03:21 +03:00
|
|
|
cache.RemovePage(tabs[curTab].page.URL)
|
2020-08-28 01:55:42 +03:00
|
|
|
handleURL(t, t.page.URL, 0) // goURL is not used bc history shouldn't be added to
|
2020-07-08 04:13:45 +03:00
|
|
|
if t == tabs[curTab] {
|
|
|
|
// Display the bottomBar state that handleURL set
|
|
|
|
t.applyBottomBar()
|
|
|
|
}
|
|
|
|
}(tabs[curTab])
|
2020-06-18 23:54:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// URL loads and handles the provided URL for the current tab.
|
|
|
|
// It should be an absolute URL.
|
|
|
|
func URL(u string) {
|
2020-12-06 04:35:15 +03:00
|
|
|
t := tabs[curTab]
|
|
|
|
if strings.HasPrefix(u, "about:") {
|
2021-12-09 01:57:28 +03:00
|
|
|
go goURL(t, u)
|
|
|
|
} else {
|
2021-12-23 08:23:06 +03:00
|
|
|
go goURL(t, client.FixUserURL(u))
|
2020-06-24 03:07:25 +03:00
|
|
|
}
|
2020-06-18 23:54:48 +03:00
|
|
|
}
|
|
|
|
|
2021-06-25 06:59:14 +03:00
|
|
|
func RenderFromString(str string) {
|
|
|
|
t := tabs[curTab]
|
2021-12-08 03:54:54 +03:00
|
|
|
page := renderPageFromString(str)
|
2021-06-25 06:59:14 +03:00
|
|
|
setPage(t, page)
|
|
|
|
}
|
|
|
|
|
2021-12-08 03:54:54 +03:00
|
|
|
func renderPageFromString(str string) *structs.Page {
|
2021-06-25 06:59:14 +03:00
|
|
|
rendered, links := renderer.RenderGemini(str, textWidth(), false)
|
|
|
|
page := &structs.Page{
|
|
|
|
Mediatype: structs.TextGemini,
|
|
|
|
Raw: str,
|
|
|
|
Content: rendered,
|
|
|
|
Links: links,
|
|
|
|
TermWidth: termW,
|
|
|
|
}
|
|
|
|
|
2021-12-08 03:54:54 +03:00
|
|
|
return page
|
2021-06-25 06:59:14 +03:00
|
|
|
}
|
|
|
|
|
2020-06-18 23:54:48 +03:00
|
|
|
func NumTabs() int {
|
2020-07-08 04:13:45 +03:00
|
|
|
return len(tabs)
|
2020-06-18 23:54:48 +03:00
|
|
|
}
|