From 9a461a0c1655663e6a8ecd91fb4a905f156b498d Mon Sep 17 00:00:00 2001 From: Hazel Noack Date: Thu, 9 Oct 2025 12:01:15 +0200 Subject: [PATCH] improved shell --- internal/cli/shell.go | 101 +++++++++++++++++++++++---------- internal/common/color/color.go | 32 +++++++++++ 2 files changed, 102 insertions(+), 31 deletions(-) create mode 100644 internal/common/color/color.go diff --git a/internal/cli/shell.go b/internal/cli/shell.go index 4edb81c..96be00c 100644 --- a/internal/cli/shell.go +++ b/internal/cli/shell.go @@ -2,12 +2,14 @@ package cli import ( "bufio" + "errors" "fmt" "log" "os" "strconv" "strings" + "gitea.elara.ws/Hazel/music-kraken/internal/common/color" "gitea.elara.ws/Hazel/music-kraken/internal/data" "gitea.elara.ws/Hazel/music-kraken/internal/plugin" ) @@ -55,56 +57,93 @@ func printResults(musicObjects []data.MusicObject) { } } + fmt.Println() fmt.Println(strings.Join(results, "\n")) fmt.Println() } +type musicObjectStore [][]data.MusicObject + +func (s musicObjectStore) currentMusicObjects() ([]data.MusicObject, error) { + if len(s) <= 0 { + return []data.MusicObject{}, errors.New("no items to select from") + } + + return (s)[len(s)-1], nil +} + +func interpretCommand(command string, store musicObjectStore) (musicObjectStore, error) { + // going back in history + if command == ".." { + if len(store) <= 1 { + return store, errors.New("can't go back") + } + + return store[:len(store)-1], nil + } + + // fetch special music object + if index, err := strconv.Atoi(command); err == nil { + currentMusicObjects, err := store.currentMusicObjects() + if err != nil { + return store, err + } + + if index >= len(currentMusicObjects) || index < 0 { + return store, errors.New(command + " is out of bounds [0 <= " + strconv.Itoa(index) + " <= " + strconv.Itoa(len(currentMusicObjects)-1) + "]") + } + + current := currentMusicObjects[index] + + if len(current.GetSources()) <= 0 { + return store, errors.New("selected object has no sources to download") + } + + currentMusicObjects, err = plugin.FetchList(current.GetSources()[0]) + if err != nil { + return store, err + } + + return append(store, currentMusicObjects), nil + } + + // search in every other case + currentMusicObjects, err := plugin.Search(command, plugin.SearchConfig{IgnoreErrors: false}) + if err != nil { + fmt.Println(err) + fmt.Println() + } + + return append(store, currentMusicObjects), nil +} + func Shell() { plugin.RegisterPlugin(&plugin.Musify{}) fmt.Println("== MusicKraken Shell ==") fmt.Println() - currentMusicObjects := []data.MusicObject{} + store := musicObjectStore{} + for { fmt.Print("> ") reader := bufio.NewReader(os.Stdin) - line, err := reader.ReadString('\n') + command, err := reader.ReadString('\n') if err != nil { log.Fatal(err) - return } - line = strings.TrimSpace(line) - - if index, err := strconv.Atoi(line); err == nil { - if index >= len(currentMusicObjects) { - fmt.Println("\n" + line + " is out of bounds") - continue - } - - current := currentMusicObjects[index] - - if len(current.GetSources()) <= 0 { - fmt.Println("\nselected has no sources to download") - continue - } - - currentMusicObjects, err = plugin.FetchList(current.GetSources()[0]) - if err != nil { - fmt.Println(err) - fmt.Println() - } - } else { - currentMusicObjects, err = plugin.Search(line, plugin.SearchConfig{IgnoreErrors: false}) - if err != nil { - fmt.Println(err) - fmt.Println() - } + store, err = interpretCommand(strings.TrimSpace(command), store) + if err != nil { + fmt.Println() + fmt.Println(color.Yellow + err.Error() + color.Reset) + fmt.Println() } - fmt.Println() - printResults(currentMusicObjects) + currentMusicObject, err := store.currentMusicObjects() + if err == nil { + printResults(currentMusicObject) + } } } diff --git a/internal/common/color/color.go b/internal/common/color/color.go new file mode 100644 index 0000000..fba0dca --- /dev/null +++ b/internal/common/color/color.go @@ -0,0 +1,32 @@ +package color + +import ( + "fmt" + "runtime" +) + +var Reset = "\033[0m" +var Red = "\033[31m" +var Green = "\033[32m" +var Yellow = "\033[33m" +var Blue = "\033[34m" +var Purple = "\033[35m" +var Cyan = "\033[36m" +var Gray = "\033[37m" +var White = "\033[97m" + +func init() { + if runtime.GOOS == "windows" { + Reset = "" + Red = "" + Green = "" + Yellow = "" + Blue = "" + Purple = "" + Cyan = "" + Gray = "" + White = "" + } + + fmt.Println("init") +}