Linuxメモ : 「ccat」でcatをシンタックスハイライトする
ccat
catやlessをシンタックスハイライトさせるにはpygmentsやsource-highlightを使う方法があるが、ccatというコマンドがあったので試してみる。
サポートしている言語の一覧は下記。
- JavaScript
- Java
- Ruby
- Python
- Go
- C
- JSON
作者によるとcatとccatの処理時間の差はほとんどないとのこと。
$ wc -l main.go 123 main.go $ time cat main.go > /dev/null cat main.go > /dev/null 0.00s user 0.00s system 61% cpu 0.005 total $ time ccat main.go > /dev/null ccat main.go > /dev/null 0.00s user 0.00s system 78% cpu 0.007 total

インストール
ここからバイナリをダウンロードしてPATHが通っているディレクトリに置く。
Goを使っているなら下記コマンドインストールできる。
$ go get -u github.com/jingweno/ccat
Macならbrewでインストールできる。
$ brew install ccat
ヘルプメッセージ
$ ccat --help
Colorize FILE(s), or standard input, to standard output.
Usage:
ccat [OPTION]... [FILE]...
Flags:
--bg="light": set to "light" or "dark" depending on the terminal's background
-C, --color="auto": colorize the output; value can be "never", "always" or "auto"
-G, --color-code=: set color codes
-h, --help=false: help for ccat
--html=false: output html
--palette=false: show color palettes
-v, --version=false: show version
Using color is auto both by default and with --color=auto. With --color=auto,
ccat emits color codes only when standard output is connected to a terminal.
Color codes can be changed with -G KEY=VALUE. List of color codes can
be found with --palette.
Examples:
$ ccat FILE1 FILE2 ...
$ ccat --bg=dark FILE1 FILE2 ... # dark background
$ ccat --html # output html
$ ccat -G String="_darkblue_" -G Plaintext="darkred" FILE # set color codes
$ ccat --palette # show palette
$ ccat # read from standard input
$ curl https://raw.githubusercontent.com/jingweno/ccat/master/main.go | ccat使い方
catと同じように使えるのでalias cat=ccatを設定してもよいかもしれない。

現在の色設定、使用可能な色は--palatteで確認できる。

--bg=darkで背景色をdarkにする。

色を指定する例(-G String="_darkblue_" -G Plaintext="darkred")。

ccat FILE --html > out.htmlとすればHTML出力もできる(図はブラウザで表示した例)。
