diff --git a/internal/ansi/ansi.go b/internal/ansi/ansi.go deleted file mode 100644 index 1713b35..0000000 --- a/internal/ansi/ansi.go +++ /dev/null @@ -1,9 +0,0 @@ -package ansi - -import "regexp" - -var ansiRegex = regexp.MustCompile(`\x1b\[[0-9;]*m`) - -func StripANSI(s string) string { - return ansiRegex.ReplaceAllString(s, "") -} diff --git a/internal/cli/find.go b/internal/cli/find.go index 23b43b2..e64561f 100644 --- a/internal/cli/find.go +++ b/internal/cli/find.go @@ -27,11 +27,11 @@ If -j or --json is added, the results will be in JSON format.`, zets := zet.SearchZets(args[0]) - zetList := zet.CreateZetList(zets) - - result := zetList.Parse() - if jsonOutput { + zetList := zet.CreateZetList(zets, false) + + result := zetList.Parse() + json, err := json.AsJSON(result) if err != nil { @@ -40,6 +40,10 @@ If -j or --json is added, the results will be in JSON format.`, fmt.Println(json) } else { + zetList := zet.CreateZetList(zets) + + result := zetList.Parse() + fmt.Println(strings.Join(result, "\n")) } }, diff --git a/internal/cli/titles.go b/internal/cli/titles.go index 29348ae..8ce0bd3 100644 --- a/internal/cli/titles.go +++ b/internal/cli/titles.go @@ -20,11 +20,11 @@ If -j or --json is added, the results will be in JSON format.`, Run: func(cli *cobra.Command, args []string) { zets := zet.GetAllZets() - zetList := zet.CreateZetList(zets) - - result := zetList.Parse() - if jsonOutput { + zetList := zet.CreateZetList(zets, false) + + result := zetList.Parse() + json, err := json.AsJSON(result) if err != nil { @@ -33,6 +33,10 @@ If -j or --json is added, the results will be in JSON format.`, fmt.Println(json) } else { + zetList := zet.CreateZetList(zets) + + result := zetList.Parse() + fmt.Println(strings.Join(result, "\n")) } }, diff --git a/internal/json/json.go b/internal/json/json.go index 65ba5fa..77fcb1b 100644 --- a/internal/json/json.go +++ b/internal/json/json.go @@ -5,8 +5,6 @@ import ( "fmt" "strconv" "strings" - - "code.oliverdavies.uk/opdavies/cmd-zet/internal/ansi" ) type Item struct { @@ -18,8 +16,7 @@ func AsJSON(zets []string) (string, error) { var items []Item for _, entry := range zets { - cleanEntry := ansi.StripANSI(entry) - parts := strings.SplitN(cleanEntry, " ", 2) + parts := strings.SplitN(entry, " ", 2) id, _ := strconv.Atoi(parts[0]) diff --git a/internal/zet/zet.go b/internal/zet/zet.go index a7e8a9e..b18a30f 100644 --- a/internal/zet/zet.go +++ b/internal/zet/zet.go @@ -13,12 +13,20 @@ import ( ) type ZetList struct { - Ids []int + Ids []int + UseColors bool } -func CreateZetList(ids []int) *ZetList { +func CreateZetList(ids []int, useColors ...bool) *ZetList { + colorSetting := true + + if len(useColors) > 0 { + colorSetting = useColors[0] + } + return &ZetList{ - Ids: ids, + Ids: ids, + UseColors: colorSetting, } } @@ -29,7 +37,11 @@ func (z *ZetList) Parse() []string { reset := "\033[0m" for _, num := range z.Ids { - line := fmt.Sprintf("%s%s%s %s", green, strconv.Itoa(num), reset, getTitle(num)) + line := fmt.Sprintf("%s %s", strconv.Itoa(num), getTitle(num)) + + if z.UseColors { + line = fmt.Sprintf("%s%s%s %s", green, strconv.Itoa(num), reset, getTitle(num)) + } lines = append(lines, line) }