もた日記

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

Linuxメモ : Rust製のSiliconでソースコードを綺麗な画像に変換

Silicon

github.com

Rust製のSiliconを使うとCarbonのようにソースコードを綺麗な画像に変換することができる。
ブラウザを使わずにオフラインで実行、carbon-now-cliより高速に画像変換できるのが利点とのこと。

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


インストール

README.mdのインストール方法によるとバイナリのダウンロードやcargoでインストールできる。

$ cargo install silicon

Dependenciesがあるので注意。以下はUbuntuの場合。

$ sudo apt install expat
$ sudo apt install libxml2-dev
$ sudo apt install pkg-config libasound2-dev libssl-dev cmake libfreetype6-dev libexpat1-dev libxcb-composite0-dev

ヘルプメッセージ。

$ silicon --help
silicon 0.3.1

USAGE:
    silicon [FLAGS] [OPTIONS] --output <PATH> [FILE]

FLAGS:
        --from-clipboard        Read input from clipboard
    -h, --help                  Prints help information
        --list-themes           List all themes
        --no-line-number        Hide the line number
        --no-round-corner       Don't round the corner
        --no-window-controls    Hide the window controls
    -c, --to-clipboard
    -V, --version               Prints version information

OPTIONS:
    -b, --background <COLOR>         Background color of the image [default: #aaaaff]
    -f, --font <FONT>                The font list. eg. 'Hack; SimSun=31'
        --highlight-lines <LINES>    Lines to high light. rg. '1-3; 4'
    -l, --language <LANG>            The language for syntax highlighting. You can use full name ("Rust") or file extension ("rs")
        --line-pad <PAD>             Pad between lines [default: 2]
    -o, --output <PATH>              Write output image to specific location instead of cwd
        --pad-horiz <PAD>            Pad horiz [default: 80]
        --pad-vert <PAD>             Pad vert [default: 100]
        --shadow-blur-radius <R>     Blur radius of the shadow. (set it to 0 to hide shadow) [default: 0]
        --shadow-color <COLOR>       Color of shadow [default: #555555]
        --shadow-offset-x <X>        Shadow's offset in X axis [default: 0]
        --shadow-offset-y <Y>        Shadow's offset in Y axis [default: 0]
        --tab-width <WIDTH>          Tab width [default: 4]
        --theme <THEME>              The syntax highlight theme. It can be a theme name or path to a .tmTheme file [default: Dracula]

ARGS:
    <FILE>    File to read. If not set, stdin will be use


使い方

以下のようにソースコードファイルを引数として、-oオプションで出力画像を指定する。

$ silicon test.py -o test.png

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

カラーテーマの一覧と変更方法(デフォルトはDracula)。

$ silicon --list-themes
1337
DarkNeon
Dracula
GitHub
Monokai Extended
Monokai Extended Bright
Monokai Extended Light
Monokai Extended Origin
Nord
OneHalfDark
OneHalfLight
Solarized (dark)
Solarized (light)
Sublime Snazzy
TwoDark
ansi-dark
ansi-light
base16
zenburn

$ silicon test.py -o test.png --theme "Solarized (light)"

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

ウィンドウボタン非表示、行ハイライト、背景色の変更など(#fff0で透過)。

$ silicon test.py -o est.png \
--no-window-controls \
--highlight-lines '2-4' \
--background '#fff0'

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

日本語を含む場合は日本語フォントも指定する。

$ silicon test.py -o test.png
[warning] No font found for character `あ`
[warning] No font found for character `あ`
$ silicon test.py -o test.png -f 'Hack; IPAGothic'

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


vim-silicon

github.com

VimからSiliconを使いたい場合はvim-siliconを使うとよい。
Plugなどでインストールできる。

Plug 'segeljakt/vim-silicon'

以下のようにカレントバッファやビジュアルモードで選択した範囲に対して実行することができる。

" Generate an image of the current buffer and write it to /path/to/output.png
:Silicon /path/to/output.png

" Generate an image of the current buffer and write it to /path/to/output.png and clipboard.
:Silicon /path/to/output.png --to-clipboard

" Generate an image of the current buffer and write it to /path/to/<filename>.png
:Silicon /path/to/

" Generate an image of the current visual line selection and write it to /path/to/output.png
:'<,'>Silicon /path/to/output.png

" Generate an image of the current buffer, with the current visual line selection highlighted.
:'<,'>Silicon! /path/to/output.png