ひむ日記

本名は設楽です

Ghostty のテーマを毎回ランダムに変えてみる

Ghostty は 2024 年 12 月末に公開されたターミナルエミュレータである。

ghostty.org

新しいものに飛びつくのが趣味なので、もちろん僕も今年(2025 年)の元旦くらいに導入した。

2025 年 5 月現在でも Ghostty はターミナルエミュレータの中では相当若いものではあるが、とは言っても 4 ヶ月ほど使っていると流石に飽きてきてしまう。

そこで 3 分ほど飽きを防ぐ方法を考え、やはりテーマを変えるのが簡単に気分を変えられて良いのではないか、という結論に至った。

Ghostty のテーマ

Ghostty は CLI から使用可能なテーマ一覧を表示する機能を提供している。

Color Theme - Features

これを用いて良いテーマがないか探してみる。

ghostty +list-themes
themes list

多い...

$ ghostty +list-themes | wc -l
394

自分の環境では 394 個選択可能なテーマがある様子。
流石に 394 個のテーマの色合いなどを全て見て選ぶのは現実的ではない。

イデア

色合いを見た時は良いな(微妙だな)と思っても、実際に使ってみたら微妙だった(思いの外良かった)ということは往々にして起こりうる。
それを踏まえて、毎回ランダムにテーマを変えて気に入ったテーマを見つけたらメモをしておく、というような運用だと楽そう。

流石に手動でテーマを変えるのはアホらしいので自動化することにする。

テーマ文字列の取得

素朴にランダムなテーマを出力しようとしてみる。

$ ghostty +list-themes | sort -R | head -n 1
Vaughn (resources)

お尻に (resources) という謎の文字列がついている...

Ghostty のソースコードを読むと、どうやらテーマの格納場所の情報がついてくるという事情がある様子。

https://github.com/ghostty-org/ghostty/blob/464b85b64c6228f1dfcf5cd853a342e582701126/src/cli/list_themes.zig#L167

--path オプションをつけなければ)必ず {theme} ({location})\n という形でテーマが返ってくるようなので、その規則に従って正規表現で削る。

$ ghostty +list-themes | sort -R | head -n 1 | sed 's/ (.*)$//'
catppuccin-latte

良さそう。

ランダムにテーマを変えてみる

### update_theme_randomly.pl

#!/usr/bin/env perl
#
# Update Ghostty theme randomly.

use strict;
use warnings;
use Cwd 'abs_path';

my $DOTFILES_ROOT = abs_path($0);
$DOTFILES_ROOT =~ s!/ghostty/[^/]+$!!;

# Set random seed if provided
srand($ARGV[0]) if @ARGV;

# Get available themes and select one randomly
my $themes = [ map { s/ \(.*\)$//; $_ } `ghostty +list-themes` ];
chomp @$themes;
my $new_theme = $themes->[int(rand(@$themes))];

# Config file path
my $config_file = "$DOTFILES_ROOT/ghostty/config";

my $updated = 0;
my $content = '';

# Read existing configuration
open(my $in, '<', $config_file) or die "Cannot open $config_file: $!";
while (my $line = <$in>) {
    if ($line =~ /^theme/) {
        # Replace existing theme line
        $content .= "theme = $new_theme\n";
        $updated = 1;
    } else {
        $content .= $line;
    }
}
close $in;

# Append theme if not found in existing config
if (!$updated) {
    $content .= "theme = $new_theme\n";
}

# Write updated configuration
open(my $out, '>', $config_file) or die "Cannot write to $config_file: $!";
print $out $content;
close $out;

これをターミナル起動時に呼び出すために、.zshrc とかにスクリプトを呼ぶ処理を書く(Ghostty の設定の更新を行なっていないので、テーマの更新は 1 回分ずれる)。

# update ghostty theme
$HOME/.dotfiles/ghostty/update_theme_randomly.pl
起動 1 回目
起動 2 回目

これで毎回新鮮な気持ちでターミナル操作が行えそう!

皆さんも良きターミナルライフを!!