Skip to content

Commit e87e5b2

Browse files
scopalecthomas
authored andcommitted
cmd: add optional silent fail mode on no specific lexer
Makes use of chroma easier in/as less(1) preprocessors. https://manpages.debian.org/less#INPUT_PREPROCESSOR export LESSOPEN='| p() { chroma --fail "$1" || cat "$1"; }; p "%s"' https://manpages.debian.org/lesspipe#USER_DEFINED_FILTERS ln -s ~/go/bin/chroma ~/.lessfilter
1 parent 2e23e7f commit e87e5b2

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

Diff for: README.md

+18
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,24 @@ A command-line interface to Chroma is included. It can be installed with:
255255
go get -u github.com/alecthomas/chroma/cmd/chroma
256256
```
257257
258+
The CLI can be used as a preprocessor to colorise output of `less(1)`,
259+
see documentation for the `LESSOPEN` environment variable.
260+
261+
The `--fail` flag can be used to suppress output and return with exit status
262+
1 to facilitate falling back to some other preprocessor in case chroma
263+
does not resolve a specific lexer to use for the given file. For example:
264+
265+
```shell
266+
export LESSOPEN='| p() { chroma --fail "$1" || cat "$1"; }; p "%s"'
267+
```
268+
269+
Replace `cat` with your favourite fallback preprocessor.
270+
271+
When invoked as `.lessfilter`, the `--fail` flag is automatically turned
272+
on under the hood for easy integration with [lesspipe shipping with
273+
Debian and derivatives](https://manpages.debian.org/lesspipe#USER_DEFINED_FILTERS);
274+
for that setup the `chroma` executable can be just symlinked to `~/.lessfilter`.
275+
258276
<a id="markdown-whats-missing-compared-to-pygments" name="whats-missing-compared-to-pygments"></a>
259277
## What's missing compared to Pygments?
260278

Diff for: cmd/chroma/main.go

+9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io/ioutil"
88
"os"
99
"os/signal"
10+
"path"
1011
"runtime"
1112
"runtime/pprof"
1213
"sort"
@@ -43,6 +44,7 @@ command, for Go.
4344
Trace bool `help:"Trace lexer states as they are traversed."`
4445
Check bool `help:"Do not format, check for tokenisation errors instead."`
4546
Filename string `help:"Filename to use for selecting a lexer when reading from stdin."`
47+
Fail bool `help:"Exit silently with status 1 if no specific lexer was found."`
4648

4749
Lexer string `help:"Lexer to use when formatting." default:"autodetect" short:"l" enum:"${lexers}"`
4850
Style string `help:"Style to use for formatting." default:"swapoff" short:"s" enum:"${styles}"`
@@ -105,6 +107,10 @@ func main() {
105107
}()
106108
defer pprof.StopCPUProfile()
107109
}
110+
if path.Base(os.Args[0]) == ".lessfilter" {
111+
// https://manpages.debian.org/lesspipe#USER_DEFINED_FILTERS
112+
cli.Fail = true
113+
}
108114

109115
var out io.Writer = os.Stdout
110116
if runtime.GOOS == "windows" && isatty.IsTerminal(os.Stdout.Fd()) {
@@ -238,6 +244,9 @@ func listAll() {
238244
func lex(ctx *kong.Context, path string, contents string) chroma.Iterator {
239245
lexer := selexer(path, contents)
240246
if lexer == nil {
247+
if cli.Fail {
248+
ctx.Exit(1)
249+
}
241250
lexer = lexers.Fallback
242251
}
243252
if rel, ok := lexer.(*chroma.RegexLexer); ok {

0 commit comments

Comments
 (0)