-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathscreen.go
158 lines (134 loc) · 3.84 KB
/
screen.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
// mauview - A Go TUI library based on tcell.
// Copyright © 2019 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package mauview
import (
"github.com/gdamore/tcell/v2"
)
// Screen is a subset of the tcell Screen.
// See https://godoc.org/maunium.net/go/tcell#Screen for documentation.
type Screen interface {
Clear()
Fill(rune, tcell.Style)
SetStyle(style tcell.Style)
SetCell(x, y int, style tcell.Style, ch ...rune)
GetContent(x, y int) (mainc rune, combc []rune, style tcell.Style, width int)
SetContent(x int, y int, mainc rune, combc []rune, style tcell.Style)
ShowCursor(x int, y int)
HideCursor()
Size() (int, int)
Colors() int
CharacterSet() string
CanDisplay(r rune, checkFallbacks bool) bool
HasKey(tcell.Key) bool
}
// ProxyScreen is a proxy to a tcell Screen with a specific allowed drawing area.
type ProxyScreen struct {
Parent Screen
OffsetX, OffsetY int
Width, Height int
Style tcell.Style
}
func NewProxyScreen(parent Screen, offsetX, offsetY, width, height int) Screen {
return &ProxyScreen{
Parent: parent,
OffsetX: offsetX,
OffsetY: offsetY,
Width: width,
Height: height,
Style: tcell.StyleDefault,
}
}
func (ss *ProxyScreen) IsInArea(x, y int) bool {
return x >= ss.OffsetX && x <= ss.OffsetX+ss.Width &&
y >= ss.OffsetY && y <= ss.OffsetY+ss.Height
}
func (ss *ProxyScreen) YEnd() int {
return ss.OffsetY + ss.Height
}
func (ss *ProxyScreen) XEnd() int {
return ss.OffsetX + ss.Width
}
func (ss *ProxyScreen) OffsetMouseEvent(event MouseEvent) MouseEvent {
return OffsetMouseEvent(event, -ss.OffsetX, -ss.OffsetY)
}
func (ss *ProxyScreen) Clear() {
ss.Fill(' ', ss.Style)
}
func (ss *ProxyScreen) Fill(r rune, style tcell.Style) {
for x := ss.OffsetX; x < ss.XEnd(); x++ {
for y := ss.OffsetY; y < ss.YEnd(); y++ {
ss.Parent.SetCell(x, y, style, r)
}
}
}
func (ss *ProxyScreen) SetStyle(style tcell.Style) {
ss.Style = style
}
func (ss *ProxyScreen) adjustCoordinates(x, y int) (int, int, bool) {
if x < 0 || y < 0 || (ss.Width >= 0 && x >= ss.Width) || (ss.Height >= 0 && y >= ss.Height) {
return -1, -1, false
}
x += ss.OffsetX
y += ss.OffsetY
return x, y, true
}
func (ss *ProxyScreen) SetCell(x, y int, style tcell.Style, ch ...rune) {
x, y, ok := ss.adjustCoordinates(x, y)
if ok {
ss.Parent.SetCell(x, y, style, ch...)
}
}
func (ss *ProxyScreen) GetContent(x, y int) (mainc rune, combc []rune, style tcell.Style, width int) {
x, y, ok := ss.adjustCoordinates(x, y)
if ok {
return ss.Parent.GetContent(x, y)
}
return 0, nil, tcell.StyleDefault, 0
}
func (ss *ProxyScreen) SetContent(x int, y int, mainc rune, combc []rune, style tcell.Style) {
x, y, ok := ss.adjustCoordinates(x, y)
if ok {
ss.Parent.SetContent(x, y, mainc, combc, style)
}
}
func (ss *ProxyScreen) ShowCursor(x, y int) {
x, y, ok := ss.adjustCoordinates(x, y)
if ok {
ss.Parent.ShowCursor(x, y)
}
}
func (ss *ProxyScreen) HideCursor() {
ss.Parent.HideCursor()
}
// Size returns the size of this subscreen.
//
// If the subscreen doesn't fit in the parent with the set offset and size,
// the returned size is whatever can actually be rendered.
func (ss *ProxyScreen) Size() (width int, height int) {
width, height = ss.Parent.Size()
width -= ss.OffsetX
height -= ss.OffsetY
if width > ss.Width {
width = ss.Width
}
if height > ss.Height {
height = ss.Height
}
return
}
func (ss *ProxyScreen) Colors() int {
return ss.Parent.Colors()
}
func (ss *ProxyScreen) CharacterSet() string {
return ss.Parent.CharacterSet()
}
func (ss *ProxyScreen) CanDisplay(r rune, checkFallbacks bool) bool {
return ss.Parent.CanDisplay(r, checkFallbacks)
}
func (ss *ProxyScreen) HasKey(key tcell.Key) bool {
return ss.Parent.HasKey(key)
}