buffer.go 6.96 KB
Newer Older
Patrick Devine's avatar
Patrick Devine committed
1
2
3
4
package readline

import (
	"fmt"
5
	"os"
Patrick Devine's avatar
Patrick Devine committed
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

	"github.com/emirpasic/gods/lists/arraylist"
	"golang.org/x/term"
)

type Buffer struct {
	Pos       int
	Buf       *arraylist.List
	Prompt    *Prompt
	LineWidth int
	Width     int
	Height    int
}

func NewBuffer(prompt *Prompt) (*Buffer, error) {
21
	fd := int(os.Stdout.Fd())
Michael Yang's avatar
Michael Yang committed
22
23
24
	width, height := 80, 24
	if termWidth, termHeight, err := term.GetSize(fd); err == nil {
		width, height = termWidth, termHeight
Patrick Devine's avatar
Patrick Devine committed
25
26
	}

Michael Yang's avatar
Michael Yang committed
27
	lwidth := width - len(prompt.prompt())
Patrick Devine's avatar
Patrick Devine committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

	b := &Buffer{
		Pos:       0,
		Buf:       arraylist.New(),
		Prompt:    prompt,
		Width:     width,
		Height:    height,
		LineWidth: lwidth,
	}

	return b, nil
}

func (b *Buffer) MoveLeft() {
	if b.Pos > 0 {
		if b.Pos%b.LineWidth == 0 {
			fmt.Printf(CursorUp + CursorBOL + cursorRightN(b.Width))
		} else {
46
			fmt.Print(CursorLeft)
Patrick Devine's avatar
Patrick Devine committed
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
		}
		b.Pos -= 1
	}
}

func (b *Buffer) MoveLeftWord() {
	if b.Pos > 0 {
		var foundNonspace bool
		for {
			v, _ := b.Buf.Get(b.Pos - 1)
			if v == ' ' {
				if foundNonspace {
					break
				}
			} else {
				foundNonspace = true
			}
			b.MoveLeft()

			if b.Pos == 0 {
				break
			}
		}
	}
}

func (b *Buffer) MoveRight() {
	if b.Pos < b.Size() {
		b.Pos += 1
		if b.Pos%b.LineWidth == 0 {
Michael Yang's avatar
Michael Yang committed
77
			fmt.Printf(CursorDown + CursorBOL + cursorRightN(len(b.Prompt.prompt())))
Patrick Devine's avatar
Patrick Devine committed
78
		} else {
79
			fmt.Print(CursorRight)
Patrick Devine's avatar
Patrick Devine committed
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
		}
	}
}

func (b *Buffer) MoveRightWord() {
	if b.Pos < b.Size() {
		for {
			b.MoveRight()
			v, _ := b.Buf.Get(b.Pos)
			if v == ' ' {
				break
			}

			if b.Pos == b.Size() {
				break
			}
		}
	}
}

func (b *Buffer) MoveToStart() {
	if b.Pos > 0 {
		currLine := b.Pos / b.LineWidth
		if currLine > 0 {
			for cnt := 0; cnt < currLine; cnt++ {
105
				fmt.Print(CursorUp)
Patrick Devine's avatar
Patrick Devine committed
106
107
			}
		}
Michael Yang's avatar
Michael Yang committed
108
		fmt.Printf(CursorBOL + cursorRightN(len(b.Prompt.prompt())))
Patrick Devine's avatar
Patrick Devine committed
109
110
111
112
113
114
115
116
117
118
		b.Pos = 0
	}
}

func (b *Buffer) MoveToEnd() {
	if b.Pos < b.Size() {
		currLine := b.Pos / b.LineWidth
		totalLines := b.Size() / b.LineWidth
		if currLine < totalLines {
			for cnt := 0; cnt < totalLines-currLine; cnt++ {
119
				fmt.Print(CursorDown)
Patrick Devine's avatar
Patrick Devine committed
120
121
			}
			remainder := b.Size() % b.LineWidth
Michael Yang's avatar
Michael Yang committed
122
			fmt.Printf(CursorBOL + cursorRightN(len(b.Prompt.prompt())+remainder))
Patrick Devine's avatar
Patrick Devine committed
123
		} else {
124
			fmt.Print(cursorRightN(b.Size() - b.Pos))
Patrick Devine's avatar
Patrick Devine committed
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
		}

		b.Pos = b.Size()
	}
}

