2020-06-23 20:07:25 -04:00
|
|
|
package display
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gdamore/tcell"
|
|
|
|
"github.com/makeworld-the-better-one/amfora/bookmarks"
|
2020-07-28 16:58:32 -04:00
|
|
|
"github.com/makeworld-the-better-one/amfora/config"
|
2020-06-24 11:37:32 -04:00
|
|
|
"github.com/makeworld-the-better-one/amfora/renderer"
|
|
|
|
"github.com/makeworld-the-better-one/amfora/structs"
|
|
|
|
"github.com/spf13/viper"
|
2020-06-23 20:07:25 -04:00
|
|
|
"gitlab.com/tslocum/cview"
|
|
|
|
)
|
|
|
|
|
|
|
|
// For adding and removing bookmarks, basically a clone of the input modal.
|
2020-07-28 16:58:32 -04:00
|
|
|
var bkmkModal = cview.NewModal()
|
2020-06-23 20:07:25 -04:00
|
|
|
|
|
|
|
// bkmkCh is for the user action
|
|
|
|
var bkmkCh = make(chan int) // 1, 0, -1 for add/update, cancel, and remove
|
|
|
|
var bkmkModalText string // The current text of the input field in the modal
|
|
|
|
|
|
|
|
func bkmkInit() {
|
2020-06-24 11:37:32 -04:00
|
|
|
if viper.GetBool("a-general.color") {
|
2020-07-28 16:58:32 -04:00
|
|
|
bkmkModal.SetBackgroundColor(config.GetColor("bkmk_modal_bg")).
|
|
|
|
SetButtonBackgroundColor(config.GetColor("btn_bg")).
|
|
|
|
SetButtonTextColor(config.GetColor("btn_text")).
|
|
|
|
SetTextColor(config.GetColor("bkmk_modal_text"))
|
|
|
|
bkmkModal.GetForm().
|
|
|
|
SetLabelColor(config.GetColor("bkmk_modal_label")).
|
|
|
|
SetFieldBackgroundColor(config.GetColor("bkmk_modal_field_bg")).
|
|
|
|
SetFieldTextColor(config.GetColor("bkmk_modal_field_text"))
|
|
|
|
bkmkModal.GetFrame().
|
|
|
|
SetBorderColor(config.GetColor("bkmk_modal_text")).
|
|
|
|
SetTitleColor(config.GetColor("bkmk_modal_text"))
|
2020-06-24 11:37:32 -04:00
|
|
|
} else {
|
|
|
|
bkmkModal.SetBackgroundColor(tcell.ColorBlack).
|
|
|
|
SetButtonBackgroundColor(tcell.ColorWhite).
|
2020-07-28 16:58:32 -04:00
|
|
|
SetButtonTextColor(tcell.ColorBlack).
|
|
|
|
SetTextColor(tcell.ColorWhite)
|
2020-06-24 11:37:32 -04:00
|
|
|
bkmkModal.GetForm().
|
|
|
|
SetLabelColor(tcell.ColorWhite).
|
|
|
|
SetFieldBackgroundColor(tcell.ColorWhite).
|
|
|
|
SetFieldTextColor(tcell.ColorBlack)
|
2020-07-28 16:58:32 -04:00
|
|
|
bkmkModal.GetFrame().
|
|
|
|
SetBorderColor(tcell.ColorWhite).
|
|
|
|
SetTitleColor(tcell.ColorWhite)
|
2020-06-24 11:37:32 -04:00
|
|
|
}
|
|
|
|
|
2020-06-23 20:07:25 -04:00
|
|
|
bkmkModal.SetBorder(true)
|
2020-07-28 16:58:32 -04:00
|
|
|
bkmkModal.GetFrame().
|
|
|
|
SetTitleAlign(cview.AlignCenter).
|
|
|
|
SetTitle(" Add Bookmark ")
|
2020-06-23 20:07:25 -04:00
|
|
|
bkmkModal.SetDoneFunc(func(buttonIndex int, buttonLabel string) {
|
|
|
|
switch buttonLabel {
|
|
|
|
case "Add":
|
|
|
|
bkmkCh <- 1
|
|
|
|
case "Change":
|
|
|
|
bkmkCh <- 1
|
|
|
|
case "Remove":
|
|
|
|
bkmkCh <- -1
|
|
|
|
case "Cancel":
|
|
|
|
bkmkCh <- 0
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bkmk displays the "Add a bookmark" modal.
|
|
|
|
// It accepts the default value for the bookmark name that will be displayed, but can be changed by the user.
|
|
|
|
// It also accepts a bool indicating whether this page already has a bookmark.
|
|
|
|
// It returns the bookmark name and the bookmark action:
|
|
|
|
// 1, 0, -1 for add/update, cancel, and remove
|
|
|
|
func openBkmkModal(name string, exists bool) (string, int) {
|
|
|
|
// Basically a copy of Input()
|
|
|
|
|
2020-06-29 13:30:20 -04:00
|
|
|
// Reset buttons before input field, to make sure the input is in focus
|
2020-06-23 20:07:25 -04:00
|
|
|
bkmkModal.ClearButtons()
|
|
|
|
if exists {
|
|
|
|
bkmkModal.SetText("Change or remove the bookmark for the current page?")
|
|
|
|
bkmkModal.AddButtons([]string{"Change", "Remove", "Cancel"})
|
|
|
|
} else {
|
|
|
|
bkmkModal.SetText("Create a bookmark for the current page?")
|
|
|
|
bkmkModal.AddButtons([]string{"Add", "Cancel"})
|
|
|
|
}
|
2020-06-29 13:30:20 -04:00
|
|
|
|
|
|
|
// Remove and re-add input field - to clear the old text
|
|
|
|
bkmkModal.GetForm().Clear(false)
|
|
|
|
bkmkModalText = ""
|
|
|
|
bkmkModal.GetForm().AddInputField("Name: ", name, 0, nil,
|
|
|
|
func(text string) {
|
|
|
|
// Store for use later
|
|
|
|
bkmkModalText = text
|
|
|
|
})
|
|
|
|
|
2020-06-23 20:07:25 -04:00
|
|
|
tabPages.ShowPage("bkmk")
|
|
|
|
tabPages.SendToFront("bkmk")
|
|
|
|
App.SetFocus(bkmkModal)
|
|
|
|
App.Draw()
|
|
|
|
|
|
|
|
action := <-bkmkCh
|
|
|
|
tabPages.SwitchToPage(strconv.Itoa(curTab))
|
2020-07-28 18:09:36 -04:00
|
|
|
App.SetFocus(tabs[curTab].view)
|
|
|
|
App.Draw()
|
2020-06-23 20:07:25 -04:00
|
|
|
|
|
|
|
return bkmkModalText, action
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bookmarks displays the bookmarks page on the current tab.
|
2020-07-07 21:13:45 -04:00
|
|
|
func Bookmarks(t *tab) {
|
2020-06-23 20:07:25 -04:00
|
|
|
// Gather bookmarks
|
|
|
|
rawContent := "# Bookmarks\r\n\r\n"
|
2020-06-24 13:18:23 -04:00
|
|
|
m, keys := bookmarks.All()
|
|
|
|
for i := range keys {
|
|
|
|
rawContent += fmt.Sprintf("=> %s %s\r\n", keys[i], m[keys[i]])
|
2020-06-23 20:07:25 -04:00
|
|
|
}
|
|
|
|
// Render and display
|
2020-09-01 16:41:30 -04:00
|
|
|
content, links := renderer.RenderGemini(rawContent, textWidth(), leftMargin(), false)
|
2020-07-02 23:55:24 -04:00
|
|
|
page := structs.Page{
|
2020-07-03 17:28:56 -04:00
|
|
|
Raw: rawContent,
|
|
|
|
Content: content,
|
|
|
|
Links: links,
|
2020-08-25 21:03:21 -04:00
|
|
|
URL: "about:bookmarks",
|
2020-07-03 17:28:56 -04:00
|
|
|
Width: termW,
|
|
|
|
Mediatype: structs.TextGemini,
|
2020-07-02 23:55:24 -04:00
|
|
|
}
|
2020-07-07 21:13:45 -04:00
|
|
|
setPage(t, &page)
|
|
|
|
t.applyBottomBar()
|
2020-06-23 20:07:25 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// addBookmark goes through the process of adding a bookmark for the current page.
|
|
|
|
// It is the high-level way of doing it. It should be called in a goroutine.
|
|
|
|
// It can also be called to edit an existing bookmark.
|
|
|
|
func addBookmark() {
|
2020-08-25 21:03:21 -04:00
|
|
|
if !strings.HasPrefix(tabs[curTab].page.URL, "gemini://") {
|
2020-06-23 20:07:25 -04:00
|
|
|
// Can't make bookmarks for other kinds of URLs
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-25 21:03:21 -04:00
|
|
|
name, exists := bookmarks.Get(tabs[curTab].page.URL)
|
2020-06-23 20:07:25 -04:00
|
|
|
// Open a bookmark modal with the current name of the bookmark, if it exists
|
|
|
|
newName, action := openBkmkModal(name, exists)
|
|
|
|
switch action {
|
|
|
|
case 1:
|
|
|
|
// Add/change the bookmark
|
2020-08-25 21:03:21 -04:00
|
|
|
bookmarks.Set(tabs[curTab].page.URL, newName)
|
2020-06-23 20:07:25 -04:00
|
|
|
case -1:
|
2020-08-25 21:03:21 -04:00
|
|
|
bookmarks.Remove(tabs[curTab].page.URL)
|
2020-06-23 20:07:25 -04:00
|
|
|
}
|
|
|
|
// Other case is action = 0, meaning "Cancel", so nothing needs to happen
|
|
|
|
}
|