MCPcopy Create free account
hub / github.com/B33pBeeps/redthread / Serialize

Method Serialize

internal/app/render.go:98–163  ·  view source on GitHub ↗

--- serialize --------------------------------------------------------- Serialize walks the grid, coalescing adjacent cells that share style. Returns a string containing ANSI SGR + text, suitable for Bubble Tea's View() return value.

()

Source from the content-addressed store, hash-verified

96// Returns a string containing ANSI SGR + text, suitable for Bubble Tea's
97// View() return value.
98func (c *Canvas) Serialize() string {
99 var b strings.Builder
100 b.Grow(c.W * c.H * 2)
101
102 const reset = "\x1b[0m"
103 bg := c.DefaultBg // constant across the canvas
104
105 for y := 0; y < c.H; y++ {
106 curFg := (*RGB)(nil)
107 curAttr := uint8(255) // sentinel != any real attr so first cell forces write
108 styled := false
109 for x := 0; x < c.W; x++ {
110 cell := c.cells[y*c.W+x]
111 r := cell.Rune
112 if r == 0 {
113 r = ' '
114 }
115 // compute target style
116 sameFg := (cell.Fg == nil && curFg == nil) ||
117 (cell.Fg != nil && curFg != nil && *cell.Fg == *curFg)
118 sameAttr := cell.Attr == curAttr
119 if !(sameFg && sameAttr) {
120 // close previous run
121 if styled {
122 b.WriteString(reset)
123 styled = false
124 }
125 // A solid background means even fg-less cells need styling.
126 if cell.Fg != nil || cell.Attr != 0 || bg != nil {
127 if cell.Attr&AttrBold != 0 {
128 b.WriteString("\x1b[1m")
129 }
130 if cell.Attr&AttrFaint != 0 {
131 b.WriteString("\x1b[2m")
132 }
133 if cell.Attr&AttrItalic != 0 {
134 b.WriteString("\x1b[3m")
135 }
136 if cell.Attr&AttrUnder != 0 {
137 b.WriteString("\x1b[4m")
138 }
139 if cell.Attr&AttrStrike != 0 {
140 b.WriteString("\x1b[9m")
141 }
142 if bg != nil {
143 b.WriteString(bg.BgSGR())
144 }
145 if cell.Fg != nil {
146 b.WriteString(cell.Fg.SGR())
147 }
148 styled = true
149 }
150 curFg = cell.Fg
151 curAttr = cell.Attr
152 }
153 b.WriteRune(r)
154 }
155 if styled {

Callers 4

ViewMethod · 0.95
ViewMethod · 0.80

Calls 2

BgSGRMethod · 0.80
SGRMethod · 0.80

Tested by 1