2020-06-18 16:54:48 -04:00
|
|
|
package display
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-06-29 13:56:27 -04:00
|
|
|
"net/url"
|
2020-06-18 16:54:48 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
"github.com/makeworld-the-better-one/amfora/cache"
|
2020-07-28 16:58:32 -04:00
|
|
|
"github.com/makeworld-the-better-one/amfora/config"
|
2020-06-23 20:07:25 -04:00
|
|
|
"github.com/makeworld-the-better-one/amfora/renderer"
|
2020-06-18 16:54:48 -04:00
|
|
|
"github.com/makeworld-the-better-one/amfora/structs"
|
2020-08-27 20:04:02 -04:00
|
|
|
"github.com/makeworld-the-better-one/go-gemini"
|
2020-06-23 20:07:25 -04:00
|
|
|
"github.com/spf13/viper"
|
2020-06-18 16:54:48 -04:00
|
|
|
"gitlab.com/tslocum/cview"
|
|
|
|
)
|
|
|
|
|
2020-07-07 21:13:45 -04: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 16:54:48 -04:00
|
|
|
|
2020-06-29 15:01:41 -04:00
|
|
|
// Terminal dimensions
|
2020-06-21 16:53:12 -04:00
|
|
|
var termW int
|
2020-06-29 15:01:41 -04:00
|
|
|
var termH int
|
2020-06-21 16:53:12 -04:00
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
// The user input and URL display bar at the bottom
|
2020-07-28 16:58:32 -04:00
|
|
|
var bottomBar = cview.NewInputField()
|
2020-06-18 16:54:48 -04: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-23 20:07:25 -04: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 16:54:48 -04:00
|
|
|
// Ex: "info", "error", "input", "yesno"
|
2020-06-23 20:07:25 -04:00
|
|
|
var tabPages = cview.NewPages()
|
2020-06-18 16:54:48 -04:00
|
|
|
|
|
|
|
// The tabs at the top with titles
|
|
|
|
var tabRow = cview.NewTextView().
|
|
|
|
SetDynamicColors(true).
|
|
|
|
SetRegions(true).
|
|
|
|
SetScrollable(true).
|
|
|
|
SetWrap(false).
|
|
|
|
SetHighlightedFunc(func(added, removed, remaining []string) {
|
|
|
|
// There will always only be one string in added - never multiple highlights
|
|
|
|
// Remaining should always be empty
|
|
|
|
i, _ := strconv.Atoi(added[0])
|
|
|
|
tabPages.SwitchToPage(strconv.Itoa(i)) // Tab names are just numbers, zero-indexed
|
|
|
|
})
|
|
|
|
|
|
|
|
// Root layout
|
|
|
|
var layout = cview.NewFlex().
|
2020-07-28 16:58:32 -04:00
|
|
|
SetDirection(cview.FlexRow)
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-07-01 20:37:34 -04:00
|
|
|
var renderedNewTabContent string
|
|
|
|
var newTabLinks []string
|
2020-07-07 21:13:45 -04:00
|
|
|
var newTabPage structs.Page
|
2020-07-01 20:37:34 -04:00
|
|
|
|
2020-06-21 16:53:12 -04:00
|
|
|
var App = cview.NewApplication().
|
|
|
|
EnableMouse(false).
|
|
|
|
SetRoot(layout, true).
|
|
|
|
SetAfterResizeFunc(func(width int, height int) {
|
2020-06-21 23:49:43 -04:00
|
|
|
// Store for calculations
|
2020-06-21 16:53:12 -04:00
|
|
|
termW = width
|
2020-06-29 15:01:41 -04:00
|
|
|
termH = height
|
2020-06-21 17:15:21 -04:00
|
|
|
|
2020-07-02 23:55:24 -04:00
|
|
|
// Make sure the current tab content is reformatted when the terminal size changes
|
2020-07-07 21:13:45 -04:00
|
|
|
go func(t *tab) {
|
2020-07-28 16:58:32 -04:00
|
|
|
t.reformatMu.Lock() // Only one reformat job per tab
|
|
|
|
defer t.reformatMu.Unlock()
|
2020-07-03 12:22:44 -04:00
|
|
|
// Use the current tab, but don't affect other tabs if the user switches tabs
|
2020-07-07 21:13:45 -04:00
|
|
|
reformatPageAndSetView(t, t.page)
|
|
|
|
}(tabs[curTab])
|
2020-06-21 16:53:12 -04:00
|
|
|
})
|
2020-06-18 16:54:48 -04:00
|
|
|
|
|
|
|
func Init() {
|
|
|
|
tabRow.SetChangedFunc(func() {
|
|
|
|
App.Draw()
|
|
|
|
})
|
|
|
|
|
2020-07-01 13:39:13 -04:00
|
|
|
helpInit()
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-07-28 16:58:32 -04:00
|
|
|
layout.
|
|
|
|
AddItem(tabRow, 1, 1, false).
|
|
|
|
AddItem(nil, 1, 1, false). // One line of empty space above the page
|
|
|
|
AddItem(tabPages, 0, 1, true).
|
|
|
|
AddItem(nil, 1, 1, false). // One line of empty space before bottomBar
|
|
|
|
AddItem(bottomBar, 1, 1, false)
|
|
|
|
|
2020-06-24 11:37:32 -04:00
|
|
|
if viper.GetBool("a-general.color") {
|
2020-07-28 16:58:32 -04:00
|
|
|
layout.SetBackgroundColor(config.GetColor("bg"))
|
|
|
|
tabRow.SetBackgroundColor(config.GetColor("bg"))
|
|
|
|
|
|
|
|
bottomBar.SetBackgroundColor(config.GetColor("bottombar_bg"))
|
|
|
|
bottomBar.
|
|
|
|
SetLabelColor(config.GetColor("bottombar_label")).
|
|
|
|
SetFieldBackgroundColor(config.GetColor("bottombar_bg")).
|
|
|
|
SetFieldTextColor(config.GetColor("bottombar_text"))
|
2020-06-24 11:37:32 -04:00
|
|
|
} else {
|
2020-07-28 16:58:32 -04:00
|
|
|
bottomBar.SetBackgroundColor(tcell.ColorWhite)
|
|
|
|
bottomBar.
|
|
|
|
SetLabelColor(tcell.ColorBlack).
|
|
|
|
SetFieldBackgroundColor(tcell.ColorWhite).
|
|
|
|
SetFieldTextColor(tcell.ColorBlack)
|
2020-06-24 11:37:32 -04:00
|
|
|
}
|
2020-06-18 16:54:48 -04:00
|
|
|
bottomBar.SetDoneFunc(func(key tcell.Key) {
|
2020-07-07 21:13:45 -04:00
|
|
|
tab := curTab
|
|
|
|
|
2020-07-28 19:06:00 -04:00
|
|
|
tabs[tab].saveScroll()
|
|
|
|
|
2020-07-07 21:13:45 -04:00
|
|
|
// 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 13:56:27 -04:00
|
|
|
|
2020-08-27 11:47:57 -04:00
|
|
|
//nolint:exhaustive
|
2020-06-18 16:54:48 -04:00
|
|
|
switch key {
|
|
|
|
case tcell.KeyEnter:
|
2020-06-21 16:53:12 -04:00
|
|
|
// Figure out whether it's a URL, link number, or search
|
|
|
|
// And send out a request
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-06-20 17:17:34 -04:00
|
|
|
query := bottomBar.GetText()
|
|
|
|
|
|
|
|
if strings.TrimSpace(query) == "" {
|
2020-06-18 16:54:48 -04:00
|
|
|
// Ignore
|
2020-07-07 21:13:45 -04:00
|
|
|
reset()
|
2020-06-18 16:54:48 -04:00
|
|
|
return
|
|
|
|
}
|
2020-08-18 19:48:10 -04:00
|
|
|
if query[0] == '.' && tabs[tab].hasContent() {
|
|
|
|
// Relative url
|
2020-08-25 21:03:21 -04:00
|
|
|
current, err := url.Parse(tabs[tab].page.URL)
|
2020-06-29 13:56:27 -04:00
|
|
|
if err != nil {
|
|
|
|
// This shouldn't occur
|
|
|
|
return
|
|
|
|
}
|
2020-08-18 17:32:53 -04:00
|
|
|
target, err := current.Parse(query)
|
|
|
|
if err != nil {
|
2020-08-18 19:48:10 -04:00
|
|
|
// Invalid relative url
|
2020-06-29 13:56:27 -04:00
|
|
|
return
|
|
|
|
}
|
2020-08-18 17:32:53 -04:00
|
|
|
URL(target.String())
|
2020-06-29 13:56:27 -04:00
|
|
|
return
|
|
|
|
}
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-06-20 17:17:34 -04:00
|
|
|
i, err := strconv.Atoi(query)
|
2020-06-18 16:54:48 -04:00
|
|
|
if err != nil {
|
2020-07-01 13:39:13 -04: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-07 21:13:45 -04:00
|
|
|
reset()
|
2020-07-01 13:39:13 -04:00
|
|
|
return
|
|
|
|
}
|
2020-07-07 21:13:45 -04:00
|
|
|
if i <= len(tabs[tab].page.Links) && i > 0 {
|
2020-07-01 13:39:13 -04:00
|
|
|
// Open new tab and load link
|
2020-07-07 21:13:45 -04:00
|
|
|
oldTab := tab
|
2020-07-01 13:39:13 -04:00
|
|
|
NewTab()
|
|
|
|
// Resolve and follow link manually
|
2020-08-25 21:03:21 -04:00
|
|
|
prevParsed, _ := url.Parse(tabs[oldTab].page.URL)
|
2020-07-07 21:13:45 -04:00
|
|
|
nextParsed, err := url.Parse(tabs[oldTab].page.Links[i-1])
|
2020-07-01 13:39:13 -04:00
|
|
|
if err != nil {
|
|
|
|
Error("URL Error", "link URL could not be parsed")
|
2020-07-07 21:13:45 -04:00
|
|
|
reset()
|
2020-07-01 13:39:13 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
URL(prevParsed.ResolveReference(nextParsed).String())
|
|
|
|
return
|
|
|
|
}
|
2020-06-20 17:17:34 -04:00
|
|
|
} else {
|
2020-07-01 13:39:13 -04:00
|
|
|
// It's a full URL or search term
|
|
|
|
// Detect if it's a search or URL
|
2020-08-27 11:47:57 -04:00
|
|
|
if strings.Contains(query, " ") ||
|
|
|
|
(!strings.Contains(query, "//") && !strings.Contains(query, ".") && !strings.HasPrefix(query, "about:")) {
|
2020-08-27 20:04:02 -04:00
|
|
|
u := viper.GetString("a-general.search") + "?" + gemini.QueryEscape(query)
|
2020-08-04 21:05:12 -04:00
|
|
|
cache.RemovePage(u) // Don't use the cached version of the search
|
2020-07-02 11:55:41 -04:00
|
|
|
URL(u)
|
2020-07-01 13:39:13 -04:00
|
|
|
} else {
|
|
|
|
// Full URL
|
2020-08-04 21:05:12 -04:00
|
|
|
cache.RemovePage(query) // Don't use cached version for manually entered URL
|
2020-07-01 13:39:13 -04:00
|
|
|
URL(query)
|
|
|
|
}
|
|
|
|
return
|
2020-06-20 17:17:34 -04:00
|
|
|
}
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
2020-07-07 21:13:45 -04:00
|
|
|
if i <= len(tabs[tab].page.Links) && i > 0 {
|
2020-06-20 17:17:34 -04:00
|
|
|
// It's a valid link number
|
2020-08-25 21:03:21 -04:00
|
|
|
followLink(tabs[tab], tabs[tab].page.URL, tabs[tab].page.Links[i-1])
|
2020-06-18 16:54:48 -04:00
|
|
|
return
|
|
|
|
}
|
2020-07-01 13:39:13 -04:00
|
|
|
// Invalid link number, don't do anything
|
2020-07-07 21:13:45 -04:00
|
|
|
reset()
|
|
|
|
return
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-07-28 19:06:00 -04:00
|
|
|
case tcell.KeyEsc:
|
2020-06-18 16:54:48 -04:00
|
|
|
// Set back to what it was
|
2020-07-07 21:13:45 -04:00
|
|
|
reset()
|
|
|
|
return
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
|
|
|
// Other potential keys are Tab and Backtab, they are ignored
|
|
|
|
})
|
|
|
|
|
|
|
|
// Render the default new tab content ONCE and store it for later
|
2020-07-02 23:55:24 -04:00
|
|
|
renderedNewTabContent, newTabLinks = renderer.RenderGemini(newTabContent, textWidth(), leftMargin())
|
2020-07-07 21:13:45 -04:00
|
|
|
newTabPage = structs.Page{
|
2020-07-03 17:28:56 -04:00
|
|
|
Raw: newTabContent,
|
|
|
|
Content: renderedNewTabContent,
|
|
|
|
Links: newTabLinks,
|
2020-08-25 21:03:21 -04:00
|
|
|
URL: "about:newtab",
|
2020-07-03 17:28:56 -04:00
|
|
|
Width: -1, // Force reformatting on first display
|
|
|
|
Mediatype: structs.TextGemini,
|
2020-07-02 23:55:24 -04:00
|
|
|
}
|
2020-06-18 16:54:48 -04: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 14:37:18 -04:00
|
|
|
_, ok = App.GetFocus().(*cview.Modal)
|
|
|
|
if ok {
|
|
|
|
// It's focused on a modal right now, nothing should interrupt
|
|
|
|
return event
|
|
|
|
}
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-07-07 21:13:45 -04:00
|
|
|
if tabs[curTab].mode == tabModeDone {
|
|
|
|
// All the keys and operations that can only work while NOT loading
|
|
|
|
|
|
|
|
// History arrow keys
|
|
|
|
if event.Modifiers() == tcell.ModAlt {
|
|
|
|
if event.Key() == tcell.KeyLeft {
|
|
|
|
histBack(tabs[curTab])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if event.Key() == tcell.KeyRight {
|
|
|
|
histForward(tabs[curTab])
|
|
|
|
return nil
|
|
|
|
}
|
2020-06-28 21:06:58 -04:00
|
|
|
}
|
2020-07-07 21:13:45 -04:00
|
|
|
|
2020-08-27 11:47:57 -04:00
|
|
|
//nolint:exhaustive
|
2020-07-07 21:13:45 -04:00
|
|
|
switch event.Key() {
|
|
|
|
case tcell.KeyCtrlR:
|
|
|
|
Reload()
|
|
|
|
return nil
|
|
|
|
case tcell.KeyCtrlH:
|
|
|
|
URL(viper.GetString("a-general.home"))
|
2020-06-28 21:06:58 -04:00
|
|
|
return nil
|
2020-07-07 21:13:45 -04:00
|
|
|
case tcell.KeyCtrlB:
|
|
|
|
Bookmarks(tabs[curTab])
|
|
|
|
tabs[curTab].addToHistory("about:bookmarks")
|
|
|
|
return nil
|
|
|
|
case tcell.KeyCtrlD:
|
|
|
|
go addBookmark()
|
|
|
|
return nil
|
|
|
|
case tcell.KeyPgUp:
|
|
|
|
tabs[curTab].pageUp()
|
|
|
|
return nil
|
|
|
|
case tcell.KeyPgDn:
|
|
|
|
tabs[curTab].pageDown()
|
|
|
|
return nil
|
2020-07-09 19:28:39 -04:00
|
|
|
case tcell.KeyCtrlS:
|
|
|
|
if tabs[curTab].hasContent() {
|
|
|
|
savePath, err := downloadPage(tabs[curTab].page)
|
|
|
|
if err != nil {
|
|
|
|
Error("Download Error", fmt.Sprintf("Error saving page content: %v", err))
|
|
|
|
} else {
|
|
|
|
Info(fmt.Sprintf("Page content saved to %s. ", savePath))
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
Info("The current page has no content, so it couldn't be downloaded.")
|
|
|
|
}
|
|
|
|
return nil
|
2020-07-07 21:13:45 -04:00
|
|
|
case tcell.KeyRune:
|
|
|
|
// Regular key was sent
|
|
|
|
switch string(event.Rune()) {
|
|
|
|
case " ":
|
|
|
|
// 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 "R":
|
|
|
|
Reload()
|
|
|
|
return nil
|
|
|
|
case "b":
|
|
|
|
histBack(tabs[curTab])
|
|
|
|
return nil
|
|
|
|
case "f":
|
|
|
|
histForward(tabs[curTab])
|
|
|
|
return nil
|
|
|
|
case "u":
|
|
|
|
tabs[curTab].pageUp()
|
|
|
|
return nil
|
|
|
|
case "d":
|
|
|
|
tabs[curTab].pageDown()
|
|
|
|
return nil
|
|
|
|
}
|
2020-07-19 11:27:39 -04:00
|
|
|
|
|
|
|
// Number key: 1-9, 0
|
|
|
|
i, err := strconv.Atoi(string(event.Rune()))
|
|
|
|
if err == nil {
|
|
|
|
if i == 0 {
|
|
|
|
i = 10 // 0 key is for link 10
|
|
|
|
}
|
|
|
|
if i <= len(tabs[curTab].page.Links) && i > 0 {
|
|
|
|
// It's a valid link number
|
2020-08-25 21:03:21 -04:00
|
|
|
followLink(tabs[curTab], tabs[curTab].page.URL, tabs[curTab].page.Links[i-1])
|
2020-07-19 11:27:39 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-06-28 21:06:58 -04:00
|
|
|
}
|
|
|
|
}
|
2020-08-27 11:47:57 -04:00
|
|
|
|
2020-07-07 21:13:45 -04:00
|
|
|
// All the keys and operations that can work while a tab IS loading
|
2020-06-28 21:06:58 -04:00
|
|
|
|
2020-08-27 11:47:57 -04:00
|
|
|
//nolint:exhaustive
|
2020-06-18 16:54:48 -04:00
|
|
|
switch event.Key() {
|
2020-07-28 17:16:57 -04:00
|
|
|
case tcell.KeyCtrlT:
|
|
|
|
if tabs[curTab].page.Mode == structs.ModeLinkSelect {
|
2020-08-25 21:03:21 -04:00
|
|
|
next, err := resolveRelLink(tabs[curTab], tabs[curTab].page.URL, tabs[curTab].page.Selected)
|
2020-07-28 17:16:57 -04:00
|
|
|
if err != nil {
|
|
|
|
Error("URL Error", err.Error())
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
NewTab()
|
|
|
|
URL(next)
|
|
|
|
} else {
|
|
|
|
NewTab()
|
|
|
|
}
|
|
|
|
return nil
|
2020-06-18 16:54:48 -04:00
|
|
|
case tcell.KeyCtrlW:
|
|
|
|
CloseTab()
|
|
|
|
return nil
|
|
|
|
case tcell.KeyCtrlQ:
|
|
|
|
Stop()
|
|
|
|
return nil
|
2020-07-26 11:21:33 -04:00
|
|
|
case tcell.KeyCtrlC:
|
|
|
|
Stop()
|
|
|
|
return nil
|
2020-08-06 15:08:19 -04:00
|
|
|
case tcell.KeyF1:
|
|
|
|
// Wrap around, allow for modulo with negative numbers
|
|
|
|
n := NumTabs()
|
|
|
|
SwitchTab((((curTab - 1) % n) + n) % n)
|
|
|
|
return nil
|
|
|
|
case tcell.KeyF2:
|
|
|
|
SwitchTab((curTab + 1) % NumTabs())
|
|
|
|
return nil
|
2020-06-18 16:54:48 -04:00
|
|
|
case tcell.KeyRune:
|
|
|
|
// Regular key was sent
|
2020-08-06 13:55:43 -04:00
|
|
|
|
|
|
|
if num, err := config.KeyToNum(event.Rune()); err == nil {
|
|
|
|
// It's a Shift+Num key
|
|
|
|
if num == 0 {
|
|
|
|
// Zero key goes to the last tab
|
|
|
|
SwitchTab(NumTabs() - 1)
|
|
|
|
} else {
|
|
|
|
SwitchTab(num - 1)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
switch string(event.Rune()) {
|
|
|
|
case "q":
|
|
|
|
Stop()
|
|
|
|
return nil
|
|
|
|
case "?":
|
|
|
|
Help()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 21:13:45 -04:00
|
|
|
|
|
|
|
// Let another element handle the event, it's not a special global key
|
2020-06-18 16:54:48 -04: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() {
|
2020-07-07 21:13:45 -04:00
|
|
|
// Create TextView and change curTab
|
|
|
|
// Set the TextView options, and the changed func to App.Draw()
|
2020-06-18 16:54:48 -04:00
|
|
|
// SetDoneFunc to do link highlighting
|
|
|
|
// Add view to pages and switch to it
|
|
|
|
|
2020-07-07 21:13:45 -04:00
|
|
|
// Process current tab before making a new one
|
2020-07-01 20:44:16 -04:00
|
|
|
if curTab > -1 {
|
2020-07-07 21:13:45 -04:00
|
|
|
// Turn off link selecting mode in the current tab
|
|
|
|
tabs[curTab].view.Highlight("")
|
|
|
|
// Save bottomBar state
|
|
|
|
tabs[curTab].saveBottomBar()
|
2020-07-28 18:14:03 -04:00
|
|
|
tabs[curTab].saveScroll()
|
2020-07-01 20:44:16 -04:00
|
|
|
}
|
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
curTab = NumTabs()
|
2020-06-29 15:20:24 -04:00
|
|
|
|
2020-07-07 21:13:45 -04:00
|
|
|
tabs = append(tabs, makeNewTab())
|
|
|
|
temp := newTabPage // Copy
|
|
|
|
setPage(tabs[curTab], &temp)
|
2020-06-29 15:20:24 -04:00
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
// Can't go backwards, but this isn't the first page either.
|
|
|
|
// The first page will be the next one the user goes to.
|
2020-07-07 21:13:45 -04:00
|
|
|
tabs[curTab].history.pos = -1
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-07-07 21:13:45 -04:00
|
|
|
tabPages.AddAndSwitchToPage(strconv.Itoa(curTab), tabs[curTab].view, true)
|
|
|
|
App.SetFocus(tabs[curTab].view)
|
2020-06-18 16:54:48 -04:00
|
|
|
|
|
|
|
// Add tab number to the actual place where tabs are show on the screen
|
|
|
|
// Tab regions are 0-indexed but text displayed on the screen starts at 1
|
2020-06-24 11:37:32 -04:00
|
|
|
if viper.GetBool("a-general.color") {
|
2020-07-28 16:58:32 -04:00
|
|
|
fmt.Fprintf(tabRow, `["%d"][%s] %d [%s][""]|`,
|
|
|
|
curTab,
|
|
|
|
config.GetColorString("tab_num"),
|
|
|
|
curTab+1,
|
|
|
|
config.GetColorString("tab_divider"),
|
|
|
|
)
|
2020-06-24 11:37:32 -04:00
|
|
|
} else {
|
|
|
|
fmt.Fprintf(tabRow, `["%d"] %d [""]|`, curTab, curTab+1)
|
|
|
|
}
|
2020-06-18 16:54:48 -04:00
|
|
|
tabRow.Highlight(strconv.Itoa(curTab)).ScrollToHighlight()
|
|
|
|
|
|
|
|
bottomBar.SetLabel("")
|
|
|
|
bottomBar.SetText("")
|
2020-07-07 21:13:45 -04:00
|
|
|
tabs[curTab].saveBottomBar()
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-06-21 23:39:33 -04:00
|
|
|
// Draw just in case
|
2020-06-18 16:54:48 -04: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-07 21:13:45 -04:00
|
|
|
tabs = tabs[:len(tabs)-1]
|
2020-06-18 16:54:48 -04:00
|
|
|
tabPages.RemovePage(strconv.Itoa(curTab))
|
|
|
|
|
|
|
|
if curTab <= 0 {
|
|
|
|
curTab = NumTabs() - 1
|
|
|
|
} else {
|
|
|
|
curTab--
|
|
|
|
}
|
|
|
|
|
|
|
|
tabPages.SwitchToPage(strconv.Itoa(curTab)) // Go to previous page
|
2020-08-05 13:31:59 -04:00
|
|
|
rewriteTabRow()
|
2020-07-07 21:13:45 -04:00
|
|
|
// Restore previous tab's state
|
|
|
|
tabs[curTab].applyAll()
|
|
|
|
|
|
|
|
App.SetFocus(tabs[curTab].view)
|
2020-06-18 16:54:48 -04: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-07 21:13:45 -04:00
|
|
|
// Save current tab attributes
|
|
|
|
if curTab > -1 {
|
|
|
|
// Save bottomBar state
|
|
|
|
tabs[curTab].saveBottomBar()
|
2020-07-28 18:14:03 -04:00
|
|
|
tabs[curTab].saveScroll()
|
2020-07-07 21:13:45 -04:00
|
|
|
}
|
|
|
|
|
2020-06-18 16:54:48 -04:00
|
|
|
curTab = tab % NumTabs()
|
2020-07-07 21:13:45 -04:00
|
|
|
|
|
|
|
// Display tab
|
|
|
|
reformatPageAndSetView(tabs[curTab], tabs[curTab].page)
|
2020-06-18 16:54:48 -04:00
|
|
|
tabPages.SwitchToPage(strconv.Itoa(curTab))
|
|
|
|
tabRow.Highlight(strconv.Itoa(curTab)).ScrollToHighlight()
|
2020-07-07 21:13:45 -04:00
|
|
|
tabs[curTab].applyAll()
|
2020-06-18 16:54:48 -04:00
|
|
|
|
2020-07-07 21:13:45 -04:00
|
|
|
App.SetFocus(tabs[curTab].view)
|
2020-06-18 16:54:48 -04:00
|
|
|
|
|
|
|
// Just in case
|
|
|
|
App.Draw()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Reload() {
|
2020-07-07 21:13:45 -04:00
|
|
|
if !tabs[curTab].hasContent() {
|
2020-07-01 13:39:13 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:03:21 -04:00
|
|
|
parsed, _ := url.Parse(tabs[curTab].page.URL)
|
2020-07-07 21:13:45 -04:00
|
|
|
go func(t *tab) {
|
2020-08-25 21:03:21 -04:00
|
|
|
cache.RemovePage(tabs[curTab].page.URL)
|
2020-08-05 13:31:59 -04:00
|
|
|
cache.RemoveFavicon(parsed.Host)
|
2020-08-27 18:55:42 -04:00
|
|
|
handleURL(t, t.page.URL, 0) // goURL is not used bc history shouldn't be added to
|
2020-07-07 21:13:45 -04:00
|
|
|
if t == tabs[curTab] {
|
|
|
|
// Display the bottomBar state that handleURL set
|
|
|
|
t.applyBottomBar()
|
|
|
|
}
|
|
|
|
}(tabs[curTab])
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// URL loads and handles the provided URL for the current tab.
|
|
|
|
// It should be an absolute URL.
|
|
|
|
func URL(u string) {
|
2020-06-23 20:07:25 -04:00
|
|
|
// Some code is copied in followLink()
|
|
|
|
|
2020-08-25 19:17:06 -04:00
|
|
|
if u == "about:bookmarks" { //nolint:goconst
|
2020-07-07 21:13:45 -04:00
|
|
|
Bookmarks(tabs[curTab])
|
|
|
|
tabs[curTab].addToHistory("about:bookmarks")
|
2020-06-23 20:07:25 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if u == "about:newtab" {
|
2020-07-07 21:13:45 -04:00
|
|
|
temp := newTabPage // Copy
|
|
|
|
setPage(tabs[curTab], &temp)
|
2020-06-23 20:07:25 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(u, "about:") {
|
|
|
|
Error("Error", "Not a valid 'about:' URL.")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-27 19:57:06 -04:00
|
|
|
if !strings.HasPrefix(u, "//") && !strings.HasPrefix(u, "gemini://") && !strings.Contains(u, "://") {
|
|
|
|
// Assume it's a Gemini URL
|
|
|
|
u = "gemini://" + u
|
|
|
|
}
|
2020-07-07 21:13:45 -04:00
|
|
|
go goURL(tabs[curTab], u)
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func NumTabs() int {
|
2020-07-07 21:13:45 -04:00
|
|
|
return len(tabs)
|
2020-06-18 16:54:48 -04:00
|
|
|
}
|