fx/help.go

72 lines
1.4 KiB
Go
Raw Normal View History

2022-03-11 15:15:24 +00:00
package main
import (
"fmt"
"reflect"
"strings"
2022-05-08 12:19:58 +00:00
"github.com/charmbracelet/bubbles/key"
"github.com/charmbracelet/lipgloss"
2022-03-11 15:15:24 +00:00
)
2022-04-18 14:54:32 +00:00
func usage(keyMap KeyMap) string {
title := lipgloss.NewStyle().Bold(true)
2022-05-08 12:19:58 +00:00
pad := lipgloss.NewStyle().PaddingLeft(4)
return fmt.Sprintf(`
%v
Terminal JSON viewer
2022-03-11 15:15:24 +00:00
2022-05-08 12:19:58 +00:00
%v
fx data.json
fx data.json .field
curl ... | fx
2022-04-18 14:54:32 +00:00
2022-05-08 12:19:58 +00:00
%v
-h, --help print help
2022-05-08 12:19:58 +00:00
-v, --version print version
--print-code print code of the reducer
2022-04-18 14:54:32 +00:00
2022-05-08 12:19:58 +00:00
%v
2022-04-18 14:54:32 +00:00
%v
2022-05-08 12:19:58 +00:00
%v
[https://fx.wtf]
2022-04-18 14:54:32 +00:00
`,
2022-05-08 12:19:58 +00:00
title.Render("fx "+version),
2022-04-18 14:54:32 +00:00
title.Render("Usage"),
2022-05-08 12:19:58 +00:00
title.Render("Flags"),
2022-04-18 14:54:32 +00:00
title.Render("Key Bindings"),
2022-05-08 12:19:58 +00:00
strings.Join(keyMapInfo(keyMap, pad), "\n"),
2022-04-18 14:54:32 +00:00
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
}