refactor(config-settings): shared frame renderer + line-diff redraw #133

Merged
rootiest merged 13 commits from refactor/config-settings-renderer into main 2026-09-08 16:28:53 +00:00
9 changed files with 5699 additions and 205 deletions
@@ -0,0 +1,47 @@
# Copyright (C) 2026 Rootiest
# SPDX-License-Identifier: AGPL-3.0-or-later
# SYNOPSIS
# __config_settings_diff_redraw <old_joined> <new_joined>
#
# DESCRIPTION
# Rewrites an on-screen panel frame in place, touching only the lines
# that changed. Assumes the cursor is already positioned at the top-left
# of the frame (the caller moves it there with a plain \e[<n>A -- no
# \e[J -- before calling this). Line count in old_joined and new_joined
# must be equal; a caller facing a height or width change should use the
# existing full erase+redraw path instead of calling this.
#
# Unchanged lines advance the cursor with a bare newline, leaving
# whatever is already on screen untouched. Changed lines clear just that
# line (\e[2K), return to its start (\r), print the new content, and
# advance (\n). This is what removes the erase-then-redraw flicker: the
# screen is never blanked, only the handful of lines that actually
# differ are ever touched, and each of those is cleared and rewritten in
# the same breath rather than blanked-then-paused-then-filled.
#
# ARGUMENTS
# old_joined Previous frame, lines joined with \n
# new_joined New frame, lines joined with \n (same line count as old)
#
# EXIT STATUS
# 0 Always
#
# RETURNS
# The ANSI sequence needed to turn the old frame into the new one,
# printed to stdout
#
# EXAMPLE
# __config_settings_diff_redraw (string join \n -- $prev_frame) \
# (string join \n -- $new_frame)
function __config_settings_diff_redraw
set -l old (string split \n -- $argv[1])
set -l new (string split \n -- $argv[2])
for i in (seq (count $new))
if test "$old[$i]" = "$new[$i]"
printf '\n'
else
printf '\e[2K\r%s\n' $new[$i]
end
end
end
+27 -56
View File
@@ -42,10 +42,15 @@ function __config_settings_draw
set -l labels Aliases Auto-exec Overrides Integrations Logging Greeting Master
# ── Width tier: 6-col buffer per side before stepping up ──────────────
# ── Width tier ────────────────────────────────────────────────────────
# The tier thresholds live in __config_settings_frame; this file only
# chooses which hand-authored description set goes with the width.
# IW = inner width (chars between │ │); desc field = IW - 33.
# All four layouts are exactly 16 lines tall — panel_h in caller stays 16.
set -l iw 50
# Descriptions are authored to fit their field exactly at every tier
# (43/43, 39/39, 35/35, 17/17), which is why the rows below pass `pad`
# and not `cut` -- see the NOTES in __config_settings_frame.
set -l iw (__config_settings_frame width)
set -l descs \
"cmd shadows" \
startup \
@@ -55,8 +60,8 @@ function __config_settings_draw
fish_greeting \
"disable all"
if test "$COLUMNS" -ge 90
set iw 76
switch $iw
case 76
set descs \
"shadows: ls→eza, cat→bat, cd→z, rm→trash" \
"Fisher bootstrap, themes, py-venv activate" \
@@ -65,8 +70,7 @@ function __config_settings_draw
"scrollback capture & paru/yay AUR wrappers" \
"fish_greeting & first-run welcome banner" \
"master off-switch: overrides all categories"
else if test "$COLUMNS" -ge 86
set iw 72
case 72
set descs \
"ls→eza, cat→bat, cd→zoxide, rm→trash" \
"Fisher bootstrap, themes, py-venv auto" \
@@ -75,8 +79,7 @@ function __config_settings_draw
"scrollback capture & paru/yay wrappers" \
"fish_greeting: first-run welcome banner" \
"master off-switch for all categories"
else if test "$COLUMNS" -ge 82
set iw 68
case 68
set descs \
"ls→eza, cat→bat, cd→z, rm→trash" \
"Fisher, themes, py-venv activate" \
@@ -95,8 +98,7 @@ function __config_settings_draw
# ── Top border ────────────────────────────────────────────────────────
# ┌─ Opinionated Settings (iw-23)×─ ┐ total = iw+2
printf '%s┌─%s Opinionated Settings %s┐\n' \
$p $c_head $c_reset(string repeat -n (math $iw - 23) '─')
__config_settings_frame title $iw $p "$c_head Opinionated Settings $c_reset"
# ── Page-tab header ───────────────────────────────────────────────────
set -l active_idx 0
@@ -109,36 +111,18 @@ function __config_settings_draw
printf '%s│%s│\n' $p $HBR
# ── Category rows 0–5 ─────────────────────────────────────────────────
# Label field 12 wide; the description field falls out of it inside the
# frame (field_w = iw - 21 - label_w = iw - 33). `pad`, not `cut`: these
# descriptions are authored per tier to fit exactly, so truncating them
# would be a silent no-op that discards that property.
for i in (seq 0 5)
set -l idx (math $i + 1)
set -l var $vars[$idx]
set -l label $labels[$idx]
set -l desc $descs[$idx]
set -l val (__config_settings_get_val $var $cur_scope)
# Badge: 7 visible chars, coloured
set -l badge
switch $val
case on
set badge "$c_ok"" ON$c_reset"
case off
set badge "$c_err""OFF $c_reset"
case '*'
set badge "$c_dim""DEFAULT$c_reset"
end
# Cursor: 2 visible chars
set -l curs " "
if test $i -eq $cur_row
set curs "$c_sel$c_reset "
end
# Label padded to 12, desc padded to (iw-33), right margin 3
set -l lpad (string pad -r -w 12 -- $label)
set -l dpad (string pad -r -w (math $iw - 33) -- $desc)
printf '%s│ %s%s [ %s ] %s │\n' $p $curs $lpad $badge $dpad
set -l val (__config_settings_get_val $vars[$idx] $cur_scope)
__config_settings_frame row $iw $p \
(__config_settings_frame cursor $i $cur_row) \
$labels[$idx] 12 \
(__config_settings_frame badge $val) \
$descs[$idx] pad
end
# ── Separator before Master ───────────────────────────────────────────
@@ -146,24 +130,11 @@ function __config_settings_draw
# ── Master row (index 6) ──────────────────────────────────────────────
set -l val (__config_settings_get_val $vars[7] $cur_scope)
set -l badge
switch $val
case on
set badge "$c_ok"" ON$c_reset"
case off
set badge "$c_err""OFF $c_reset"
case '*'
set badge "$c_dim""DEFAULT$c_reset"
end
set -l curs " "
if test $cur_row -eq 6
set curs "$c_sel$c_reset "
end
printf '%s│ %s%s [ %s ] %s │\n' \
$p $curs \
(string pad -r -w 12 -- Master) \
$badge \
(string pad -r -w (math $iw - 33) -- $descs[7])
__config_settings_frame row $iw $p \
(__config_settings_frame cursor 6 $cur_row) \
Master 12 \
(__config_settings_frame badge $val) \
$descs[7] pad
# ── Filler (Dots Path moved to the Paths page) ────────────────────────
printf '%s│ %s%s│\n' $p \
+31 -57
View File
@@ -47,23 +47,14 @@ function __config_settings_draw_subcat
set -l n (count $rows)
# ── Width tier: matches __config_settings_draw's 6-col-per-side steps ──
set -l iw 50
if test "$COLUMNS" -ge 90
set iw 76
else if test "$COLUMNS" -ge 86
set iw 72
else if test "$COLUMNS" -ge 82
set iw 68
end
set -l iw (__config_settings_frame width)
set -l HBR (string repeat -n $iw '─')
set -l p (string repeat -n (math --scale=0 "max(0, ($COLUMNS - ($iw + 2)) / 2)") ' ')
# Label field is 13 wide (one wider than __config_settings_draw's 12) --
# the longest real sub-category label ("Notifications") is 13 chars.
# Description field absorbs the difference so every row still totals
# iw+2, matching the surrounding box lines exactly.
set -l label_w 13
set -l desc_w (math $iw - 34)
# the longest real sub-category label ("Notifications") is 13 chars. The
# description field absorbs the difference inside the frame
# (field_w = iw - 21 - label_w = iw - 34), so every row still totals iw+2.
set -l cat_label (string replace -r '^__fish_config_op_' '' -- $category_var)
# Scope indicator: toggling a row on this page writes -U (Universal,
@@ -72,32 +63,18 @@ function __config_settings_draw_subcat
set -l scope_label Universal
test "$cur_scope" = session; and set scope_label Session
# Title layout is "┌─ Sub-categories: <label> (<scope>) ───┐"; the
# dash count must absorb every visible char added around cat_label so
# the line still totals iw+2, matching the surrounding box exactly --
# see the DESCRIPTION doc comment above for why this is hand-verified,
# not eyeballed.
set -l title_dashes (math $iw - (string length -- $cat_label) - (string length -- $scope_label) - 22)
printf '%s┌─%s Sub-categories: %s (%s)%s %s┐\n' \
$p $c_head "$cat_label" $scope_label "$c_reset" (string repeat -n (math "max(0, $title_dashes)") '─')
# dash count absorbs every visible char added around cat_label so the
# line still totals iw+2, matching the surrounding box exactly. The
# frame derives it from the segment's visible width, so it no longer
# has to be hand-verified here.
__config_settings_frame title $iw $p \
"$c_head Sub-categories: $cat_label ($scope_label)$c_reset "
printf '%s│%s│\n' $p $HBR
# Row 0: the category's own toggle, still meaningful as the cascade
# default any DEFAULT-valued sub-category below falls back to.
set -l cat_val (__config_settings_get_val $category_var $cur_scope)
set -l cat_badge
switch $cat_val
case on
set cat_badge "$c_ok"" ON$c_reset"
case off
set cat_badge "$c_err""OFF $c_reset"
case '*'
set cat_badge "$c_dim""DEFAULT$c_reset"
end
set -l cat_curs " "
if test $cur_row -eq 0
set cat_curs "$c_sel$c_reset "
end
set -l cat_desc "cascade default"
if test $iw -ge 68
set cat_desc "default for all sub-cats below"
@@ -105,38 +82,35 @@ function __config_settings_draw_subcat
if test $iw -ge 72
set cat_desc "default for all sub-categories below"
end
printf '%s│ %s%s [ %s ] %s │\n' $p $cat_curs \
(string pad -r -w $label_w -- "(category)") $cat_badge \
(string pad -r -w $desc_w -- (string sub -l $desc_w -- $cat_desc))
# `cut` for the same reason as the sub-category rows below. It also
# truncates the label, which the old code did not -- provably inert here,
# since "(category)" is a 10-char literal against a 13-wide field.
__config_settings_frame row $iw $p \
(__config_settings_frame cursor 0 $cur_row) \
"(category)" 13 \
(__config_settings_frame badge $cat_val) \
$cat_desc cut
printf '%s│ %s │\n' $p (string repeat -n (math $iw - 6) '─')
for i in (seq 1 $n)
set -l fields (string split -- \t $rows[$i])
set -l slug $fields[1]
set -l label $fields[2]
set -l desc $fields[3]
set -l subcat_var "$category_var"_(string replace -a -- '-' '_' $slug)
set -l subcat_var "$category_var"_(string replace -a -- '-' '_' $fields[1])
set -l val (__config_settings_get_val $subcat_var $cur_scope)
set -l badge
switch $val
case on
set badge "$c_ok"" ON$c_reset"
case off
set badge "$c_err""OFF $c_reset"
case '*'
set badge "$c_dim""DEFAULT$c_reset"
end
set -l curs " "
if test $i -eq $cur_row
set curs "$c_sel$c_reset "
end
set -l lpad (string pad -r -w $label_w -- (string sub -l $label_w -- $label))
set -l dpad (string pad -r -w $desc_w -- (string sub -l $desc_w -- $desc))
printf '%s│ %s%s [ %s ] %s │\n' $p $curs $lpad $badge $dpad
# `cut`: these labels and descriptions are static data from
# __config_settings_subcats, not authored per width tier the way
# __config_settings_draw's are, and several run well past the
# narrower tiers' fields. `string pad` only ever grows a string, so
# they must be truncated before padding or the box stops being
# rectangular. This is the divergence the DESCRIPTION block above
# documents -- do not "simplify" it to `pad`.
__config_settings_frame row $iw $p \
(__config_settings_frame cursor $i $cur_row) \
$label 13 \
(__config_settings_frame badge $val) \
$desc cut
end
printf '%s│%s│\n' $p $HBR
+15 -35
View File
@@ -60,21 +60,12 @@ function __config_settings_draw_value
set -l nrows (count $vars)
# ── Width tier (same thresholds as the toggle page) ───────────────────
set -l iw 50
if test "$COLUMNS" -ge 90
set iw 76
else if test "$COLUMNS" -ge 86
set iw 72
else if test "$COLUMNS" -ge 82
set iw 68
end
set -l iw (__config_settings_frame width)
set -l HBR (string repeat -n $iw '─')
set -l p (string repeat -n (math --scale=0 "max(0, ($COLUMNS - ($iw + 2)) / 2)") ' ')
# ── Line 1: top border with title ─────────────────────────────────────
set -l title_dashes (math $iw - (string length -- $title) - 3)
printf '%s┌─%s %s %s┐\n' \
$p $c_head "$title$c_reset" (string repeat -n $title_dashes '─')
__config_settings_frame title $iw $p "$c_head $title$c_reset "
# ── Line 2: page-tab header ───────────────────────────────────────────
printf '%s│%s│\n' $p (__config_settings_pagetab $active_idx $iw)
@@ -95,15 +86,9 @@ function __config_settings_draw_value
set -l field
if test $type = bool
# Booleans store true/false (sponge convention); unset = DEFAULT.
set -l val (__config_settings_get_raw $var)
switch $val
case true
set badge "$c_ok"" ON$c_reset"
case false
set badge "$c_err""OFF $c_reset"
case '*'
set badge "$c_dim""DEFAULT$c_reset"
end
# The frame is told this page's vocabulary rather than merging
# true/on: a hand-set "on" here must keep rendering DEFAULT.
set badge (__config_settings_frame badge (__config_settings_get_raw $var) true false)
set field "default: $hint"
else
set -l raw (__config_settings_get_raw $var)
@@ -139,23 +124,18 @@ function __config_settings_draw_value
set field "$shown"(set_color --reverse)" "(set_color normal)
end
set -l curs " "
if test $i -eq $cur_row
set curs "$c_sel$c_reset "
end
set -l fw (math $iw - 33)
set -l lpad (string pad -r -w 12 -- $label)
# The edit field is already length-constrained and contains a reverse
# caret; running it through `string shorten` miscounts the escapes, so
# pad it directly. Non-edit fields still shorten to add an ellipsis.
set -l fpad
# `shorten` ellipsises the value: unlike the toggle page's per-tier
# descriptions, these fields hold arbitrary user values. The edit row
# is the exception -- its field is already length-constrained above
# and carries a reverse-video caret whose escapes `string shorten`
# miscounts, so it pads directly.
set -l fit shorten
if test "$edit_mode" = edit -a $i -eq $cur_row
set fpad (string pad -r -w $fw -- "$field")
else
set fpad (string pad -r -w $fw -- (string shorten -m $fw -- "$field"))
set fit pad
end
printf '%s│ %s%s [ %s ] %s │\n' $p $curs $lpad $badge $fpad
__config_settings_frame row $iw $p \
(__config_settings_frame cursor $i $cur_row) \
$label 12 $badge $field $fit
end
# ── Pad blank rows so chrome(6) + nrows + blanks = 16 ─────────────────
+155
View File
@@ -0,0 +1,155 @@
# Copyright (C) 2026 Rootiest
# SPDX-License-Identifier: AGPL-3.0-or-later
# SYNOPSIS
# __config_settings_frame width
# __config_settings_frame title <iw> <p> <segment>
# __config_settings_frame badge <value> [<on_word> <off_word>]
# __config_settings_frame cursor <row> <cur_row>
# __config_settings_frame row <iw> <p> <curs> <label> <label_w> <badge> <field> <fit>
#
# DESCRIPTION
# The pieces of the config-settings panel that every page draws identically:
# the width tier, the title border, the ON/OFF/DEFAULT badge, the cursor cell
# and one table row. __config_settings_draw, __config_settings_draw_subcat
# and __config_settings_draw_value each keep their own sequence of lines and
# their own per-tier text; only these shared computations live here.
#
# Deliberately owns no page height. Every verb prints exactly one line or one
# fragment, so how tall a page is remains a property of its caller's line
# sequence: __config_settings_draw and __config_settings_draw_value are a
# fixed 16 lines, __config_settings_draw_subcat is 7 + <sub-category count>,
# and config-settings.fish's wrap-aware erase (\e[<N>A\e[J, sized from
# panel_h) depends on that difference surviving exactly as it is.
#
# Row geometry. A row is
# │ + 2 spaces + cursor(2) + label(label_w) + " [ " + badge(7) + " ] "
# + field(field_w) + 3 spaces + │
# which totals 23 + label_w + field_w and must equal iw + 2. So
# field_w = iw - 21 - label_w
# and that single sum reproduces both hand-maintained constants: label_w 12
# gives iw-33 (the toggle and value pages), label_w 13 gives iw-34 (the
# sub-category page, whose description field absorbs its wider label).
#
# Title geometry. "┌─" + segment + dashes + "┐" totals iw + 2, so
# dashes = iw - visible(segment) - 1
# The segment arrives already coloured because the three pages place their
# set_color reset at different byte offsets around the same visible text
# (__config_settings_draw resets after the trailing space, the other two
# before it). That difference is invisible on screen and visible to cmp, so
# each page keeps its own bytes while sharing the arithmetic; --visible
# discounts the escapes.
#
# ARGUMENTS
# width No arguments. Prints the inner width for the current $COLUMNS:
# >= 90 -> 76, >= 86 -> 72, >= 82 -> 68, otherwise 50.
# title iw, the centering prefix, and the pre-coloured title segment.
# badge The stored value, then optionally the words this page uses for true
# and false (default on/off; the value pages store true/false).
# Anything else renders DEFAULT.
# cursor This row's index and the highlighted row's index.
# row iw, centering prefix, cursor cell, label, label field width,
# rendered badge, field text, and the fit policy:
# pad pad label and field, never shrink either
# cut truncate both to their field width, then pad
# shorten pad the label, ellipsise the field with string shorten
#
# EXIT STATUS
# 0 Verb recognised
# 1 Unknown verb
#
# RETURNS
# The requested panel line or fragment, printed to stdout
#
# NOTES
# `string pad` only ever grows a string, never shrinks it. That is the whole
# reason `cut` and `shorten` exist: a caller whose text can exceed its field
# must shrink it first, or the box stops being rectangular.
#
# `pad` is not "the default when you don't care". __config_settings_draw's
# descriptions are authored per width tier to fit their field exactly -- at
# every tier the longest is precisely the field width -- so applying `cut`
# there would be a byte-for-byte no-op that silently discards that property
# and passes the render golden. Which policy a page uses is a real decision,
# named at each call site. See tests/config-settings-render.fish.
#
# EXAMPLE
# set -l iw (__config_settings_frame width)
# __config_settings_frame row $iw $p (__config_settings_frame cursor 0 0) \
# Aliases 12 (__config_settings_frame badge on) "cmd shadows" pad
function __config_settings_frame
switch $argv[1]
# ── Width tier: 6-col buffer per side before stepping up ──────────
case width
if test "$COLUMNS" -ge 90
echo 76
else if test "$COLUMNS" -ge 86
echo 72
else if test "$COLUMNS" -ge 82
echo 68
else
echo 50
end
# ── Title border: ┌─<segment><dashes>┐ ────────────────────────────
case title
set -l iw $argv[2]
set -l vis (string length --visible -- "$argv[4]")
set -l dashes (math "max(0, $iw - $vis - 1)")
printf '%s┌─%s%s┐\n' $argv[3] "$argv[4]" (string repeat -n $dashes '─')
# ── Badge: 7 visible columns, coloured ────────────────────────────
# The truthy/falsy words are arguments rather than one merged
# vocabulary: merging would make a hand-set "on" in a true/false
# variable render as ON where it renders DEFAULT today.
case badge
set -l on_word on
set -l off_word off
if test (count $argv) -ge 4
set on_word $argv[3]
set off_word $argv[4]
end
switch "$argv[2]"
case $on_word
printf '%s ON%s' (set_color green) (set_color normal)
case $off_word
printf '%sOFF %s' (set_color red) (set_color normal)
case '*'
printf '%sDEFAULT%s' (set_color brblack) (set_color normal)
end
# ── Cursor cell: 2 visible columns ────────────────────────────────
case cursor
if test $argv[2] -eq $argv[3]
printf '%s▶%s ' (set_color --bold magenta) (set_color normal)
else
printf ' '
end
# ── One table row ─────────────────────────────────────────────────
case row
set -l iw $argv[2]
set -l p $argv[3]
set -l curs $argv[4]
set -l label "$argv[5]"
set -l label_w $argv[6]
set -l badge $argv[7]
set -l field "$argv[8]"
set -l fit $argv[9]
set -l field_w (math $iw - 21 - $label_w)
switch $fit
case cut
set label (string sub -l $label_w -- "$label")
set field (string sub -l $field_w -- "$field")
case shorten
set field (string shorten -m $field_w -- "$field")
end
printf '%s│ %s%s [ %s ] %s │\n' $p $curs \
(string pad -r -w $label_w -- "$label") $badge \
(string pad -r -w $field_w -- "$field")
case '*'
echo "__config_settings_frame: unknown verb '$argv[1]'" >&2
return 1
end
end
+62 -28
View File
@@ -140,7 +140,8 @@ function config-settings --description 'Interactive TUI for managing fish config
set -l cur_page 0 # 0=Universal 1=Session 2=Sponge 3=Paths
set -l cur_row 0
set -l panel_h 16
set -l panel_h 0 # real value set by the first dispatch call below
set -l new_frame # captured by __cs_dispatch_draw
set -l last_cols $COLUMNS
# ── Terminal setup ────────────────────────────────────
@@ -148,37 +149,34 @@ function config-settings --description 'Interactive TUI for managing fish config
trap 'printf "\e[?25h"; set -g __config_settings_exit 1' INT
# ── Draw dispatch (page 0/1 = toggle table; 2/3 = value page) ─────────
# Records the actual line count of whatever it just drew into panel_h,
# so every erase (redraw loop, inline editor, final cleanup) matches
# reality -- the sub-category page is n+7 lines (2-6 sub-categories:
# 9-13 lines), never the category list's fixed 16.
# Captures the page's rendered lines into new_frame and derives panel_h
# from their count. panel_h is never hand-set again: the sub-category
# page is n+7 lines (2-6 sub-categories: 9-13 lines), never the
# category list's fixed 16, and deriving it from the real output means
# that fact can no longer drift out of sync with what got drawn.
function __cs_dispatch_draw --no-scope-shadowing
switch $cur_page
case 0
if test $in_subcat -eq 1
__config_settings_draw_subcat $subcat_row universal $toggle_vars[(math $cur_row + 1)]
set panel_h (math 7 + (count (__config_settings_subcats $toggle_vars[(math $cur_row + 1)])))
set new_frame (__config_settings_draw_subcat $subcat_row universal $toggle_vars[(math $cur_row + 1)])
else
__config_settings_draw $cur_row universal $toggle_vars
set panel_h 16
set new_frame (__config_settings_draw $cur_row universal $toggle_vars)
end
case 1
if test $in_subcat -eq 1
__config_settings_draw_subcat $subcat_row session $toggle_vars[(math $cur_row + 1)]
set panel_h (math 7 + (count (__config_settings_subcats $toggle_vars[(math $cur_row + 1)])))
set new_frame (__config_settings_draw_subcat $subcat_row session $toggle_vars[(math $cur_row + 1)])
else
__config_settings_draw $cur_row session $toggle_vars
set panel_h 16
set new_frame (__config_settings_draw $cur_row session $toggle_vars)
end
case 2
__config_settings_draw_value $cur_row sponge
set panel_h 16
set new_frame (__config_settings_draw_value $cur_row sponge)
case 3
__config_settings_draw_value $cur_row paths
set panel_h 16
set new_frame (__config_settings_draw_value $cur_row paths)
end
set panel_h (count $new_frame)
end
__cs_dispatch_draw
printf '%s\n' $new_frame
# ── Event loop ────────────────────────────────────────
# __config_settings_read_key reads a single keypress from /dev/tty in raw
@@ -337,13 +335,16 @@ function config-settings --description 'Interactive TUI for managing fish config
set -l buf (__config_settings_get_raw $varname)
test "$buf" = DEFAULT; and set buf ""
set -l committed 0
while true
# Full erase once to enter edit mode; per-keystroke
# redraws below diff against the previous edit frame.
set -l edit_frame (__config_settings_draw_value $cur_row $page edit "$buf")
set -l prev_edit_frame
set -l pml (math --scale=0 "($last_cols + 78) / 2")
set -l eh (math --scale=0 "$panel_h * max(1, ceil($pml / $COLUMNS))")
printf '\e[%dA\e[J' $eh
printf '%s\n' $edit_frame
set last_cols $COLUMNS
__config_settings_draw_value $cur_row $page edit "$buf"
while true
set -l ek (__config_settings_read_key)
or break
switch $ek
@@ -361,6 +362,23 @@ function config-settings --description 'Interactive TUI for managing fish config
case '*'
set buf "$buf$ek"
end
set prev_edit_frame $edit_frame
set edit_frame (__config_settings_draw_value $cur_row $page edit "$buf")
if test (count $edit_frame) -eq (count $prev_edit_frame) -a "$COLUMNS" = "$last_cols" -a $COLUMNS -ge 52
# `| string collect` is required on each join --
# see the identical note in the main loop's
# diff-path call.
printf '\e[%dA' (count $edit_frame)
__config_settings_diff_redraw (string join \n -- $prev_edit_frame | string collect) (string join \n -- $edit_frame | string collect)
else
set -l ph (count $prev_edit_frame)
set -l pml (math --scale=0 "($last_cols + 78) / 2")
set -l eh (math --scale=0 "$ph * max(1, ceil($pml / $COLUMNS))")
printf '\e[%dA\e[J' $eh
printf '%s\n' $edit_frame
end
set last_cols $COLUMNS
end
if test $committed -eq 1
# Empty buffer reverts to the row default (a value, or
@@ -377,6 +395,7 @@ function config-settings --description 'Interactive TUI for managing fish config
printf '\e[%dA\e[J' $eh
set last_cols $COLUMNS
__cs_dispatch_draw
printf '%s\n' $new_frame
set did_redraw 1
end
end
@@ -400,15 +419,30 @@ function config-settings --description 'Interactive TUI for managing fish config
continue
end
# Wrap-aware erase: a panel drawn on a wider terminal has longer lines
# (due to center-padding) that wrap into extra physical rows when the
# terminal narrows. 78 = widest box (IW=76+2); the formula gives the
# worst-case old line width for any tier drawn at last_cols.
set -l prev_max_lw (math --scale=0 "($last_cols + 78) / 2")
set -l erase_h (math --scale=0 "$panel_h * max(1, ceil($prev_max_lw / $COLUMNS))")
printf '\e[%dA\e[J' $erase_h
set last_cols $COLUMNS
set -l old_h $panel_h
set -l prev_frame $new_frame
__cs_dispatch_draw
if test $panel_h -eq $old_h -a "$COLUMNS" = "$last_cols" -a $COLUMNS -ge 52
# Diff path: geometry and width unchanged since the last frame --
# move up without erasing, rewrite only the lines that changed.
# `| string collect` is required on each join: command
# substitution always re-splits on newlines, so without it
# __config_settings_diff_redraw would receive many positional
# arguments instead of the two joined strings it expects.
printf '\e[%dA' $panel_h
__config_settings_diff_redraw (string join \n -- $prev_frame | string collect) (string join \n -- $new_frame | string collect)
else
# Full-redraw path: resize, page switch, or subcat enter/exit --
# same wrap-aware erase math as before, unchanged. 78 = widest
# box (IW=76+2); the formula gives the worst-case old line width
# for any tier drawn at last_cols.
set -l prev_max_lw (math --scale=0 "($last_cols + 78) / 2")
set -l erase_h (math --scale=0 "$old_h * max(1, ceil($prev_max_lw / $COLUMNS))")
printf '\e[%dA\e[J' $erase_h
printf '%s\n' $new_frame
end
set last_cols $COLUMNS
end
# ── Cleanup ───────────────────────────────────────────
File diff suppressed because it is too large Load Diff
+285
View File
@@ -0,0 +1,285 @@
#!/usr/bin/env fish
# Copyright (C) 2026 Rootiest
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Golden-output harness for the config-settings TUI renderer.
#
# fish tests/config-settings-render.fish compare against the golden
# fish tests/config-settings-render.fish --update rewrite the golden
#
# __config_settings_draw, __config_settings_draw_subcat and
# __config_settings_draw_value are hand-tuned layout code: field widths, dash
# counts and pad targets are arithmetic on the width tier, verified by eye once
# and never since. Any refactor of them has to be byte-identical, so this
# renders every page at every width tier, in both scopes, with the cursor on
# every row, and byte-compares the result against a committed baseline.
#
# The golden holds RAW output: the set_color escapes and box drawing exactly as
# the draw functions emit them, plus the \e[<N>A\e[J erase config-settings.fish
# would emit for that panel. Nothing is normalized, folded or pretty-printed --
# the gate is cmp(1) over the bytes, and a one-space change anywhere fails it.
# `diff` is only used to *display* a failure, through cat -v.
#
# Everything runs inside a throwaway HOME/XDG_CONFIG_HOME sandbox. The fixtures
# have to be real universal variables (the Universal page reads universal scope
# through `set --show`), and this repo doubles as a live ~/.config/fish whose
# fish_variables must never be touched by a test run. `fish --no-config` is not
# an option here: under -N, `set -U` silently degrades to global scope, which
# would render the Universal page as all-DEFAULT and prove nothing.
#
# Every external utility below is called through `command`. The outer pass runs
# under the user's real config, which shadows rm (-> trash), cat (-> bat) and
# mkdir, and aliases cp to `cp -i` -- a bare `cp` over an existing golden would
# sit waiting for a confirmation that never comes.
set -l self (command realpath (status filename))
set -l repo (command realpath (command dirname $self)/..)
set -l golden $repo/tests/golden/config-settings-render.txt
# ╭──────────────────────────────────────────────────────────────────────────╮
# │ Outer pass: sandbox, run the render, compare or update │
# ╰──────────────────────────────────────────────────────────────────────────╯
if not set -q CS_RENDER_OUT
set -l sandbox (command mktemp -d)
command mkdir -p $sandbox/home $sandbox/xdg/fish
set -l out (command mktemp)
set -l errf (command mktemp)
# TERM is pinned so set_color emits a fixed sequence set regardless of the
# terminal this runs from; COLUMNS is set per case inside the render pass.
command env -i \
HOME=$sandbox/home \
XDG_CONFIG_HOME=$sandbox/xdg \
PATH="$PATH" \
TERM=xterm-256color \
CS_RENDER_OUT=$out \
fish $self >/dev/null 2>$errf
set -l render_status $status
command rm -rf $sandbox
if test $render_status -ne 0
echo " FAIL render pass exited $render_status"
command cat $errf >&2
command rm -f $out $errf
exit 1
end
set -l cases (command grep -c '^### ' $out)
if contains -- --update $argv
command mkdir -p (command dirname $golden)
command cp $out $golden
echo "golden updated: $cases cases, "(command wc -l <$golden | string trim)" lines, "(command wc -c <$golden | string trim)" bytes"
command rm -f $out $errf
exit 0
end
if not test -f $golden
echo " FAIL no golden at $golden -- run with --update to create it"
command rm -f $out $errf
exit 1
end
if command cmp -s $out $golden
echo "$cases/$cases config-settings render cases byte-identical"
command rm -f $out $errf
exit 0
end
echo " FAIL rendering differs from $golden"
# cat -v only to make the escapes readable in the report; the gate above is
# a raw byte compare, never this.
command diff (command cat -v $golden | psub) (command cat -v $out | psub) | command head -40
command rm -f $out $errf
exit 1
end
# ╭──────────────────────────────────────────────────────────────────────────╮
# │ Render pass (inside the sandbox) │
# ╰──────────────────────────────────────────────────────────────────────────╯
# Sourced, not autoloaded: XDG_CONFIG_HOME points at the empty sandbox. Only
# the draw path is ever called -- __config_settings_apply and
# __config_settings_set_value are defined here and never invoked.
# __fish_palette is sourced too: the draw functions call it (instead of
# declaring $c_head/$c_dim/etc. inline) and it doesn't match the
# __config_settings_* glob, so without this line every draw function's
# colors silently unset and the golden loses all its escape sequences.
source $repo/functions/__fish_palette.fish
for f in $repo/functions/__config_settings_*.fish
source $f
end
set -l toggle_vars \
__fish_config_op_aliases \
__fish_config_op_autoexec \
__fish_config_op_overrides \
__fish_config_op_integrations \
__fish_config_op_logging \
__fish_config_op_greeting \
__fish_config_opinionated
set -l categories $toggle_vars[1..6]
# ── Fixtures ──────────────────────────────────────────────────────────────
# Chosen so every badge branch is live somewhere in the golden: an explicit
# truthy (ON), an explicit falsy (OFF), and an unset variable (DEFAULT). The
# session values deliberately differ from the universal ones so the two pages
# cannot render identically by accident.
set -U __fish_config_op_aliases on
set -U __fish_config_op_autoexec off
set -U __fish_config_op_integrations on
set -U __fish_config_op_logging off
set -U __fish_config_opinionated off
# __fish_config_op_overrides, __fish_config_op_greeting: unset -> DEFAULT
set -g __fish_config_op_aliases off
set -g __fish_config_op_overrides on
set -g __fish_config_op_greeting off
# Sub-category fixtures. "Notifications" is the 13-char label the subcat page's
# label field was widened for; multiplexer-capture exercises the slug's '-'->'_'
# rewrite into a variable name.
set -U __fish_config_op_aliases_filesystem on
set -U __fish_config_op_aliases_search off
set -U __fish_config_op_integrations_notifications on
set -U __fish_config_op_logging_multiplexer_capture off
set -g __fish_config_op_aliases_search on
# Value-page fixtures: one per type badge (INT / LIST / PATH) plus unset rows
# for DEFAULT, plus a value long enough to force `string shorten`'s ellipsis at
# every tier.
set -U sponge_delay 5
set -U sponge_purge_only_on_exit true
set -U sponge_allow_previously_successful false
set -U __fish_sponge_extra_sensitive KOPIA_PASSWORD MY_CORP_AUTH
# sponge_successful_exit_codes: unset -> DEFAULT
set -U __fish_scrollback_history_dir /home/tester/very/long/scrollback/history/directory
set -U __fish_user_dots_path /home/tester/.config/.user-dots/fish
set -U __fish_user_dots_symlink false
# __fish_scrollback_history_max_files: unset -> DEFAULT
# ── Case emitter ──────────────────────────────────────────────────────────
# panel_h is passed in by the caller, independently declaring the expected
# height for each page so it can be cross-checked against the draw functions'
# real output: the category list and both value pages are a fixed 16 lines, a
# sub-category page is 7 + <sub-category count>. The golden records the
# declared height, the measured line count, and the erase sequence derived
# from the declared height -- so flattening the fixed/dynamic divergence, or
# changing a page's height at all, breaks the compare three ways.
function _cs_render_case --argument-names label panel_h
set -l cmd $argv[3..]
set -l t (command mktemp)
$cmd >$t
set -l lines (command wc -l <$t | string trim)
# Wrap-aware erase, byte-for-byte what config-settings.fish emits for a
# panel of this height at this width in the steady state (last_cols ==
# COLUMNS). 78 is the widest box (IW=76 + 2 borders).
set -l pml (math --scale=0 "($COLUMNS + 78) / 2")
set -l eh (math --scale=0 "$panel_h * max(1, ceil($pml / $COLUMNS))")
printf '### %s COLUMNS=%d PANEL_H=%d LINES=%d ERASE=' $label $COLUMNS $panel_h $lines
printf '\e[%dA\e[J' $eh
printf '\n'
command cat $t
command rm -f $t
end
# ── Frame-verb fragments ──────────────────────────────────────────────────
# The shared computations pinned directly, not only through the pages that use
# them. <END> marks the end of each fragment so trailing padding -- which is
# the entire point of a 7-column badge or a 2-column cursor cell -- shows up in
# a diff instead of being invisible whitespace.
function _cs_frame_case --argument-names label
printf '### frame %s\n' $label
$argv[2..]
printf '<END>\n'
end
# ── Cases ─────────────────────────────────────────────────────────────────
# 100 -> IW 76, 88 -> IW 72, 84 -> IW 68, 70 -> IW 50: one COLUMNS value per
# width tier, each a few columns above its threshold.
begin
for cols in 100 88 84 70
set -g COLUMNS $cols
for scope in universal session
for row in (seq 0 6)
_cs_render_case "toggle scope=$scope row=$row" 16 \
__config_settings_draw $row $scope $toggle_vars
end
end
for category in $categories
set -l n (count (__config_settings_subcats $category))
for scope in universal session
for row in (seq 0 $n)
_cs_render_case "subcat cat=$category scope=$scope row=$row" (math 7 + $n) \
__config_settings_draw_subcat $row $scope $category
end
end
end
for row in (seq 0 4)
_cs_render_case "value page=sponge row=$row" 16 \
__config_settings_draw_value $row sponge
end
for row in (seq 0 3)
_cs_render_case "value page=paths row=$row" 16 \
__config_settings_draw_value $row paths
end
# Inline editor: only reachable on non-bool rows. Short buffer, a
# buffer long enough to tail-anchor against the caret, and an empty one.
_cs_render_case "edit page=sponge row=0 buf=short" 16 \
__config_settings_draw_value 0 sponge edit 12
_cs_render_case "edit page=sponge row=4 buf=long" 16 \
__config_settings_draw_value 4 sponge edit "KOPIA_PASSWORD MY_CORP_AUTH ANOTHER_SECRET_NAME"
_cs_render_case "edit page=paths row=0 buf=long" 16 \
__config_settings_draw_value 0 paths edit /home/tester/very/long/scrollback/history/directory
_cs_render_case "edit page=paths row=2 buf=empty" 16 \
__config_settings_draw_value 2 paths edit ""
end
# Emitted last on purpose: everything above is page output, so the page
# section's byte offsets never move when this section grows.
for cols in 100 88 84 70
set -g COLUMNS $cols
_cs_frame_case "width cols=$cols" __config_settings_frame width
end
set -g COLUMNS 100
set -l head (set_color --bold cyan)
set -l rst (set_color normal)
set -l p_test (string repeat -n 11 ' ')
for v in on off DEFAULT ''
_cs_frame_case "badge onoff val=$v" __config_settings_frame badge $v
end
for v in true false DEFAULT
_cs_frame_case "badge boolean val=$v" __config_settings_frame badge $v true false
end
_cs_frame_case "cursor hit" __config_settings_frame cursor 3 3
_cs_frame_case "cursor miss" __config_settings_frame cursor 3 4
_cs_frame_case "title toggle-page" __config_settings_frame title 76 $p_test \
"$head Opinionated Settings $rst"
_cs_frame_case "title subcat-page" __config_settings_frame title 76 $p_test \
"$head Sub-categories: aliases (Universal)$rst "
_cs_frame_case "title value-page" __config_settings_frame title 76 $p_test \
"$head Sponge Settings$rst "
set -l badge_on (__config_settings_frame badge on)
_cs_frame_case "row pad lw=12" __config_settings_frame row 76 $p_test \
(__config_settings_frame cursor 0 0) Aliases 12 $badge_on "cmd shadows" pad
_cs_frame_case "row cut lw=13" __config_settings_frame row 76 $p_test \
(__config_settings_frame cursor 0 1) Notifications 13 $badge_on "done, WakaTime hook" cut
_cs_frame_case "row cut lw=13 overlong" __config_settings_frame row 50 $p_test \
(__config_settings_frame cursor 0 0) Notifications 13 $badge_on \
"ls, cat, cd, du, mkdir, rm, mv, zoxide" cut
_cs_frame_case "row shorten lw=12" __config_settings_frame row 76 $p_test \
(__config_settings_frame cursor 0 0) "Log dir" 12 $badge_on \
/home/tester/very/long/scrollback/history/directory shorten
_cs_frame_case "row shorten lw=12 empty" __config_settings_frame row 50 $p_test \
(__config_settings_frame cursor 1 0) "Log max" 12 $badge_on "" shorten
end >$CS_RENDER_OUT
+54
View File
@@ -163,3 +163,57 @@ function test_functions_keep_their_palette
return 0
end
check "colored --help output keeps its escape sequences" true (test_functions_keep_their_palette; and echo true; or echo false)
section "session: config-settings diff redraw"
# Locks in the invariant the diff-redraw renderer depends on: each draw
# function's real line count must match the height config-settings.fish's
# dispatch derives from it (count $new_frame) -- see
# __cs_dispatch_draw in functions/config-settings.fish.
function test_draw_line_count_matches_panel_h
set -l toggle_vars \
__fish_config_op_aliases __fish_config_op_autoexec \
__fish_config_op_overrides __fish_config_op_integrations \
__fish_config_op_logging __fish_config_op_greeting \
__fish_config_opinionated
set -l lines (__config_settings_draw 0 universal $toggle_vars)
if test (count $lines) -ne 16
echo " __config_settings_draw: expected 16 lines, got "(count $lines)
return 1
end
set -l vlines (__config_settings_draw_value 0 sponge)
if test (count $vlines) -ne 16
echo " __config_settings_draw_value: expected 16 lines, got "(count $vlines)
return 1
end
set -l n (count (__config_settings_subcats __fish_config_op_aliases))
set -l slines (__config_settings_draw_subcat 0 universal __fish_config_op_aliases)
set -l want (math 7 + $n)
if test (count $slines) -ne $want
echo " __config_settings_draw_subcat: expected $want lines, got "(count $slines)
return 1
end
return 0
end
check "draw functions' line counts match their panel heights" true (test_draw_line_count_matches_panel_h; and echo true; or echo false)
function test_diff_redraw_unchanged_lines_are_bare_newlines
functions -q __config_settings_diff_redraw; or return 1
set -l old (string join \n -- AAA BBB CCC | string collect)
set -l new (string join \n -- AAA BBB CCC | string collect)
set -l out (__config_settings_diff_redraw "$old" "$new" | string collect -N)
test "$out" = \n\n\n
end
check "diff_redraw: unchanged lines are bare newlines" true (test_diff_redraw_unchanged_lines_are_bare_newlines; and echo true; or echo false)
function test_diff_redraw_changed_line_is_cleared_and_rewritten
functions -q __config_settings_diff_redraw; or return 1
set -l old (string join \n -- AAA BBB CCC | string collect)
set -l new (string join \n -- AAA XYZ CCC | string collect)
set -l out (__config_settings_diff_redraw "$old" "$new" | string collect -N)
test "$out" = \n\e\[2K\rXYZ\n\n
end
check "diff_redraw: a changed line is cleared and rewritten" true (test_diff_redraw_changed_line_is_cleared_and_rewritten; and echo true; or echo false)