-
Notifications
You must be signed in to change notification settings - Fork 0
/
text_grid.go
150 lines (137 loc) · 3.21 KB
/
text_grid.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
package text
import (
"fmt"
"slices"
"github.com/relvox/iridescence_go/geom"
)
var TAB_SIZE = 4
type TextGrid struct {
Lines []string
Cursor geom.Point2
}
func NewTextGrid() *TextGrid {
return &TextGrid{
Lines: []string{""},
Cursor: [2]int{},
}
}
func (g *TextGrid) AppendRune(r rune) {
// fmt.Println(g.Cursor, string(r))
switch c := r; {
case c == 8: // backspace
if g.Cursor.X() == 0 {
if g.Cursor.Y() == 0 {
break
}
g.Cursor[0] = len(g.Lines[g.Cursor.Y()-1])
g.Lines[g.Cursor.Y()-1] += g.Lines[g.Cursor.Y()]
g.Cursor[1]--
if len(g.Lines)-1 > g.Cursor.Y()+1 {
copy(g.Lines[g.Cursor.Y()+1:], g.Lines[g.Cursor.Y()+2:])
}
g.Lines = g.Lines[:len(g.Lines)-1]
break
}
p := g.Cursor.X()
if p == len(g.Lines[g.Cursor.Y()]) {
g.Lines[g.Cursor.Y()] = g.Lines[g.Cursor.Y()][:p-1]
} else {
g.Lines[g.Cursor.Y()] = g.Lines[g.Cursor.Y()][:p] + g.Lines[g.Cursor.Y()][p+1:]
}
g.Cursor[0]--
case c == '\t':
for i := 0; i < TAB_SIZE; i++ {
g.AppendRune(' ')
}
case c == '\n':
g.Lines = append(g.Lines, "")
g.Cursor[0] = 0
g.Cursor[1] += 1
// case c >= ' ' && c <= '~':
case c == 56:
case c == '\r':
panic(fmt.Errorf("unsupported rune: \\r [%d]", r))
case c == 127: // delete
panic(fmt.Errorf("unsupported rune: \\DEL [%d]", r))
default:
g.Lines[g.Cursor.Y()] += string(c)
g.Cursor[0] += 1
// log.Println(fmt.Errorf("unknown rune: %c [%d]", r, r))
}
}
func (g *TextGrid) AppendString(s string) {
for _, r := range s {
g.AppendRune(r)
}
}
func (g *TextGrid) Print(a ...any) {
g.AppendString(fmt.Sprint(a...))
}
func (g *TextGrid) Printf(format string, a ...any) {
g.AppendString(fmt.Sprintf(format, a...))
}
func (g *TextGrid) Println(a ...any) {
g.AppendString(fmt.Sprintln(a...))
}
func (g *TextGrid) RewindTo(cur geom.Point2) {
newX, newY := cur.XY()
oldX, oldY := g.Cursor.XY()
if oldY < newY {
return
} else if oldY == newY {
if oldX <= newX {
return
} else {
g.Lines[g.Cursor.Y()] = g.Lines[g.Cursor.Y()][:newX+1]
g.Cursor[0] = newX
}
} else {
g.Lines = g.Lines[:newY+1]
g.Cursor[1] = newY
g.Lines[g.Cursor.Y()] = g.Lines[g.Cursor.Y()][:newX]
g.Cursor[0] = newX
}
}
// BoxedLines
// width, height, font - in units
// marginSpaceWrap - in runes
func (g *TextGrid) BoxedLines(width, height, font, marginSpaceWrap int, flipBreak bool) []string {
rWidth, rHeight := width/font-2, height/font/2-1
var results [][]string = make([][]string, 0, len(g.Lines))
for _, line := range g.Lines {
var subLines []string
for len(line) > rWidth {
var b int
for b = rWidth; b > rWidth-marginSpaceWrap; b-- {
if line[b] == ' ' {
break
}
}
if line[b] != ' ' {
subLines, line = append(subLines, line[:rWidth]), line[rWidth:]
} else {
subLines, line = append(subLines, line[:b]), line[b+1:]
}
}
if len(subLines) == 0 {
results = append(results, []string{line})
continue
}
if len(line) != 0 {
subLines = append(subLines, line)
}
results = append(results, subLines)
}
var result []string
for _, subLines := range results {
if flipBreak {
slices.Reverse(subLines)
}
result = append(result, subLines...)
}
h := len(result)
if h > rHeight {
return result[h-rHeight:]
}
return result
}