Compare commits
2 Commits
5be641cbfb
...
e9d1d20c63
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e9d1d20c63 | ||
|
|
9a461a0c16 |
@@ -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"
|
||||||
)
|
)
|
||||||
@@ -50,61 +52,103 @@ func printResults(musicObjects []data.MusicObject) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, source := range m.GetSources() {
|
sources := m.GetSources()
|
||||||
|
if len(sources) > 0 {
|
||||||
|
for _, source := range sources {
|
||||||
results[i] += "\n\t- " + source.Url
|
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(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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
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