Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve printing from KV #17

Merged
merged 1 commit into from
Jan 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

Expand Down Expand Up @@ -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
Expand Down
40 changes: 24 additions & 16 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ 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"
)

var (
Version = ""
CommitSHA = ""

truncateAt uint = 1024
reverseIterate bool
keysIterate bool
valuesIterate bool
showBinary bool
delimiterIterate string

rootCmd = &cobra.Command{
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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, "@")
Expand Down Expand Up @@ -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)
Expand Down