Debian Codegraph @

screen/screen.go

raw symbols SCIP
1 // Package screen provides functions and helpers to manipulate a [uv.Screen].
2 package screen
3
4 import (
5 uv "github.com/charmbracelet/ultraviolet"
6 )
7
8 // Clear clears the screen with empty cells. This is equivalent to filling the
9 // screen with empty cells.
10 //
11 // If the screen implements a [Clear] method, it will be called instead of
12 // filling the screen with empty cells.
13 func Clear(scr uv.Screen) {
14 if c, ok := scr.(interface {
15 Clear()
16 }); ok {
17 c.Clear()
18 return
19 }
20 Fill(scr, nil)
21 }
22
23 // ClearArea clears the given area of the screen with empty cells. This is
24 // equivalent to filling the area with empty cells.
25 //
26 // If the screen implements a [ClearArea] method, it will be called instead of
27 // filling the area with empty cells.
28 func ClearArea(scr uv.Screen, area uv.Rectangle) {
29 if c, ok := scr.(interface {
30 ClearArea(area uv.Rectangle)
31 }); ok {
32 c.ClearArea(area)
33 return
34 }
35 FillArea(scr, nil, area)
36 }
37
38 // Fill fills the screen with the given cell. If the cell is nil, it fills the
39 // screen with empty cells.
40 //
41 // If the screen implements a [Fill] method, it will be called instead of
42 // filling the screen with empty cells.
43 func Fill(scr uv.Screen, cell *uv.Cell) {
44 if f, ok := scr.(interface {
45 Fill(cell *uv.Cell)
46 }); ok {
47 f.Fill(cell)
48 return
49 }
50 FillArea(scr, cell, scr.Bounds())
51 }
52
53 // FillArea fills the given area of the screen with the given cell. If the cell
54 // is nil, it fills the area with empty cells.
55 //
56 // If the screen implements a [FillArea] method, it will be called instead of
57 // filling the area with empty cells.
58 func FillArea(scr uv.Screen, cell *uv.Cell, area uv.Rectangle) {
59 if f, ok := scr.(interface {
60 FillArea(cell *uv.Cell, area uv.Rectangle)
61 }); ok {
62 f.FillArea(cell, area)
63 return
64 }
65 for y := area.Min.Y; y < area.Max.Y; y++ {
66 for x := area.Min.X; x < area.Max.X; x++ {
67 scr.SetCell(x, y, cell)
68 }
69 }
70 }
71
72 // CloneArea clones the given area of the screen and returns a new buffer
73 // with the same size as the area. The new buffer will contain the same cells
74 // as the area in the screen.
75 // Use [uv.Buffer.Draw] to draw the cloned buffer to a screen again.
76 //
77 // If the screen implements a [CloneArea] method, it will be called instead of
78 // cloning the area manually.
79 func CloneArea(scr uv.Screen, area uv.Rectangle) *uv.Buffer {
80 if c, ok := scr.(interface {
81 CloneArea(area uv.Rectangle) *uv.Buffer
82 }); ok {
83 return c.CloneArea(area)
84 }
85 buf := uv.NewBuffer(area.Dx(), area.Dy())
86 for y := area.Min.Y; y < area.Max.Y; y++ {
87 for x := area.Min.X; x < area.Max.X; x++ {
88 cell := scr.CellAt(x, y)
89 if cell == nil || cell.IsZero() {
90 continue
91 }
92 buf.SetCell(x-area.Min.X, y-area.Min.Y, cell.Clone())
93 }
94 }
95 return buf
96 }
97
98 // Clone creates a new [uv.Buffer] clone of the given screen. The new buffer will
99 // have the same size as the screen and will contain the same cells.
100 // Use [uv.Buffer.Draw] to draw the cloned buffer to a screen again.
101 //
102 // If the screen implements a [Clone] method, it will be called instead of
103 // cloning the entire screen manually.
104 func Clone(scr uv.Screen) *uv.Buffer {
105 if c, ok := scr.(interface {
106 Clone() *uv.Buffer
107 }); ok {
108 return c.Clone()
109 }
110 return CloneArea(scr, scr.Bounds())
111 }