1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package main
18
19 import (
20 "fmt"
21 "os"
22
23 "github.com/gdamore/tcell/v2"
24 "github.com/gdamore/tcell/v2/views"
25 )
26
27 var app = &views.Application{}
28 var window = &mainWindow{}
29
30 type model struct {
31 x int
32 y int
33 endx int
34 endy int
35 hide bool
36 enab bool
37 loc string
38 }
39
40 func (m *model) GetBounds() (int, int) {
41 return m.endx, m.endy
42 }
43
44 func (m *model) MoveCursor(offx, offy int) {
45 m.x += offx
46 m.y += offy
47 m.limitCursor()
48 }
49
50 func (m *model) limitCursor() {
51 if m.x < 0 {
52 m.x = 0
53 }
54 if m.x > m.endx-1 {
55 m.x = m.endx - 1
56 }
57 if m.y < 0 {
58 m.y = 0
59 }
60 if m.y > m.endy-1 {
61 m.y = m.endy - 1
62 }
63 m.loc = fmt.Sprintf("Cursor is %d,%d", m.x, m.y)
64 }
65
66 func (m *model) GetCursor() (int, int, bool, bool) {
67 return m.x, m.y, m.enab, !m.hide
68 }
69
70 func (m *model) SetCursor(x int, y int) {
71 m.x = x
72 m.y = y
73
74 m.limitCursor()
75 }
76
77 func (m *model) GetCell(x, y int) (rune, tcell.Style, []rune, int) {
78 dig := []rune{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
79 var ch rune
80 style := tcell.StyleDefault
81 if x >= 60 || y >= 15 {
82 return ch, style, nil, 1
83 }
84 colors := []tcell.Color{
85 tcell.ColorWhite,
86 tcell.ColorGreen,
87 tcell.ColorMaroon,
88 tcell.ColorNavy,
89 tcell.ColorOlive,
90 }
91 if y == 0 && x < len(m.loc) {
92 style = style.
93 Foreground(tcell.ColorWhite).
94 Background(tcell.ColorLime)
95 ch = rune(m.loc[x])
96 } else {
97 ch = dig[(x)%len(dig)]
98 style = style.
99 Foreground(colors[(y)%len(colors)]).
100 Background(tcell.ColorBlack)
101 }
102 return ch, style, nil, 1
103 }
104
105 type mainWindow struct {
106 main *views.CellView
107 keybar *views.SimpleStyledText
108 status *views.SimpleStyledTextBar
109 model *model
110
111 views.Panel
112 }
113
114 func (a *mainWindow) HandleEvent(ev tcell.Event) bool {
115
116 switch ev := ev.(type) {
117 case *tcell.EventKey:
118 switch ev.Key() {
119 case tcell.KeyCtrlL:
120 app.Refresh()
121 return true
122 case tcell.KeyRune:
123 switch ev.Rune() {
124 case 'Q', 'q':
125 app.Quit()
126 return true
127 case 'S', 's':
128 a.model.hide = false
129 a.updateKeys()
130 return true
131 case 'H', 'h':
132 a.model.hide = true
133 a.updateKeys()
134 return true
135 case 'E', 'e':
136 a.model.enab = true
137 a.updateKeys()
138 return true
139 case 'D', 'd':
140 a.model.enab = false
141 a.updateKeys()
142 return true
143 }
144 }
145 }
146 return a.Panel.HandleEvent(ev)
147 }
148
149 func (a *mainWindow) Draw() {
150 a.status.SetLeft(a.model.loc)
151 a.Panel.Draw()
152 }
153
154 func (a *mainWindow) updateKeys() {
155 m := a.model
156 w := "[%AQ%N] Quit"
157 if !m.enab {
158 w += " [%AE%N] Enable cursor"
159 } else {
160 w += " [%AD%N] Disable cursor"
161 if !m.hide {
162 w += " [%AH%N] Hide cursor"
163 } else {
164 w += " [%AS%N] Show cursor"
165 }
166 }
167 a.keybar.SetMarkup(w)
168 app.Update()
169 }
170
171 func main() {
172
173 window.model = &model{endx: 60, endy: 15}
174
175 title := views.NewTextBar()
176 title.SetStyle(tcell.StyleDefault.
177 Background(tcell.ColorTeal).
178 Foreground(tcell.ColorWhite))
179 title.SetCenter("CellView Test", tcell.StyleDefault)
180 title.SetRight("Example v1.0", tcell.StyleDefault)
181
182 window.keybar = views.NewSimpleStyledText()
183 window.keybar.RegisterStyle('N', tcell.StyleDefault.
184 Background(tcell.ColorSilver).
185 Foreground(tcell.ColorBlack))
186 window.keybar.RegisterStyle('A', tcell.StyleDefault.
187 Background(tcell.ColorSilver).
188 Foreground(tcell.ColorRed))
189
190 window.status = views.NewSimpleStyledTextBar()
191 window.status.SetStyle(tcell.StyleDefault.
192 Background(tcell.ColorBlue).
193 Foreground(tcell.ColorYellow))
194 window.status.RegisterLeftStyle('N', tcell.StyleDefault.
195 Background(tcell.ColorYellow).
196 Foreground(tcell.ColorBlack))
197
198 window.status.SetLeft("My status is here.")
199 window.status.SetRight("%UCellView%N demo!")
200 window.status.SetCenter("Cen%ST%Ner")
201
202 window.main = views.NewCellView()
203 window.main.SetModel(window.model)
204 window.main.SetStyle(tcell.StyleDefault.
205 Background(tcell.ColorBlack))
206
207 window.SetMenu(window.keybar)
208 window.SetTitle(title)
209 window.SetContent(window.main)
210 window.SetStatus(window.status)
211
212 window.updateKeys()
213
214 app.SetStyle(tcell.StyleDefault.
215 Foreground(tcell.ColorWhite).
216 Background(tcell.ColorBlack))
217 app.SetRootWidget(window)
218 if e := app.Run(); e != nil {
219 fmt.Fprintln(os.Stderr, e.Error())
220 os.Exit(1)
221 }
222 }
223
View as plain text