Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 35 additions & 5 deletions bin/omarchy-display-text-size
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,18 @@ set_factor() {
gsettings set "$GKEY_SCHEMA" "$GKEY_NAME" "$1" 2>/dev/null || true
}

# Ghostty's GTK runtime multiplies font-size by this setting. Read back the
# effective value so a failed set/reset does not cause a second size mismatch.
gtk_factor() {
local factor
factor="$(gsettings get "$GKEY_SCHEMA" "$GKEY_NAME" 2>/dev/null)" || return 1
if [[ $factor =~ ^[0-9]+(\.[0-9]+)?$ ]] && awk -v f="$factor" 'BEGIN { exit !(f > 0) }'; then
echo "$factor"
else
return 1
fi
}

# ---- terminal font point size ----

# px base-size -> terminal point size, rounded to the nearest integer.
Expand All @@ -134,7 +146,20 @@ term_pt_for() {
# untouched — that is omarchy-font-set's job. Live-reload signals mirror
# omarchy-font-set; foot has no reload signal, so running instances are nudged.
set_terminal_size() {
local pt="$1"
local pt="$1" ghostty_pt

if [[ -f ~/.config/ghostty/config ]]; then
# GTK already scales Ghostty's font. Compensate for that multiplier while
# retaining the same apparent point size as the other terminals.
local factor
if factor=$(gtk_factor); then
ghostty_pt=$(awk -v p="$pt" -v f="$factor" \
'BEGIN { printf "%.4f", p / f }' | sed -E 's/\.?0+$//')
else
echo "Cannot read GTK text scaling; terminal font sizes were left unchanged." >&2
return 1
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
fi
fi

if [[ -f ~/.config/alacritty/alacritty.toml ]]; then
sed -i -E "s/^size[[:space:]]*=.*/size = $pt/" ~/.config/alacritty/alacritty.toml
Expand All @@ -151,7 +176,7 @@ set_terminal_size() {
fi

if [[ -f ~/.config/ghostty/config ]]; then
sed -i -E "s/^font-size = .*/font-size = $pt/" ~/.config/ghostty/config
sed -i -E "s/^font-size = .*/font-size = $ghostty_pt/" ~/.config/ghostty/config
pkill -SIGUSR2 ghostty 2>/dev/null || true
fi

Expand All @@ -176,10 +201,15 @@ set_terminal_size() {
fi
}

# Report the current terminal point size from whichever config we find first.
# Report the apparent point size from whichever terminal config we find first.
term_current_pt() {
if [[ -f ~/.config/ghostty/config ]]; then
grep -oP '^font-size = \K[0-9.]+' ~/.config/ghostty/config | head -1
local pt factor
pt=$(grep -oP '^font-size = \K[0-9.]+' ~/.config/ghostty/config | head -1)
if [[ -n $pt ]] && factor=$(gtk_factor); then
awk -v p="$pt" -v f="$factor" \
'BEGIN { printf "%.2f", p * f }' | sed -E 's/\.?0+$//'
fi
elif [[ -f ~/.config/alacritty/alacritty.toml ]]; then
grep -oP '^size[[:space:]]*=[[:space:]]*\K[0-9.]+' ~/.config/alacritty/alacritty.toml | head -1
elif [[ -f ~/.config/kitty/kitty.conf ]]; then
Expand Down Expand Up @@ -211,7 +241,7 @@ case "${1:-}" in
reset_base_size
gsettings reset "$GKEY_SCHEMA" "$GKEY_NAME" 2>/dev/null || true
set_terminal_size "$TERM_DEFAULT_PT"
exit 0
exit $?
;;
esac

Expand Down
143 changes: 143 additions & 0 deletions test/shell.d/ghostty-text-scaling-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
#!/bin/bash
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved

set -euo pipefail
source "$(dirname "${BASH_SOURCE[0]}")/base-test.sh"

scratch=$(mktemp -d)
trap 'rm -rf "$scratch"' EXIT
export HOME="$scratch/home" OMARCHY_PATH="$ROOT"
export PATH="$scratch/bin:$ROOT/bin:$PATH"
export FACTOR_FILE="$scratch/factor"
mkdir -p "$scratch/bin" "$HOME/.config/"{ghostty,foot,kitty,alacritty,omarchy}
cat >"$scratch/bin/gsettings" <<'STUB'
#!/bin/bash
case "$1:$3" in
get:font-name) echo "'Sans ${GTK_FONT_PT:-11}'" ;;
get:text-scaling-factor)
[[ ${FAIL_READ:-0} == "1" ]] && exit 1
cat "$FACTOR_FILE"
;;
set:text-scaling-factor)
[[ ${FAIL_WRITE:-0} == "1" ]] && exit 1
printf '%s\n' "$4" >"$FACTOR_FILE"
;;
reset:text-scaling-factor)
[[ ${FAIL_WRITE:-0} == "1" ]] && exit 1
echo 1.0 >"$FACTOR_FILE"
;;
esac
STUB
for command in pkill omarchy-notification-send; do
printf '#!/bin/bash\nexit 0\n' >"$scratch/bin/$command"
done
for command in pgrep omarchy-cmd-present; do
printf '#!/bin/bash\nexit 1\n' >"$scratch/bin/$command"
done
chmod +x "$scratch/bin/"*
printf 'font-family = My Font\nfont-size = 9\nbackground = #123456\n' >"$HOME/.config/ghostty/config"
printf '[main]\nfont=My Font:size=9:weight=bold\n' >"$HOME/.config/foot/foot.ini"
printf 'font_size 9.0\n' >"$HOME/.config/kitty/kitty.conf"
printf '[font]\nsize = 9\n' >"$HOME/.config/alacritty/alacritty.toml"
printf '[font]\nfamily = "My Font"\n[bar]\nheight = 24\n' >"$HOME/.config/omarchy/shell.toml"
echo 1.0 >"$FACTOR_FILE"

