Skip to content

Instantly share code, notes, and snippets.

@GOROman
Created January 16, 2025 11:52
Show Gist options
  • Save GOROman/98d17b95586e1638af63652a23c316b8 to your computer and use it in GitHub Desktop.
Save GOROman/98d17b95586e1638af63652a23c316b8 to your computer and use it in GitHub Desktop.
iTerm2にデカイ文字を画像で出してみるウル技(テク) 【小結級】
#!/bin/zsh
# フォントとか色々設定
font="/System/Library/Fonts/ヒラギノ角ゴシック W3.ttc"
output_path="/tmp"
pointsize=100
image_size="1024x150"
background_color="black"
fill_color="white"
# カウンタ変数を初期化
typeset -i counter=0
# 画像出力関数
output() {
# カウンタをインクリメント
((counter++))
# 文字列の長さを取得
local length=${#1}
# 画像ファイル名を決定(交互に変えないと反映しないので)
if ((counter % 2 == 1)); then
image_file="output1.png"
else
image_file="output2.png"
fi
# フォントサイズを設定(てきとう)
local fontsize=140
if ((length > 10)); then
fontsize=100
fi
if ((length > 20)); then
fontsize=40
fi
if ((length > 40)); then
fontsize=20
fi
# magick コマンドで画像を生成
magick -size $image_size xc:$background_color -font "$font" -pointsize $fontsize -fill $2 -draw "text 20,120 '$1'" $output_path/$image_file
# OSAScriptでiTerm2の背景画像を設定
osascript -e "tell application \"iTerm2\" to tell current session of current window to set background image to \"$output_path/$image_file\""
}
# 1文字入力ごとに実行される関数
execute_on_keypress() {
# BUFFER が空か?
if [[ -z $BUFFER ]]; then
output "" "white" 10
else
output $BUFFER "white" 100
fi
}
clear() {
# 背景画像をリセット
osascript -e "tell application \"iTerm2\" to tell current session of current window to set background image to \"\""
}
# 画面クリア時に背景をリセットする関数
execute_on_clear() {
clear
}
function capture_command_output() {
local cmd="$1"
# echo "CMD: $cmd"
local result=$(eval "$cmd" 2>&1)
if [[ -z $result ]]; then
clear
else
# エラー、警告、失敗のパターンを検索
local error_lines=$(echo "$result" | grep -i "error\|warning\|failed")
if [[ -n $error_lines ]]; then
# ' を \' に置き換え
error_lines=$(echo "$error_lines" | sed "s/\'/’/g")
output "$error_lines" "red" 30
# エスケープシーケンスで赤色で出力
# echo -e "\033[31m$error_lines\033[0m"
else
output "$result" "yellow" 100
fi
fi
}
# `preexec` フックに登録 (コマンド実行前に実行)
autoload -Uz add-zsh-hook
add-zsh-hook preexec capture_command_output
# キー入力ごとに呼ばれるフック
function zle-line-pre-redraw() {
execute_on_keypress # 入力内容を背景に設定
}
function zle-line-finish() {
execute_on_clear # 画面クリア時に背景をリセット
}
# フックを登録
zle -N zle-line-pre-redraw
zle -N zle-line-finish
@GOROman
Copy link
Author

GOROman commented Jan 16, 2025

コレは何??

iTerm2 に打ち込んでるコマンドをデカイ文字を出したり、結果をデカい文字で出す。

output

1文字打つたびに画像を毎回生成してるのですげー遅いです。

ユースケース

  • プレゼン
  • ライブ配信時
  • 老眼で辛い

使い方

  • iTerm2 を起動
    • Settings > Profiles > Window > Background Image
      • ✅ Enable
      • Mode: Scale to Fit
      • 仮の画像を適当にセット
      • Blendingはお好みで
image
  • Image Magick をインストールしておく
brew install imagemagick
  • 起動
source dekamoji.sh

解除方法

シェルからログアウトする

@GOROman
Copy link
Author

GOROman commented Jan 16, 2025

出力結果もハンドリング

image

@GOROman
Copy link
Author

GOROman commented Jan 16, 2025

エラーっぽい文字列があった場合は赤くしている

local error_lines=$(echo "$result" | grep -i "error\|warning\|failed")
image

@GOROman
Copy link
Author

GOROman commented Jan 16, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment