testsAndMisc/linux_configuration/periodic_background/i3-configuration/i3blocks/persist_common.sh
Krzysztof kuhy Rudnicki 4f2b0ccdf7 Dissolve linux_configuration/scripts/ and single_use/
Two levels of pure taxonomy carrying no information, and a 2-directory
cap leaves no room for them. Categories now sit directly under
linux_configuration/: features, fixes, utils, misc, fresh-install,
installers, diagnostics, gaming, boot_recovery, periodic_background, lib,
meta, tests. A script's own lib/ and tests/ are exempt from the cap, so
those stay put.

Depth violations 248 -> 139; roughly 40 of the remainder are inside
digital_wellbeing and hosts, deferred until the guard-lib repoint.

The moves are the easy half. What breaks is every path that counted
levels, and three classes of it had to be found by resolving paths rather
than reading diffs:

  - 52 relative `source` lines for lib/common.sh and lib/android.sh. The
    correct prefix differs per file: 40 were ../../ and 12 were ../../../,
    so a blanket replace would have fixed one set and broken the other.
  - REPO_ROOT in 7 fixes/lib/tests harnesses and setup_syncyomi_guard.sh,
    each walking up four levels from a directory now two levels shallower.
    This caused a real regression -- the fixes suite went red with
    "log_warn: command not found" -- caught by running it and comparing
    against the pre-move baseline worktree.
  - conftest.py's sys.path entries, which took the pytest gate down with
    14 collection errors, and the `# shellcheck source=` directives, which
    took shellcheck down with SC1091.

16 symlinks in $HOME broke, including the entire i3blocks bar. All
repointed, and cpu_monitor/memory/disk/time/volume were run to confirm
they emit real output rather than merely resolving.

Verified: 1798 tests pass at 100% coverage, 8 of 9 lib/tests suites pass
(the ninth needs the coverage jail, identically at baseline), pre-commit
is clean, and the two untracked secret configs are still ignored after
their gitignore paths changed.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PJrqKERAo5QUeEHWS5Asgp
2026-08-23 18:53:30 +02:00

68 lines
1.6 KiB
Bash
Executable File

#!/bin/bash
# Shared helpers for persist-mode i3blocks scripts.
set -u
declare -gA I3BLOCKS_LAST_TS=()
declare -gA I3BLOCKS_LAST_STATE=()
# Return current epoch seconds using bash builtin time formatting.
i3blocks_now_ts() {
if [[ -n ${I3BLOCKS_TEST_NOW_TS:-} ]]; then
printf '%s\n' "$I3BLOCKS_TEST_NOW_TS"
return 0
fi
printf '%(%s)T' -1
}
# Return 0 when enough time elapsed since last emit timestamp for key.
# Usage: if i3blocks_should_emit_by_interval_key "wifi" 2; then ...
i3blocks_should_emit_by_interval_key() {
local key=$1
local min_interval_s=$2
local now last
now=$(i3blocks_now_ts)
last=${I3BLOCKS_LAST_TS[$key]:-0}
if (( now - last < min_interval_s )); then
return 1
fi
I3BLOCKS_LAST_TS[$key]=$now
return 0
}
# Return 0 when new state differs from current value for key, then update.
# Usage: if i3blocks_update_if_changed_key "wifi_output" "$line"; then emit; fi
i3blocks_update_if_changed_key() {
local key=$1
local new_state=$2
local current_state
if [[ -v I3BLOCKS_LAST_STATE[$key] ]]; then
current_state=${I3BLOCKS_LAST_STATE[$key]}
else
current_state=''
fi
if [[ -v I3BLOCKS_LAST_STATE[$key] && $current_state == "$new_state" ]]; then
return 1
fi
I3BLOCKS_LAST_STATE[$key]=$new_state
return 0
}
# Wait for a number of seconds without forking an external `sleep` process.
# Uses bash builtin read timeout. Set I3BLOCKS_TEST_SKIP_WAIT=1 to bypass in tests.
i3blocks_wait_seconds() {
local timeout_s=$1
if [[ ${I3BLOCKS_TEST_SKIP_WAIT:-0} -eq 1 ]]; then
return 0
fi
IFS= read -r -t "$timeout_s" || true
}