-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathclipregion.go
83 lines (74 loc) · 2.47 KB
/
clipregion.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
package winman
import "github.com/gdamore/tcell/v2"
// ClipRegion implements tcell.Screen and only allows setting content within
// a defined region
type ClipRegion struct {
tcell.Screen
x int
y int
width int
height int
style tcell.Style
}
// NewClipRegion Creates a new clipped screen with the given rectangular coordinates
func NewClipRegion(screen tcell.Screen, x, y, width, height int) *ClipRegion {
return &ClipRegion{
Screen: screen,
x: x,
y: y,
width: width,
height: height,
style: tcell.StyleDefault,
}
}
// InRect returns true if the given coordinates are within this clipped region
func (cr *ClipRegion) InRect(x, y int) bool {
return !(x < cr.x || y < cr.y || x >= cr.x+cr.width || y >= cr.y+cr.height)
}
// Fill implements tcell.Screen.Fill
func (cr *ClipRegion) Fill(ch rune, style tcell.Style) {
for x := cr.x; x < cr.width; x++ {
for y := cr.y; y < cr.height; y++ {
cr.SetContent(x, y, ch, nil, style)
}
}
}
// SetCell is an older API, and will be removed. Please use
// SetContent instead; SetCell is implemented in terms of SetContent.
func (cr *ClipRegion) SetCell(x int, y int, style tcell.Style, ch ...rune) {
if len(ch) > 0 {
cr.SetContent(x, y, ch[0], ch[1:], style)
} else {
cr.SetContent(x, y, ' ', nil, style)
}
}
// SetContent sets the contents of the given cell location. If
// the coordinates are out of range, then the operation is ignored.
//
// The first rune is the primary non-zero width rune. The array
// that follows is a possible list of combining characters to append,
// and will usually be nil (no combining characters.)
//
// The results are not displayed until Show() or Sync() is called.
//
// Note that wide (East Asian full width) runes occupy two cells,
// and attempts to place character at next cell to the right will have
// undefined effects. Wide runes that are printed in the
// last column will be replaced with a single width space on output.
func (cr *ClipRegion) SetContent(x int, y int, mainc rune, combc []rune, style tcell.Style) {
if cr.InRect(x, y) {
cr.Screen.SetContent(x, y, mainc, combc, style)
}
}
// ShowCursor is used to display the cursor at a given location.
// If the coordinates -1, -1 are given or are otherwise outside the
// dimensions of the screen, the cursor will be hidden.
func (cr *ClipRegion) ShowCursor(x int, y int) {
if cr.InRect(x, y) {
cr.Screen.ShowCursor(x, y)
}
}
// Clear clears the clipped region
func (cr *ClipRegion) Clear() {
cr.Fill(' ', cr.style)
}