👻

Ghosttyでプロンプト文字列に色が付かない問題

に公開

Ghosttyは高速&Zero-Configを謳う今話題のターミナルエミュレータだ。
おそらく環境依存であろうが、見た目に影響する以下事象について記録しておく。

(参考) 筆者の環境 (Kubuntu)
$ cat /etc/os-release && plasmashell --version
PRETTY_NAME="Ubuntu 25.10"
NAME="Ubuntu"
VERSION_ID="25.10"
VERSION="25.10 (Questing Quokka)"
VERSION_CODENAME=questing
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=questing
LOGO=ubuntu-logo
plasmashell 6.4.5
QThreadStorage: entry 8 destroyed before end of thread 0x582f28d5db50
QThreadStorage: entry 3 destroyed before end of thread 0x582f28d5db50
QThreadStorage: entry 2 destroyed before end of thread 0x582f28d5db50

事象: プロンプト文字列に色がつかない

Ghosttyはインストール初期時点ではプロンプト文字列(ユーザー名・ホスト名・カレントディレクトリ名の表示部)に色がつかず、白文字表示となる。

Konsole vs Ghostty
上がGhostty、下がKonsole(KDE Plasmaのプリインストールのターミナル)

原因: PS1による色付き文字制御が適用されていない

プロンプト文字列の見た目はターミナルが参照している環境変数PS1で制御されるのだが、以下のようにGhosttyとKonsoleでPS1の値が異なることが分かる。

Konsole vs Ghostty, PS1
Ghostty(上側)のPS1には、色を指定する制御文字(\033[...m等)が含まれていない

PS1の仕様等については以下の記事で解説している。
https://zenn.dev/kcabo/articles/555d9cc6dad0c3

対処: .bashrc内の判定条件を編集する

.bashrcを編集することで対処可能。

# 現在の設定行の確認
$ grep 'xterm-color' ~/.bashrc
    xterm-color|*-256color) color_prompt=yes;;
    
# xterm-ghosttyを条件に追加するように置換
$ sed -i 's/xterm-color|/xterm-color|xterm-ghostty|/' ~/.bashrc

# 置換後の設定行の確認
$ grep 'xterm-color' ~/.bashrc
    xterm-color|xterm-ghostty|*-256color) color_prompt=yes;;

# .bashrcの再読み込み
$ . .bashrc

(参考) 上記でやっていること

Ghosttyは起動時、環境変数TERMにxterm-ghosttyを割り当てる。

$ echo $TERM # Ghostty上で実行
xterm-ghostty

一方Konsole等ではxterm-256colorを割り当てる。

$ echo $TERM # KonsoleやVSCodeのターミナル上で実行
xterm-256color

TERMとは、ターミナルの種別を示す環境変数である。

TERMの正体

多くのUNIX系システムは、Terminfoと呼ばれるターミナル定義データベースを持つ。一部のTUIアプリケーション(vim等)はTERMの値をキーにTerminfoを検索する。
Terminfoには各ターミナルが持つ機能(ケイパビリティ)が格納されており、アプリケーションはこのTerminfoの情報に基づいて入出力を制御する。この仕組みにより、ターミナルの違いを吸収しつつリッチなTUIを動かせるというわけだ。

Ghosttyは独自のTerminfoエントリを持つ。実際に中身を覗いてみる。

# Terminfoの実体は/usr/share/terminfo/配下にある
$ ls /usr/share/terminfo/x/
xterm           xterm-color   xterm-ghostty  xterm-r5  xterm-vt220
xterm-256color  xterm-debian  xterm-mono     xterm-r6  xterm-xfree86

# 中身を見るにはinfocmpコマンド
$ infocmp xterm-ghostty
#       Reconstructed via infocmp from file: /usr/share/terminfo/x/xterm-ghostty
xterm-ghostty|ghostty|Ghostty,
        am, bce, ccc, hs, km, mc5i, mir, msgr, npc, xenl,
        colors#0x100, cols#80, it#8, lines#24, pairs#0x7fff,
        acsc=++\,\,--..00``aaffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz{{||}}~~,
        bel=^G, blink=\E[5m, bold=\E[1m, cbt=\E[Z, civis=\E[?25l,
        clear=\E[H\E[2J, cnorm=\E[?12l\E[?25h, cr=\r,
        csr=\E[%i%p1%d;%p2%dr, cub=\E[%p1%dD, cub1=^H,
~~~後略~~~

PS1は、$TERMの値に応じてセットされる。~/.bashrcで確認できる。

.bashrc (抜粋)
# set a fancy prompt (non-color, unless we know we "want" color)
case "$TERM" in
    xterm-color|*-256color) color_prompt=yes;;
esac

# ~~~中略~~~

if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
fi

上記コマンドでは、このうち一部を書き換えて色付きのPS1がセットされるようにしているわけだ。

.bashrc (抜粋)
case "$TERM" in
-    xterm-color|*-256color) color_prompt=yes;;
+    xterm-color|xterm-ghostty|*-256color) color_prompt=yes;;
esac

参考リンク

https://ghostty.org/docs/help/terminfo
https://nxmnpg.lemoda.net/ja/5/terminfo

Discussion