New Phase 1b in tests/run-tests.fish: catches a bare C1-shadowed-command call in a functions/*.fish body with no matching uses-shadow(name) or self-limiting(name) in that function's own CLASSIFICATION header. This is exactly the check discussed after the rm and cd audits -- runtime auto-unwrapping isn't viable in fish (there's no hook finer than shadowing itself, and rewriting behavior invisibly at runtime is its own footgun); a static lint using the CLASSIFICATION tag as the declared-intentional marker is. Scoped to functions/*.fish only: the one-function-per-file convention there makes body extraction exact with no block-depth parser needed. Added a new self-limiting(name) tag to the schema for the case a bare call is safe not because the caller did anything, but because the shadow's own logic already neutralizes the override: rm's and mkdir's flag checks (verified precisely -- rm falls back to command rm for any flag except a bare -r/-R/--recursive alone, which still routes to trash; mkdir falls back to command mkdir -p for any flag, no exception), and grep/fgrep/egrep/dir/vdir/cat's own tty auto-detection (--color=auto, and bat's default color behavior -- verified byte-identical to stock cat when piped, since bat also auto-disables highlighting on a non-terminal). Explicit and durable rather than a silent lint exemption: if a shadow's bypass condition is ever weakened, every self-limiting site is one grep away instead of silently wrong. Running the first draft of the lint surfaced three more real bugs, none previously audited: - config-help.fish's --man pager path checks `type -q less` (proving it wants the real less binary specifically, for less-only -R/+N flag syntax) then called it bare, routing through our own $PAGER -> ov -> less -> more -> cat fallback chain instead -- which could hand those less-specific flags to a completely different program. Now command less. - _fish_deps_install.fish and _fish_deps_update.fish's binary-upgrade paths cp a freshly downloaded binary over an already-installed one with no existence guard -- the update flow's target is guaranteed to already exist. Our cp shadow forces -i unconditionally (a plain alias, not flag-aware like rm's), so this would hang waiting on a confirmation prompt in any non-interactive run. Now command cp. Same two files' lazydocker install path piped curl output into bare bash, invoking our shell-switch wrapper instead of a plain subshell. Now command bash. - agents-init.fish's AGENTS.md/CLAUDE.md relocation calls mv bare in four places; each is already guarded by a preceding test -f check on the destination, so the -i alias was unlikely to ever fire in practice, but explicit command mv removes the reliance on that guard entirely rather than leaving it as the only thing standing between a file move and an unattended hang. The remaining ~65 flagged call sites across ~24 files were reviewed individually and tagged self-limiting(rm)/self-limiting(mkdir) (verified flagged with -f/-rf or -p) and self-limiting(grep)/ self-limiting(cat) (verified piped, captured, or -q/-c; none display color to a human), plus uses-shadow(ls) for two existence-check-only calls (cffetch.fish, ffetch.fish) whose output is redirected to /dev/null.
214 lines
8.2 KiB
Fish
214 lines
8.2 KiB
Fish
# Copyright (C) 2026 Rootiest
|
|
# SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
# CATEGORY
|
|
# 11-pager-and-logging
|
|
#
|
|
# COMPONENT
|
|
# integrations/history-logs
|
|
#
|
|
# CLASSIFICATION
|
|
# bypasses-shadow(cat), self-limiting(rm), network
|
|
#
|
|
# SYNOPSIS
|
|
# logs [-h] [-c <category>]
|
|
#
|
|
# DESCRIPTION
|
|
# Interactively browses terminal log files (scrollback, paru, yay) sorted
|
|
# newest-first using fzf. Supports viewing in $PAGER, editing, and deletion.
|
|
#
|
|
# Keybindings inside the fzf browser:
|
|
# Enter Open in $PAGER
|
|
# Ctrl+E Open in $EDITOR
|
|
# Ctrl+D Delete (with confirmation)
|
|
# ? Toggle keybind help overlay
|
|
#
|
|
# Paru and yay logs open in ov with syntax highlighting and sticky section
|
|
# headers. Scrollback logs open in ov with per-command sticky prompt headers
|
|
# based on OSC 133 markers.
|
|
#
|
|
# ARGUMENTS
|
|
# -h, --help Show help message
|
|
# -c, --category cat Filter to one category: scrollback, paru, or yay
|
|
#
|
|
# EXIT STATUS
|
|
# 0 File viewed or no file selected
|
|
# 1 No log files found
|
|
#
|
|
# EXAMPLE
|
|
# logs -c paru
|
|
# logs
|
|
# logs -c scrollback
|
|
function logs --description 'Browse terminal log files interactively with fzf'
|
|
# Opinionated guard (C4): integrations disabled
|
|
if not __fish_config_op_enabled (status current-function)
|
|
__fish_palette
|
|
echo "$c_err"'logs: disabled by __fish_config_op_integrations'"$c_reset" >&2
|
|
return 1
|
|
end
|
|
|
|
set -l options h/help c/category=
|
|
argparse $options -- $argv
|
|
or return 1
|
|
|
|
if set -q _flag_help
|
|
__fish_palette
|
|
echo "$c_head""Usage:$c_reset "$c_cmd"logs"$c_reset" ["$c_arg"OPTIONS"$c_reset"]"
|
|
echo ""
|
|
echo "Browse and open terminal log files interactively."
|
|
echo "Logs sorted newest-first. Type to fuzzy-filter by date or category."
|
|
echo ""
|
|
echo "$c_head""Options:$c_reset"
|
|
echo " "$c_flag"-h, --help"$c_reset" Show this help"
|
|
echo " "$c_flag"-c, --category"$c_reset" Limit to one category: scrollback, paru, yay"
|
|
echo ""
|
|
echo "$c_head""Keys in fzf:$c_reset"
|
|
echo " "$c_arg"Enter"$c_reset" Open in \$PAGER"
|
|
echo " "$c_arg"Ctrl-E"$c_reset" Open in \$EDITOR"
|
|
echo " "$c_arg"Ctrl-D"$c_reset" Delete selected log"
|
|
echo " "$c_arg"Ctrl-C"$c_reset" Quit"
|
|
return 0
|
|
end
|
|
|
|
set -l log_dir (set -q SCROLLBACK_HISTORY_DIR; and echo $SCROLLBACK_HISTORY_DIR; or echo "$HOME/.terminal_history")
|
|
set -l editor (set -q EDITOR; and echo $EDITOR; or echo nvim)
|
|
|
|
# Scan primary dir and the legacy kitty dir (deduped)
|
|
set -l scan_dirs $log_dir
|
|
set -l legacy_dir "$HOME/.terminal_history"
|
|
if test -d $legacy_dir; and not contains $legacy_dir $scan_dirs
|
|
set -a scan_dirs $legacy_dir
|
|
end
|
|
|
|
# Strip junk scrollback logs before building the file list
|
|
for dir in $scan_dirs
|
|
_scrollback_prune_junk $dir
|
|
end
|
|
|
|
# Collect entries: "SORTKEY|FILEPATH|CATEGORY"
|
|
set -l entries
|
|
for dir in $scan_dirs
|
|
test -d $dir || continue
|
|
for f in $dir/*.log $dir/*.txt
|
|
test -f $f || continue
|
|
set -l base (string replace -r '\.(log|txt)$' '' (basename $f))
|
|
set -l ts (string match -r '[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}-[0-9]{2}-[0-9]{2}' $base)
|
|
test -n "$ts" || continue
|
|
set -l cat (string replace "_$ts" '' $base)
|
|
if set -q _flag_category; and test $cat != $_flag_category
|
|
continue
|
|
end
|
|
set -a entries "$ts|$f|$cat"
|
|
end
|
|
end
|
|
|
|
if test (count $entries) -eq 0
|
|
echo "No log files found"
|
|
return 1
|
|
end
|
|
|
|
# Sort newest-first (ISO timestamp prefix is lexicographically correct)
|
|
set -l sorted (printf '%s\n' $entries | sort -r)
|
|
|
|
# Build fzf input lines: "FILEPATH<TAB>DATETIME [CATEGORY]"
|
|
# Field 1 (hidden via --with-nth 2) = filepath for use in {1} placeholders
|
|
set -l fzf_lines
|
|
for entry in $sorted
|
|
set -l parts (string split '|' $entry)
|
|
set -l ts_parts (string split '_' $parts[1])
|
|
set -l date $ts_parts[1]
|
|
set -l time (string replace -ra '-' ':' $ts_parts[2])
|
|
|
|
# Declare before switch so the variable survives past the block
|
|
set -l label "[$parts[3]]"
|
|
switch $parts[3]
|
|
case scrollback
|
|
set label (printf '\033[36m[scrollback]\033[0m')
|
|
case paru
|
|
set label (printf '\033[32m[paru ]\033[0m')
|
|
case yay
|
|
set label (printf '\033[33m[yay ]\033[0m')
|
|
end
|
|
|
|
set -a fzf_lines (printf '%s\t%s %s %s' $parts[2] $date $time $label)
|
|
end
|
|
|
|
set -l tmpfile (mktemp)
|
|
set -l helpfile (mktemp)
|
|
set -l togglescript (mktemp)
|
|
set -l deletescript (mktemp)
|
|
set -l helpflag "$tmpfile.help"
|
|
printf '%s\n' $fzf_lines >$tmpfile
|
|
printf 'Terminal Logs — Key Bindings\n\n Enter View selected log in pager\n Ctrl-E Edit selected log in editor\n Ctrl-D Delete selected log\n Ctrl-C Quit\n ? Toggle this help\n\nFiltering:\n Type to fuzzy-filter by date or category\n Use -c flag to limit: scrollback, paru, yay' >$helpfile
|
|
# Help toggle script — avoids nested parens inside transform()
|
|
printf '#!/bin/sh\nif test -f %s; then\n rm -f %s\n printf "change-preview(command cat {1})"\nelse\n touch %s\n printf "change-preview(command cat %s)"\nfi\n' \
|
|
$helpflag $helpflag $helpflag $helpfile >$togglescript
|
|
chmod +x $togglescript
|
|
# Delete confirmation script — avoids nested parens inside execute(), and ensures
|
|
# tmpfile is updated before +reload fires (execute is synchronous, execute-silent is not)
|
|
printf '%s\n' \
|
|
'#!/bin/sh' \
|
|
'FILE="$1"' \
|
|
'printf "Delete %s? [Y/n] " "$(basename "$FILE")"' \
|
|
'read confirm' \
|
|
'case "$confirm" in [nN]) exit 0 ;; esac' \
|
|
"fish -c \"rm \$FILE\"" \
|
|
"grep -vF \"\$FILE\" $tmpfile > $tmpfile.new" \
|
|
"mv $tmpfile.new $tmpfile" >$deletescript
|
|
chmod +x $deletescript
|
|
|
|
set -l selected (command cat $tmpfile | fzf \
|
|
--ansi \
|
|
--delimiter '\t' \
|
|
--with-nth 2 \
|
|
--preview 'command cat {1}' \
|
|
--preview-window 'right:60%:wrap' \
|
|
--prompt 'Terminal Logs -> ' \
|
|
--header '?: Help Enter: View Ctrl-E: Edit Ctrl-D: Delete Ctrl-C: Quit' \
|
|
--bind "ctrl-e:execute($editor {1})" \
|
|
--bind "ctrl-d:execute($deletescript {1})+reload(command cat $tmpfile)" \
|
|
--bind "?:transform($togglescript)")
|
|
|
|
rm -f $tmpfile $tmpfile.new $helpfile $helpflag $togglescript $deletescript
|
|
|
|
test -n "$selected" || return 0
|
|
|
|
set -l file (echo $selected | cut -f1)
|
|
test -f $file || return 1
|
|
|
|
set -l base (basename $file)
|
|
|
|
# 1. Maintain base multi-color parsing highlights
|
|
set -l paru_flags -M "^\s*:: .*,upgrading \S+,downloading\.\.\.,\(\s*\d+/\d+\),(Total.*Size:|Net Upgrade Size:|\s*Package \(\d+\))"
|
|
|
|
set -l use_ov 0
|
|
if command -q ov
|
|
if not set -q PAGER; or test "$PAGER" = ov
|
|
set use_ov 1
|
|
end
|
|
end
|
|
|
|
if test $use_ov -eq 1
|
|
if string match -qr '^(paru|yay)' $base
|
|
set -l section_regex '^\s*Package \(\d+\)|^\s*:: (Proceed|Retrieving|Running pre-transaction|Processing package changes|Running post-transaction|Looking for .* upgrades\.\.\.)|^\s*==>'
|
|
set paru_flags $paru_flags --section-delimiter $section_regex --section-header -c
|
|
command ov $paru_flags $file
|
|
else if string match -qr '^scrollback' $base
|
|
# Write a minimal ov config to a temp file so the section header
|
|
# is displayed without ov's default slateblue background, preserving
|
|
# the original ANSI colors of the starship prompt.
|
|
set -l ov_cfg (mktemp --suffix .yaml)
|
|
printf 'Mode:\n scrollback:\n Style:\n SectionLine:\n Background: ""\n Foreground: ""\n' >$ov_cfg
|
|
command ov --config $ov_cfg --view-mode scrollback \
|
|
--section-delimiter '\x1b\]133;A' --section-header --section-header-num 1 $file
|
|
rm -f $ov_cfg
|
|
else
|
|
command ov $file
|
|
end
|
|
else if set -q PAGER; and command -q $PAGER
|
|
command cat $file | command $PAGER
|
|
else
|
|
command cat $file
|
|
end
|
|
end
|