Skip to content

Commit 74fe5d1

Browse files
committed
refactor(svg): reduce cognitive complexity
1 parent bf889fa commit 74fe5d1

File tree

5 files changed

+74
-56
lines changed

5 files changed

+74
-56
lines changed

internal/svg/svg.go

Lines changed: 67 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -175,63 +175,80 @@ func (c *Canvas) createFrames() {
175175
}
176176

177177
c.Gtransform(fmt.Sprintf("translate(%d)", c.paddedWidth()*i))
178+
c.renderRows(term)
179+
c.Gend()
180+
}
181+
}
178182

179-
for row := 0; row < c.Header.Height; row++ {
180-
frame := ""
181-
lastColor := term.Cell(0, row).FG
182-
lastColummn := 0
183-
lastBold := isBold(term.Cell(0, row))
184-
lastItalic := isItalic(term.Cell(0, row))
185-
lastUnderline := isUnderline(term.Cell(0, row))
186-
lastDim := isDim(term.Cell(0, row))
183+
func (c *Canvas) renderRows(term vt10x.Terminal) {
184+
for row := 0; row < c.Header.Height; row++ {
185+
c.renderRow(term, row)
186+
}
187+
}
187188

188-
for col := 0; col < c.Header.Width; col++ {
189-
cell := term.Cell(col, row)
190-
c.addBG(cell.BG)
191-
cellIsBold := isBold(cell)
192-
cellIsItalic := isItalic(cell)
193-
cellIsUnderline := isUnderline(cell)
194-
cellIsDim := isDim(cell)
195-
196-
if cell.Char == ' ' || cell.FG != lastColor || cellIsBold != lastBold || cellIsItalic != lastItalic || cellIsUnderline != lastUnderline || cellIsDim != lastDim {
197-
if frame != "" {
198-
class := c.colors[color.GetColor(lastColor)]
199-
if cellIsBold {
200-
class += " bold"
201-
}
202-
if cellIsItalic {
203-
class += " italic"
204-
}
205-
if cellIsUnderline {
206-
class += " underline"
207-
}
208-
if cellIsDim {
209-
class += " dim"
210-
}
211-
c.Text(lastColummn*colWidth,
212-
row*rowHeight, frame, fmt.Sprintf(`class="%s"`, class), c.applyBG(cell.BG))
213-
214-
frame = ""
215-
}
216-
217-
if cell.Char == ' ' {
218-
lastColummn = col + 1
219-
continue
220-
}
221-
lastColor = cell.FG
222-
lastColummn = col
223-
224-
}
225-
226-
frame += string(cell.Char)
189+
func (c *Canvas) renderRow(term vt10x.Terminal, row int) {
190+
frame := ""
191+
lastColor := term.Cell(0, row).FG
192+
lastColummn := 0
193+
lastBold := isBold(term.Cell(0, row))
194+
lastItalic := isItalic(term.Cell(0, row))
195+
lastUnderline := isUnderline(term.Cell(0, row))
196+
lastDim := isDim(term.Cell(0, row))
197+
198+
for col := 0; col < c.Header.Width; col++ {
199+
cell := term.Cell(col, row)
200+
c.addBG(cell.BG)
201+
202+
if c.cellAttributesChanged(cell, lastColor, lastBold, lastItalic, lastUnderline, lastDim) {
203+
if frame != "" {
204+
class := c.buildClassString(lastColor, lastBold, lastItalic, lastUnderline, lastDim)
205+
c.Text(lastColummn*colWidth, row*rowHeight, frame, fmt.Sprintf(`class="%s"`, class), c.applyBG(cell.BG))
206+
frame = ""
227207
}
228208

229-
if strings.TrimSpace(frame) != "" {
230-
c.Text(lastColummn*colWidth, row*rowHeight, frame, fmt.Sprintf(`class="%s"`, c.colors[color.GetColor(lastColor)]))
209+
if cell.Char == ' ' {
210+
lastColummn = col + 1
211+
continue
231212
}
213+
lastColor = cell.FG
214+
lastColummn = col
215+
lastBold = isBold(cell)
216+
lastItalic = isItalic(cell)
217+
lastUnderline = isUnderline(cell)
218+
lastDim = isDim(cell)
232219
}
233-
c.Gend()
220+
221+
frame += string(cell.Char)
222+
}
223+
224+
if strings.TrimSpace(frame) != "" {
225+
c.Text(lastColummn*colWidth, row*rowHeight, frame, fmt.Sprintf(`class="%s"`, c.colors[color.GetColor(lastColor)]))
226+
}
227+
}
228+
229+
func (c *Canvas) cellAttributesChanged(
230+
cell vt10x.Glyph, lastColor vt10x.Color, lastBold, lastItalic, lastUnderline, lastDim bool,
231+
) bool {
232+
return cell.Char == ' ' || cell.FG != lastColor ||
233+
isBold(cell) != lastBold || isItalic(cell) != lastItalic ||
234+
isUnderline(cell) != lastUnderline || isDim(cell) != lastDim
235+
}
236+
237+
func (c *Canvas) buildClassString(fgColor vt10x.Color, bold, italic, underline, dim bool) string {
238+
class := c.colors[color.GetColor(fgColor)]
239+
if bold {
240+
class += " bold"
241+
}
242+
if italic {
243+
class += " italic"
244+
}
245+
if underline {
246+
class += " underline"
247+
}
248+
if dim {
249+
class += " dim"
234250
}
251+
return class
235252
}
236253

237254
func (c *Canvas) addBG(bg vt10x.Color) {

internal/svg/testdata/TestExportOutput.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88
<circle cx="43" cy="20" r="7" style="fill:#ffbd2e" />
99
<circle cx="66" cy="20" r="7" style="fill:#18c132" />
1010
<g transform="translate(20,60)" >
11-
<g style="animation-duration:3.35s;animation-iteration-count:infinite;animation-name:k;animation-timing-function:steps(1,end);font-family:Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;font-size:20px">
11+
<g style="animation-duration:3.35s;animation-iteration-count:infinite;animation-name:k;animation-timing-function:steps(1,end);font-family:Monago,Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;font-size:20px">
1212
<style type="text/css">
1313
<![CDATA[
14-
@keyframes k {79.807%{transform:translateX(-0px)}82.298%{transform:translateX(-2596px)}87.777%{transform:translateX(-5192px)}92.767%{transform:translateX(-7788px)}100.000%{transform:translateX(-10384px)}}.a{fill:#e5e5e5}
14+
@keyframes k {79.807%{transform:translateX(-0px)}82.298%{transform:translateX(-2596px)}87.777%{transform:translateX(-5192px)}92.767%{transform:translateX(-7788px)}100.000%{transform:translateX(-10384px)}}.bold{font-weight:bold}.italic{font-style:italic}.underline{text-decoration:underline}.dim{opacity:0.5}.a{fill:#e5e5e5}
1515
]]>
1616
</style>
1717
<g transform="translate(0)">

internal/svg/testdata/TestExportOutputNoWindow.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
xmlns:xlink="http://www.w3.org/1999/xlink">
66
<rect x="0" y="0" width="2596" height="1510" style="fill:#282d35" />
77
<g transform="translate(20,30)" >
8-
<g style="animation-duration:3.35s;animation-iteration-count:infinite;animation-name:k;animation-timing-function:steps(1,end);font-family:Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;font-size:20px">
8+
<g style="animation-duration:3.35s;animation-iteration-count:infinite;animation-name:k;animation-timing-function:steps(1,end);font-family:Monago,Monaco,Consolas,Menlo,'Bitstream Vera Sans Mono','Powerline Symbols',monospace;font-size:20px">
99
<style type="text/css">
1010
<![CDATA[
11-
@keyframes k {79.807%{transform:translateX(-0px)}82.298%{transform:translateX(-2596px)}87.777%{transform:translateX(-5192px)}92.767%{transform:translateX(-7788px)}100.000%{transform:translateX(-10384px)}}.a{fill:#e5e5e5}
11+
@keyframes k {79.807%{transform:translateX(-0px)}82.298%{transform:translateX(-2596px)}87.777%{transform:translateX(-5192px)}92.767%{transform:translateX(-7788px)}100.000%{transform:translateX(-10384px)}}.bold{font-weight:bold}.italic{font-style:italic}.underline{text-decoration:underline}.dim{opacity:0.5}.a{fill:#e5e5e5}
1212
]]>
1313
</style>
1414
<g transform="translate(0)">

pkg/asciicast/asciicast_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestWriteRecords(t *testing.T) {
4646
}
4747

4848
want := goldie.New(t)
49-
want.AssertWithTemplate(t, "TestMarshal", record.Header, got)
49+
want.AssertWithTemplate(t, "TestMarshal", record, got)
5050
}
5151

5252
func TestToRelativeTime(t *testing.T) {
@@ -111,6 +111,7 @@ func setup(t *testing.T) *asciicast.Cast {
111111
t.Setenv("SHELL", "TEST_SHELL")
112112

113113
cast := asciicast.New()
114+
cast.Header.Timestamp = 1337
114115

115116
cast.Events = append(cast.Events,
116117
asciicast.Event{Time: 1, EventType: asciicast.Output, EventData: "First"},
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{"version":2,"width":0,"height":0,"timestamp":{{.Timestamp}},"env":{"SHELL":"TEST_SHELL","TERM":"TEST_TERM"}}
1+
{"version":2,"width":0,"height":0,"timestamp":1337,"env":{"SHELL":"TEST_SHELL","TERM":"TEST_TERM"}}
22
[1,"o","First"]
33
[2,"o","Second"]
44
[3,"i","Third"]

0 commit comments

Comments
 (0)