bombadillo/bookmarks.go

145 lines
3.4 KiB
Go
Raw Normal View History

package main
import (
"fmt"
"strings"
2019-09-12 05:53:36 +00:00
"tildegit.org/sloum/bombadillo/cui"
)
//------------------------------------------------\\
// + + + T Y P E S + + + \\
//--------------------------------------------------\\
type Bookmarks struct {
IsOpen bool
IsFocused bool
Position int
Length int
Titles []string
Links []string
}
//------------------------------------------------\\
// + + + R E C E I V E R S + + + \\
//--------------------------------------------------\\
func (b *Bookmarks) Add(v []string) (string, error) {
if len(v) < 2 {
return "", fmt.Errorf("Received %d arguments, expected 2+", len(v))
}
b.Titles = append(b.Titles, strings.Join(v[1:], " "))
b.Links = append(b.Links, v[0])
b.Length = len(b.Titles)
return "Bookmark added successfully", nil
}
func (b *Bookmarks) Delete(i int) (string, error) {
if i < len(b.Titles) && len(b.Titles) == len(b.Links) {
b.Titles = append(b.Titles[:i], b.Titles[i+1:]...)
b.Links = append(b.Links[:i], b.Links[i+1:]...)
b.Length = len(b.Titles)
return "Bookmark deleted successfully", nil
}
return "", fmt.Errorf("Bookmark %d does not exist", i)
}
func (b *Bookmarks) ToggleOpen() {
b.IsOpen = !b.IsOpen
if b.IsOpen {
b.IsFocused = true
} else {
b.IsFocused = false
2019-09-20 23:15:53 +00:00
cui.Clear("screen")
}
}
func (b *Bookmarks) ToggleFocused() {
if b.IsOpen {
b.IsFocused = !b.IsFocused
}
}
func (b Bookmarks) IniDump() string {
if len(b.Titles) < 0 {
return ""
}
out := "[BOOKMARKS]\n"
for i := 0; i < len(b.Titles); i++ {
out += b.Titles[i]
out += "="
out += b.Links[i]
out += "\n"
}
return out
}
// Get a list, including link nums, of bookmarks
// as a string slice
func (b Bookmarks) List() []string {
var out []string
for i, t := range b.Titles {
out = append(out, fmt.Sprintf("[%d] %s", i, t))
}
return out
}
2019-09-12 05:53:36 +00:00
func (b Bookmarks) Render(termwidth, termheight int) []string {
width := 40
termheight -= 3
2019-09-17 02:38:07 +00:00
var walll, wallr, floor, ceil, tr, tl, br, bl string
2019-09-12 05:53:36 +00:00
if termwidth < 40 {
width = termwidth
}
if b.IsFocused {
2019-09-17 02:38:07 +00:00
walll = cui.Shapes["awalll"]
wallr = cui.Shapes["awallr"]
2019-09-12 05:53:36 +00:00
ceil = cui.Shapes["aceiling"]
2019-09-17 02:38:07 +00:00
floor = cui.Shapes["afloor"]
2019-09-12 05:53:36 +00:00
tr = cui.Shapes["atr"]
br = cui.Shapes["abr"]
tl = cui.Shapes["atl"]
bl = cui.Shapes["abl"]
} else {
2019-09-17 02:38:07 +00:00
walll = cui.Shapes["walll"]
wallr = cui.Shapes["wallr"]
2019-09-12 05:53:36 +00:00
ceil = cui.Shapes["ceiling"]
2019-09-17 02:38:07 +00:00
floor = cui.Shapes["floor"]
2019-09-12 05:53:36 +00:00
tr = cui.Shapes["tr"]
br = cui.Shapes["br"]
tl = cui.Shapes["tl"]
bl = cui.Shapes["bl"]
}
out := make([]string, 0, 5)
contentWidth := width - 2
top := fmt.Sprintf("%s%s%s", tl, strings.Repeat(ceil, contentWidth), tr)
2019-09-12 05:53:36 +00:00
out = append(out, top)
marks := b.List()
for i := 0; i < termheight - 2; i++ {
2019-09-17 02:38:07 +00:00
if i + b.Position >= len(b.Titles) {
out = append(out, fmt.Sprintf("%s%-*.*s%s", walll, contentWidth, contentWidth, "", wallr))
2019-09-12 05:53:36 +00:00
} else {
2019-09-17 02:38:07 +00:00
out = append(out, fmt.Sprintf("%s%-*.*s%s", walll, contentWidth, contentWidth, marks[i + b.Position], wallr))
2019-09-12 05:53:36 +00:00
}
}
2019-09-17 02:38:07 +00:00
bottom := fmt.Sprintf("%s%s%s", bl, strings.Repeat(floor, contentWidth), br)
2019-09-12 05:53:36 +00:00
out = append(out, bottom)
return out
}
// TODO handle scrolling of the bookmarks list
2019-09-12 05:53:36 +00:00
// either here with a scroll up/down or in the client
// code for scroll
//------------------------------------------------\\
// + + + F U N C T I O N S + + + \\
//--------------------------------------------------\\
func MakeBookmarks() Bookmarks {
return Bookmarks{false, false, 0, 0, make([]string, 0), make([]string, 0)}
}