summaryrefslogtreecommitdiffstats
path: root/ui/stashhelp.go
blob: 8ab9c222de4a0f9043b72b397bbfbe92fb082812 (plain)
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package ui

import (
	"fmt"
	"strings"

	"github.com/muesli/reflow/ansi"
)

// helpEntry is a entry in a help menu containing values for a keystroke and
// it's associated action.
type helpEntry struct{ key, val string }

// helpColumn is a group of helpEntries which will be rendered into a column.
type helpColumn []helpEntry

// newHelpColumn creates a help column from pairs of string arguments
// represeting keys and values. If the arguments are not even (and therein
// not every key has a matching value) the function will panic.
func newHelpColumn(pairs ...string) (h helpColumn) {
	if len(pairs)%2 != 0 {
		panic("help text group must have an even number of items")
	}

	for i := 0; i < len(pairs); i = i + 2 {
		h = append(h, helpEntry{key: pairs[i], val: pairs[i+1]})
	}

	return
}

// render returns styled and formatted rows from keys and values.
func (h helpColumn) render(height int) (rows []string) {
	keyWidth, valWidth := h.maxWidths()

	for i := 0; i < height; i++ {
		var (
			b    = strings.Builder{}
			k, v string
		)
		if i < len(h) {
			k = h[i].key
			v = h[i].val

			switch k {
			case "s":
				k = greenFg(k)
				v = semiDimGreenFg(v)
			default:
				k = grayFg(k)
				v = midGrayFg(v)
			}
		}
		b.WriteString(k)
		b.WriteString(strings.Repeat(" ", keyWidth-ansi.PrintableRuneWidth(k))) // pad keys
		b.WriteString("  ")                                                     // gap
		b.WriteString(v)
		b.WriteString(strings.Repeat(" ", valWidth-ansi.PrintableRuneWidth(v))) // pad vals
		rows = append(rows, b.String())
	}

	return
}

// maxWidths returns the widest key and values in the column, respectively.
func (h helpColumn) maxWidths() (maxKey int, maxVal int) {
	for _, v := range h {
		kw := ansi.PrintableRuneWidth(v.key)
		vw := ansi.PrintableRuneWidth(v.val)
		if kw > maxKey {
			maxKey = kw
		}
		if vw > maxVal {
			maxVal = vw
		}
	}

	return
}

// helpView returns either the mini or full help view depending on the state of
// the model, as well as the total height of the help view.
func (m stashModel) helpView() (string, int) {
	numDocs := len(m.getVisibleMarkdowns())

	// Help for when we're filtering
	if m.filterState == filtering {
		var h []string

		switch numDocs {
		case 0:
			h = []string{"enter/esc", "cancel"}
		case 1:
			h = []string{"enter", "open", "esc", "cancel"}
		default:
			h = []string{"enter", "confirm", "esc", "cancel", "ctrl+j/ctrl+k ↑/↓", "choose"}
		}

		return m.renderHelp(h)
	}

	// Help for when we're interacting with a single document
	switch m.selectionState {
	case selectionSettingNote:
		return m.renderHelp([]string{"enter", "confirm", "esc", "cancel"}, []string{"q", "quit"})
	case selectionPromptingDelete:
		return m.renderHelp([]string{"y", "delete", "n", "cancel"}, []string{"q", "quit"})
	}

	var (
		isStashed     bool
		isStashable   bool
		navHelp       []string
		filterHelp    []string
		selectionHelp []string
		sectionHelp   []string
		appHelp       []string
	)

	if numDocs > 0 {
		md := m.selectedMarkdown()
		isStashed = md != nil && md.docType == StashedDoc
		isStashable = md != nil && md.docType == LocalDoc && m.online()
	}

	if numDocs > 0 && m.showFullHelp {
		navHelp = []string{"enter", "open", "j/k ↑/↓", "choose"}
	}

	if len(m.sections) > 1 {
		if m.showFullHelp {
			navHelp = append(navHelp, "tab/shift+tab", "section")
		} else {
			navHelp = append(navHelp, "tab", "section")
		}
	}

	if m.paginator().TotalPages > 1 {
		navHelp = append(navHelp, "h/l ←/→", "page")
	}

	// If we're browsing a filtered set
	if m.filterState == filterApplied {
		filterHelp = []string{"/", "edit search", "esc", "clear search"}
	} else {
		filterHelp = []string{"/", "find"}
	}

	if isStashed {
		selectionHelp = []string{"x", "delete", "m", "set memo"}
	} else if isStashable {
		selectionHelp = []string{"s", "stash"}
	}

	// If there are errors
	if m.err != nil {
		appHelp = append(appHelp, "!", "errors")
	}

	appHelp = append(appHelp, "q", "quit")

	// Detailed help
	if m.showFullHelp {
		if m.filterState != filtering {
			appHelp = append(appHelp, "?", "close help")
		}
		return m.renderHelp(navHelp, filterHelp, selectionHelp, sectionHelp, appHelp)
	}

	// Mini help
	if m.filterState != filtering {
		appHelp = append(appHelp, "?", "more")
	}
	return m.renderHelp(navHelp, filterHelp, selectionHelp, sectionHelp, appHelp)
}

// renderHelp returns the rendered help view and associated line height for
// the given groups of help items.
func (m stashModel) renderHelp(groups ...[]string) (string, int) {
	if m.showFullHelp {
		str := m.fullHelpView(groups...)
		numLines := strings.Count(str, "\n") + 1
		return str, numLines
	}
	return m.miniHelpView(concatStringSlices(groups...)...), 1
}

// Builds the help view from various sections pieces, truncating it if the view
// would otherwise wrap to two lines. Help view entires should come in as pairs,
// with the first being the key and the second being the help text.
func (m stashModel) miniHelpView(entries ...string) string {
	if len(entries) == 0 {
		return ""
	}

	var (
		truncationChar  = subtleStyle.Render("…")
		truncationWidth = ansi.PrintableRuneWidth(truncationChar)
	)

	var (
		next       string
		leftGutter = "  "
		maxWidth   = m.common.width -
			stashViewHorizontalPadding -
			truncationWidth -
			ansi.PrintableRuneWidth(leftGutter)
		s = leftGutter
	)

	for i := 0; i < len(entries); i = i + 2 {
		k := entries[i]
		v := entries[i+1]

		switch k {
		case "s":
			k = greenFg(k)
			v = semiDimGreenFg(v)
		default:
			k = grayFg(k)
			v = midGrayFg(v)
		}

		next = fmt.Sprintf("%s %s", k, v)

		if i < len(entries)-2 {
			next += dividerDot
		}

		// Only this (and the following) help text items if we have the
		// horizontal space
		if ansi.PrintableRuneWidth(s)+ansi.PrintableRuneWidth(next) >= maxWidth {
			s += truncationChar
			break
		}

		s += ne