1
0
mirror of https://github.com/makew0rld/amfora.git synced 2024-12-04 14:46:29 -05:00

🚸 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 - Make the help table look better
### Removed ### Removed
- Opening multiple URLs from the command line (threading issues) - Opening multiple URLs from the command line
### Fixed ### Fixed
- Reset bottom bar on error / invalid URL - Reset bottom bar on error / invalid URL

View File

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

View File

@ -2,6 +2,7 @@ package bookmarks
import ( import (
"encoding/base32" "encoding/base32"
"sort"
"strings" "strings"
"github.com/makeworld-the-better-one/amfora/config" "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. // bkmkKey returns the viper key for the given bookmark URL.
// Note that URLs are the keys, NOT the bookmark name. // Note that URLs are the keys, NOT the bookmark name.
func bkmkKey(url string) string { 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)) 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. // All returns all the bookmarks in a map of URLs to names.
func All() map[string]string { // It also returns a slice of map keys, sorted so that the map *values*
ret := make(map[string]string) // 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{}) bkmksMap, ok := bkmkStore.AllSettings()["bookmarks"].(map[string]interface{})
if !ok { if !ok {
// No bookmarks stored yet, return empty map // 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 { for b32Url, name := range bkmksMap {
if n, ok := name.(string); n == "" || !ok { if n, ok := name.(string); n == "" || !ok {
// name is not a string, or it's empty - ignore // name is not a string, or it's empty - ignore
// Likely means it is a removed bookmark
continue continue
} }
url, err := base32.StdEncoding.DecodeString(strings.ToUpper(b32Url)) 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 // This would only happen if a user messed around with the bookmarks file
continue 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() { func Bookmarks() {
// Gather bookmarks // Gather bookmarks
rawContent := "# Bookmarks\r\n\r\n" rawContent := "# Bookmarks\r\n\r\n"
for url, name := range bookmarks.All() { m, keys := bookmarks.All()
rawContent += fmt.Sprintf("=> %s %s\r\n", url, name) for i := range keys {
rawContent += fmt.Sprintf("=> %s %s\r\n", keys[i], m[keys[i]])
} }
// Render and display // Render and display
content, links := renderer.RenderGemini(rawContent, textWidth()) content, links := renderer.RenderGemini(rawContent, textWidth())