run_size() {
"$ROOT/bin/omarchy-display-text-size" "$@"
}

ghostty_size() {
sed -n 's/^font-size = //p' "$HOME/.config/ghostty/config"
}

assert_apparent_size() {
local expected=$1 actual factor
actual=$(ghostty_size)
factor=$(cat "$FACTOR_FILE")
awk -v actual="$actual" -v factor="$factor" -v expected="$expected" \
'BEGIN { error = actual * factor - expected; exit !(error > -0.001 && error < 0.001) }' ||
fail "Ghostty renders the requested $expected pt" "configured=$actual GTK=$factor"
local output
output=$(run_size)
[[ $output == *"terminal font: $expected pt"* ]] || fail "report shows Ghostty apparent size" "$output"
}

run_size 16
assert_apparent_size 12
grep -qx 'font=My Font:size=12:weight=bold' "$HOME/.config/foot/foot.ini" || fail "Foot retains its scaled size and font options"
grep -qx 'font_size 12.0' "$HOME/.config/kitty/kitty.conf" || fail "Kitty retains its scaled size"
grep -qx 'size = 12' "$HOME/.config/alacritty/alacritty.toml" || fail "Alacritty retains its scaled size"
grep -qx 'font-family = My Font' "$HOME/.config/ghostty/config" || fail "Ghostty family is preserved"
grep -qx 'background = #123456' "$HOME/.config/ghostty/config" || fail "Ghostty unrelated config is preserved"
grep -qx 'height = 24' "$HOME/.config/omarchy/shell.toml" || fail "shell unrelated config is preserved"
pass "16px scales Ghostty once and preserves other terminal behavior and customization"

for font in 9 11 12 10.5; do
export GTK_FONT_PT="$font"
for size in {9..20}; do
run_size "$size"
expected=$(awk -v s="$size" 'BEGIN { printf "%d", int(s * 9 / 12 + 0.5) }')
assert_apparent_size "$expected"
before=$(ghostty_size)
run_size "$size"
[[ $(ghostty_size) == "$before" ]] || fail "repeated size changes are idempotent"
done
done
pass "all accepted sizes compensate for quantized GTK factors and are idempotent"

run_size reset
assert_apparent_size 9
[[ $(ghostty_size) == "9" ]] || fail "reset restores Ghostty's default config value"
! grep -q '^base-size' "$HOME/.config/omarchy/shell.toml" || fail "reset removes shell override"
pass "reset restores defaults"

echo 1.5 >"$FACTOR_FILE"
export FAIL_WRITE=1
run_size 16
assert_apparent_size 12
run_size reset
assert_apparent_size 9
unset FAIL_WRITE
pass "failed GTK set and reset compensate for the factor actually in use"

# A failed read must not be mistaken for factor 1 while 1.5 is active.
echo 1.5 >"$FACTOR_FILE"
FAIL_WRITE=1 run_size 16
for terminal in ghostty foot kitty alacritty; do
cp -r "$HOME/.config/$terminal" "$scratch/$terminal-before"
done
assert_terminal_configs_unchanged() {
local terminal
for terminal in ghostty foot kitty alacritty; do
diff -r "$scratch/$terminal-before" "$HOME/.config/$terminal" >/dev/null ||
fail "unreadable GTK factor leaves $terminal config unchanged"
done
}
for action in 18 reset; do
if FAIL_READ=1 run_size "$action" >"$scratch/out" 2>"$scratch/err"; then
fail "unreadable GTK factor returns a failure for $action"
fi
grep -q 'terminal font sizes were left unchanged' "$scratch/err" || fail "failure explains untouched terminal sizes"
assert_terminal_configs_unchanged
done
output=$(FAIL_READ=1 run_size)
[[ $output == *"terminal font: n/a pt"* ]] || fail "failed read does not falsely report an apparent size"
pass "failed GTK reads preserve every terminal config, fail set/reset, and report unknown size"

for factor in 0 invalid; do
echo "$factor" >"$FACTOR_FILE"
if FAIL_WRITE=1 run_size 16 >"$scratch/out" 2>"$scratch/err"; then
fail "invalid GTK factor returns a failure"
fi
assert_terminal_configs_unchanged
done
pass "invalid GTK factors leave terminal config untouched"

sed -i '/^font-size = /d' "$HOME/.config/ghostty/config"
output=$(run_size)
[[ $output == *"terminal font: n/a pt"* ]] || fail "missing Ghostty size does not become zero"
rm "$HOME/.config/ghostty/config"
run_size 16
[[ ! -e $HOME/.config/ghostty/config ]] || fail "absent Ghostty config remains absent"
output=$(run_size)
[[ $output == *"terminal font: 12 pt"* ]] || fail "other terminal reporting remains unchanged"
pass "missing Ghostty config and size retain existing behavior"