Refactor
Signed-off-by: Oliver Davies <oliver@oliverdavies.uk>
This commit is contained in:
parent
a28e1cb7a0
commit
4d11b5354c
5 changed files with 33 additions and 25 deletions
|
@ -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, "")
|
|
||||||
}
|
|
|
@ -27,11 +27,11 @@ If -j or --json is added, the results will be in JSON format.`,
|
||||||
|
|
||||||
zets := zet.SearchZets(args[0])
|
zets := zet.SearchZets(args[0])
|
||||||
|
|
||||||
zetList := zet.CreateZetList(zets)
|
|
||||||
|
|
||||||
result := zetList.Parse()
|
|
||||||
|
|
||||||
if jsonOutput {
|
if jsonOutput {
|
||||||
|
zetList := zet.CreateZetList(zets, false)
|
||||||
|
|
||||||
|
result := zetList.Parse()
|
||||||
|
|
||||||
json, err := json.AsJSON(result)
|
json, err := json.AsJSON(result)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -40,6 +40,10 @@ If -j or --json is added, the results will be in JSON format.`,
|
||||||
|
|
||||||
fmt.Println(json)
|
fmt.Println(json)
|
||||||
} else {
|
} else {
|
||||||
|
zetList := zet.CreateZetList(zets)
|
||||||
|
|
||||||
|
result := zetList.Parse()
|
||||||
|
|
||||||
fmt.Println(strings.Join(result, "\n"))
|
fmt.Println(strings.Join(result, "\n"))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -20,11 +20,11 @@ If -j or --json is added, the results will be in JSON format.`,
|
||||||
Run: func(cli *cobra.Command, args []string) {
|
Run: func(cli *cobra.Command, args []string) {
|
||||||
zets := zet.GetAllZets()
|
zets := zet.GetAllZets()
|
||||||
|
|
||||||
zetList := zet.CreateZetList(zets)
|
|
||||||
|
|
||||||
result := zetList.Parse()
|
|
||||||
|
|
||||||
if jsonOutput {
|
if jsonOutput {
|
||||||
|
zetList := zet.CreateZetList(zets, false)
|
||||||
|
|
||||||
|
result := zetList.Parse()
|
||||||
|
|
||||||
json, err := json.AsJSON(result)
|
json, err := json.AsJSON(result)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -33,6 +33,10 @@ If -j or --json is added, the results will be in JSON format.`,
|
||||||
|
|
||||||
fmt.Println(json)
|
fmt.Println(json)
|
||||||
} else {
|
} else {
|
||||||
|
zetList := zet.CreateZetList(zets)
|
||||||
|
|
||||||
|
result := zetList.Parse()
|
||||||
|
|
||||||
fmt.Println(strings.Join(result, "\n"))
|
fmt.Println(strings.Join(result, "\n"))
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -5,8 +5,6 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"code.oliverdavies.uk/opdavies/cmd-zet/internal/ansi"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type Item struct {
|
type Item struct {
|
||||||
|
@ -18,8 +16,7 @@ func AsJSON(zets []string) (string, error) {
|
||||||
var items []Item
|
var items []Item
|
||||||
|
|
||||||
for _, entry := range zets {
|
for _, entry := range zets {
|
||||||
cleanEntry := ansi.StripANSI(entry)
|
parts := strings.SplitN(entry, " ", 2)
|
||||||
parts := strings.SplitN(cleanEntry, " ", 2)
|
|
||||||
|
|
||||||
id, _ := strconv.Atoi(parts[0])
|
id, _ := strconv.Atoi(parts[0])
|
||||||
|
|
||||||
|
|
|
@ -13,12 +13,20 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type ZetList struct {
|
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{
|
return &ZetList{
|
||||||
Ids: ids,
|
Ids: ids,
|
||||||
|
UseColors: colorSetting,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +37,11 @@ func (z *ZetList) Parse() []string {
|
||||||
reset := "\033[0m"
|
reset := "\033[0m"
|
||||||
|
|
||||||
for _, num := range z.Ids {
|
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)
|
lines = append(lines, line)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue