0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

端末に色付き文字を表示する ANSI エスケープシーケンスのスニペット (bash / zsh と python)

0
Posted at

この記事は単に端末に色々な色で文字を表示するスニペットです。
以下のように表示されます (標準色の色味はお手元の端末のカラーテーマに依存します)。

image.png

スニペット

  • 冒頭のエスケープシーケンス \033[XXm] が文字色の指示で、末尾のエスケープシーケンス \033[0m] が全文字装飾のリセットの指示です。
    • 文字色ではなく背景色にしたい場合は冒頭の \033[ の直後の数字に 10 を足します (standard colors は 30 番台を 40 番台に、bright colors は 90 番台を 100 番台に、8/24-bit colors は 38 を 48 にする)。
  • わかりやすさのためにエスケープシーケンスと文字列の間に空白を2つ開けていますが、詰めて構いません。

bash / zsh

# ANSI 8 colors (3-bit) = standard colors
printf "\033[30m  %s  \033[0m  \n" "Hello world"  # black
printf "\033[31m  %s  \033[0m  \n" "Hello world"  # red
printf "\033[32m  %s  \033[0m  \n" "Hello world"  # green
printf "\033[33m  %s  \033[0m  \n" "Hello world"  # yellow
printf "\033[34m  %s  \033[0m  \n" "Hello world"  # blue
printf "\033[35m  %s  \033[0m  \n" "Hello world"  # magenta
printf "\033[36m  %s  \033[0m  \n" "Hello world"  # cyan
printf "\033[37m  %s  \033[0m  \n" "Hello world"  # white
printf "\n"

# ANSI 16 colors (4-bit) = standard colors + bright colors
printf "\033[90m  %s  \033[0m  \n" "Hello world"  # bright black
printf "\033[91m  %s  \033[0m  \n" "Hello world"  # bright red
printf "\033[92m  %s  \033[0m  \n" "Hello world"  # bright green
printf "\033[93m  %s  \033[0m  \n" "Hello world"  # bright yellow
printf "\033[94m  %s  \033[0m  \n" "Hello world"  # bright blue
printf "\033[95m  %s  \033[0m  \n" "Hello world"  # bright magenta
printf "\033[96m  %s  \033[0m  \n" "Hello world"  # bright cyan
printf "\033[97m  %s  \033[0m  \n" "Hello world"  # bright white
printf "\n"

# ANSI 256 colors (8-bit)
printf "\033[38;5;76m  %s  \033[0m  \n" "Hello world"  # 256-color index 76 (95, 215, 0)
printf "\033[38;5;214m  %s  \033[0m  \n" "Hello world"  # 256-color index 214 (255, 175, 0)
printf "\033[38;5;211m  %s  \033[0m  \n" "Hello world"  # 256-color index 211 (255, 135, 175)
printf "\n"
# values = [0, 95, 135, 175, 215, 255]
# (values[(76 - 16) // 36], values[((76 - 16) % 36) // 6], values[(76 - 16) % 6]) = (95, 215, 0)
# (values[(214 - 16) // 36], values[((214 - 16) % 36) // 6], values[(214 - 16) % 6]) = (255, 175, 0)
# (values[(211 - 16) // 36], values[((211 - 16) % 36) // 6], values[(211 - 16) % 6]) = (255, 135, 175)

# True color (24-bit)
printf "\033[38;2;95;215;0m  %s  \033[0m  \n" "Hello world"  # (95, 215, 0)
printf "\033[38;2;255;175;0m  %s  \033[0m  \n" "Hello world"  # (255, 175, 0)
printf "\033[38;2;255;135;175m  %s  \033[0m  \n" "Hello world"  # (255, 135, 175)

python

# ANSI 8 colors (3-bit) = standard colors
for i in range(30, 37 + 1):
    print(f'\033[{i}m  Hello world  \033[0m')
print()

# ANSI 16 colors (4-bit) = standard colors + bright colors
for i in range(90, 97 + 1):
    print(f'\033[{i}m  Hello world  \033[0m')
print()

# ANSI 256 colors (8-bit)
for i in [76, 214, 211]:
    print(f'\033[38;5;{i}m  Hello world  \033[0m')
print()

# True color (24-bit)
for r, g, b in [(95, 215, 0), (255, 175, 0), (255, 135, 175)]:
    print(f'\033[38;2;{r};{g};{b}m  Hello world  \033[0m')

標準 16 色は colorama を利用した方が可読性がよいです。

from colorama import Fore
print(Fore.RED + 'Hello world' + Fore.RESET)
print(Fore.LIGHTRED_EX + 'Hello world' + Fore.RESET)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?