Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
43 changes: 37 additions & 6 deletions bin/omarchy-display-text-size
Original file line number Diff line number Diff line change
Expand Up @@ -129,10 +129,40 @@ term_pt_for() {
'BEGIN { printf "%d", int(s * p / b + 0.5) }'
}

# Foot has no config-reload signal, but it binds Control+equal / Control+minus
# to 0.5pt font steps. Step every running foot window by the size delta via
# Hyprland's send_shortcut, which delivers the keypress without focusing it.
resize_foot_windows() {
local old_pt="$1" new_pt="$2"
local steps key
steps="$(awk -v from="$old_pt" -v to="$new_pt" 'BEGIN { printf "%d", (to - from) * 2 }')"
((steps != 0)) || return 0
key="equal"
if ((steps < 0)); then
key="minus"
steps=$((-steps))
fi

# No addresses means there is no compositor to ask (or no foot windows to
# step); report failure so the caller can fall back to the notification.
local addresses
addresses="$(hyprctl clients -j 2>/dev/null | jq -r '.[] | select(.class == "foot" or .class == "footclient") | .address')"
[[ -n $addresses ]] || return 1

local address press
for address in $addresses; do
for ((press = 0; press < steps; press++)); do
hyprctl dispatch "hl.dsp.send_shortcut({ window = \"address:$address\", mods = \"CTRL\", key = \"$key\" })" >/dev/null 2>&1 ||
hyprctl dispatch sendshortcut "CTRL, $key, address:$address" >/dev/null 2>&1 ||
return 1
done
done
}

