From 3db81c3ac607f5d8fd82c439ba855d2689bd013f Mon Sep 17 00:00:00 2001 From: makeworld Date: Wed, 24 Jun 2020 13:18:23 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=B8=20Bookmarks=20are=20sorted=20alpha?= =?UTF-8?q?betically=20-=20fixes=20#17?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 +- amfora.go | 13 +++++-------- bookmarks/bookmarks.go | 29 +++++++++++++++++++++++------ display/bookmarks.go | 5 +++-- 4 files changed, 32 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44399cb..7fa4571 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/amfora.go b/amfora.go index d92a5b9..c23d7e3 100644 --- a/amfora.go +++ b/amfora.go @@ -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]) } diff --git a/bookmarks/bookmarks.go b/bookmarks/bookmarks.go index 8dd3baf..bd22600 100644 --- a/bookmarks/bookmarks.go +++ b/bookmarks/bookmarks.go @@ -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 } diff --git a/display/bookmarks.go b/display/bookmarks.go index 1941515..5895d11 100644 --- a/display/bookmarks.go +++ b/display/bookmarks.go @@ -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())