1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-03-17 20:58:08 +02:00

Make tty_indexed.go respond to None like tty_truecolour.go (#869)

This commit is contained in:
Johan Walles 2023-10-15 16:16:10 +02:00 committed by GitHub
parent 9ae4daed01
commit b127e3569c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 1 deletions

View File

@ -242,12 +242,21 @@ func (c *indexedTTYFormatter) Format(w io.Writer, style *chroma.Style, it chroma
theme := styleToEscapeSequence(c.table, style)
for token := it(); token != chroma.EOF; token = it() {
clr, ok := theme[token.Type]
// This search mimics how styles.Get() is used in tty_truecolour.go.
if !ok {
clr, ok = theme[token.Type.SubCategory()]
if !ok {
clr = theme[token.Type.Category()]
clr, ok = theme[token.Type.Category()]
if !ok {
clr, ok = theme[chroma.Text]
if !ok {
clr = theme[chroma.Background]
}
}
}
}
if clr != "" {
fmt.Fprint(w, clr)
}

View File

@ -1,6 +1,7 @@
package formatters
import (
"strings"
"testing"
assert "github.com/alecthomas/assert/v2"
@ -11,3 +12,25 @@ func TestClosestColour(t *testing.T) {
actual := findClosest(ttyTables[256], chroma.MustParseColour("#e06c75"))
assert.Equal(t, chroma.MustParseColour("#d75f87"), actual)
}
func TestNoneColour(t *testing.T) {
formatter := TTY256
tokenType := chroma.None
style, err := chroma.NewStyle("test", chroma.StyleEntries{
chroma.Background: "#D0ab1e",
})
assert.NoError(t, err)
stringBuilder := strings.Builder{}
err = formatter.Format(&stringBuilder, style, chroma.Literator(chroma.Token{
Type: tokenType,
Value: "WORD",
}))
assert.NoError(t, err)
// "178" = #d7af00 approximates #d0ab1e
//
// 178 color ref: https://jonasjacek.github.io/colors/
assert.Equal(t, "\033[38;5;178mWORD\033[0m", stringBuilder.String())
}