-
Notifications
You must be signed in to change notification settings - Fork 0
/
screen.go
290 lines (240 loc) · 8.32 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"os/exec"
"time"
"github.com/gdamore/tcell/v2"
)
var defStyle = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDefault) // Provide option to change, later.
var boxStyle = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorRed)
var noteBoxStyle = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorWhite)
var focusedListStyle = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorDarkOrange)
var unfocusedListStyle = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorMediumPurple)
var noteViewBoxStyle = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorGreen)
var confirmPromptBoxStyle = tcell.StyleDefault.Background(tcell.ColorDefault).Foreground(tcell.ColorYellow)
var errBoxStyle = tcell.StyleDefault.Background(tcell.ColorOrangeRed).Foreground(tcell.ColorBlack)
var errTextStyle = tcell.StyleDefault.Background(tcell.ColorOrangeRed).Foreground(tcell.ColorBlack)
// Enum for controlling state
type context int
const (
ctxMain context = iota // Main = main screen with note lists. No input boxes, no notes opened.
ctxNoteView // NoteView = the screen, in tcell, for viewing full note contents. Not the text editor.
ctxConfirm
)
var currentCtx = ctxMain
// Enum for confirmation prompt string substitution
type action string
const (
ActionQuit action = "quit"
ActionDeleteNote = "delete this note"
ActionDeleteList = "delete this list"
ActionSave = "save"
)
var attemptedAction = action("")
var errMsg = ""
// TODO: There's currently no guarantee that these strings will fit the window. Build them upon window resize.
var keyBindingsStrings = []string{
" q: Quit, s: Save, Arrows: Change selection, Shift+Arrows: Move selection, u: Refresh, o: OtherCmds ",
" Notes:: a: Add, e: Edit, d: Delete, v: View, o: OtherCmds ",
" Lists:: A: Add, r: Rename, D: Delete, o: OtherCmds ",
" q,v: Back, s: Save, e: Edit, d: Delete, arrows: Change selection, Shift+arrows: Move selection, u: refresh ",
}
var keyBindingsStringIndex = 0
var screenListCap int
var screenNoteCap int
func drawScreen(s tcell.Screen) {
s.Clear() // Because of the background square, this might not be necessary.
xmax, ymax := s.Size()
drawBox(s, 0, 0, xmax-1, ymax-1, boxStyle, "", "") // Background
kan.draw(s)
midX, midY := (xmax-1)/2, (ymax-1)/2
switch currentCtx {
case ctxNoteView:
var noteViewW, noteViewH = xmax - 8, ymax - 8
b := BoundaryBox{midX - (noteViewW / 2), midY - (noteViewH / 2), midX + (noteViewW / 2), midY + (noteViewH / 2)}
x1, y1, x2, y2 := b.x1, b.y1, b.x2, b.y2
windowTitle := " Note "
drawBox(s, x1, y1, x2, y2, noteViewBoxStyle, windowTitle, "")
drawText(s, x1+2, y1+2, x2-2, y2-2, defStyle, kan.currentNote().Text)
drawText(s, 5, ymax-1, xmax-1, ymax-1, defStyle, keyBindingsStrings[3])
case ctxConfirm:
promptMsg := fmt.Sprintf(" Are you sure you want to %s? [y/N] ", attemptedAction)
var confirmBoxW, confirmBoxH = len(promptMsg) + 2, 3
b := BoundaryBox{midX - (confirmBoxW / 2), midY - (confirmBoxH / 2), midX + (confirmBoxW / 2), midY + (confirmBoxH / 2)}
x1, y1, x2, y2 := b.x1, b.y1, b.x2, b.y2
drawBox(s, x1, y1, x2, y2, confirmPromptBoxStyle, "", "")
drawText(s, x1+1, y1+1, x2-1, y2-1, defStyle, promptMsg)
default:
drawText(s, 5, ymax-1, xmax-1, ymax-1, defStyle, keyBindingsStrings[keyBindingsStringIndex])
}
// Show selected list and note numbers at top right of screen
currentNoteIndex := kan.curNoteIdx + 1
if kan.currentList().length() == 0 {
currentNoteIndex = 0
}
noteNumber := fmt.Sprintf(" Note %d/%d ", currentNoteIndex, kan.currentList().length())
noteNumberXPos := xmax - 2 - len(noteNumber)
drawText(s, noteNumberXPos, 0, xmax-1, 0, defStyle, noteNumber)
currentListIndex := kan.curListIdx + 1
if len(kan.Lists) == 0 {
currentListIndex = 0
}
listNumber := fmt.Sprintf(" List %d/%d ", currentListIndex, len(kan.Lists))
drawText(s, noteNumberXPos-len(listNumber), 0, xmax-10, 0, defStyle, listNumber)
if DEBUG_MODE {
drawBox(s, 1, ymax-5, xmax-2, ymax-2, errBoxStyle, " DEBUG ", errMsg)
}
if infoBox.visible {
infoBox.draw(s)
}
s.Show()
}
func defErr() string {
return ""
}
// fn is expected to be something like list.newNote, list.editNote etc
func openEditorStart(s tcell.Screen, defText string, fn func(string)) {
// Suspend and Resume are needed to stop text editor from bugging out. Took me too long to figure this out.
err := s.Suspend()
if err != nil {
log.Fatalf("%+v", err)
}
newText := openEditor(defText)
fn(newText)
err = s.Resume()
if err != nil {
log.Fatalf("%+v", err)
}
}
// Create a temporary file, write string s to the file.
// Open a text editor which either uses the user's EDITOR environment variable or vi.
// After the file is closed by the user, read it and return its contents.
func openEditor(s string) string {
// I don't know how to hook directly into a text editor, I tried, didn't work, so I won't. Using tempfiles instead.
file, err := ioutil.TempFile(dir, "note*.txt")
if err != nil {
log.Fatal(err)
}
defer file.Close()
_, err = file.Write([]byte(s))
if err != nil {
log.Fatalf("%+v", err)
}
file.Close() // Closing before editing manually seems like a good idea, right?
editor := os.Getenv("EDITOR")
if editor == "" {
editor = "vi"
}
cmd := exec.Command(editor, file.Name())
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err != nil {
log.Fatalf("%+v", err)
}
b, err := os.ReadFile(file.Name())
if err != nil {
log.Fatalf("%+v", err)
}
return string(b)
}
// Initialize tcell.screen
func newScreen() tcell.Screen {
s, err := tcell.NewScreen()
if err != nil {
log.Fatalf("%+v", err)
}
if err := s.Init(); err != nil {
log.Fatalf("%+v", err)
}
return s
}
func drawText(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, text string) {
row := y1
col := x1
for _, r := range []rune(text) {
s.SetContent(col, row, r, nil, style)
col++
if col >= x2 {
row++
col = x1
}
if row > y2 {
break
}
}
}
// Draw a box with a title on the left-side of the top border
func drawBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, title, contents string) {
if y2 < y1 {
y1, y2 = y2, y1
}
if x2 < x1 {
x1, x2 = x2, x1
}
for row := y1; row <= y2; row++ {
for col := x1; col <= x2; col++ {
s.SetContent(col, row, ' ', nil, style)
}
}
for col := x1; col <= x2; col++ {
s.SetContent(col, y1, tcell.RuneHLine, nil, style)
s.SetContent(col, y2, tcell.RuneHLine, nil, style)
}
for row := y1; row <= y2; row++ {
s.SetContent(x1, row, tcell.RuneVLine, nil, style)
s.SetContent(x2, row, tcell.RuneVLine, nil, style)
}
if y1 != y2 && x1 != x2 {
s.SetContent(x1, y1, tcell.RuneULCorner, nil, style)
s.SetContent(x2, y1, tcell.RuneURCorner, nil, style)
s.SetContent(x1, y2, tcell.RuneLLCorner, nil, style)
s.SetContent(x2, y2, tcell.RuneLRCorner, nil, style)
}
textStyle := defStyle
if style == errBoxStyle {
textStyle = errTextStyle
}
titleOffsetX := 2
drawText(s, x1+titleOffsetX, y1, x1+titleOffsetX+len(title), y1, defStyle, title)
drawText(s, x1+1, y1+1, x2-1, y2-1, textStyle, contents)
}
func drawErrBox(s tcell.Screen, x1, y1, x2, y2 int, style tcell.Style, title, text string) {
drawBox(s, x1, y1, x2, y2, style, title, text)
}
func maxListsOnScreen(screenWidth int) int {
listSpace := screenWidth - 2 - listMarginX
return listSpace / (listWidth + listMarginX)
}
func maxNotesOnScreen(screenHeight int) int {
// 4 == window border + list border
noteSpace := screenHeight - 4 - 2*(listMarginY-1) - (noteMargin - 1)
return noteSpace / (noteHeight + (noteMargin - 1))
}
type InfoBox struct {
msg string
visible bool
}
var infoBox = InfoBox{visible: false}
func showInfoBox(dur time.Duration, msg string) {
hideInfoBox := func() {
infoBox.visible = false
drawScreen(s)
}
infoBox.msg = msg
infoBox.visible = true
time.AfterFunc(dur, hideInfoBox)
}
func (i *InfoBox) draw(s tcell.Screen) {
xmax, ymax := s.Size()
midX, midY := (xmax-1)/2, (ymax-1)/2
infoMsg := fmt.Sprintf(" %s ", i.msg)
var infoBoxW, infoBoxH = len(infoMsg) + 2, 3
x1, y1, x2, y2 := midX-(infoBoxW/2), midY-(infoBoxH/2), midX+(infoBoxW/2), midY+(infoBoxH/2)
drawBox(s, x1, y1, x2, y2, errBoxStyle, "", "")
drawText(s, x1+1, y1+1, x2-1, y2-1, errTextStyle, infoMsg)
}