File tree 2 files changed +27
-0
lines changed
2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change @@ -255,6 +255,24 @@ A command-line interface to Chroma is included. It can be installed with:
255
255
go get -u github.com/alecthomas/chroma/cmd/chroma
256
256
```
257
257
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
+
258
276
<a id="markdown-whats-missing-compared-to-pygments" name="whats-missing-compared-to-pygments"></a>
259
277
## What' s missing compared to Pygments ?
260
278
Original file line number Diff line number Diff line change 7
7
"io/ioutil"
8
8
"os"
9
9
"os/signal"
10
+ "path"
10
11
"runtime"
11
12
"runtime/pprof"
12
13
"sort"
@@ -43,6 +44,7 @@ command, for Go.
43
44
Trace bool `help:"Trace lexer states as they are traversed."`
44
45
Check bool `help:"Do not format, check for tokenisation errors instead."`
45
46
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."`
46
48
47
49
Lexer string `help:"Lexer to use when formatting." default:"autodetect" short:"l" enum:"${lexers}"`
48
50
Style string `help:"Style to use for formatting." default:"swapoff" short:"s" enum:"${styles}"`
@@ -105,6 +107,10 @@ func main() {
105
107
}()
106
108
defer pprof .StopCPUProfile ()
107
109
}
110
+ if path .Base (os .Args [0 ]) == ".lessfilter" {
111
+ // https://manpages.debian.org/lesspipe#USER_DEFINED_FILTERS
112
+ cli .Fail = true
113
+ }
108
114
109
115
var out io.Writer = os .Stdout
110
116
if runtime .GOOS == "windows" && isatty .IsTerminal (os .Stdout .Fd ()) {
@@ -238,6 +244,9 @@ func listAll() {
238
244
func lex (ctx * kong.Context , path string , contents string ) chroma.Iterator {
239
245
lexer := selexer (path , contents )
240
246
if lexer == nil {
247
+ if cli .Fail {
248
+ ctx .Exit (1 )
249
+ }
241
250
lexer = lexers .Fallback
242
251
}
243
252
if rel , ok := lexer .(* chroma.RegexLexer ); ok {
You can’t perform that action at this time.
0 commit comments