func (b *Buffer) Size() int {
	return b.Buf.Size()
}

func (b *Buffer) Add(r rune) {
	if b.Pos == b.Buf.Size() {
		fmt.Printf("%c", r)
		b.Buf.Add(r)
		b.Pos += 1
		if b.Pos > 0 && b.Pos%b.LineWidth == 0 {
			fmt.Printf("\n%s", b.Prompt.AltPrompt)
		}
	} else {
		fmt.Printf("%c", r)
		b.Buf.Insert(b.Pos, r)
		b.Pos += 1
		if b.Pos > 0 && b.Pos%b.LineWidth == 0 {
			fmt.Printf("\n%s", b.Prompt.AltPrompt)
		}
		b.drawRemaining()
	}
}

func (b *Buffer) drawRemaining() {
	var place int
	remainingText := b.StringN(b.Pos)
	if b.Pos > 0 {
		place = b.Pos % b.LineWidth
	}
160
	fmt.Print(CursorHide)
Patrick Devine's avatar
Patrick Devine committed
161
162
163
164
165

	// render the rest of the current line
	currLine := remainingText[:min(b.LineWidth-place, len(remainingText))]
	if len(currLine) > 0 {
		fmt.Printf(ClearToEOL + currLine)
166
		fmt.Print(cursorLeftN(len(currLine)))
Patrick Devine's avatar
Patrick Devine committed
167
	} else {
168
		fmt.Print(ClearToEOL)
Patrick Devine's avatar
Patrick Devine committed
169
170
171
172
173
174
175
176
177
178
179
180
181
	}

	// render the other lines
	if len(remainingText) > len(currLine) {
		remaining := []rune(remainingText[len(currLine):])
		var totalLines int
		for i, c := range remaining {
			if i%b.LineWidth == 0 {
				fmt.Printf("\n%s", b.Prompt.AltPrompt)
				totalLines += 1
			}
			fmt.Printf("%c", c)
		}
182
183
		fmt.Print(ClearToEOL)
		fmt.Print(cursorUpN(totalLines))
Patrick Devine's avatar
Patrick Devine committed
184
185
186
		fmt.Printf(CursorBOL + cursorRightN(b.Width-len(currLine)))
	}

187
	fmt.Print(CursorShow)
Patrick Devine's avatar
Patrick Devine committed
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
}

func (b *Buffer) Remove() {
	if b.Buf.Size() > 0 && b.Pos > 0 {
		if b.Pos%b.LineWidth == 0 {
			// if the user backspaces over the word boundary, do this magic to clear the line
			// and move to the end of the previous line
			fmt.Printf(CursorBOL + ClearToEOL)
			fmt.Printf(CursorUp + CursorBOL + cursorRightN(b.Width) + " " + CursorLeft)
		} else {
			fmt.Printf(CursorLeft + " " + CursorLeft)
		}

		var eraseExtraLine bool
		if (b.Size()-1)%b.LineWidth == 0 {
			eraseExtraLine = true
		}

		b.Pos -= 1
		b.Buf.Remove(b.Pos)

		if b.Pos < b.Size() {
			b.drawRemaining()
			// this erases a line which is left over when backspacing in the middle of a line and there
			// are trailing characters which go over the line width boundary
			if eraseExtraLine {
				remainingLines := (b.Size() - b.Pos) / b.LineWidth
				fmt.Printf(cursorDownN(remainingLines+1) + CursorBOL + ClearToEOL)
				place := b.Pos % b.LineWidth
Michael Yang's avatar
Michael Yang committed
217
				fmt.Printf(cursorUpN(remainingLines+1) + cursorRightN(place+len(b.Prompt.prompt())))
Patrick Devine's avatar
Patrick Devine committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
			}
		}
	}
}

