2019-03-17 16:58:39 +00:00
|
|
|
package gopher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-03-21 05:29:09 +00:00
|
|
|
//------------------------------------------------\\
|
|
|
|
// + + + T Y P E S + + + \\
|
|
|
|
//--------------------------------------------------\\
|
|
|
|
|
|
|
|
//Bookmarks is a holder for titles and links that
|
|
|
|
//can be retrieved by index
|
2019-03-17 16:58:39 +00:00
|
|
|
type Bookmarks struct {
|
2019-04-28 19:46:47 +00:00
|
|
|
Titles []string
|
|
|
|
Links []string
|
2019-03-17 16:58:39 +00:00
|
|
|
}
|
|
|
|
|
2019-03-21 05:29:09 +00:00
|
|
|
//------------------------------------------------\\
|
|
|
|
// + + + R E C E I V E R S + + + \\
|
|
|
|
//--------------------------------------------------\\
|
|
|
|
|
|
|
|
// Add adds a new title and link combination to the bookmarks
|
2019-04-28 19:46:47 +00:00
|
|
|
// struct. It takes as input a string slice in which the first
|
2019-03-21 05:29:09 +00:00
|
|
|
// element represents the link and all following items represent
|
|
|
|
// the title of the bookmark (they will be joined with spaces).
|
2019-03-17 16:58:39 +00:00
|
|
|
func (b *Bookmarks) Add(v []string) error {
|
|
|
|
if len(v) < 2 {
|
|
|
|
return fmt.Errorf("Received %d arguments, expected 2 or more", len(v))
|
|
|
|
}
|
|
|
|
b.Titles = append(b.Titles, strings.Join(v[1:], " "))
|
|
|
|
b.Links = append(b.Links, v[0])
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Bookmarks) Del(i int) error {
|
|
|
|
if i < len(b.Titles) && i < len(b.Links) {
|
2019-04-28 19:46:47 +00:00
|
|
|
b.Titles = append(b.Titles[:i], b.Titles[i+1:]...)
|
|
|
|
b.Links = append(b.Links[:i], b.Links[i+1:]...)
|
2019-03-17 16:58:39 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Bookmark %d does not exist", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeBookmarks() Bookmarks {
|
|
|
|
return Bookmarks{[]string{}, []string{}}
|
|
|
|
}
|