forked from reeflective/readline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
209 lines (170 loc) · 5.16 KB
/
prompt.go
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
package readline
import (
"strings"
"unicode/utf8"
ansi "github.com/acarl005/stripansi"
)
// prompt stores all prompt functions and strings,
// and is in charge of printing them as well as
// computing any resulting offsets.
type Prompt struct {
primary string
primaryF func() string
right string
rightF func() string
secondary string
secondaryF func() string
transient string
transientF func() string
tooltip string
tooltipF func(tip string) string
// True if some logs have printed asynchronously
// since last loop. Check refresh prompt funcs.
stillOnRefresh bool
// The offset used on the first line, where either
// the full prompt (or the last line) is. Used for
// correctly replacing the cursor.
inputAt int
}
// Primary uses a function returning the string to use as the primary prompt.
func (p *Prompt) Primary(prompt func() string) {
p.primaryF = prompt
}
// Right uses a function returning the string to use as the right prompt.
func (p *Prompt) Right(prompt func() string) {
p.rightF = prompt
}
// Secondary uses a function returning the prompt to use as the secondary prompt.
func (p *Prompt) Secondary(prompt func() string) {
p.secondaryF = prompt
}
// Transient uses a function returning the prompt to use as a transient prompt.
func (p *Prompt) Transient(prompt func() string) {
p.transientF = prompt
}
// Tooltip uses a function returning the prompt to use as a tooltip prompt.
func (p *Prompt) Tooltip(prompt func(tip string) string) {
p.tooltipF = prompt
}
// initPrompt is ran once at the beginning of an instance start.
func (p *Prompt) init(rl *Instance) {
// Generate the prompt strings for this run
if p.primaryF != nil {
p.primary = p.primaryF()
}
if p.rightF != nil {
p.right = p.rightF()
}
if p.transientF != nil {
p.transient = p.transientF()
}
if p.secondaryF != nil {
p.secondary = p.secondaryF()
}
// Compute some offsets needed by the last line.
rl.Prompt.compute(rl)
// Print the primary prompt, potentially excluding the last line.
print(p.getPrimary())
p.stillOnRefresh = false
}
// getPromptPrimary returns either the entire prompt if
// it's a single-line, or everything except the last line.
func (p *Prompt) getPrimary() string {
var primary string
lastLineIndex := strings.LastIndex(p.primary, "\n")
if lastLineIndex != -1 {
primary = p.primary[:lastLineIndex+1]
} else {
primary = p.primary
}
return primary
}
// Get the last line of the prompt to be printed.
func (p *Prompt) getPrimaryLastLine() string {
var lastLine string
lastLineIndex := strings.LastIndex(p.primary, "\n")
if lastLineIndex != -1 {
lastLine = p.primary[lastLineIndex+1:]
} else {
lastLine = p.primary
}
return lastLine
}
// computePromptAlt computes the correct lengths and offsets
// for all prompt components, but does not print any of them.
func (p *Prompt) compute(rl *Instance) {
prompt := p.primary
lastLineIndex := strings.LastIndex(prompt, "\n")
if lastLineIndex != -1 {
rl.Prompt.inputAt = getRealLength(prompt[lastLineIndex+1:])
} else {
rl.Prompt.inputAt = getRealLength(prompt)
}
}
// update is called after each key/widget processing, and refreshes
// the prompts that need to be at these intervals.
func (p *Prompt) update(rl *Instance) {
if rl.Prompt.tooltipF == nil {
return
}
var tooltipWord string
shellWords := strings.Split(string(rl.line), " ")
if len(shellWords) > 0 {
tooltipWord = shellWords[0]
}
rl.Prompt.tooltip = rl.Prompt.tooltipF(tooltipWord)
}
func (p *Prompt) printLast(rl *Instance) {
// Either use RPROMPT or tooltip.
var rprompt string
if p.tooltip != "" {
rprompt = p.tooltip
} else {
rprompt = p.right
}
// Print the primary prompt in any case.
defer print(p.getPrimaryLastLine())
if rprompt == "" {
return
}
// Only print the right prompt if the input line
// is shorter than the adjusted terminal width.
lineFits := (rl.Prompt.inputAt + len(rl.line) +
getRealLength(rprompt) + 1) < GetTermWidth()
if !lineFits {
return
}
// First go back to beginning of line, and clear everything
moveCursorBackwards(GetTermWidth())
print(seqClearLine)
print(seqClearScreenBelow)
// Go to where we must print the right prompt, print and go back
forwardOffset := GetTermWidth() - getRealLength(rprompt) - 1
moveCursorForwards(forwardOffset)
print(rprompt)
moveCursorBackwards(GetTermWidth())
}
func (p *Prompt) printTransient(rl *Instance) {
if p.transientF == nil || !rl.config.PromptTransient {
return
}
// First offset the newlines returned by our widgets,
// and clear everything below us.
moveCursorBackwards(GetTermWidth())
moveCursorUp(rl.fullY)
promptLines := strings.Count(p.primary, "\n")
moveCursorUp(promptLines)
print(seqClearLine)
print(seqClearScreenBelow)
// And print both the prompt and the input line.
print(p.transient)
println(string(rl.line))
}
// getRealLength - Some strings will have ANSI escape codes, which might be wrongly
// interpreted as legitimate parts of the strings. This will bother if some prompt
// components depend on other's length, so we always pass the string in this for
// getting its real-printed length.
func getRealLength(s string) (l int) {
colorStripped := ansi.Strip(s)
return utf8.RuneCountInString(colorStripped)
}