Compare commits
2 Commits
5be641cbfb
...
e9d1d20c63
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e9d1d20c63 | ||
|
|
9a461a0c16 |
@@ -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"
|
||||
)
|
||||
@@ -50,61 +52,103 @@ func printResults(musicObjects []data.MusicObject) {
|
||||
}
|
||||
}
|
||||
|
||||
for _, source := range m.GetSources() {
|
||||
results[i] += "\n\t- " + source.Url
|
||||
sources := m.GetSources()
|
||||
if len(sources) > 0 {
|
||||
for _, source := range sources {
|
||||
results[i] += "\n\t- " + source.Url
|
||||
}
|
||||
} else {
|
||||
results[i] = color.StrikeThrough + results[i] + color.Reset
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
87
internal/common/color/color.go
Normal file
87
internal/common/color/color.go
Normal file
@@ -0,0 +1,87 @@
|
||||
package color
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
)
|
||||
|
||||
var (
|
||||
Reset = "\033[0m"
|
||||
|
||||
/////////////
|
||||
// Special //
|
||||
/////////////
|
||||
|
||||
Bold = "\033[1m"
|
||||
Underline = "\033[4m"
|
||||
StrikeThrough = "\033[9m"
|
||||
|
||||
/////////////////
|
||||
// Text colors //
|
||||
/////////////////
|
||||
|
||||
Black = "\033[30m"
|
||||
Red = "\033[31m"
|
||||
Green = "\033[32m"
|
||||
Yellow = "\033[33m"
|
||||
Blue = "\033[34m"
|
||||
Purple = "\033[35m"
|
||||
Cyan = "\033[36m"
|
||||
Gray = "\033[37m"
|
||||
White = "\033[97m"
|
||||
|
||||
///////////////////////
|
||||
// Background colors //
|
||||
///////////////////////
|
||||
|
||||
BlackBackground = "\033[40m"
|
||||
RedBackground = "\033[41m"
|
||||
GreenBackground = "\033[42m"
|
||||
YellowBackground = "\033[43m"
|
||||
BlueBackground = "\033[44m"
|
||||
PurpleBackground = "\033[45m"
|
||||
CyanBackground = "\033[46m"
|
||||
GrayBackground = "\033[47m"
|
||||
WhiteBackground = "\033[107m"
|
||||
)
|
||||
|
||||
func init() {
|
||||
if runtime.GOOS == "windows" {
|
||||
Reset = ""
|
||||
|
||||
/////////////
|
||||
// Special //
|
||||
/////////////
|
||||
|
||||
Bold = ""
|
||||
Underline = ""
|
||||
StrikeThrough = ""
|
||||
|
||||
/////////////////
|
||||
// Text colors //
|
||||
/////////////////
|
||||
|
||||
Black = ""
|
||||
Red = ""
|
||||
Green = ""
|
||||
Yellow = ""
|
||||
Blue = ""
|
||||
Purple = ""
|
||||
Cyan = ""
|
||||
Gray = ""
|
||||
White = ""
|
||||
|
||||
///////////////////////
|
||||
// Background colors //
|
||||
///////////////////////
|
||||
|
||||
BlackBackground = ""
|
||||
RedBackground = ""
|
||||
GreenBackground = ""
|
||||
YellowBackground = ""
|
||||
BlueBackground = ""
|
||||
PurpleBackground = ""
|
||||
CyanBackground = ""
|
||||
GrayBackground = ""
|
||||
WhiteBackground = ""
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user