From a58152469f88bcff55dc0cce7dfcc512becbaa76 Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Sun, 8 May 2022 14:19:58 +0200 Subject: [PATCH] Drop flags --- go.mod | 2 +- go.sum | 4 ++-- help.go | 38 +++++++++++++++++++++----------------- main.go | 49 +++++++++++++++++++++++++------------------------ 4 files changed, 49 insertions(+), 44 deletions(-) diff --git a/go.mod b/go.mod index 0360559..d1bee3e 100644 --- a/go.mod +++ b/go.mod @@ -26,7 +26,7 @@ require ( github.com/muesli/reflow v0.3.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect - golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 // indirect + golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 // indirect golang.org/x/text v0.3.7 // indirect gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect diff --git a/go.sum b/go.sum index b9655d0..2a9ff45 100644 --- a/go.sum +++ b/go.sum @@ -76,8 +76,8 @@ golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc= -golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 h1:nonptSpoQ4vQjyraW20DXPAglgQfVnM9ZC6MmNLMR60= +golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 h1:EH1Deb8WZJ0xc0WK//leUHXcX9aLE5SymusoTmMZye8= golang.org/x/term v0.0.0-20220411215600-e5f449aeb171/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= diff --git a/help.go b/help.go index 76e446f..5658c3c 100644 --- a/help.go +++ b/help.go @@ -2,36 +2,40 @@ package main import ( "fmt" - "github.com/charmbracelet/bubbles/key" - "github.com/charmbracelet/lipgloss" "reflect" "strings" + + "github.com/charmbracelet/bubbles/key" + "github.com/charmbracelet/lipgloss" ) func usage(keyMap KeyMap) string { title := lipgloss.NewStyle().Bold(true) - return fmt.Sprintf(`fx - terminal JSON viewer + pad := lipgloss.NewStyle().PaddingLeft(4) + return fmt.Sprintf(` + %v + Terminal JSON viewer -%v - fx data.json - fx data.json .field - curl ... | fx + %v + fx data.json + fx data.json .field + curl ... | fx -%v -%v + %v + -v, --version print version + --print-code print code of the reducer + %v %v - [https://fx.wtf] + + %v + [https://fx.wtf] `, + title.Render("fx "+version), title.Render("Usage"), + title.Render("Flags"), title.Render("Key Bindings"), - strings.Join( - keyMapInfo( - keyMap, - lipgloss.NewStyle().PaddingLeft(2), - ), - "\n", - ), + strings.Join(keyMapInfo(keyMap, pad), "\n"), title.Render("More info"), ) } diff --git a/main.go b/main.go index 12ec3c0..8a9ed2a 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,6 @@ import ( "path" "runtime/pprof" "strings" - "time" . "github.com/antonmedv/fx/pkg/dict" . "github.com/antonmedv/fx/pkg/json" @@ -22,12 +21,28 @@ import ( "github.com/muesli/termenv" ) +var ( + flagVersion bool + flagPrintCode bool +) + func main() { - if len(os.Args) == 2 && (os.Args[1] == "-v" || os.Args[1] == "-V" || os.Args[1] == "--version") { + var args []string + for _, arg := range os.Args[1:] { + switch arg { + case "-v", "-V", "--version": + flagVersion = true + case "--print-code": + flagPrintCode = true + default: + args = append(args, arg) + } + + } + if flagVersion && len(args) == 0 { fmt.Println(version) return } - cpuProfile := os.Getenv("CPU_PROFILE") if cpuProfile != "" { f, err := os.Create(cpuProfile) @@ -39,7 +54,6 @@ func main() { panic(err) } } - themeId, ok := os.LookupEnv("FX_THEME") if !ok { themeId = "1" @@ -51,19 +65,16 @@ func main() { if termenv.ColorProfile() == termenv.Ascii { theme = Themes["0"] } - stdinIsTty := isatty.IsTerminal(os.Stdin.Fd()) stdoutIsTty := isatty.IsTerminal(os.Stdout.Fd()) - filePath := "" fileName := "" - var args []string var dec *json.Decoder if stdinIsTty { // Nothing was piped, maybe file argument? - if len(os.Args) >= 2 { - filePath = os.Args[1] - f, err := os.Open(os.Args[1]) + if len(args) >= 1 { + filePath = args[0] + f, err := os.Open(filePath) if err != nil { switch err.(type) { case *fs.PathError: @@ -75,11 +86,10 @@ func main() { } fileName = path.Base(filePath) dec = json.NewDecoder(f) - args = os.Args[2:] + args = args[1:] } } else { dec = json.NewDecoder(os.Stdin) - args = os.Args[1:] } if dec == nil { fmt.Println(usage(DefaultKeyMap())) @@ -91,26 +101,24 @@ func main() { fmt.Println("JSON Parse Error:", err.Error()) os.Exit(1) } - lang, ok := os.LookupEnv("FX_LANG") if !ok { lang = "js" } - if dec.More() { exitCode := stream(dec, jsonObject, lang, args, theme) os.Exit(exitCode) } - if len(args) > 0 || !stdoutIsTty { - if len(args) > 0 && args[0] == "--print-code" { - fmt.Print(GenerateCode(lang, args[1:])) + if len(args) > 0 && flagPrintCode { + fmt.Print(GenerateCode(lang, args)) return } exitCode := Reduce(jsonObject, lang, args, theme) os.Exit(exitCode) } + // Start interactive mode. expand := map[string]bool{"": true} if array, ok := jsonObject.(Array); ok { for i := range array { @@ -130,10 +138,8 @@ func main() { canBeExpanded[it.Path] = len(it.Object.(Array)) > 0 } }) - input := textinput.New() input.Prompt = "" - m := &model{ fileName: fileName, theme: theme, @@ -152,11 +158,6 @@ func main() { searchInput: input, } m.collectSiblings(m.json, "") - - // TODO: delete after tea can reopen stdin. - // https://github.com/charmbracelet/bubbletea/issues/302 - time.Sleep(100 * time.Millisecond) - p := tea.NewProgram(m, tea.WithAltScreen(), tea.WithMouseCellMotion()) if err := p.Start(); err != nil { panic(err)