-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtui_mode.go
More file actions
89 lines (83 loc) · 2.25 KB
/
Copy pathtui_mode.go
File metadata and controls
89 lines (83 loc) · 2.25 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
package main
// Modes.
//
// There were six, and nothing said so. Three were booleans, one a nullable
// pointer, one the length of a slice — `len(m.queryResults) > 0` short-circuits
// updateNav into a handler where eight keys work and every other binding is
// silently dead — and one more pointer that is explicitly NOT a mode. The
// dispatch chain listed five of them.
//
// The consequence was five different meanings for Esc (close everything /
// cancel and wipe / destroy state / step back / cancel one thing then another)
// and four for ctrl+c, including search, where it fell through into the text
// input so the program could not be quit at all.
//
// The set is now named in one place and the exits are uniform:
//
// Esc pop one level, NEVER destroying data
// ctrl+c quit, from anywhere
// q quit while browsing; an ordinary character wherever text is typed
//
// The mode is derived rather than stored. A stored copy would be a second
// source of truth for something the existing fields already determine, and the
// first inconsistency between them would be a bug nobody could see.
type uiMode int
const (
modeBrowse uiMode = iota
modeHelp
modeForm
modeQuery
modeSearch
modeExport
modeResults // a JPGQL result list is open
modeGoto // typing a vertex id to jump straight to
)
func (m tuiModel) mode() uiMode {
switch {
case m.helpOpen:
return modeHelp
case m.form != nil:
return modeForm
case m.queryMode:
return modeQuery
case m.searchMode:
return modeSearch
case m.exportMode:
return modeExport
case m.gotoMode:
return modeGoto
case len(m.queryResults) > 0:
return modeResults
default:
return modeBrowse
}
}
func (k uiMode) String() string {
switch k {
case modeHelp:
return "help"
case modeForm:
return "form"
case modeQuery:
return "query"
case modeSearch:
return "search"
case modeExport:
return "export"
case modeResults:
return "results"
case modeGoto:
return "go to"
default:
return "browse"
}
}
// typesText reports whether the mode has a text field taking keystrokes. In
// those, `q` is a letter and quitting is ctrl+c — everywhere else `q` quits.
func (k uiMode) typesText() bool {
switch k {
case modeQuery, modeSearch, modeExport, modeForm, modeGoto:
return true
}
return false
}