Better usage output

This commit is contained in:
Anton Medvedev 2022-04-18 16:54:32 +02:00
parent 6ed6ba2ec4
commit c642fbd28e
2 changed files with 33 additions and 6 deletions

34
help.go
View File

@ -8,10 +8,36 @@ import (
"strings" "strings"
) )
var helpStyle = lipgloss.NewStyle().PaddingLeft(4).PaddingTop(2).PaddingBottom(2) func usage(keyMap KeyMap) string {
title := lipgloss.NewStyle().Bold(true)
return fmt.Sprintf(`fx - terminal JSON viewer
func (m *model) helpView() []string { %v
v := reflect.ValueOf(m.keyMap) fx data.json
fx data.json .field
curl ... | fx
%v
%v
%v
[https://fx.wtf]
`,
title.Render("Usage"),
title.Render("Key Bindings"),
strings.Join(
keyMapInfo(
keyMap,
lipgloss.NewStyle().PaddingLeft(2),
),
"\n",
),
title.Render("More info"),
)
}
func keyMapInfo(keyMap KeyMap, style lipgloss.Style) []string {
v := reflect.ValueOf(keyMap)
fields := reflect.VisibleFields(v.Type()) fields := reflect.VisibleFields(v.Type())
keys := make([]string, 0) keys := make([]string, 0)
@ -36,5 +62,5 @@ func (m *model) helpView() []string {
strings.Join(desc, "\n"), strings.Join(desc, "\n"),
) )
return strings.Split(helpStyle.Render(content), "\n") return strings.Split(style.Render(content), "\n")
} }

View File

@ -10,6 +10,7 @@ import (
"github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/bubbles/textinput" "github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea" tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mattn/go-isatty" "github.com/mattn/go-isatty"
"github.com/muesli/termenv" "github.com/muesli/termenv"
"golang.org/x/term" "golang.org/x/term"
@ -69,7 +70,7 @@ func main() {
args = os.Args[1:] args = os.Args[1:]
} }
if dec == nil { if dec == nil {
fmt.Println("No input provided. Usage: `fx data.json` or `curl ... | fx`") fmt.Println(usage(DefaultKeyMap()))
os.Exit(1) os.Exit(1)
} }
dec.UseNumber() dec.UseNumber()
@ -446,7 +447,7 @@ func (m *model) render() {
m.recalculateViewportHeight() m.recalculateViewportHeight()
if m.showHelp { if m.showHelp {
m.lines = m.helpView() m.lines = keyMapInfo(m.keyMap, lipgloss.NewStyle().PaddingLeft(4).PaddingTop(2).PaddingBottom(2))
return return
} }