-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender.go
More file actions
75 lines (66 loc) · 1.6 KB
/
Copy pathrender.go
File metadata and controls
75 lines (66 loc) · 1.6 KB
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
package main
import (
"bytes"
"fmt"
)
// ANSI escape sequences
const (
esc = "\x1b["
// Alternate screen buffer
AltScreenOn = "\x1b[?1049h"
AltScreenOff = "\x1b[?1049l"
// Cursor visibility
HideCursor = esc + "?25l"
ShowCursor = esc + "?25h"
// Reset attributes
Reset = esc + "0m"
)
// MoveCursor writes the escape sequence to move cursor to row, col (1-based).
func MoveCursor(buf *bytes.Buffer, row, col int) {
fmt.Fprintf(buf, "%s%d;%dH", esc, row, col)
}
// BrightnessStyle returns the ANSI style string for a given brightness level (0–5).
func BrightnessStyle(brightness int) string {
switch brightness {
case 5: // head — bold bright white
return esc + "1;97m"
case 4: // bold bright green
return esc + "1;92m"
case 3: // normal green
return esc + "0;32m"
case 2: // dim green
return esc + "2;32m"
case 1: // dim gray
return esc + "2;90m"
default: // 0 — empty
return ""
}
}
// RenderFrame writes the full screen contents into buf using ANSI escape codes.
// It tracks the previous style to avoid redundant style changes.
func RenderFrame(buf *bytes.Buffer, screen *Screen) {
var lastStyle string
for r := 0; r < screen.Rows; r++ {
MoveCursor(buf, r+1, 1)
for c := 0; c < screen.Cols; c++ {
cell := &screen.Grid[r][c]
if cell.Brightness == 0 {
if lastStyle != "" {
buf.WriteString(Reset)
lastStyle = ""
}
buf.WriteByte(' ')
continue
}
style := BrightnessStyle(cell.Brightness)
if style != lastStyle {
buf.WriteString(style)
lastStyle = style
}
buf.WriteRune(cell.Char)
}
}
if lastStyle != "" {
buf.WriteString(Reset)
}
}