-
Notifications
You must be signed in to change notification settings - Fork 0
/
canvas.go
514 lines (451 loc) · 14.1 KB
/
canvas.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// ntcharts - Copyright (c) 2024 Neomantra Corp.
// Package canvas implements an abstract 2D area used to plot
// arbitary runes that can be displayed using the bubbletea framework.
package canvas
// File contains a Model using the bubbletea framework
// representing the state of the canvas,
// types and functions of the coordinates system used by the canvas
// and types and functions of the contents of the canvas.
import (
"image"
"math"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
zone "github.com/lrstanley/bubblezone"
)
// Point is alias for image.Point
type Point = image.Point
// Float64Point represents a point in a coordinate system
// with floating point precision.
type Float64Point struct {
X float64
Y float64
}
// NewFloat64PointFromPoint returns a new Float64Point from a given Point.
func NewFloat64PointFromPoint(p Point) Float64Point {
return Float64Point{X: float64(p.X), Y: float64(p.Y)}
}
// Mul returns a Float64Point with both X and Y values
// multiplied by the X and Y values of the given Float64Point.
func (p Float64Point) Mul(f Float64Point) Float64Point {
return Float64Point{p.X * f.X, p.Y * f.Y}
}
// Add returns a Float64Point with both X and Y values
// added to the X and Y values of the given Float64Point.
func (p Float64Point) Add(f Float64Point) Float64Point {
return Float64Point{p.X + f.X, p.Y + f.Y}
}
// Sub returns a Float64Point with both X and Y values
// subtracted by the X and Y values of the given Float64Point.
func (p Float64Point) Sub(f Float64Point) Float64Point {
return Float64Point{p.X - f.X, p.Y - f.Y}
}
// NewPointFromFloat64Point returns a new Point from a given Float64Point.
func NewPointFromFloat64Point(f Float64Point) Point {
return Point{int(math.Round(f.X)), int(math.Round(f.Y))}
}
// CanvasYCoordinates returns a sequence of Y coordinates in the
// canvas coordinates system (X,Y is top left) from a given sequence of Y coordinates
// in the Cartesian coordinates system (X,Y is bottom left)
// by passing the graph X axis in the canvas coordinates system.
func CanvasYCoordinates(xAxis int, seq []int) (r []int) {
r = make([]int, 0, len(seq))
for _, v := range seq {
r = append(r, CanvasYCoordinate(xAxis, v))
}
return
}
// CanvasYCoordinate returns a Y coordinates in the
// canvas coordinates system (X,Y is top left) from a Y coordinate
// in the Cartesian coordinates system (X,Y is bottom left)
// by passing the graph X axis in the canvas coordinates system.
func CanvasYCoordinate(xAxis int, y int) (r int) {
return xAxis - y
}
// CanvasPoints returns a sequence of Points in the
// canvas coordinates system (X,Y is top left) from a given sequence of Points
// in the Cartesian coordinates system (X,Y is bottom left)
// by passing the graph origin in the canvas coordinates system.
func CanvasPoints(origin Point, seq []Point) (r []Point) {
r = make([]Point, 0, len(seq))
for _, v := range seq {
r = append(r, CanvasPoint(origin, v))
}
return
}
// CanvasPoint returns a Point in the
// canvas coordinates system (X,Y is top left) from a given Point
// in the Cartesian coordinates system (X,Y is bottom left)
// by passing the graph origin in the canvas coordinates system.
func CanvasPoint(origin Point, p Point) (r Point) {
return Point{origin.X + p.X, origin.Y - p.Y}
}
// CanvasPointFromFloat64Point returns a Point
// in the canvas coordinates systems (X,Y is top left)
// from a canvas Float64Point in the
// Cartesian coordinates system (X,Y is bottom left)
// by passing the graph origin in the canvas coordinates system.
func CanvasPointFromFloat64Point(origin Point, f Float64Point) Point {
// round coordinates to nearest integer and
// convert Cartesian coordinates to canvas coordinates
return CanvasPoint(origin, NewPointFromFloat64Point(f))
}
// CanvasFloat64Point returns a Float64Point in the
// canvas coordinates system (X,Y is top left) from a given Float64Point
// in the Cartesian coordinates system (X,Y is bottom left)
// by passing the graph origin in the canvas coordinates system.
func CanvasFloat64Point(origin Point, p Float64Point) (r Float64Point) {
return Float64Point{float64(origin.X) + p.X, float64(origin.Y) - p.Y}
}
var defaultStyle = lipgloss.NewStyle()
// Cell contains a rune and lipgloss Style for rendering
type Cell struct {
Rune rune
Style lipgloss.Style
}
// NewCell returns Cell with given rune and default lipgloss Style.
func NewCell(r rune) Cell {
return Cell{Rune: r, Style: defaultStyle}
}
// NewCellWithStyle returns Cell with given rune and lipgloss Style.
func NewCellWithStyle(r rune, s lipgloss.Style) Cell {
return Cell{Rune: r, Style: s}
}
// CellLine is a slice of Cells
type CellLine []Cell
// Model contains state of a canvas
type Model struct {
Style lipgloss.Style // default style applied to all cells
KeyMap KeyMap // KeyMap used for keyboard msgs during Update()
UpdateHandler UpdateHandler // callback invoked during Update()
// overall canvas size
area image.Rectangle // 0,0 is top left of canvas
content []CellLine
focus bool
// simulates a viewport width and height
// to display contents of the canvas
ViewWidth int
ViewHeight int
// internal coordinates tracking viewport cursor
// the contents will be displayed from top to bottom
// and left to right of the cursor
// 0,0 is top left of canvas
cursor Point
// bubblezone Manager used to handler mouse events during Update()
zoneManager *zone.Manager
zoneID string
}
// New returns a canvas Model initialized with given width, height
// and various options.
func New(w, h int, opts ...Option) Model {
m := Model{
Style: defaultStyle,
KeyMap: DefaultKeyMap(),
UpdateHandler: DefaultUpdateHandler(),
area: image.Rect(0, 0, w, h),
content: make([]CellLine, h),
ViewWidth: w,
ViewHeight: h,
}
for i := range m.content {
m.content[i] = make(CellLine, w)
}
for _, opt := range opts {
opt(&m)
}
return m
}
// Width returns canvas width.
func (m *Model) Width() int {
return m.area.Dx()
}
// Height returns canvas height.
func (m *Model) Height() int {
return m.area.Dy()
}
// Cursor returns Point containg (X,Y) coordinates pointing to top left of viewport.
func (m *Model) Cursor() Point {
return m.cursor
}
// SetCursor sets (X,Y) coordinates of cursor pointing to top left of viewport.
// Coordinates will be bounded by canvas if x, y coordinates are out of bound.
func (m *Model) SetCursor(p Point) {
m.cursor.X = p.X
m.cursor.Y = p.Y
if m.cursor.X < 0 {
m.cursor.X = 0
} else if m.cursor.X >= m.area.Dx() {
m.cursor.X = m.area.Dx() - 1
}
if m.cursor.Y < 0 {
m.cursor.Y = 0
} else if m.cursor.Y >= m.area.Dy() {
m.cursor.Y = m.area.Dy() - 1
}
}
// Resize will resize canvas to new height and width, and resets cursor.
// Will truncate existing content if canvas size shrinks.
// Does not change viewport for displaying contents.
func (m *Model) Resize(w, h int) {
// create new lines and copy over previous contents
newLines := make([]CellLine, h)
for i := range newLines {
newLines[i] = make(CellLine, w)
// copy over previous line
if i < m.area.Dy() {
for j, r := range m.content[i] {
if j >= w {
break
}
newLines[i][j] = r
}
}
}
m.area = image.Rect(0, 0, w, h)
m.cursor.X = 0
m.cursor.Y = 0
m.content = newLines
}
// Clear will reset canvas contents.
func (m *Model) Clear() {
for i := range m.content {
m.content[i] = make(CellLine, m.area.Dx())
}
}
// SetLines copies []string into canvas as contents.
// Each string element represents a line in the canvas starting from top to bottom.
// Truncates contents if contents are greater than canvas height and width.
func (m *Model) SetLines(lines []string) bool {
return m.SetLinesWithStyle(lines, m.Style)
}
// SetLinesWithStyle copies []string into canvas as contents with style applied to all Cells.
// Each string element represents a line in the canvas starting from top to bottom.
// Truncates contents if contents are greater than canvas height and width.
func (m *Model) SetLinesWithStyle(lines []string, s lipgloss.Style) bool {
for y, l := range lines {
if y >= m.area.Dy() {
break
}
if !m.SetStringWithStyle(Point{0, y}, l, s) {
return false // should not happen
}
}
return true
}
// SetString copies string as rune values into canvas CellLine starting at coordinates (X, Y).
// Truncates values execeeding the canvas width.
func (m *Model) SetString(p Point, l string) bool {
return m.SetStringWithStyle(p, l, m.Style)
}
// SetStringWithStyle copies string as rune values into canvas CellLine starting at coordinates (X, Y).
// Style will be applied to all Cells.
// Truncates values execeeding the canvas width.
func (m *Model) SetStringWithStyle(p Point, l string, s lipgloss.Style) bool {
return m.SetRunesWithStyle(p, []rune(l), s)
}
// SetRunes copies rune values into canvas CellLine starting at coordinates (X, Y).
// Style will be applied to all Cells.
// Truncates values execeeding the canvas width.
func (m *Model) SetRunes(p Point, l []rune) bool {
return m.SetRunesWithStyle(p, l, m.Style)
}
// SetRunesWithStyle copies rune values into canvas CellLine starting at coordinates (X, Y).
// Style will be applied to all Cells.
// Truncates values execeeding the canvas width.
func (m *Model) SetRunesWithStyle(p Point, l []rune, s lipgloss.Style) bool {
if !m.insideYBounds(p.Y) {
return false
}
xIdx := p.X
for _, r := range l {
if m.insideXBounds(xIdx) {
m.content[p.Y][xIdx] = NewCellWithStyle(r, s)
}
xIdx += 1
}
return true
}
// SetCell sets a Cell using (X,Y) coordinates of canvas.
func (m *Model) SetCell(p Point, c Cell) bool {
if !p.In(m.area) {
return false
}
m.content[p.Y][p.X] = c
return true
}
// SetCellStyle sets Cell.Style using (X,Y) coordinates of canvas.
func (m *Model) SetCellStyle(p Point, s lipgloss.Style) bool {
if !p.In(m.area) {
return false
}
m.content[p.Y][p.X].Style = s
return true
}
// SetRune sets Cell.Rune using (X,Y) coordinates of canvas.
func (m *Model) SetRune(p Point, r rune) bool {
if !p.In(m.area) {
return false
}
m.content[p.Y][p.X] = NewCell(r)
return true
}
// Cell returns Cell located at (X,Y) coordinates of canvas.
// Returns default Cell if coorindates are out of bounds.
func (m *Model) Cell(p Point) (c Cell) {
if !p.In(m.area) {
return
}
c = m.content[p.Y][p.X]
return
}
// Fill sets all content in canvas to Cell.
func (m *Model) Fill(c Cell) {
for i := range m.content {
for j := range m.content[i] {
m.content[i][j] = c
}
}
}
// FillLine sets all Cells in a CellLine y away
// from origin to given Cell.
func (m *Model) FillLine(y int, c Cell) {
if !m.insideYBounds(y) {
return
}
for j := range m.content[y] {
m.content[y][j] = c
}
}
// SetStyle applies a lipgloss.Style to all Cells to change
// visual elements of each rune in the canvas.
func (m *Model) SetStyle(s lipgloss.Style) {
for i := range m.content {
for j := range m.content[i] {
m.content[i][j].Style = s
}
}
}
// SetZoneManager enables mouse functionality
// by setting a bubblezone.Manager to the canvas.
// The bubblezone.Manager can check bubbletea mouse event Msgs
// passed to the UpdateHandler handler during an Update().
// The root bubbletea model must wrap the View() string with
// bubblezone.Manager.Scan() to enable mouse functionality.
// To disable mouse functionality after enabling, call SetZoneManager on nil.
func (m *Model) SetZoneManager(zm *zone.Manager) {
m.zoneManager = zm
if (zm != nil) && (m.zoneID == "") {
m.zoneID = zm.NewPrefix()
}
}
// ZoneManager will return canvas zone Manager.
func (m *Model) ZoneManager() *zone.Manager {
return m.zoneManager
}
// ZoneID will return canvas zone ID used by zone Manager.
func (m *Model) ZoneID() string {
return m.zoneID
}
// ShiftUp moves all Cells up once.
// Last CellLine will be set to a new CellLine.
func (m *Model) ShiftUp() {
c := m.content
copy(c, c[1:])
c[len(c)-1] = make(CellLine, m.area.Dx())
}
// ShiftDown moves all Cells down once.
// First CellLine will be set to a new CellLine.
func (m *Model) ShiftDown() {
c := m.content
copy(c[1:], c)
c[0] = make(CellLine, m.area.Dx())
}
// ShiftLeft moves all Cells left once.
// Last cell in each CellLine will be a new default Cell.
func (m *Model) ShiftLeft() {
for i := range m.content {
cl := m.content[i]
copy(cl, cl[1:])
cl[len(cl)-1] = Cell{}
}
}
// ShiftRight moves all Cells right once.
// First cell in each CellLine will be a new default Cell.
func (m *Model) ShiftRight() {
for i := range m.content {
cl := m.content[i]
copy(cl[1:], cl)
cl[0] = Cell{}
}
}
// Focused returns whether canvas is being focused.
func (m *Model) Focused() bool {
return m.focus
}
// Focus enables Update events processing.
func (m *Model) Focus() {
m.focus = true
}
// Blur disables Update events processing.
func (m *Model) Blur() {
m.focus = false
}
// Init initializes the canvas.
func (m Model) Init() tea.Cmd {
return nil
}
// Update processes bubbletea Msg to by invoking
// UpdateHandler callback if canvas is focused.
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
if !m.focus {
return m, nil
}
m.UpdateHandler(&m, msg)
return m, nil
}
// View returns a string used by the bubbletea framework to display the canvas.
func (m Model) View() (r string) {
var sb strings.Builder
sb.Grow(m.area.Dx() * m.area.Dy())
endY := m.cursor.Y + m.ViewHeight - 1
endX := m.cursor.X + m.ViewWidth - 1
for i := m.cursor.Y; i <= endY; i++ {
if i >= m.area.Dy() {
break
}
for j := m.cursor.X; j <= endX; j++ {
if j >= m.area.Dx() {
break
}
cell := m.content[i][j]
if cell.Rune == 0 {
sb.WriteString(cell.Style.Render(" "))
} else {
sb.WriteString(cell.Style.Render(string(cell.Rune)))
}
}
if i != endY {
sb.WriteRune('\n')
}
}
r = sb.String()
if m.zoneManager != nil {
r = m.zoneManager.Mark(m.zoneID, r)
}
return
}
// insideXBounds returns whether X coordinate is within canvas bounds.
func (m *Model) insideXBounds(x int) bool {
if (x < 0) || (x >= m.area.Dx()) {
return false
}
return true
}
// insideYBounds returns whether Y coordinate is within canvas bounds.
func (m *Model) insideYBounds(y int) bool {
if (y < 0) || (y >= m.area.Dy()) {
return false
}
return true
}