fx/help.go

67 lines
1.2 KiB
Go
Raw Normal View History

2022-03-11 15:15:24 +00:00
package main
import (
"fmt"
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/lipgloss"
"reflect"
"strings"
)
2022-04-18 14:54:32 +00:00
func usage(keyMap KeyMap) string {
title := lipgloss.NewStyle().Bold(true)
return fmt.Sprintf(`fx - terminal JSON viewer
2022-03-11 15:15:24 +00:00
2022-04-18 14:54:32 +00:00
%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)
2022-03-11 15:15:24 +00:00
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 width(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"),
)
2022-04-18 14:54:32 +00:00
return strings.Split(style.Render(content), "\n")
2022-03-11 15:15:24 +00:00
}