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"
)
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 := reflect.ValueOf(m.keyMap)
%v
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())
keys := make([]string, 0)
@ -36,5 +62,5 @@ func (m *model) helpView() []string {
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/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/mattn/go-isatty"
"github.com/muesli/termenv"
"golang.org/x/term"
@ -69,7 +70,7 @@ func main() {
args = os.Args[1:]
}
if dec == nil {
fmt.Println("No input provided. Usage: `fx data.json` or `curl ... | fx`")
fmt.Println(usage(DefaultKeyMap()))
os.Exit(1)
}
dec.UseNumber()
@ -446,7 +447,7 @@ func (m *model) render() {
m.recalculateViewportHeight()
if m.showHelp {
m.lines = m.helpView()
m.lines = keyMapInfo(m.keyMap, lipgloss.NewStyle().PaddingLeft(4).PaddingTop(2).PaddingBottom(2))
return
}