-
Notifications
You must be signed in to change notification settings - Fork 319
Expand file tree
/
Copy pathcanvas.go
More file actions
88 lines (73 loc) · 2.05 KB
/
canvas.go
File metadata and controls
88 lines (73 loc) · 2.05 KB
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
package lipgloss
import (
uv "github.com/charmbracelet/ultraviolet"
"github.com/charmbracelet/x/ansi"
)
// Canvas is a cell-buffer that can be used to compose and draw [uv.Drawable]s
// like [Layer]s.
//
// Composed drawables are drawn onto the canvas in the order they were
// composed, meaning later drawables will appear "on top" of earlier ones.
//
// A canvas can read, modify, and render its cell contents.
//
// It implements [uv.Screen] and [uv.Drawable].
type Canvas struct {
scr uv.ScreenBuffer
}
var _ uv.Screen = (*Canvas)(nil)
// NewCanvas creates a new [Canvas] with the given size.
func NewCanvas(width, height int) *Canvas {
c := new(Canvas)
c.scr = uv.NewScreenBuffer(width, height)
c.scr.Method = ansi.GraphemeWidth
return c
}
// Resize resizes the canvas to the given width and height.
func (c *Canvas) Resize(width, height int) {
c.scr.Resize(width, height)
}
// Clear clears the canvas.
func (c *Canvas) Clear() {
c.scr.Clear()
}
// Bounds implements [uv.Screen].
func (c *Canvas) Bounds() uv.Rectangle {
return c.scr.Bounds()
}
// Width returns the width of the canvas.
func (c *Canvas) Width() int {
return c.scr.Width()
}
// Height returns the height of the canvas.
func (c *Canvas) Height() int {
return c.scr.Height()
}
// CellAt implements [uv.Screen].
func (c *Canvas) CellAt(x int, y int) *uv.Cell {
return c.scr.CellAt(x, y)
}
// SetCell implements [uv.Screen].
func (c *Canvas) SetCell(x int, y int, cell *uv.Cell) {
c.scr.SetCell(x, y, cell)
}
// WidthMethod implements [uv.Screen].
func (c *Canvas) WidthMethod() uv.WidthMethod {
return c.scr.WidthMethod()
}
// Compose composes a [Layer] or any [uv.Drawable] onto the [Canvas].
func (c *Canvas) Compose(drawer uv.Drawable) *Canvas {
drawer.Draw(c, c.Bounds())
return c
}
// Draw draws the [Canvas] onto the given [uv.Screen] within the specified
// area.
//
// It implements [uv.Drawable].
func (c *Canvas) Draw(scr uv.Screen, area uv.Rectangle) {
c.scr.Draw(scr, area)
}
// Render renders the canvas into a styled string.
func (c *Canvas) Render() string {
return c.scr.Render()
}