From 4d0dbf4b944aaa1afee118de2bea68f8efa1c6c1 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Sun, 8 May 2022 16:15:09 +0200 Subject: [PATCH] Add FX_SHOW_SIZE config option --- main.go | 9 +++++++++ print.go | 46 +++++++++++++++++++++++++++++++--------------- util.go | 15 +++++++++++++++ 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/main.go b/main.go index 7ed1f24..bf27934 100644 --- a/main.go +++ b/main.go @@ -65,6 +65,13 @@ func main() { if termenv.ColorProfile() == termenv.Ascii { theme = Themes["0"] } + var showSize bool + if s, ok := os.LookupEnv("FX_SHOW_SIZE"); ok { + if s == "true" { + showSize = true + } + } + stdinIsTty := isatty.IsTerminal(os.Stdin.Fd()) stdoutIsTty := isatty.IsTerminal(os.Stdout.Fd()) filePath := "" @@ -165,6 +172,7 @@ func main() { fileName: fileName, theme: theme, json: object, + showSize: showSize, width: 80, height: 60, mouseWheelDelta: 3, @@ -196,6 +204,7 @@ type model struct { footerHeight int wrap bool theme Theme + showSize bool // Show number of elements in preview fileName string json interface{} diff --git a/print.go b/print.go index 49e680c..f62c16c 100644 --- a/print.go +++ b/print.go @@ -2,10 +2,11 @@ package main import ( "fmt" + "strings" + . "github.com/antonmedv/fx/pkg/dict" . "github.com/antonmedv/fx/pkg/json" "github.com/antonmedv/fx/pkg/theme" - "strings" ) func (m *model) connect(path string, lineNumber int) { @@ -117,46 +118,61 @@ func (m *model) preview(v interface{}, path string, selectableValues bool) strin if selectableValues && m.cursorPath() == path { previewStyle = m.theme.Cursor } - printValue := func(value interface{}) string { - switch value.(type) { + printValue := func(v interface{}) string { + switch v := v.(type) { case nil, bool, Number: - return previewStyle(fmt.Sprintf("%v", value)) + return previewStyle(fmt.Sprintf("%v", v)) case string: - return previewStyle(fmt.Sprintf("%q", value)) + return previewStyle(fmt.Sprintf("%q", v)) case *Dict: - return previewStyle("{\u2026}") + if m.showSize { + return previewStyle(toLowerNumber(fmt.Sprintf("{\u2026%v\u2026}", len(v.Keys)))) + } else { + return previewStyle("{\u2026}") + } case Array: - return previewStyle("[\u2026]") + if m.showSize { + return previewStyle(toLowerNumber(fmt.Sprintf("[\u2026%v\u2026]", len(v)))) + } else { + return previewStyle("[\u2026]") + } } return "..." } - switch v.(type) { + switch v := v.(type) { case *Dict: output := m.printOpenBracket("{", searchResult, path, selectableValues) - keys := v.(*Dict).Keys + keys := v.Keys for _, k := range keys { key := fmt.Sprintf("%q", k) output += previewStyle(key + ": ") - value, _ := v.(*Dict).Get(k) + value, _ := v.Get(k) output += printValue(value) break } if len(keys) > 1 { - output += previewStyle(", \u2026") + if m.showSize { + output += previewStyle(toLowerNumber(fmt.Sprintf(", \u2026%v\u2026", len(v.Keys)-1))) + } else { + output += previewStyle(", \u2026") + } } output += m.printCloseBracket("}", searchResult, path, selectableValues) return output case Array: output := m.printOpenBracket("[", searchResult, path, selectableValues) - slice := v.(Array) - for _, value := range slice { + for _, value := range v { output += printValue(value) break } - if len(slice) > 1 { - output += previewStyle(", \u2026") + if len(v) > 1 { + if m.showSize { + output += previewStyle(toLowerNumber(fmt.Sprintf(", \u2026%v\u2026", len(v)-1))) + } else { + output += previewStyle(", \u2026") + } } output += m.printCloseBracket("]", searchResult, path, selectableValues) return output diff --git a/util.go b/util.go index a2b8e2d..78b13ba 100644 --- a/util.go +++ b/util.go @@ -2,6 +2,8 @@ package main import ( "fmt" + "strings" + "github.com/charmbracelet/lipgloss" ) @@ -33,3 +35,16 @@ func width(s string) int { func accessor(path string, to interface{}) string { return fmt.Sprintf("%v[%v]", path, to) } + +func toLowerNumber(s string) string { + var out strings.Builder + for _, r := range s { + switch { + case '0' <= r && r <= '9': + out.WriteRune('\u2080' + (r - '\u0030')) + default: + out.WriteRune(r) + } + } + return out.String() +}