Skip to content

Commit 3634696

Browse files
alecthomasclaude
andcommitted
refactor: share TTY attribute escape building
The bold/underline/italic SGR block was duplicated between the indexed and true-colour formatters; attrEscapeSequence now builds it once. Colour rendering stays with each formatter as it legitimately differs. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent caf24e9 commit 3634696

2 files changed

Lines changed: 9 additions & 11 deletions

File tree

formatters/tty_indexed.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ var ttyTables = map[int]*ttyTable{
181181
},
182182
}
183183

184-
func entryToEscapeSequence(table *ttyTable, entry chroma.StyleEntry) string {
184+
// attrEscapeSequence returns the SGR escape codes for entry's bold, underline
185+
// and italic attributes.
186+
func attrEscapeSequence(entry chroma.StyleEntry) string {
185187
out := ""
186188
if entry.Bold == chroma.Yes {
187189
out += "\033[1m"
@@ -192,6 +194,11 @@ func entryToEscapeSequence(table *ttyTable, entry chroma.StyleEntry) string {
192194
if entry.Italic == chroma.Yes {
193195
out += "\033[3m"
194196
}
197+
return out
198+
}
199+
200+
func entryToEscapeSequence(table *ttyTable, entry chroma.StyleEntry) string {
201+
out := attrEscapeSequence(entry)
195202
if entry.Colour.IsSet() {
196203
out += table.foreground[findClosest(table, entry.Colour)]
197204
}

formatters/tty_truecolour.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,7 @@ func trueColourFormatter(w io.Writer, style *chroma.Style, it iter.Seq[chroma.To
5454
continue
5555
}
5656

57-
formatting := ""
58-
if entry.Bold == chroma.Yes {
59-
formatting += "\033[1m"
60-
}
61-
if entry.Underline == chroma.Yes {
62-
formatting += "\033[4m"
63-
}
64-
if entry.Italic == chroma.Yes {
65-
formatting += "\033[3m"
66-
}
57+
formatting := attrEscapeSequence(entry)
6758
if entry.Colour.IsSet() {
6859
formatting += fmt.Sprintf("\033[38;2;%d;%d;%dm", entry.Colour.Red(), entry.Colour.Green(), entry.Colour.Blue())
6960
}

0 commit comments

Comments
 (0)