2020-07-21 11:30:32 -04:00
|
|
|
package display
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"net/url"
|
2020-11-05 10:38:40 -05:00
|
|
|
"strings"
|
2020-07-21 11:30:32 -04:00
|
|
|
|
2021-04-07 12:56:32 -04:00
|
|
|
"code.rocketnine.space/tslocum/cview"
|
2020-07-21 11:30:32 -04:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This file contains funcs that are small, self-contained utilities.
|
|
|
|
|
2021-02-17 14:17:13 -05:00
|
|
|
// makeContentLayout returns a flex that contains the given TextView
|
2021-02-27 18:17:49 -05:00
|
|
|
// along with the provided left margin, as well as a single empty
|
2021-02-17 14:17:13 -05:00
|
|
|
// line at the top, for a top margin.
|
2021-02-27 18:17:49 -05:00
|
|
|
func makeContentLayout(tv *cview.TextView, leftMargin int) *cview.Flex {
|
2021-02-17 14:17:13 -05:00
|
|
|
// Create horizontal flex with the left margin as an empty space
|
|
|
|
horiz := cview.NewFlex()
|
|
|
|
horiz.SetDirection(cview.FlexColumn)
|
2021-02-27 18:17:49 -05:00
|
|
|
if leftMargin > 0 {
|
|
|
|
horiz.AddItem(nil, leftMargin, 0, false)
|
|
|
|
}
|
2021-02-17 14:17:13 -05:00
|
|
|
horiz.AddItem(tv, 0, 1, true)
|
|
|
|
|
|
|
|
// Create a vertical flex with the other one and a top margin
|
|
|
|
vert := cview.NewFlex()
|
|
|
|
vert.SetDirection(cview.FlexRow)
|
|
|
|
vert.AddItem(nil, 1, 0, false)
|
|
|
|
vert.AddItem(horiz, 0, 1, true)
|
|
|
|
|
|
|
|
return vert
|
2020-11-05 10:38:40 -05:00
|
|
|
}
|
|
|
|
|
2021-02-17 14:17:13 -05:00
|
|
|
// makeTabLabel takes a string and adds spacing to it, making it
|
|
|
|
// suitable for display as a tab label.
|
|
|
|
func makeTabLabel(s string) string {
|
|
|
|
return " " + s + " "
|
|
|
|
}
|
|
|
|
|
|
|
|
// tabNumber gets the index of the tab in the tabs slice. It returns -1
|
|
|
|
// if the tab is not in that slice.
|
|
|
|
func tabNumber(t *tab) int {
|
2020-07-21 11:30:32 -04:00
|
|
|
tempTabs := tabs
|
|
|
|
for i := range tempTabs {
|
|
|
|
if tempTabs[i] == t {
|
2021-02-17 14:17:13 -05:00
|
|
|
return i
|
2020-07-21 11:30:32 -04:00
|
|
|
}
|
|
|
|
}
|
2021-02-17 14:17:13 -05:00
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// escapeMeta santizes a META string for use within a cview modal.
|
|
|
|
func escapeMeta(meta string) string {
|
|
|
|
return cview.Escape(strings.ReplaceAll(meta, "\n", ""))
|
|
|
|
}
|
|
|
|
|
|
|
|
// isValidTab indicates whether the passed tab is still being used, even if it's not currently displayed.
|
|
|
|
func isValidTab(t *tab) bool {
|
|
|
|
return tabNumber(t) != -1
|
2020-07-21 11:30:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func leftMargin() int {
|
2021-12-22 20:36:12 -05:00
|
|
|
// Return the left margin size that centers the text, assuming it's the max width
|
|
|
|
// https://github.com/makeworld-the-better-one/amfora/issues/233
|
|
|
|
|
|
|
|
lm := (termW - viper.GetInt("a-general.max_width")) / 2
|
|
|
|
if lm < 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return lm
|
2020-07-21 11:30:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func textWidth() int {
|
|
|
|
if termW <= 0 {
|
|
|
|
// This prevent a flash of 1-column text on startup, when the terminal
|
|
|
|
// width hasn't been initialized.
|
|
|
|
return viper.GetInt("a-general.max_width")
|
|
|
|
}
|
|
|
|
|
2021-12-22 20:36:12 -05:00
|
|
|
// Subtract left and right margin from total width to get text width
|
|
|
|
// Left and right margin are equal because text is automatically centered, see:
|
|
|
|
// https://github.com/makeworld-the-better-one/amfora/issues/233
|
2020-07-21 11:30:32 -04:00
|
|
|
|
2021-12-22 20:36:12 -05:00
|
|
|
max := termW - leftMargin()*2
|
2020-07-21 11:30:32 -04:00
|
|
|
if max < viper.GetInt("a-general.max_width") {
|
|
|
|
return max
|
|
|
|
}
|
|
|
|
return viper.GetInt("a-general.max_width")
|
|
|
|
}
|
|
|
|
|
|
|
|
// resolveRelLink returns an absolute link for the given absolute link and relative one.
|
|
|
|
// 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) {
|
2021-05-13 16:38:53 -04:00
|
|
|
if !t.hasContent() || t.isAnAboutPage() {
|
2020-07-21 11:30:32 -04:00
|
|
|
return next, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
prevParsed, _ := url.Parse(prev)
|
|
|
|
nextParsed, err := url.Parse(next)
|
|
|
|
if err != nil {
|
2020-08-27 17:57:19 -04:00
|
|
|
return "", errors.New("link URL could not be parsed") //nolint:goerr113
|
2020-07-21 11:30:32 -04:00
|
|
|
}
|
|
|
|
return prevParsed.ResolveReference(nextParsed).String(), nil
|
|
|
|
}
|