improved shell
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
32
internal/common/color/color.go
Normal file
32
internal/common/color/color.go
Normal file
@@ -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")
|
||||
}
|
||||
Reference in New Issue
Block a user