format another way

This commit is contained in:
Aleksandr Trushkin
2024-02-07 17:45:00 +03:00
parent b8ce09f7ec
commit 504e5fce29
2 changed files with 38 additions and 12 deletions

View File

@ -17,6 +17,7 @@ import (
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"time"
@ -399,6 +400,10 @@ func newViewItemsParamsKnownValues() *cli.Command {
Name: "regex",
Usage: "Registers regex to match",
},
&cli.BoolFlag{
Name: "keys-only",
Usage: "prints only keys",
},
},
Action: decorateAction(viewItemsParamsKnownValuesAction),
}
@ -611,17 +616,31 @@ func viewItemsParamsKnownValuesAction(ctx context.Context, c *cli.Command) error
}
}
tbl := table.New("key", "values").WithWriter(c.Writer)
for k, v := range requestedValues {
values := make([]string, 0, len(v))
for value := range v {
values = append(values, strconv.Quote(value))
}
tbl.AddRow(k, values)
}
tbl.Print()
bw := bufio.NewWriter(c.Writer)
_, _ = bw.WriteString("Matches:\n")
return nil
if c.Bool("keys-only") {
for k := range requestedValues {
_, _ = bw.WriteString(k + "\n")
}
} else {
for k, v := range requestedValues {
_, _ = bw.WriteString(k + ": ")
values := make([]string, 0, len(v))
for item := range v {
values = append(values, strconv.Quote(item))
}
valuesStr := "[" + strings.Join(values, ",") + "]\n"
_, _ = bw.WriteString(valuesStr)
}
}
_, _ = bw.WriteString("\nPatterns:\n")
for _, pattern := range m.Patterns() {
_, _ = bw.WriteString(pattern + "\n")
}
return bw.Flush()
}
func viewItemsCountAction(ctx context.Context, c *cli.Command) error {

View File

@ -158,7 +158,14 @@ func (r *radixMatcher) Register(pattern string) {
}
func (r *radixMatcher) Patterns() []string {
out := make([]string, len(r.saved))
copy(out, r.saved)
ownIdx := len(r.saved)
out := make([]string, len(r.saved)+len(r.regexps))
copy(out, r.saved[:ownIdx])
for i := 0; i < len(r.regexps); i++ {
idx := i + ownIdx
out[idx] = r.regexps[i].String()
}
return out
}