🚸 Bookmarks are sorted alphabetically - fixes #17

This commit is contained in:
makeworld 2020-06-24 13:18:23 -04:00
parent e8e23c7fe6
commit 3db81c3ac6
4 changed files with 32 additions and 17 deletions

View File

@ -28,7 +28,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Make the help table look better
### Removed
- Opening multiple URLs from the command line (threading issues)
- Opening multiple URLs from the command line
### Fixed
- Reset bottom bar on error / invalid URL

View File

@ -18,12 +18,12 @@ func main() {
if len(os.Args) > 1 {
if os.Args[1] == "--version" || os.Args[1] == "-v" {
fmt.Println(version)
fmt.Print(version + "\r\n")
return
}
if os.Args[1] == "--help" || os.Args[1] == "-h" {
fmt.Println("Amfora is a fancy terminal browser for the Gemini protocol.\r\n")
fmt.Println("Usage:\r\namfora [URL]\r\namfora --version, -v")
fmt.Print("Amfora is a fancy terminal browser for the Gemini protocol.\r\n\r\n")
fmt.Print("Usage:\r\namfora [URL]\r\namfora --version, -v\r\n")
return
}
}
@ -34,11 +34,8 @@ func main() {
}
display.Init()
if len(os.Args[1:]) == 0 {
// There should always be a tab
display.NewTab()
} else {
display.NewTab()
display.NewTab()
if len(os.Args[1:]) > 0 {
display.URL(os.Args[1])
}

View File

@ -2,6 +2,7 @@ package bookmarks
import (
"encoding/base32"
"sort"
"strings"
"github.com/makeworld-the-better-one/amfora/config"
@ -12,7 +13,7 @@ var bkmkStore = config.BkmkStore
// bkmkKey returns the viper key for the given bookmark URL.
// Note that URLs are the keys, NOT the bookmark name.
func bkmkKey(url string) string {
// Keys are base32 encoded URLs to prevent any bad chars like periods from being used
// Keys are base32 encoded URLs to prevent any special chars like periods from being used
return "bookmarks." + base32.StdEncoding.EncodeToString([]byte(url))
}
@ -36,17 +37,25 @@ func Remove(url string) {
}
// All returns all the bookmarks in a map of URLs to names.
func All() map[string]string {
ret := make(map[string]string)
// It also returns a slice of map keys, sorted so that the map *values*
// are in alphabetical order, with case ignored.
func All() (map[string]string, []string) {
bkmks := make(map[string]string)
bkmksMap, ok := bkmkStore.AllSettings()["bookmarks"].(map[string]interface{})
if !ok {
// No bookmarks stored yet, return empty map
return ret
return bkmks, []string{}
}
inverted := make(map[string]string) // Holds inverted map, name->URL
var names []string // Holds bookmark names, for sorting
var keys []string // Final sorted keys (URLs), for returning at the end
for b32Url, name := range bkmksMap {
if n, ok := name.(string); n == "" || !ok {
// name is not a string, or it's empty - ignore
// Likely means it is a removed bookmark
continue
}
url, err := base32.StdEncoding.DecodeString(strings.ToUpper(b32Url))
@ -54,7 +63,15 @@ func All() map[string]string {
// This would only happen if a user messed around with the bookmarks file
continue
}
ret[string(url)] = name.(string)
bkmks[string(url)] = name.(string)
inverted[name.(string)] = string(url)
names = append(names, name.(string))
}
return ret
// Sort, then turn back into URL keys
sort.Strings(names)
for _, name := range names {
keys = append(keys, inverted[name])
}
return bkmks, keys
}

View File

@ -99,8 +99,9 @@ func openBkmkModal(name string, exists bool) (string, int) {
func Bookmarks() {
// Gather bookmarks
rawContent := "# Bookmarks\r\n\r\n"
for url, name := range bookmarks.All() {
rawContent += fmt.Sprintf("=> %s %s\r\n", url, name)
m, keys := bookmarks.All()
for i := range keys {
rawContent += fmt.Sprintf("=> %s %s\r\n", keys[i], m[keys[i]])
}
// Render and display
content, links := renderer.RenderGemini(rawContent, textWidth())