Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
ターミナル出力の色付け / Terminal output coloring
Search
Yoichi NAKAYAMA
September 08, 2020
Programming
0
170
ターミナル出力の色付け / Terminal output coloring
Yoichi NAKAYAMA
September 08, 2020
Tweet
Share
More Decks by Yoichi NAKAYAMA
See All by Yoichi NAKAYAMA
git-jumpを解放する / loosen editor dependency of git-jump
yoichi
0
1.4k
小さなOSS貢献の実践例 / A practical example of a small contribution to OSS
yoichi
0
140
パイプの話 / on pipe
yoichi
0
520
バイナリリーディング / Binary Reading
yoichi
0
390
OSS開発を楽しく続けてきた話
yoichi
0
840
Other Decks in Programming
See All in Programming
React 使いじゃなくても知っておきたい教養としての React
oukayuka
18
5.7k
STUNMESH-go: Wireguard NAT穿隧工具的源起與介紹
tjjh89017
0
370
大規模FlutterプロジェクトのCI実行時間を約8割削減した話
teamlab
PRO
0
470
Claude Code と OpenAI o3 で メタデータ情報を作る
laket
0
130
可変性を制する設計: 構造と振る舞いから考える概念モデリングとその実装
a_suenami
10
1.7k
未来を拓くAI技術〜エージェント開発とAI駆動開発〜
leveragestech
2
130
Scale out your Claude Code ~自社専用Agentで10xする開発プロセス~
yukukotani
9
2.1k
AIのメモリー
watany
13
1.4k
コーディングは技術者(エンジニア)の嗜みでして / Learning the System Development Mindset from Rock Lady
mackey0225
2
480
新しいモバイルアプリ勉強会(仮)について
uetyo
1
260
CEDEC2025 長期運営ゲームをあと10年続けるための0から始める自動テスト ~4000項目を50%自動化し、月1→毎日実行にした3年間~
akatsukigames_tech
0
130
decksh - a little language for decks
ajstarks
4
21k
Featured
See All Featured
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4k
jQuery: Nuts, Bolts and Bling
dougneiner
64
7.8k
Embracing the Ebb and Flow
colly
86
4.8k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
367
26k
Producing Creativity
orderedlist
PRO
347
40k
Learning to Love Humans: Emotional Interface Design
aarron
273
40k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
248
1.3M
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
3.1k
Keith and Marios Guide to Fast Websites
keithpitt
411
22k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
15
1.6k
Stop Working from a Prison Cell
hatefulcrawdad
271
21k
A Tale of Four Properties
chriscoyier
160
23k
Transcript
ターミナル出力の色付け Yoichi Nakayama
ターミナル出力 プログラムが標準出力(stdout)や標準エラー出力(stderr)に書き込んだ内容はプログ ラムを実行したターミナル(terminal, 端末)上に出力される
ターミナル出力の例: ls ディレクトリ、実行権限が付いたファイルに色が付く
ターミナル出力の例: git diff ヘッダ、追加行、削除行に色が付く
ターミナル出力の例: git diff with delta シンタックスハイライト+行内の変更箇所にも色が付く https://github.com/dandavison/delta
ターミナル出力の例: emacs -nw モードに応じたシンタックスハイライト
色の付いた出力 普通に文字列を出力したときは単色だった プログラムによってはカラフルな出力がされていた →どうやったら色付けできるんだろう?
ls の出力を見てみる 出力をodにパイプしてバイナリダンプ 文字列と改行(0a)のみで特に変わったものは入ってない 改行?元々は一行の出力だったのに
感づかれてしまった パイプに気づいてlsは出力内容を変える→このままでは調べられない
回避 --color オプションを付けるとパイプしても色が付く→よさそう
色付き出力 色付き文字の手前に esc [ (CSI = Control Sequence Indicator)で始まる何か https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_sequences
試してみる • echo -e で \xNN をバイトの16進表現と解釈してくれる • \x1b =
esc • esc [ 30~37 m 文字色設定 • esc [ 0 m リセット
まとめ • CSI シーケンスを出力してターミナル出力を制御できる • esc [ 30~37 m で文字色を8色から選んで設定できた
• パイプを検出すると色付けをやめる理由:余計なものが付くから ◦ ls | grep ^dir1$ ターミナルの色付け完全に理解した!
色付けの例: emacs -nw M-x list-colors-display → 8色で終わりじゃない
256 colors • 0~7: standard colors (ESC [ 30~37 m
と同じ) • 8~15: high intensity colors (ESC [ 90~97 m と同じ) • 16~231: 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5) • 232~255: grayscale
True Color (24bit) R(0~255), G(0~255), B(0~255) supported terminals: https://gist.github.com/XVilka/8346728#terminals--true-color
応用: longcat -pixterm ▄ (U+2584, LOWER HALF BLOCK) の文字色と背景色を指定している https://yoichi22.hatenablog.com/entry/2020/07/12/110838
おしまい ターミナル出力のさらなるトピック • DA1, DA2, DA3 • Sixel Graphics •
...