-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
580 lines (553 loc) · 15.3 KB
/
main.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
package main
import (
"bufio"
"flag"
"fmt"
"net"
"os"
"strconv"
"strings"
"github.com/gdamore/tcell/v2"
)
var (
block rune = '█'
colors []string = []string{
"black",
"maroon",
"green",
"olive",
"navy",
"purple",
"teal",
"silver",
"grey",
"red",
"lime",
"yellow",
"blue",
"fuchsia",
"aqua",
"white",
}
tools = map[string]int{
"Pencil": 0,
"Region": 8,
"Border": 16,
"Text": 24,
}
actions = map[string]int{
"Save": 0,
"Load": 6,
"Clear": 12,
"Exit": 19,
}
selectedColor string = "white"
selectedTool string = "Pencil"
hostServer bool
connectAddress string
port int
canvasFile string
connections []net.Conn
)
func setContent(screen tcell.Screen, x, y int, letter rune, style tcell.Style, send bool) {
screen.SetContent(x, y, letter, nil, style)
if len(connections) > 0 && y >= 4 && send {
for _, connection := range connections {
foregroundColorName, backgroundColorName := getColor(style)
if foregroundColorName == "" && backgroundColorName == "" {
foregroundColorName = "reset"
backgroundColorName = "reset"
}
go fmt.Fprintf(connection, fmt.Sprintf("set:%v,%v,%v,%v,%v\n", x, y, foregroundColorName, backgroundColorName, string(letter)))
}
}
}
func drawRegion(
screen tcell.Screen,
x1, y1, x2, y2 int,
style tcell.Style,
borderStyle tcell.Style,
letter rune,
drawBorders bool,
send bool,
) {
if y2 < y1 {
y1, y2 = y2, y1
}
if x2 < x1 {
x1, x2 = x2, x1
}
if drawBorders {
for col := x1; col <= x2; col++ {
setContent(screen, col, y1, tcell.RuneHLine, borderStyle, false)
setContent(screen, col, y2, tcell.RuneHLine, borderStyle, false)
}
for row := y1 + 1; row < y2; row++ {
setContent(screen, x1, row, tcell.RuneVLine, borderStyle, false)
setContent(screen, x2, row, tcell.RuneVLine, borderStyle, false)
}
if y1 != y2 && x1 != x2 {
setContent(screen, x1, y1, tcell.RuneULCorner, borderStyle, false)
setContent(screen, x2, y1, tcell.RuneURCorner, borderStyle, false)
setContent(screen, x1, y2, tcell.RuneLLCorner, borderStyle, false)
setContent(screen, x2, y2, tcell.RuneLRCorner, borderStyle, false)
}
}
for row := y1 + 1; row < y2; row++ {
for col := x1 + 1; col < x2; col++ {
setContent(screen, col, row, letter, style, false)
}
}
if len(connections) > 0 && y1 >= 4 && send {
for _, connection := range connections {
foregroundColorName, backgroundColorName := getColor(style)
if foregroundColorName == "" && backgroundColorName == "" {
foregroundColorName = "reset"
backgroundColorName = "reset"
}
borderForegroundColorName, borderBackgroundColorName := getColor(borderStyle)
if borderForegroundColorName == "" && borderBackgroundColorName == "" {
borderForegroundColorName = "reset"
borderBackgroundColorName = "reset"
}
go fmt.Fprintf(connection, fmt.Sprintf(
"region:%v,%v,%v,%v,%v,%v,%v,%v,%v,%v\n",
x1,
y1,
x2,
y2,
foregroundColorName,
backgroundColorName,
borderForegroundColorName,
borderBackgroundColorName,
string(letter),
drawBorders,
))
}
}
}
func clearRegion(screen tcell.Screen, x1, y1, x2, y2 int, send bool) {
if y2 < y1 {
y1, y2 = y2, y1
}
if x2 < x1 {
x1, x2 = x2, x1
}
defaultStyle := tcell.StyleDefault.
Background(tcell.ColorReset).
Foreground(tcell.ColorReset)
for row := y1; row <= y2; row++ {
for col := x1; col <= x2; col++ {
setContent(screen, col, row, ' ', defaultStyle, false)
}
}
if len(connections) > 0 && y1 >= 4 && send {
for _, connection := range connections {
go fmt.Fprintf(connection, fmt.Sprintf(
"clearRegion:%v,%v,%v,%v\n",
x1,
y1,
x2,
y2,
))
}
}
}
func main() {
flag.BoolVar(&hostServer, "host", false, "Host a termcanvas server")
flag.StringVar(&connectAddress, "connect", "", "Connect to a termcanvas server")
flag.IntVar(&port, "port", 55055, "The port to host on or connect to")
flag.StringVar(&canvasFile, "canvas", "", "The canvas file to load")
flag.Parse()
screen, err := tcell.NewScreen()
if err != nil {
fmt.Printf("Unable to create screen: %v\n", err.Error())
os.Exit(1)
}
if err := screen.Init(); err != nil {
fmt.Printf("Unable to create screen: %v\n", err.Error())
os.Exit(1)
}
defaultStyle := tcell.StyleDefault.
Background(tcell.ColorReset).
Foreground(tcell.ColorReset)
screen.SetStyle(defaultStyle)
screen.EnableMouse()
screen.EnablePaste()
screen.Clear()
var pressed, erase bool
var startX, startY, lastX, lastY int
var textX, textY int = 0, 4
if hostServer && connectAddress != "" {
screen.Fini()
fmt.Println("You cannot host a server and connect to a server at the same time!")
os.Exit(1)
}
if canvasFile != "" && connectAddress != "" {
screen.Fini()
fmt.Println("You cannot load a canvas and connect to a server at the same time!")
os.Exit(1)
}
if hostServer {
listener, err := net.Listen("tcp", ":"+strconv.Itoa(port))
if err != nil {
screen.Fini()
fmt.Printf("Unable to listen for connections: %v\n", err.Error())
os.Exit(1)
}
go handleConnections(listener, screen)
}
if connectAddress != "" {
connection, err := net.Dial("tcp", connectAddress+":"+strconv.Itoa(port))
if err != nil {
screen.Fini()
fmt.Printf("Unable to connect to server: %v\n", err.Error())
os.Exit(1)
}
go handleConnection(connection, screen)
}
if canvasFile != "" {
fileData, err := os.ReadFile(canvasFile)
if err != nil {
screen.Fini()
fmt.Printf("Unable to load %v: %v\n", canvasFile, err.Error())
os.Exit(1)
} else {
drawData(string(fileData), screen)
}
}
colorsLength := len(colors)
toolsLength := 0
for tool := range tools {
toolsLength += len(tool) + 2
}
actionsLength := 0
for action := range actions {
actionsLength += len(action) + 2
}
colorsOffset := 7
toolsOffset := colorsOffset + colorsLength + 2
actionsOffset := toolsOffset + toolsLength + 2
remainingOffset := actionsOffset + actionsLength + 2
for {
width, height := screen.Size()
drawRegion(screen, 0, 0, width, 3, defaultStyle, defaultStyle, ' ', false, false)
drawRegion(screen, 0, 0, 5, 3, tcell.StyleDefault.Foreground(tcell.GetColor(selectedColor)), defaultStyle, block, true, false)
drawRegion(screen, colorsOffset-1, 0, colorsLength+colorsOffset, 3, defaultStyle, defaultStyle, ' ', true, false)
for index, color := range colors {
drawRegion(screen,
index+(colorsOffset-1),
0,
index+(colorsOffset+1),
3,
tcell.StyleDefault.Foreground(tcell.GetColor(color)),
defaultStyle,
block,
false,
false,
)
}
drawRegion(screen, toolsOffset-1, 0, toolsLength+toolsOffset-2, 3, defaultStyle, defaultStyle, ' ', true, false)
for tool, offset := range tools {
for letterOffset, letter := range tool {
drawRegion(
screen,
toolsOffset+letterOffset+offset-1,
0,
toolsOffset+letterOffset+offset+1,
2,
tcell.StyleDefault.Foreground(tcell.ColorWhite),
defaultStyle,
letter,
false,
false,
)
}
}
selectedToolOffset := 0
for tool, offset := range tools {
if selectedTool == tool {
selectedToolOffset = offset
break
}
}
for i := 0; i < len(selectedTool); i++ {
setContent(
screen,
selectedToolOffset+toolsOffset+i,
2,
'^',
tcell.StyleDefault.Foreground(tcell.ColorWhite),
false,
)
}
drawRegion(screen, actionsOffset-3, 0, actionsLength+actionsOffset-4, 3, defaultStyle, defaultStyle, ' ', true, false)
for action, offset := range actions {
for letterOffset, letter := range action {
setContent(
screen,
actionsOffset-2+letterOffset+offset,
1,
letter,
tcell.StyleDefault.Foreground(tcell.ColorWhite),
false,
)
}
}
if len(connections) > 0 {
for letterOffset, letter := range "Connected to:" {
setContent(
screen,
remainingOffset-2+letterOffset-1,
1,
letter,
tcell.StyleDefault.Foreground(tcell.ColorWhite),
false,
)
}
addresses := ""
for _, connection := range connections {
addresses += connection.RemoteAddr().String() + ", "
}
for letterOffset, letter := range strings.Trim(addresses, ", ") {
setContent(
screen,
remainingOffset-2+letterOffset-1,
2,
letter,
tcell.StyleDefault.Foreground(tcell.ColorWhite),
false,
)
}
}
screen.Show()
event := screen.PollEvent()
switch event := event.(type) {
case *tcell.EventKey:
if event.Key() == tcell.KeyEscape {
exit(screen)
}
if selectedTool == "Text" {
if textX >= width || textX <= 0 {
textX = 0
}
if textY >= height || textY <= 0 {
textY = 4
}
if event.Key() == tcell.KeyEnter {
textX = 0
textY++
} else if event.Key() == tcell.KeyBackspace || event.Key() == tcell.KeyBackspace2 {
textX--
_, _, style, _ := screen.GetContent(textX, textY)
_, backgroundColor, _ := style.Decompose()
textColor := tcell.StyleDefault.
Foreground(backgroundColor).
Background(backgroundColor)
setContent(screen, textX, textY, ' ', textColor, true)
} else {
_, _, style, _ := screen.GetContent(textX, textY)
originalForegroundColor, originalBackgroundColor, _ := style.Decompose()
foregroundColor, backgroundColor := tcell.GetColor(selectedColor), originalBackgroundColor
if backgroundColor == 0 {
backgroundColor = originalForegroundColor
}
textColor := tcell.StyleDefault.
Foreground(foregroundColor).
Background(backgroundColor)
setContent(screen, textX, textY, event.Rune(), textColor, true)
textX++
}
}
case *tcell.EventResize:
screen.Sync()
case *tcell.EventMouse:
x, y := event.Position()
button := event.Buttons()
if button == 1 {
if y <= 3 {
if x < colorsLength+colorsOffset && x-colorsOffset >= 0 {
selectedColor = colors[x-colorsOffset]
} else if x-toolsOffset < toolsLength-2 && x >= colorsLength+colorsOffset+2 {
for tool, offset := range tools {
if x-toolsOffset >= offset && x-toolsOffset <= (offset+len(tool)+1) {
selectedTool = tool
if selectedTool == "Text" {
textX, textY = 0, 4
}
}
}
} else if x-actionsOffset < actionsLength-4 && x >= toolsLength {
for action, offset := range actions {
if x-actionsOffset+2 >= offset && x-actionsOffset+2 <= (offset+len(action)+1) {
if action == "Exit" {
exit(screen)
} else if action == "Clear" {
screen.Clear()
for _, connection := range connections {
go fmt.Fprintf(connection, "clear\n")
}
} else if action == "Save" {
data, _ := dumpData(screen)
screen.Suspend()
reader := bufio.NewScanner(os.Stdin)
fmt.Print("(Save) File Path: ")
reader.Scan()
filePath := reader.Text()
if strings.TrimSpace(filePath) == "" {
screen.Resume()
drawData(string(data), screen)
screen.PostEvent(tcell.NewEventResize(width, height))
break
}
err := os.WriteFile(filePath, []byte(data), 0644)
if err != nil {
fmt.Printf("Unable to write to file: %v\n", err.Error())
} else {
fmt.Printf("Successfully saved to %v!\n", filePath)
}
fmt.Print("Press Enter to continue...")
reader.Scan()
screen.Resume()
drawData(string(data), screen)
screen.PostEvent(tcell.NewEventResize(width, height))
} else if action == "Load" {
data, _ := dumpData(screen)
screen.Suspend()
reader := bufio.NewScanner(os.Stdin)
fmt.Print("(Load) File Path: ")
reader.Scan()
filePath := reader.Text()
if strings.TrimSpace(filePath) == "" {
screen.Resume()
drawData(string(data), screen)
screen.PostEvent(tcell.NewEventResize(width, height))
break
}
fileData, err := os.ReadFile(filePath)
if err != nil {
fmt.Printf("Unable to load %v: %v\n", filePath, err.Error())
fmt.Print("Press Enter to continue...")
reader.Scan()
screen.Resume()
drawData(string(data), screen)
screen.PostEvent(tcell.NewEventResize(width, height))
} else {
screen.Resume()
drawData(string(fileData), screen)
screen.PostEvent(tcell.NewEventResize(width, height))
}
}
}
}
}
} else {
if selectedTool == "Pencil" {
setContent(screen, x, y, block, tcell.StyleDefault.Foreground(tcell.GetColor(selectedColor)), true)
} else if selectedTool == "Region" {
if !pressed {
pressed = true
startX = x
startY = y
}
if lastX+lastY != 0 {
drawRegion(screen, startX, startY, lastX, lastY, defaultStyle, defaultStyle, ' ', false, true)
}
lastX = x
lastY = y
drawRegion(screen, startX, startY, x, y, tcell.StyleDefault.Foreground(tcell.GetColor(selectedColor)), defaultStyle, block, false, true)
} else if selectedTool == "Border" {
if !pressed {
pressed = true
startX = x
startY = y
}
if lastX+lastY != 0 {
clearRegion(screen, startX, startY, lastX, lastY, true)
}
lastX = x
lastY = y
drawRegion(screen, startX, startY, x, y, defaultStyle, tcell.StyleDefault.Foreground(tcell.GetColor(selectedColor)), ' ', true, true)
} else if selectedTool == "Text" {
textX, textY = x, y
}
}
} else if button == 2 {
if selectedTool == "Pencil" {
setContent(screen, x, y, ' ', defaultStyle, true)
} else if selectedTool == "Region" {
if !pressed {
pressed = true
erase = true
startX = x
startY = y
}
drawRegion(screen, startX, startY, x, y, defaultStyle, defaultStyle, ' ', false, true)
} else if selectedTool == "Border" {
if !pressed {
pressed = true
erase = true
startX = x
startY = y
}
drawRegion(screen, startX, startY, x, y, defaultStyle, defaultStyle, ' ', false, true)
}
} else if button == 0 {
if pressed {
pressed = false
lastX, lastY = 0, 0
if !erase {
if selectedTool == "Region" {
drawRegion(screen, startX, startY, x, y, tcell.StyleDefault.Foreground(tcell.GetColor(selectedColor)), defaultStyle, block, false, true)
} else if selectedTool == "Border" {
drawRegion(screen, startX, startY, x, y, defaultStyle, tcell.StyleDefault.Foreground(tcell.GetColor(selectedColor)), ' ', true, true)
}
}
}
}
}
}
}
func exit(screen tcell.Screen) {
for _, connection := range connections {
fmt.Fprintf(connection, "exit\n")
connection.Close()
}
data, empty := dumpData(screen)
screen.Fini()
if empty {
os.Exit(0)
}
reader := bufio.NewScanner(os.Stdin)
save := ""
for {
if save == "y" || save == "n" {
break
}
fmt.Print("Would you like to save your drawing? [Y]es/[N]o: ")
reader.Scan()
save = reader.Text()
if len(save) > 0 {
save = strings.ToLower(string(save[0]))
}
}
if save == "y" {
var saved bool
for !saved {
fmt.Print("(Save) File Path: ")
reader.Scan()
filePath := reader.Text()
if strings.TrimSpace(filePath) == "" {
continue
}
err := os.WriteFile(filePath, []byte(data), 0644)
if err != nil {
fmt.Printf("Unable to write to file: %v\n", err.Error())
} else {
fmt.Printf("Successfully saved to %v!\n", filePath)
saved = true
}
}
}
os.Exit(0)
}