/
repaints.go
112 lines (99 loc) · 2.72 KB
/
repaints.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
package readline
import (
"fmt"
"strings"
"github.com/nyaosorg/go-readline-ny/internal/moji"
)
const (
CursorPositionDummyRune = '\uE000'
)
func (B *Buffer) refreshColor() ColorSequence {
defaultColor := B.Coloring.Init()
position := int16(0)
var tmpbuf strings.Builder
for i, cell := range B.Buffer {
if i == B.Cursor {
B.Coloring.Next(CursorPositionDummyRune)
}
B.Buffer[i].position = position
if tab, ok := cell.Moji.(*moji.Tab); ok {
tab.SetPosition(position)
B.Buffer[i].color = ColorSequence(B.Coloring.Next('\t'))
} else if codepoint, ok := moji.MojiToRune(cell.Moji); ok {
B.Buffer[i].color = ColorSequence(B.Coloring.Next(codepoint))
} else {
cell.Moji.PrintTo(&tmpbuf)
var cs ColorSequence
for _, c := range tmpbuf.String() {
cs = ColorSequence(B.Coloring.Next(c))
}
B.Buffer[i].color = cs
tmpbuf.Reset()
}
position += int16(cell.Moji.Width())
}
if len(B.Buffer) == B.Cursor {
B.Coloring.Next(CursorPositionDummyRune)
}
return defaultColor
}
// InsertAndRepaint inserts str and repaint the editline.
func (B *Buffer) InsertAndRepaint(str string) {
B.ReplaceAndRepaint(B.Cursor, str)
}
// GotoHead move screen-cursor to the top of the viewarea.
// It should be called before text is changed.
func (B *Buffer) GotoHead() {
fmt.Fprintf(B.Out, "\x1B[%dG", B.topColumn+1)
}
func (B *Buffer) repaint() {
all, left := B.view2()
B.GotoHead()
B.puts(all)
B.eraseline()
B.GotoHead()
B.puts(left)
}
// DrawFromHead draw all text in viewarea and
// move screen-cursor to the position where it should be.
func (B *Buffer) DrawFromHead() {
B.repaint()
}
// ReplaceAndRepaint replaces the string between `pos` and cursor's position to `str`
func (B *Buffer) ReplaceAndRepaint(pos int, str string) {
// Replace Buffer
B.Delete(pos, B.Cursor-pos)
// Define ViewStart , Cursor
B.Cursor = pos + B.InsertString(pos, str)
B.joinUndo() // merge delete and insert
B.ResetViewStart()
B.repaint()
}
// RepaintAfterPrompt repaints the all characters in the editline except for prompt.
func (B *Buffer) RepaintAfterPrompt() {
B.ResetViewStart()
B.repaint()
}
// RepaintAll repaints the all characters in the editline including prompt.
func (B *Buffer) RepaintAll() {
B.Out.Flush()
B.topColumn, _ = B.callPromptWriter()
B.repaint()
}
// RepaintLastLine repaints the last line of the prompt and input-line.
// IMPORTANT: This method requires setting valid Editor.PromptWriter
func (B *Buffer) RepaintLastLine() {
B.Out.Flush()
var prompt string
if B.PromptWriter == nil {
prompt = "\r> "
} else {
var buffer strings.Builder
buffer.WriteByte('\r')
B.PromptWriter(&buffer)
prompt = buffer.String()
prompt = strings.ReplaceAll(prompt, "\n", "\r")
}
B.Out.WriteString(prompt)
B.repaint()
}