# Set the font point size in terminal configs, creating Kitty overrides when
# it inherits its size from the system config. Family is left
# 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.
# omarchy-font-set; foot's running windows are stepped to the new size instead.
set_terminal_size() {
local pt="$1"

Expand All @@ -156,12 +186,13 @@ set_terminal_size() {
fi

if [[ -f ~/.config/foot/foot.ini ]]; then
local old_pt
old_pt="$(grep -oP ':size=\K[0-9.]+' ~/.config/foot/foot.ini | head -1)"
sed -i -E "s/(:size=)[0-9.]+/\1$pt/" ~/.config/foot/foot.ini
# Foot has no config-reload signal, so a running instance keeps its startup
# size until relaunched (new windows pick up the change). Nudge the user —
# but reuse the same notification id (freedesktop replaces_id) so dragging
# through several sizes refreshes one toast instead of stacking a pile.
if pgrep -x foot >/dev/null 2>&1; then
if ! resize_foot_windows "${old_pt:-$pt}" "$pt" && pgrep -x foot >/dev/null 2>&1; then
# Stepping the running windows failed (no Hyprland?), so nudge the user —
# reusing the same notification id (freedesktop replaces_id) so dragging
# through several sizes refreshes one toast instead of stacking a pile.
local id_file="${XDG_RUNTIME_DIR:-/tmp}/omarchy-display-text-size.foot-notif-id"
local prev_id=""
[[ -f $id_file ]] && read -r prev_id <"$id_file" 2>/dev/null
Expand Down
140 changes: 140 additions & 0 deletions test/shell.d/display-text-size-foot-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/bin/bash

set -euo pipefail

source "$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)/base-test.sh"

require_command jq

test_tmp=$(mktemp -d)
trap 'rm -rf "$test_tmp"' EXIT

mock_bin="$test_tmp/bin"
call_log="$test_tmp/calls"
runtime_dir="$test_tmp/runtime"
home_dir="$test_tmp/home"
clients_json="$test_tmp/clients.json"
foot_ini="$home_dir/.config/foot/foot.ini"
mkdir -p "$mock_bin" "$runtime_dir" "$home_dir/.config/foot"
touch "$call_log"

cat >"$mock_bin/hyprctl" <<'SH'
#!/bin/bash
printf 'hyprctl %s\n' "$*" >>"$CALL_LOG"
if [[ $1 == "clients" ]]; then
if [[ ${HYPRCTL_DOWN:-0} == "1" ]]; then
exit 1
fi
cat "$CLIENTS_JSON"
elif [[ $1 == "dispatch" ]]; then
if [[ ${DISPATCH_FAIL:-0} == "1" ]]; then
exit 1
fi
fi
SH

cat >"$mock_bin/gsettings" <<'SH'
#!/bin/bash
printf 'gsettings %s\n' "$*" >>"$CALL_LOG"
if [[ $1 == "get" && $3 == "font-name" ]]; then
printf "'Inter Variable 11'\n"
fi
SH

cat >"$mock_bin/pgrep" <<'SH'
#!/bin/bash
printf 'pgrep %s\n' "$*" >>"$CALL_LOG"
if [[ ${FOOT_RUNNING:-0} == "1" ]]; then
exit 0
else
exit 1
fi
SH

cat >"$mock_bin/omarchy-notification-send" <<'SH'
#!/bin/bash
printf 'omarchy-notification-send %s\n' "$*" >>"$CALL_LOG"
printf '42\n'
SH

chmod +x "$mock_bin"/*

cat >"$clients_json" <<'JSON'
[
{"class": "foot", "address": "0xf001"},
{"class": "footclient", "address": "0xf002"},
{"class": "Alacritty", "address": "0xaaaa"}
]
JSON

run_text_size() {
CALL_LOG="$call_log" CLIENTS_JSON="$clients_json" HOME="$home_dir" \
XDG_RUNTIME_DIR="$runtime_dir" PATH="$mock_bin:$ROOT/bin:$PATH" \
"$ROOT/bin/omarchy-display-text-size" "$@"
}

press_count() {
grep -Fc "address:$1\", mods = \"CTRL\", key = \"$2\"" "$call_log" || true
}

dispatch_count() {
grep -Fc 'hyprctl dispatch' "$call_log" || true
}

notification_count() {
grep -Fc 'omarchy-notification-send' "$call_log" || true
}

# 16px maps to a 12pt terminal font; from 9pt that is six half-point steps up.
printf 'font=monospace:size=9\n' >"$foot_ini"
run_text_size 16 >/dev/null

grep -Fq ':size=12' "$foot_ini" || fail "foot.ini is rewritten to the new point size" "$(cat "$foot_ini")"
pass "foot.ini is rewritten to the new point size"

(( $(press_count 0xf001 equal) == 6 )) || fail "the foot window is stepped up six half-points" "$(cat "$call_log")"
pass "the foot window is stepped up six half-points"

(( $(press_count 0xf002 equal) == 6 )) || fail "footclient windows are stepped too" "$(cat "$call_log")"
pass "footclient windows are stepped too"

(( $(grep -Fc 'address:0xaaaa' "$call_log" || true) == 0 )) || fail "non-foot windows are left alone" "$(cat "$call_log")"
pass "non-foot windows are left alone"

(( $(notification_count) == 0 )) || fail "no restart notification when the windows were stepped" "$(cat "$call_log")"
pass "no restart notification when the windows were stepped"

# Back down to 12px (9pt): six half-point steps with Control+minus.
run_text_size 12 >/dev/null
(( $(press_count 0xf001 minus) == 6 )) || fail "stepping down uses Control+minus" "$(cat "$call_log")"
pass "stepping down uses Control+minus"

# The same size again is a zero delta: no keypresses at all.
dispatches=$(dispatch_count)
run_text_size 12 >/dev/null
(( $(dispatch_count) == dispatches )) || fail "an unchanged size sends no keypresses" "$(cat "$call_log")"
pass "an unchanged size sends no keypresses"

# A failed dispatch falls back to the restart notification...
DISPATCH_FAIL=1 FOOT_RUNNING=1 run_text_size 16 >/dev/null
grep -Fq 'Restart Foot' "$call_log" || fail "failed dispatch falls back to the restart notification" "$(cat "$call_log")"
pass "failed dispatch falls back to the restart notification"

# ...and a second notification replaces the first instead of stacking.
DISPATCH_FAIL=1 FOOT_RUNNING=1 run_text_size 20 >/dev/null
grep -F 'omarchy-notification-send' "$call_log" | tail -1 | grep -Fq -- '-r 42' ||
fail "repeat notifications reuse the freedesktop replaces id" "$(cat "$call_log")"
pass "repeat notifications reuse the freedesktop replaces id"

# With no compositor to ask, running foot instances still get the nudge.
printf 'font=monospace:size=9\n' >"$foot_ini"
notifications=$(notification_count)
HYPRCTL_DOWN=1 FOOT_RUNNING=1 run_text_size 16 >/dev/null
(( $(notification_count) == notifications + 1 )) || fail "no compositor still nudges running foot instances" "$(cat "$call_log")"
pass "no compositor still nudges running foot instances"

# No compositor and no foot running: nothing to notify about.
notifications=$(notification_count)
HYPRCTL_DOWN=1 run_text_size 12 >/dev/null
(( $(notification_count) == notifications )) || fail "no notification when foot is not running" "$(cat "$call_log")"
pass "no notification when foot is not running"