88 lines
1.6 KiB
Go
88 lines
1.6 KiB
Go
package cli
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"gitea.elara.ws/Hazel/music-kraken/internal/data"
|
|
"gitea.elara.ws/Hazel/music-kraken/internal/plugin"
|
|
)
|
|
|
|
func zeroPad(num int, length int) string {
|
|
str := strconv.Itoa(num)
|
|
if len(str) >= length {
|
|
return str
|
|
}
|
|
return strings.Repeat("0", length-len(str)) + str
|
|
}
|
|
|
|
func printResults(musicObjects []data.MusicObject) {
|
|
if len(musicObjects) <= 0 {
|
|
return
|
|
}
|
|
|
|
results := make([]string, len(musicObjects))
|
|
|
|
for i, m := range musicObjects {
|
|
results[i] = zeroPad(i, 2) + ": "
|
|
|
|
if a, ok := m.(data.Artist); ok {
|
|
results[i] += "#a " + a.Name
|
|
} else if a, ok := m.(data.Album); ok {
|
|
results[i] += "#r " + a.Name
|
|
|
|
for _, artist := range a.Artists {
|
|
results[i] += " - " + artist.Name
|
|
}
|
|
} else if a, ok := m.(data.Song); ok {
|
|
results[i] += "#s " + a.Name
|
|
|
|
if a.Album.Name != "" {
|
|
results[i] += " - " + a.Album.Name
|
|
}
|
|
|
|
for _, artist := range a.Artists {
|
|
results[i] += " - " + artist.Name
|
|
}
|
|
}
|
|
|
|
for _, source := range m.GetSources() {
|
|
results[i] += "\n\t- " + source.Url
|
|
}
|
|
}
|
|
|
|
fmt.Println(strings.Join(results, "\n"))
|
|
fmt.Println()
|
|
}
|
|
|
|
func Shell() {
|
|
plugin.RegisterPlugin(&plugin.Musify{})
|
|
|
|
fmt.Println("== MusicKraken Shell ==")
|
|
fmt.Println()
|
|
|
|
for {
|
|
fmt.Print("> ")
|
|
|
|
reader := bufio.NewReader(os.Stdin)
|
|
line, err := reader.ReadString('\n')
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
return
|
|
}
|
|
|
|
searchResults, err := plugin.Search(line, plugin.SearchConfig{IgnoreErrors: false})
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
fmt.Println()
|
|
}
|
|
|
|
fmt.Println()
|
|
printResults(searchResults)
|
|
}
|
|
}
|