From 24ceda746738961de595ec46ed7e0ff169d6bca0 Mon Sep 17 00:00:00 2001 From: Toby Padilla Date: Fri, 21 Jan 2022 18:18:07 -0600 Subject: [PATCH] Improve printing from KV * Added `-b` flag to `charm kv list` and `charm kv get` to optionally print binary values * Detect if output is a TTY and print binary if it's not * Don't print newlines after values unless a TTY is attached --- go.mod | 2 +- main.go | 40 ++++++++++++++++++++++++---------------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/go.mod b/go.mod index f3d60e1..1130be0 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.17 require ( github.com/charmbracelet/charm v0.9.2 github.com/dgraph-io/badger/v3 v3.2011.1 - github.com/muesli/reflow v0.3.0 github.com/spf13/cobra v1.0.0 ) @@ -43,6 +42,7 @@ require ( github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect github.com/muesli/go-app-paths v0.2.1 // indirect + github.com/muesli/reflow v0.3.0 // indirect github.com/muesli/sasquatch v0.0.0-20200811221207-66979d92330a // indirect github.com/muesli/termenv v0.9.0 // indirect github.com/muesli/toktok v0.0.0-20201007181047-c74187025f3f // indirect diff --git a/main.go b/main.go index 0fb1433..9de83d7 100644 --- a/main.go +++ b/main.go @@ -8,8 +8,8 @@ import ( "github.com/charmbracelet/charm/cmd" "github.com/charmbracelet/charm/kv" + "github.com/charmbracelet/charm/ui/common" "github.com/dgraph-io/badger/v3" - "github.com/muesli/reflow/truncate" "github.com/spf13/cobra" ) @@ -17,10 +17,10 @@ var ( Version = "" CommitSHA = "" - truncateAt uint = 1024 reverseIterate bool keysIterate bool valuesIterate bool + showBinary bool delimiterIterate string rootCmd = &cobra.Command{ @@ -112,7 +112,7 @@ func get(cmd *cobra.Command, args []string) error { if err != nil { return err } - fmt.Print(string(v)) + printFromKV("%s", v) return nil } @@ -161,24 +161,14 @@ func list(cmd *cobra.Command, args []string) error { item := it.Item() k := item.Key() if keysIterate { - fmt.Printf(pf, k) + printFromKV(pf, k) continue } err := item.Value(func(v []byte) error { - var vout string - if utf8.Valid(v) { - vout = string(v) - } else { - vout = "(omitted binary data)" - } - tv := truncate.String(vout, truncateAt) - if len(vout) > int(truncateAt) { - tv += " (truncated string)" - } if valuesIterate { - fmt.Printf(pf, tv) + printFromKV(pf, v) } else { - fmt.Printf(pf, k, tv) + printFromKV(pf, k, v) } return nil }) @@ -225,6 +215,22 @@ func nameFromArgs(args []string) (string, error) { return n, nil } +func printFromKV(pf string, vs ...[]byte) { + nb := "(omitted binary data)" + fvs := make([]interface{}, 0) + for _, v := range vs { + if common.IsTTY() && !showBinary && !utf8.Valid(v) { + fvs = append(fvs, nb) + } else { + fvs = append(fvs, string(v)) + } + } + fmt.Printf(pf, fvs...) + if common.IsTTY() && !strings.HasSuffix(pf, "\n") { + fmt.Println() + } +} + func keyParser(k string) ([]byte, string, error) { var key, db string ps := strings.Split(k, "@") @@ -261,6 +267,8 @@ func init() { listCmd.Flags().BoolVarP(&keysIterate, "keys-only", "k", false, "only print keys and don't fetch values from the db") listCmd.Flags().BoolVarP(&valuesIterate, "values-only", "v", false, "only print values") listCmd.Flags().StringVarP(&delimiterIterate, "delimiter", "d", "\t", "delimiter to separate keys and values") + listCmd.Flags().BoolVarP(&showBinary, "show-binary", "b", false, "print binary values") + getCmd.Flags().BoolVarP(&showBinary, "show-binary", "b", false, "print binary values") rootCmd.AddCommand(getCmd) rootCmd.AddCommand(setCmd)