Skip to content

Commit b127e35

Browse files
authoredOct 15, 2023
Make tty_indexed.go respond to None like tty_truecolour.go (#869)
1 parent 9ae4dae commit b127e35

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed
 

‎formatters/tty_indexed.go

+10-1
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,21 @@ func (c *indexedTTYFormatter) Format(w io.Writer, style *chroma.Style, it chroma
242242
theme := styleToEscapeSequence(c.table, style)
243243
for token := it(); token != chroma.EOF; token = it() {
244244
clr, ok := theme[token.Type]
245+
246+
// This search mimics how styles.Get() is used in tty_truecolour.go.
245247
if !ok {
246248
clr, ok = theme[token.Type.SubCategory()]
247249
if !ok {
248-
clr = theme[token.Type.Category()]
250+
clr, ok = theme[token.Type.Category()]
251+
if !ok {
252+
clr, ok = theme[chroma.Text]
253+
if !ok {
254+
clr = theme[chroma.Background]
255+
}
256+
}
249257
}
250258
}
259+
251260
if clr != "" {
252261
fmt.Fprint(w, clr)
253262
}

‎formatters/tty_indexed_test.go

+23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package formatters
22

33
import (
4+
"strings"
45
"testing"
56

67
assert "github.com/alecthomas/assert/v2"
@@ -11,3 +12,25 @@ func TestClosestColour(t *testing.T) {
1112
actual := findClosest(ttyTables[256], chroma.MustParseColour("#e06c75"))
1213
assert.Equal(t, chroma.MustParseColour("#d75f87"), actual)
1314
}
15+
16+
func TestNoneColour(t *testing.T) {
17+
formatter := TTY256
18+
tokenType := chroma.None
19+
20+
style, err := chroma.NewStyle("test", chroma.StyleEntries{
21+
chroma.Background: "#D0ab1e",
22+
})
23+
assert.NoError(t, err)
24+
25+
stringBuilder := strings.Builder{}
26+
err = formatter.Format(&stringBuilder, style, chroma.Literator(chroma.Token{
27+
Type: tokenType,
28+
Value: "WORD",
29+
}))
30+
assert.NoError(t, err)
31+
32+
// "178" = #d7af00 approximates #d0ab1e
33+
//
34+
// 178 color ref: https://jonasjacek.github.io/colors/
35+
assert.Equal(t, "\033[38;5;178mWORD\033[0m", stringBuilder.String())
36+
}

0 commit comments

Comments
 (0)
Please sign in to comment.