diff --git a/internal/cli/shell.go b/internal/cli/shell.go index 5e54d24..85213ab 100644 --- a/internal/cli/shell.go +++ b/internal/cli/shell.go @@ -10,19 +10,12 @@ import ( "strconv" "strings" + "gitea.elara.ws/Hazel/music-kraken/internal/common" "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" ) -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 @@ -31,7 +24,7 @@ func printResults(musicObjects []data.MusicObject) { results := make([]string, len(musicObjects)) for i, m := range musicObjects { - results[i] = zeroPad(i, 2) + ": " + results[i] = common.ZeroPad(i, 2) + ": " if a, ok := m.(data.Artist); ok { results[i] += "#a " + a.Name diff --git a/internal/common/strings.go b/internal/common/strings.go index 0cb3953..96abbb4 100644 --- a/internal/common/strings.go +++ b/internal/common/strings.go @@ -1,6 +1,7 @@ package common import ( + "strconv" "strings" ) @@ -12,3 +13,11 @@ func Unify(s string) string { } return s } + +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 +} diff --git a/internal/common/strings_test.go b/internal/common/strings_test.go index 2b97643..761908e 100644 --- a/internal/common/strings_test.go +++ b/internal/common/strings_test.go @@ -35,3 +35,18 @@ func TestUnify(t *testing.T) { t.Errorf(`Double whitespaces need to be removed`) } } + +func TestZeroPad(t *testing.T) { + cases := map[int]string{ + 0: "000", + 5: "005", + 1000: "1000", + 50: "050", + } + + for key, val := range cases { + if res := ZeroPad(key, 3); res != val { + t.Errorf(`did not match`) + } + } +}