Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
77 changes: 64 additions & 13 deletions bin/omarchy-display-text-size
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,47 @@ gtk_font_pt() {
fi
}

# Update GTK before touching shell/terminal configs. Ghostty needs a readable
# factor both before and after the update so a failed read can be rolled back.
set_factor() {
gsettings set "$GKEY_SCHEMA" "$GKEY_NAME" "$1" 2>/dev/null || true
local requested="$1" previous factor
if [[ -f ~/.config/ghostty/config ]]; then
if ! previous=$(gtk_factor); then
echo "Cannot read GTK text scaling; text size was left unchanged." >&2
return 1
fi
fi

if [[ $requested == "reset" ]]; then
gsettings reset "$GKEY_SCHEMA" "$GKEY_NAME" 2>/dev/null || true
else
gsettings set "$GKEY_SCHEMA" "$GKEY_NAME" "$requested" 2>/dev/null || true
fi

if [[ -f ~/.config/ghostty/config ]]; then
if factor=$(gtk_factor); then
echo "$factor"
else
if gsettings set "$GKEY_SCHEMA" "$GKEY_NAME" "$previous" 2>/dev/null; then
Comment thread
greptile-apps[bot] marked this conversation as resolved.
Resolved
echo "Cannot read GTK text scaling; restored previous scaling. Text size was left unchanged." >&2
else
echo "Cannot read or restore GTK text scaling; shell and terminal sizes were left unchanged." >&2
fi
return 1
fi
fi
}

# 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 ----
Expand All @@ -138,7 +177,14 @@ 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" factor="$2" 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.
ghostty_pt=$(awk -v p="$pt" -v f="$factor" \
'BEGIN { printf "%.4f", p / f }' | sed -E 's/\.?0+$//')
fi

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

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

Expand All @@ -180,10 +226,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 @@ -212,10 +263,10 @@ case "${1:-}" in
exit 0
;;
reset | default)
factor=$(set_factor reset) || exit 1
reset_base_size
gsettings reset "$GKEY_SCHEMA" "$GKEY_NAME" 2>/dev/null || true
set_terminal_size "$TERM_DEFAULT_PT"
exit 0
set_terminal_size "$TERM_DEFAULT_PT" "$factor"
exit $?
;;
esac

Expand All @@ -226,15 +277,15 @@ if [[ ! $size =~ ^[0-9]+$ ]] || ((size < MIN || size > MAX)); then
exit 1
fi

# Shell side: base-size in px (the omarchy shell's rem root).
set_base_size "$size"

# GTK side: multiplier anchored so 12px == 1.0, quantized so the interface
# font renders at a whole point size. Raw ratios yield fractional point sizes,
# which GTK4 menus clip at the ascenders on scale-1 monitors.
factor="$(awk -v s="$size" -v b="$SHELL_DEFAULT_PX" -v f="$(gtk_font_pt)" \
'BEGIN { printf "%.4f", int(f * s / b + 0.5) / f }')"
set_factor "$factor"
factor=$(set_factor "$factor") || exit 1

# Shell side: base-size in px (the omarchy shell's rem root).
set_base_size "$size"

# Terminal side: point size anchored so 12px == 9pt.
set_terminal_size "$(term_pt_for "$size")"
set_terminal_size "$(term_pt_for "$size")" "$factor"
9 changes: 8 additions & 1 deletion test/shell.d/desktop-helpers-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ pass "omarchy-font-set escapes special characters and preserves symlinked config

# Stub gsettings and treat foot as not running for omarchy-display-text-size
# (after omarchy-font-set has run with the original pgrep=0 stub above).
printf '#!/bin/bash\nexit 0\n' >"$tmp/bin/gsettings"
# Keep GTK at 1.0 so this test isolates symlink handling. Ghostty's
# scaling behavior with other GTK factors is covered by its own tests.
cat >"$tmp/bin/gsettings" <<'STUB'
#!/bin/bash
if [[ $1 == "get" && $3 == "text-scaling-factor" ]]; then
printf '1.0\n'
fi
STUB
printf '#!/bin/bash\nexit 1\n' >"$tmp/bin/pgrep"
chmod +x "$tmp/bin/gsettings" "$tmp/bin/pgrep"

Expand Down
184 changes: 184 additions & 0 deletions test/shell.d/ghostty-text-scaling-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
#!/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" READ_COUNT_FILE="$scratch/read-count"
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
if [[ ${FAIL_SECOND_READ:-0} == "1" ]]; then
count=$(cat "$READ_COUNT_FILE")
((count += 1))
echo "$count" >"$READ_COUNT_FILE"
((count == 2)) && exit 1
fi
cat "$FACTOR_FILE"
;;
set:text-scaling-factor)
[[ ${FAIL_WRITE:-0} == "1" ]] && exit 1
if [[ ${FAIL_ROLLBACK:-0} == "1" ]] && (( $(cat "$READ_COUNT_FILE") >= 2 )); then
exit 1
fi
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
}
cp "$HOME/.config/omarchy/shell.toml" "$scratch/shell-before"
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 'text size was left unchanged' "$scratch/err" || fail "failure explains untouched terminal sizes"
assert_terminal_configs_unchanged
cmp -s "$scratch/shell-before" "$HOME/.config/omarchy/shell.toml" || fail "unreadable GTK factor preserves shell config"
[[ $(cat "$FACTOR_FILE") == "1.5" ]] || fail "unreadable GTK factor preserves GTK setting"
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 action in 18 reset; do
echo 0 >"$READ_COUNT_FILE"
if FAIL_SECOND_READ=1 run_size "$action" >"$scratch/out" 2>"$scratch/err"; then
fail "failed GTK readback returns a failure for $action"
fi
grep -q 'restored previous scaling' "$scratch/err" || fail "readback failure reports GTK rollback"
[[ $(cat "$FACTOR_FILE") == "1.5" ]] || fail "failed readback restores previous GTK factor"
cmp -s "$scratch/shell-before" "$HOME/.config/omarchy/shell.toml" || fail "failed readback preserves shell config"
assert_terminal_configs_unchanged
done
pass "failed GTK readback rolls back scaling before shell or terminal config updates"

echo 0 >"$READ_COUNT_FILE"
if FAIL_SECOND_READ=1 FAIL_ROLLBACK=1 run_size 18 >"$scratch/out" 2>"$scratch/err"; then
fail "failed GTK rollback returns a failure"
fi
grep -q 'Cannot read or restore GTK text scaling' "$scratch/err" || fail "failed rollback reports unrestored GTK scaling"
assert_terminal_configs_unchanged
cmp -s "$scratch/shell-before" "$HOME/.config/omarchy/shell.toml" || fail "failed rollback still preserves shell config"
echo 1.5 >"$FACTOR_FILE"
pass "a failed rollback is diagnosed and still leaves shell and terminal configs untouched"

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"

printf 'font-size = 9\n' >"$scratch/ghostty-shared"
ln -s "$scratch/ghostty-shared" "$HOME/.config/ghostty/config"
run_size 16
[[ -L $HOME/.config/ghostty/config ]] || fail "Ghostty config remains a symlink"
assert_apparent_size 12
pass "Ghostty scales the font in the symlink target"