forked from gizak/termui
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathrender.go
More file actions
105 lines (86 loc) · 2.14 KB
/
render.go
File metadata and controls
105 lines (86 loc) · 2.14 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
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
package gotui
import (
"image"
"os"
"github.com/gdamore/tcell/v3"
)
// Render renders the given drawables to the screen.
func Render(items ...Drawable) {
DefaultBackend.Render(items...)
}
func (b *Backend) Render(items ...Drawable) {
if b.Screen == nil || len(items) == 0 {
return
}
minX, minY, maxX, maxY := calculateBounds(items)
// Ensure minimum buffer dimensions to prevent rendering issues
if maxX <= minX || maxY <= minY {
b.Screen.Show()
return
}
buf := NewBuffer(image.Rect(minX, minY, maxX, maxY))
for _, item := range items {
item.Lock()
item.Draw(buf)
item.Unlock()
}
if b.ScreenshotMode {
width, height := 120, 60
if err := SaveImage("screenshot.png", width, height, items...); err != nil {
panic(err)
}
os.Exit(0)
}
b.renderBuffer(buf)
}
func calculateBounds(items []Drawable) (minX, minY, maxX, maxY int) {
minX, minY = items[0].GetRect().Min.X, items[0].GetRect().Min.Y
maxX, maxY = items[0].GetRect().Max.X, items[0].GetRect().Max.Y
for _, item := range items {
r := item.GetRect()
if r.Min.X < minX {
minX = r.Min.X
}
if r.Min.Y < minY {
minY = r.Min.Y
}
if r.Max.X > maxX {
maxX = r.Max.X
}
if r.Max.Y > maxY {
maxY = r.Max.Y
}
}
return
}
func (b *Backend) renderBuffer(buf *Buffer) {
bufWidth := buf.Dx()
if bufWidth <= 0 {
b.Screen.Show()
return
}
// Get screen dimensions for clipping
screenW, screenH := b.Screen.Size()
for i, cell := range buf.Cells {
if cell.Rune == 0 {
continue
}
x := (i % bufWidth) + buf.Min.X
y := (i / bufWidth) + buf.Min.Y
// Skip cells outside visible screen area
if x < 0 || y < 0 || x >= screenW || y >= screenH {
continue
}
style := tcell.StyleDefault.
Foreground(cell.Style.Fg).
Background(cell.Style.Bg).
Bold(cell.Style.Modifier&tcell.AttrBold != 0).
Reverse(cell.Style.Modifier&tcell.AttrReverse != 0).
Dim(cell.Style.Modifier&tcell.AttrDim != 0).
Blink(cell.Style.Modifier&tcell.AttrBlink != 0).
Italic(cell.Style.Modifier&tcell.AttrItalic != 0).
StrikeThrough(cell.Style.Modifier&tcell.AttrStrikeThrough != 0)
b.Screen.SetContent(x, y, cell.Rune, nil, style)
}
b.Screen.Show()
}