Repository navigation
Expand file tree
/
Copy pathhelp.go
More file actions
278 lines (233 loc) · 6.97 KB
/
Copy pathhelp.go
File metadata and controls
278 lines (233 loc) · 6.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package main
import (
"fmt"
"os"
"path/filepath"
"reflect"
"strconv"
"strings"
"time"
"charm.land/bubbles/v2/key"
"charm.land/lipgloss/v2"
"github.com/antonmedv/fx/internal/format"
)
func usage() string {
title := lipgloss.NewStyle().Bold(true)
return fmt.Sprintf(`
%v
Terminal JSON viewer
%v
fx data.json
fx data.json .field
curl ... | fx
%v
-h, --help print help
-v, --version print version
--themes print themes
--comp <shell> print completion script
-r, --raw treat input as a raw string
-s, --slurp read all inputs into an array
%v --strict strict mode
--no-inline disable inlining in output
--no-tui print output instead of the viewer
--dungeon play the dungeon
%v
https://fx.wtf
%v
Anton Medvedev <anton@medv.io>
`,
title.Render("fx "+version),
title.Render("Usage"),
title.Render("Flags"),
formatFlags(),
title.Render("More info"),
title.Render("Author"),
)
}
var categoryOrder = []string{
"Navigation",
"Expand / Collapse",
"Search",
"Actions",
"View",
"Other",
}
func help(keyMap KeyMap) string {
titleStyle := lipgloss.NewStyle().
Bold(true)
categoryStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("8")).
MarginTop(1)
keyStyle := lipgloss.NewStyle()
descStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("8"))
dimStyle := lipgloss.NewStyle().
Foreground(lipgloss.Color("8"))
// Group bindings by category using struct tags
v := reflect.ValueOf(keyMap)
t := v.Type()
categories := make(map[string][]key.Binding)
for _, field := range reflect.VisibleFields(t) {
category := field.Tag.Get("category")
if category == "" {
continue
}
binding := v.FieldByName(field.Name).Interface().(key.Binding)
categories[category] = append(categories[category], binding)
}
var sb strings.Builder
section := func(title string) {
sb.WriteString("\n")
sb.WriteString(titleStyle.Render(" " + title))
sb.WriteString("\n")
sb.WriteString(dimStyle.Render(" ─────────────────────────────────────────"))
sb.WriteString("\n")
}
category := func(name string) {
sb.WriteString(categoryStyle.Render(" " + name))
sb.WriteString("\n")
}
row := func(keyStr, desc string) {
keyFormatted := keyStyle.Render(fmt.Sprintf("%-24s", keyStr))
descFormatted := descStyle.Render(desc)
sb.WriteString(fmt.Sprintf(" %s %s\n", keyFormatted, descFormatted))
}
text := func(lines ...string) {
sb.WriteString("\n")
for _, line := range lines {
sb.WriteString(" " + line + "\n")
}
}
section("Key Bindings")
for _, cat := range categoryOrder {
bindings, ok := categories[cat]
if !ok || len(bindings) == 0 {
continue
}
category(cat)
for _, binding := range bindings {
keyStr := binding.Help().Key
if len(keyStr) == 0 {
keys := binding.Keys()
if len(keys) > 5 {
keyStr = fmt.Sprintf("%v-%v", keys[0], keys[len(keys)-1])
} else {
keyStr = strings.Join(keys, ", ")
}
}
row(keyStr, binding.Help().Desc)
}
}
sb.WriteString("\n")
section("Commands")
text("Press : to open the command line.")
sb.WriteString("\n")
for _, c := range commands {
row(":"+c.usage(), c.desc)
}
sb.WriteString("\n")
section("Query")
text(
"Press . to open the query input,",
"type a query and press enter.",
)
category("Syntax")
row(".name", "property, .items[0] for index")
row(".items[].name", "map over array")
row("@.name", "map with expression")
row("?.active", "filter with expression")
row("x => x.a + 1", "any JS expression, x is input")
row("len keys sort uniq", "built-in functions")
row(".items len", "space separates piped queries")
category("Input Keys")
row("tab, shift+tab", "complete, cycle candidates")
row("right, end, ctrl+e", "accept suggestion")
row("up, down", "query history")
row("enter", "apply query")
row("esc", "cancel, restore original")
sb.WriteString("\n\n\n")
sb.WriteString(dimStyle.Render(" Press q or ? to close"))
sb.WriteString("\n")
return sb.String()
}
func exit() {
if showLetter(time.Now(), letterDir()) {
style := lipgloss.NewStyle().Border(lipgloss.RoundedBorder()).Padding(1, 2)
_, _ = fmt.Fprintln(os.Stderr, style.Render(`Hello, kind human. :)
This is fx speaking.
Most days, I quietly sit in your terminal and help you work
with JSON. No ads, no popups, no interruptions.
Today, I have one small request.
If fx has been useful to you, please consider supporting
the developer who keeps me alive and improving:
https://github.com/sponsors/antonmedv
Even a small contribution helps.
That’s all. Tomorrow I’ll be quiet again.
Thank you for using fx.`))
}
}
// letterDir returns the directory where fx remembers that the letter was
// shown: $XDG_CONFIG_HOME/fx, or ~/.config/fx. Empty if neither is known.
func letterDir() string {
dir := os.Getenv("XDG_CONFIG_HOME")
if dir == "" {
home, err := os.UserHomeDir()
if err != nil || home == "" {
return ""
}
dir = filepath.Join(home, ".config")
}
return filepath.Join(dir, "fx")
}
// showLetter reports whether the letter should be shown at time t. The letter
// is shown in December before Christmas, once per year: a marker file in dir
// records the year it was shown. If the marker cannot be read or written,
// the letter is shown on the first Tuesday of December instead.
func showLetter(t time.Time, dir string) bool {
if t.Month() != time.December || t.Day() >= 25 {
return false
}
if dir != "" {
if shown, ok := markLetterShown(dir, t.Year()); ok {
return !shown
}
}
return isFirstTuesdayOfDecember(t)
}
// markLetterShown records year in the marker file in dir. shown is true if
// the file already recorded year. ok is false if the marker could not be
// read or written, in which case nothing is recorded.
func markLetterShown(dir string, year int) (shown, ok bool) {
path := filepath.Join(dir, "letter")
data, err := os.ReadFile(path)
if err == nil && strings.TrimSpace(string(data)) == strconv.Itoa(year) {
return true, true
}
if err := os.MkdirAll(dir, 0o755); err != nil {
return false, false
}
if err := os.WriteFile(path, []byte(strconv.Itoa(year)+"\n"), 0o644); err != nil {
return false, false
}
return false, true
}
func isFirstTuesdayOfDecember(t time.Time) bool {
if t.Month() != time.December {
return false
}
firstOfDecember := time.Date(t.Year(), time.December, 1, 0, 0, 0, 0, t.Location())
offset := (int(time.Tuesday) - int(firstOfDecember.Weekday()) + 7) % 7
firstTuesday := firstOfDecember.AddDate(0, 0, offset)
return t.Year() == firstTuesday.Year() &&
t.Month() == firstTuesday.Month() &&
t.Day() == firstTuesday.Day()
}
// formatFlags lists the input format flags for the usage text, one line
// each, aligned with the other flags.
func formatFlags() string {
var b strings.Builder
for _, f := range format.All {
fmt.Fprintf(&b, " %-22s%s\n", f.Flag, f.Help)
}
return b.String()
}