mirror of
https://github.com/alecthomas/chroma.git
synced 2025-03-19 21:10:15 +02:00
Clear background colour for TTY formatters.
This commit is contained in:
parent
b44683eae8
commit
73d11b3c45
@ -17,6 +17,7 @@ linters:
|
|||||||
- gochecknoglobals
|
- gochecknoglobals
|
||||||
- funlen
|
- funlen
|
||||||
- godox
|
- godox
|
||||||
|
- wsl
|
||||||
|
|
||||||
linters-settings:
|
linters-settings:
|
||||||
govet:
|
govet:
|
||||||
|
@ -2,7 +2,7 @@ sudo: false
|
|||||||
language: go
|
language: go
|
||||||
script:
|
script:
|
||||||
- go test -v ./...
|
- go test -v ./...
|
||||||
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s v1.19.1
|
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s v1.20.0
|
||||||
- ./bin/golangci-lint run
|
- ./bin/golangci-lint run
|
||||||
- git clean -fdx .
|
- git clean -fdx .
|
||||||
after_success:
|
after_success:
|
||||||
|
@ -41,7 +41,7 @@ command, for Go.
|
|||||||
List bool `help:"List lexers, styles and formatters."`
|
List bool `help:"List lexers, styles and formatters."`
|
||||||
Unbuffered bool `help:"Do not buffer output."`
|
Unbuffered bool `help:"Do not buffer output."`
|
||||||
Trace bool `help:"Trace lexer states as they are traversed."`
|
Trace bool `help:"Trace lexer states as they are traversed."`
|
||||||
Check bool `help:"Do not format, check for tokenization errors instead."`
|
Check bool `help:"Do not format, check for tokenisation errors instead."`
|
||||||
Filename string `help:"Filename to use for selecting a lexer when reading from stdin."`
|
Filename string `help:"Filename to use for selecting a lexer when reading from stdin."`
|
||||||
|
|
||||||
Lexer string `help:"Lexer to use when formatting." default:"autodetect" short:"l" enum:"${lexers}"`
|
Lexer string `help:"Lexer to use when formatting." default:"autodetect" short:"l" enum:"${lexers}"`
|
||||||
@ -117,15 +117,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
defer w.Flush() // nolint: errcheck
|
defer w.Flush() // nolint: errcheck
|
||||||
|
|
||||||
if cli.JSON {
|
switch {
|
||||||
|
case cli.JSON:
|
||||||
cli.Formatter = "json"
|
cli.Formatter = "json"
|
||||||
}
|
case cli.HTML:
|
||||||
|
|
||||||
if cli.HTML {
|
|
||||||
cli.Formatter = "html"
|
cli.Formatter = "html"
|
||||||
}
|
case cli.SVG:
|
||||||
|
|
||||||
if cli.SVG {
|
|
||||||
cli.Formatter = "svg"
|
cli.Formatter = "svg"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -149,6 +146,26 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if cli.Formatter == "html" {
|
if cli.Formatter == "html" {
|
||||||
|
configureHTMLFormatter(ctx)
|
||||||
|
}
|
||||||
|
if len(cli.Files) == 0 {
|
||||||
|
contents, err := ioutil.ReadAll(os.Stdin)
|
||||||
|
ctx.FatalIfErrorf(err)
|
||||||
|
format(ctx, w, style, lex(ctx, cli.Filename, string(contents)))
|
||||||
|
} else {
|
||||||
|
for _, filename := range cli.Files {
|
||||||
|
contents, err := ioutil.ReadFile(filename)
|
||||||
|
ctx.FatalIfErrorf(err)
|
||||||
|
if cli.Check {
|
||||||
|
check(filename, lex(ctx, filename, string(contents)))
|
||||||
|
} else {
|
||||||
|
format(ctx, w, style, lex(ctx, filename, string(contents)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func configureHTMLFormatter(ctx *kong.Context) {
|
||||||
options := []html.Option{
|
options := []html.Option{
|
||||||
html.TabWidth(cli.HTMLTabWidth),
|
html.TabWidth(cli.HTMLTabWidth),
|
||||||
html.BaseLineNumber(cli.HTMLBaseLine),
|
html.BaseLineNumber(cli.HTMLBaseLine),
|
||||||
@ -191,22 +208,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
formatters.Register("html", html.New(options...))
|
formatters.Register("html", html.New(options...))
|
||||||
}
|
}
|
||||||
if len(cli.Files) == 0 {
|
|
||||||
contents, err := ioutil.ReadAll(os.Stdin)
|
|
||||||
ctx.FatalIfErrorf(err)
|
|
||||||
format(ctx, w, style, lex(ctx, cli.Filename, string(contents)))
|
|
||||||
} else {
|
|
||||||
for _, filename := range cli.Files {
|
|
||||||
contents, err := ioutil.ReadFile(filename)
|
|
||||||
ctx.FatalIfErrorf(err)
|
|
||||||
if cli.Check {
|
|
||||||
check(filename, lex(ctx, filename, string(contents)))
|
|
||||||
} else {
|
|
||||||
format(ctx, w, style, lex(ctx, filename, string(contents)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func listAll() {
|
func listAll() {
|
||||||
fmt.Println("lexers:")
|
fmt.Println("lexers:")
|
||||||
|
@ -34,7 +34,7 @@ type insertion struct {
|
|||||||
tokens []Token
|
tokens []Token
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *delegatingLexer) Tokenise(options *TokeniseOptions, text string) (Iterator, error) {
|
func (d *delegatingLexer) Tokenise(options *TokeniseOptions, text string) (Iterator, error) { // nolint: gocognit
|
||||||
tokens, err := Tokenise(Coalesce(d.language), options, text)
|
tokens, err := Tokenise(Coalesce(d.language), options, text)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -200,6 +200,7 @@ func findClosest(table *ttyTable, seeking chroma.Colour) chroma.Colour {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func styleToEscapeSequence(table *ttyTable, style *chroma.Style) map[chroma.TokenType]string {
|
func styleToEscapeSequence(table *ttyTable, style *chroma.Style) map[chroma.TokenType]string {
|
||||||
|
style = clearBackground(style)
|
||||||
out := map[chroma.TokenType]string{}
|
out := map[chroma.TokenType]string{}
|
||||||
for _, ttype := range style.Types() {
|
for _, ttype := range style.Types() {
|
||||||
entry := style.Get(ttype)
|
entry := style.Get(ttype)
|
||||||
@ -208,6 +209,17 @@ func styleToEscapeSequence(table *ttyTable, style *chroma.Style) map[chroma.Toke
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Clear the background colour.
|
||||||
|
func clearBackground(style *chroma.Style) *chroma.Style {
|
||||||
|
builder := style.Builder()
|
||||||
|
bg := builder.Get(chroma.Background)
|
||||||
|
bg.Background = 0
|
||||||
|
bg.NoInherit = true
|
||||||
|
builder.AddEntry(chroma.Background, bg)
|
||||||
|
style, _ = builder.Build()
|
||||||
|
return style
|
||||||
|
}
|
||||||
|
|
||||||
type indexedTTYFormatter struct {
|
type indexedTTYFormatter struct {
|
||||||
table *ttyTable
|
table *ttyTable
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import (
|
|||||||
var TTY16m = Register("terminal16m", chroma.FormatterFunc(trueColourFormatter))
|
var TTY16m = Register("terminal16m", chroma.FormatterFunc(trueColourFormatter))
|
||||||
|
|
||||||
func trueColourFormatter(w io.Writer, style *chroma.Style, it chroma.Iterator) error {
|
func trueColourFormatter(w io.Writer, style *chroma.Style, it chroma.Iterator) error {
|
||||||
|
style = clearBackground(style)
|
||||||
for token := it(); token != chroma.EOF; token = it() {
|
for token := it(); token != chroma.EOF; token = it() {
|
||||||
entry := style.Get(token.Type)
|
entry := style.Get(token.Type)
|
||||||
if !entry.IsZero() {
|
if !entry.IsZero() {
|
||||||
|
2
go.mod
2
go.mod
@ -20,3 +20,5 @@ require (
|
|||||||
)
|
)
|
||||||
|
|
||||||
replace github.com/GeertJohan/go.rice => github.com/alecthomas/go.rice v1.0.1-0.20190719113735-961b99d742e7
|
replace github.com/GeertJohan/go.rice => github.com/alecthomas/go.rice v1.0.1-0.20190719113735-961b99d742e7
|
||||||
|
|
||||||
|
go 1.13
|
||||||
|
@ -65,7 +65,7 @@ func httpBodyContentTypeLexer(lexer Lexer) Lexer { return &httpBodyContentTyper{
|
|||||||
|
|
||||||
type httpBodyContentTyper struct{ Lexer }
|
type httpBodyContentTyper struct{ Lexer }
|
||||||
|
|
||||||
func (d *httpBodyContentTyper) Tokenise(options *TokeniseOptions, text string) (Iterator, error) {
|
func (d *httpBodyContentTyper) Tokenise(options *TokeniseOptions, text string) (Iterator, error) { // nolint: gocognit
|
||||||
var contentType string
|
var contentType string
|
||||||
var isContentType bool
|
var isContentType bool
|
||||||
var subIterator Iterator
|
var subIterator Iterator
|
||||||
|
@ -255,7 +255,7 @@ func (l *LexerState) Get(key interface{}) interface{} {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Iterator returns the next Token from the lexer.
|
// Iterator returns the next Token from the lexer.
|
||||||
func (l *LexerState) Iterator() Token {
|
func (l *LexerState) Iterator() Token { // nolint: gocognit
|
||||||
for l.Pos < len(l.Text) && len(l.Stack) > 0 {
|
for l.Pos < len(l.Text) && len(l.Stack) > 0 {
|
||||||
// Exhaust the iterator stack, if any.
|
// Exhaust the iterator stack, if any.
|
||||||
for len(l.iteratorStack) > 0 {
|
for len(l.iteratorStack) > 0 {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user