Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion formatters/tty_indexed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a reasonable change to me, thanks 👍

if !ok {
clr, ok = theme[chroma.Text]
if !ok {
clr = theme[chroma.Background]
}
}
}
}

if clr != "" {
fmt.Fprint(w, clr)
}
Expand Down
23 changes: 23 additions & 0 deletions formatters/tty_indexed_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package formatters

import (
"strings"
"testing"

assert "github.com/alecthomas/assert/v2"
Expand All @@ -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",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😂

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

})
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())
}