Skip to content

Commit

Permalink
feat: add stylized output flag
Browse files Browse the repository at this point in the history
  • Loading branch information
bashbunni committed May 6, 2022
1 parent 659a130 commit 63e6d03
Showing 1 changed file with 43 additions and 12 deletions.
55 changes: 43 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,32 @@ import (
"github.com/charmbracelet/charm/cmd"
"github.com/charmbracelet/charm/kv"
"github.com/charmbracelet/charm/ui/common"
"github.com/charmbracelet/lipgloss"
"github.com/dgraph-io/badger/v3"
"github.com/spf13/cobra"
)

// styles.
var (
highlight = lipgloss.AdaptiveColor{Light: "#874BFD", Dark: "#7D56F4"}
keyStyle = lipgloss.NewStyle().
Align(lipgloss.Left).
Foreground(lipgloss.Color("#FAFAFA")).
Background(highlight).
Margin(1, 1, 0, 0).
Padding(0, 1)
valueStyle = lipgloss.NewStyle().
Align(lipgloss.Left).
Foreground(lipgloss.AdaptiveColor{Light: "#969B86", Dark: "#696969"}).
Margin(1, 2, 0, 0).
Padding(0, 1)
)

var (
Version = ""
CommitSHA = ""

pretty bool
reverseIterate bool
keysIterate bool
valuesIterate bool
Expand Down Expand Up @@ -123,7 +141,7 @@ func get(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
printFromKV("%s", v)
fmt.Println(valueStyle.Render(string(v)))
return nil
}

Expand Down Expand Up @@ -204,11 +222,7 @@ func list(cmd *cobra.Command, args []string) error {
continue
}
err := item.Value(func(v []byte) error {
if valuesIterate {
printFromKV(pf, v)
} else {
printFromKV(pf, k, v)
}
printFromKV(pf, k, v)
return nil
})
if err != nil {
Expand Down Expand Up @@ -254,14 +268,30 @@ func nameFromArgs(args []string) (string, error) {
return n, nil
}

func printFromKV(pf string, vs ...[]byte) {
func printFromKV(pf string, k []byte, 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))
if !pretty {
if !valuesIterate {
fvs = append(fvs, string(k))
}
for _, v := range vs {
if common.IsTTY() && !showBinary && !utf8.Valid(v) {
fvs = append(fvs, nb)
} else {
fvs = append(fvs, string(v))
}
}
} else {
if !valuesIterate {
fvs = append(fvs, keyStyle.Render(string(k)))
}
for _, v := range vs {
if common.IsTTY() && !showBinary && !utf8.Valid(v) {
fvs = append(fvs, valueStyle.Render(nb))
} else {
fvs = append(fvs, valueStyle.Render(string(v)))
}
}
}
fmt.Printf(pf, fvs...)
Expand Down Expand Up @@ -305,6 +335,7 @@ func init() {
listCmd.Flags().BoolVarP(&reverseIterate, "reverse", "r", false, "list in reverse lexicographic order")
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().BoolVarP(&pretty, "pretty", "p", false, "output with styling")
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")
Expand Down

0 comments on commit 63e6d03

Please sign in to comment.