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

feat: add option for stylized output #36

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
53 changes: 41 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,26 @@ import (
"github.com/spf13/cobra"
)

// styles.
var (
fuchsia = lipgloss.AdaptiveColor{Light: "#EE6FF8", Dark: "#EE6FF8"}
keyStyle = lipgloss.NewStyle().
Align(lipgloss.Left).
Foreground(fuchsia).
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 @@ -146,7 +162,7 @@ func get(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
printFromKV("%s", v)
fmt.Println(string(v))
return nil
}

Expand Down Expand Up @@ -268,11 +284,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 @@ -318,14 +330,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 @@ -370,6 +398,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