もた日記

くだらないことを真面目にやる

Linuxメモ : 「ccat」でcatをシンタックスハイライトする

ccat


github.com

catlessをシンタックスハイライトさせるにはpygmentssource-highlightを使う方法があるが、ccatというコマンドがあったので試してみる。
サポートしている言語の一覧は下記。

  • JavaScript
  • Java
  • Ruby
  • Python
  • Go
  • C
  • JSON

作者によるとcatccatの処理時間の差はほとんどないとのこと。

$ 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

f:id:wonder-wall:20170824204634p:plain

インストール


ここからバイナリをダウンロードして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を設定してもよいかもしれない。

f:id:wonder-wall:20170824204623p:plain

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

f:id:wonder-wall:20170824204716p:plain

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

f:id:wonder-wall:20170824204634p:plain

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

f:id:wonder-wall:20170824210149p:plain

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

f:id:wonder-wall:20170824204742p:plain