feat(@scripts): improve resume picker logic

Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
This commit is contained in:
Natalie 2026-05-17 04:20:17 -07:00
parent 0aee09f274
commit 57a428cee6

View file

@ -31,9 +31,8 @@
# rclaude <host> <dir> # remote (or local) at <dir>
# rclaude list # tmux + per-project disk view
# rclaude list sessions # tmux + per-session disk view (uuid + snippet)
# rclaude resume [pattern] # reattach / resume by uuid prefix, snippet,
# # tmux name, or cwd substring (interactive
# # picker on >1 match)
# rclaude resume # picker: live tmux + most-recent disk (--- separator)
# rclaude resume <pattern> # search tmux + disk sessions (uuid, snippet, cwd)
#
# Mirror semantics: if local $PWD is $HOME/X/Y, the remote dir defaults to
# ~/X/Y on the remote (the remote's $HOME, not $HOME from this machine).
@ -241,11 +240,33 @@ cmd_list() {
# - matches a snippet/cwd → same as session (the row identifies a UUID)
#
# Pattern matching is case-insensitive substring across host/kind/uuid/snippet/cwd.
# An empty pattern lists everything (interactive picker).
# No pattern: live tmux sessions on top, then most-recent disk sessions across
# hosts (separated by ---). Picker label space caps the combined view at 35.
cmd_resume() {
_pattern=${1:-}
_matches=$(scan_hosts | while IFS= read -r h; do list_search_on "$h"; done)
if [ -n "$_pattern" ]; then
case $_pattern in
--all|-a) _pattern="" ;;
esac
if [ -z "$_pattern" ]; then
_tmux=$(scan_hosts | while IFS= read -r h; do list_tmux_on "$h"; done)
_disk=$(scan_hosts | while IFS= read -r h; do list_sessions_on "$h"; done)
_t_count=0; _d_total=0
[ -n "$_tmux" ] && _t_count=$(printf '%s\n' "$_tmux" | wc -l | tr -d ' ')
[ -n "$_disk" ] && _d_total=$(printf '%s\n' "$_disk" | wc -l | tr -d ' ')
_d_room=$((35 - _t_count))
[ "$_d_room" -lt 0 ] && _d_room=0
if [ "$_d_room" -gt 0 ] && [ -n "$_disk" ]; then
_disk_slice=$(printf '%s\n' "$_disk" | head -n "$_d_room")
else
_disk_slice=""
fi
if [ -n "$_tmux" ] && [ -n "$_disk_slice" ]; then
_matches=$(printf '%s\n%s' "$_tmux" "$_disk_slice")
else
_matches=${_tmux:-$_disk_slice}
fi
else
_matches=$(scan_hosts | while IFS= read -r h; do list_search_on "$h"; done)
_matches=$(printf '%s\n' "$_matches" | grep -F -i -- "$_pattern" || true)
fi
_count=0
@ -256,9 +277,11 @@ cmd_resume() {
fi
if [ "$_count" -gt 1 ]; then
_keys="123456789abcdefghijklmnopqrstuvwxyz"
# Pattern-search truncation (no-pattern case is already capped above).
if [ "$_count" -gt 35 ]; then
echo "too many matches ($_count); refine pattern" >&2
exit 1
_matches=$(printf '%s\n' "$_matches" | head -n 35)
_count=35
printf 'rclaude: %s matches; showing 35 most recent\n' "$_count" >&2
fi
# For sessions, display the human-readable snippet (col 4) rather
# than the bare UUID (col 3); for tmux/disk the existing col 3 is
@ -270,12 +293,22 @@ cmd_resume() {
exit 1
fi
_i=0
_prev_kind=""
printf '%s\n' "$_matches" | while IFS= read -r _line; do
_i=$((_i + 1))
_kind_now=$(printf %s "$_line" | awk -F'\t' '{print $2}')
if [ "$_prev_kind" = "tmux" ] && [ "$_kind_now" != "tmux" ]; then
printf ' ---\n' >&2
fi
_k=$(printf %s "$_keys" | cut -c"$_i")
printf ' [%s] %s\n' "$_k" \
"$(printf %s "$_line" | awk -F'\t' "$_fmt_row")" >&2
_prev_kind=$_kind_now
done
if [ -z "$_pattern" ] && [ "${_d_total:-0}" -gt "${_d_room:-0}" ]; then
printf ' (showing %s most recent of %s disk sessions; pass a pattern to search older)\n' \
"$_d_room" "$_d_total" >&2
fi
_last_key=$(printf %s "$_keys" | cut -c"$_count")
printf 'select [1-%s]: ' "$_last_key" >&2
_old=$(stty -g 2>/dev/null || true)