improved shell
This commit is contained in:
@@ -2,12 +2,14 @@ package cli
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
"bufio"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"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/data"
|
||||||
"gitea.elara.ws/Hazel/music-kraken/internal/plugin"
|
"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(strings.Join(results, "\n"))
|
||||||
fmt.Println()
|
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() {
|
func Shell() {
|
||||||
plugin.RegisterPlugin(&plugin.Musify{})
|
plugin.RegisterPlugin(&plugin.Musify{})
|
||||||
|
|
||||||
fmt.Println("== MusicKraken Shell ==")
|
fmt.Println("== MusicKraken Shell ==")
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
|
|
||||||
currentMusicObjects := []data.MusicObject{}
|
store := musicObjectStore{}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
fmt.Print("> ")
|
fmt.Print("> ")
|
||||||
|
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
line, err := reader.ReadString('\n')
|
command, err := reader.ReadString('\n')
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
line = strings.TrimSpace(line)
|
store, err = interpretCommand(strings.TrimSpace(command), store)
|
||||||
|
|
||||||
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 {
|
if err != nil {
|
||||||
fmt.Println(err)
|
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
fmt.Println(color.Yellow + err.Error() + color.Reset)
|
||||||
} else {
|
|
||||||
currentMusicObjects, err = plugin.Search(line, plugin.SearchConfig{IgnoreErrors: false})
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println(err)
|
|
||||||
fmt.Println()
|
fmt.Println()
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println()
|
currentMusicObject, err := store.currentMusicObjects()
|
||||||
printResults(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