func (b *Buffer) Delete() {
	if b.Size() > 0 && b.Pos < b.Size() {
		b.Buf.Remove(b.Pos)
		b.drawRemaining()
		if b.Size()%b.LineWidth == 0 {
			if b.Pos != b.Size() {
				remainingLines := (b.Size() - b.Pos) / b.LineWidth
				fmt.Printf(cursorDownN(remainingLines) + CursorBOL + ClearToEOL)
				place := b.Pos % b.LineWidth
Michael Yang's avatar
Michael Yang committed
232
				fmt.Printf(cursorUpN(remainingLines) + cursorRightN(place+len(b.Prompt.prompt())))
Patrick Devine's avatar
Patrick Devine committed
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
			}
		}
	}
}

func (b *Buffer) DeleteBefore() {
	if b.Pos > 0 {
		for cnt := b.Pos - 1; cnt >= 0; cnt-- {
			b.Remove()
		}
	}
}

func (b *Buffer) DeleteRemaining() {
	if b.Size() > 0 && b.Pos < b.Size() {
		charsToDel := b.Size() - b.Pos
		for cnt := 0; cnt < charsToDel; cnt++ {
			b.Delete()
		}
	}
}

func (b *Buffer) DeleteWord() {
	if b.Buf.Size() > 0 && b.Pos > 0 {
		var foundNonspace bool
		for {
			v, _ := b.Buf.Get(b.Pos - 1)
			if v == ' ' {
				if !foundNonspace {
					b.Remove()
				} else {
					break
				}
			} else {
				foundNonspace = true
				b.Remove()
			}

			if b.Pos == 0 {
				break
			}
		}
	}
}

func (b *Buffer) ClearScreen() {
Michael Yang's avatar
Michael Yang committed
279
	fmt.Printf(ClearScreen + CursorReset + b.Prompt.prompt())
Patrick Devine's avatar
Patrick Devine committed
280
	if b.IsEmpty() {
Michael Yang's avatar
Michael Yang committed
281
		ph := b.Prompt.placeholder()
Patrick Devine's avatar
Patrick Devine committed
282
283
284
285
286
		fmt.Printf(ColorGrey + ph + cursorLeftN(len(ph)) + ColorDefault)
	} else {
		currPos := b.Pos
		b.Pos = 0
		b.drawRemaining()
Michael Yang's avatar
Michael Yang committed
287
		fmt.Printf(CursorReset + cursorRightN(len(b.Prompt.prompt())))
Patrick Devine's avatar
Patrick Devine committed
288
289
290
291
		if currPos > 0 {
			targetLine := currPos / b.LineWidth
			if targetLine > 0 {
				for cnt := 0; cnt < targetLine; cnt++ {
292
					fmt.Print(CursorDown)
Patrick Devine's avatar
Patrick Devine committed
293
294
295
296
				}
			}
			remainder := currPos % b.LineWidth
			if remainder > 0 {
297
				fmt.Print(cursorRightN(remainder))
Patrick Devine's avatar
Patrick Devine committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
			}
			if currPos%b.LineWidth == 0 {
				fmt.Printf(CursorBOL + b.Prompt.AltPrompt)
			}
		}
		b.Pos = currPos
	}
}

func (b *Buffer) IsEmpty() bool {
	return b.Buf.Empty()
}

func (b *Buffer) Replace(r []rune) {
	b.Pos = 0
	b.Buf.Clear()
Michael Yang's avatar
Michael Yang committed
314
	fmt.Printf(ClearLine + CursorBOL + b.Prompt.prompt())
Patrick Devine's avatar
Patrick Devine committed
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
	for _, c := range r {
		b.Add(c)
	}
}

func (b *Buffer) String() string {
	return b.StringN(0)
}

func (b *Buffer) StringN(n int) string {
	return b.StringNM(n, 0)
}

func (b *Buffer) StringNM(n, m int) string {
	var s string
	if m == 0 {
		m = b.Size()
	}
	for cnt := n; cnt < m; cnt++ {
		c, _ := b.Buf.Get(cnt)
		s += string(c.(rune))
	}
	return s
}

func cursorLeftN(n int) string {
	return fmt.Sprintf(CursorLeftN, n)
}

func cursorRightN(n int) string {
	return fmt.Sprintf(CursorRightN, n)
}

func cursorUpN(n int) string {
	return fmt.Sprintf(CursorUpN, n)
}

func cursorDownN(n int) string {
	return fmt.Sprintf(CursorDownN, n)
}