From 2c81fe7d8c234395c5a2d1742dda76bd12518372 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Mon, 11 Sep 2023 18:37:02 +0200 Subject: [PATCH] wip --- new/go.mod | 2 +- new/help.go | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ new/main.go | 26 +++++++++++++++++++ new/version.go | 3 +++ 4 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 new/help.go create mode 100644 new/version.go diff --git a/new/go.mod b/new/go.mod index d49289e..10cfa42 100644 --- a/new/go.mod +++ b/new/go.mod @@ -6,6 +6,7 @@ require ( github.com/charmbracelet/bubbles v0.16.1 github.com/charmbracelet/bubbletea v0.24.2 github.com/charmbracelet/lipgloss v0.7.1 + github.com/mattn/go-runewidth v0.0.14 github.com/mazznoer/colorgrad v0.9.1 ) @@ -15,7 +16,6 @@ require ( github.com/lucasb-eyer/go-colorful v1.2.0 // indirect github.com/mattn/go-isatty v0.0.18 // indirect github.com/mattn/go-localereader v0.0.1 // indirect - github.com/mattn/go-runewidth v0.0.14 // indirect github.com/mazznoer/csscolorparser v0.1.2 // indirect github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b // indirect github.com/muesli/cancelreader v0.2.2 // indirect diff --git a/new/help.go b/new/help.go new file mode 100644 index 0000000..478a361 --- /dev/null +++ b/new/help.go @@ -0,0 +1,70 @@ +package main + +import ( + "fmt" + "reflect" + "strings" + + "github.com/charmbracelet/bubbles/key" + "github.com/charmbracelet/lipgloss" +) + +func usage(keyMap KeyMap) string { + title := lipgloss.NewStyle().Bold(true) + pad := lipgloss.NewStyle().PaddingLeft(4) + return fmt.Sprintf(` + %v + Terminal JSON viewer + + %v + fx data.json + fx data.json .field + curl ... | fx + + %v + -h, --help print help + -v, --version print version + + %v +%v + + %v + [https://fx.wtf] +`, + title.Render("fx "+version), + title.Render("Usage"), + title.Render("Flags"), + title.Render("Key Bindings"), + strings.Join(keyMapInfo(keyMap, pad), "\n"), + title.Render("More info"), + ) +} + +func keyMapInfo(keyMap KeyMap, style lipgloss.Style) []string { + v := reflect.ValueOf(keyMap) + fields := reflect.VisibleFields(v.Type()) + + keys := make([]string, 0) + for i := range fields { + k := v.Field(i).Interface().(key.Binding) + str := k.Help().Key + if len(str) == 0 { + str = strings.Join(k.Keys(), ", ") + } + keys = append(keys, fmt.Sprintf("%v ", str)) + } + + desc := make([]string, 0) + for i := range fields { + k := v.Field(i).Interface().(key.Binding) + desc = append(desc, fmt.Sprintf("%v", k.Help().Desc)) + } + + content := lipgloss.JoinHorizontal( + lipgloss.Top, + strings.Join(keys, "\n"), + strings.Join(desc, "\n"), + ) + + return strings.Split(style.Render(content), "\n") +} diff --git a/new/main.go b/new/main.go index 5901c0a..6686390 100644 --- a/new/main.go +++ b/new/main.go @@ -12,6 +12,11 @@ import ( tea "github.com/charmbracelet/bubbletea" ) +var ( + flagHelp bool + flagVersion bool +) + func main() { if _, ok := os.LookupEnv("FX_PPROF"); ok { f, err := os.Create("cpu.prof") @@ -30,6 +35,27 @@ func main() { defer pprof.WriteHeapProfile(memProf) } + var args []string + for _, arg := range os.Args[1:] { + switch arg { + case "-h", "--help": + flagHelp = true + case "-v", "-V", "--version": + flagVersion = true + default: + args = append(args, arg) + } + + } + if flagHelp { + fmt.Println(usage(keyMap)) + return + } + if flagVersion { + fmt.Println(version) + return + } + data, err := io.ReadAll(os.Stdin) if err != nil { panic(err) diff --git a/new/version.go b/new/version.go new file mode 100644 index 0000000..89a7551 --- /dev/null +++ b/new/version.go @@ -0,0 +1,3 @@ +package main + +const version = "30"