From 5b74fbe42772c3b9d1d204916a01a768016307c9 Mon Sep 17 00:00:00 2001 From: rootiest Date: Tue, 16 Jun 2026 01:16:22 -0400 Subject: [PATCH] feat(c5): add zellij exit-capture logging and shared prune helper MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Zellij has no live-stream facility like tmux pipe-pane, so capture is a one-shot 'zellij action dump-screen --full' run from a fish_exit handler (conf.d/zellij-logging.fish -> functions/_zellij_dump_log.fish). The C5 guard is checked inside the helper at exit time, so toggling logging needs no sync_logging coordination — there is no persistent stream. Extract pruning into functions/_prune_terminal_logs.fish, shared by both the tmux and zellij helpers. This also fixes a latent bug in the tmux helper: globbing tmux_*.log directly errored with 'No matches for wildcard' on the first log in an empty dir. The shared helper globs via 'set' (tolerates no-match) and uses command ls/rm to bypass the eza and trash C1 shadows. --- README.md | 2 +- conf.d/zellij-logging.fish | 24 +++++++++++++++ docs/fish-config.md | 13 ++++++++- functions/_prune_terminal_logs.fish | 45 +++++++++++++++++++++++++++++ functions/_tmux_pipe_log.fish | 22 ++++---------- functions/_zellij_dump_log.fish | 40 +++++++++++++++++++++++++ 6 files changed, 127 insertions(+), 19 deletions(-) create mode 100644 conf.d/zellij-logging.fish create mode 100644 functions/_prune_terminal_logs.fish create mode 100644 functions/_zellij_dump_log.fish diff --git a/README.md b/README.md index 1df7d1b..63172b0 100644 --- a/README.md +++ b/README.md @@ -178,7 +178,7 @@ If you'd rather set them by hand, each category is controlled by a universal var | `__fish_config_op_autoexec` | Startup side-effects: Fisher bootstrap, theme apply, `paru`/`yay` wrapper generation, auto venv activation, WakaTime hook | | `__fish_config_op_overrides` | Vi mode, `exit`→`smart_exit`, `$PAGER`/`$MANPAGER`/`$CDPATH`, bang-bang history expansion, autopair, puffer, Starship prompt, theme colors | | `__fish_config_op_integrations` | Kitty/WezTerm window abbreviations, `done` notifications, `spwin`/`tab`/`split`, `hist`, `logs`, `upgrade`, WakaTime | -| `__fish_config_op_logging` | Scrollback capture on exit, tmux `pipe-pane` pane logging, `paru`/`yay` AUR log wrappers, Kitty watcher capture (sentinel-file coordinated) | +| `__fish_config_op_logging` | Scrollback capture on exit, tmux `pipe-pane` pane logging, zellij `dump-screen` capture on exit, `paru`/`yay` AUR log wrappers, Kitty watcher capture (sentinel-file coordinated) | | `__fish_config_op_greeting` | Per-session `fish_greeting` (suppresses distro greetings such as CachyOS fastfetch by overriding with an empty function); first-run welcome banner | | `__fish_config_opinionated` | Master switch — all six categories at once | diff --git a/conf.d/zellij-logging.fish b/conf.d/zellij-logging.fish new file mode 100644 index 0000000..31a3b57 --- /dev/null +++ b/conf.d/zellij-logging.fish @@ -0,0 +1,24 @@ +# Copyright (C) 2026 Rootiest +# SPDX-License-Identifier: AGPL-3.0-or-later +# +# C5 — Logging & Capture: registers a fish_exit handler that dumps the current +# Zellij pane's scrollback to a log file when the shell exits. Unlike tmux +# (streamed live via pipe-pane), Zellij has no continuous-capture facility, so +# we snapshot once on exit via `zellij action dump-screen --full`. +# +# The handler is registered whenever fish runs inside Zellij; the C5 logging +# guard is evaluated inside _zellij_dump_log at exit time, so toggling +# __fish_config_op_logging takes effect on the next exit without a restart +# (no sync_logging coordination needed, since there is no live stream to stop). +# +# Event-handler functions must be defined at startup (conf.d) so the --on-event +# binding is registered; autoloaded functions in functions/ never register. + +status is-interactive; or exit +type -q zellij; or exit +set -q ZELLIJ; or exit + +function __zellij_dump_on_exit --on-event fish_exit \ + --description 'C5 event handler: dump Zellij pane scrollback on shell exit' + _zellij_dump_log +end diff --git a/docs/fish-config.md b/docs/fish-config.md index 4c827e5..712c526 100644 --- a/docs/fish-config.md +++ b/docs/fish-config.md @@ -1678,7 +1678,7 @@ silently failing. #### C5 — Logging and Capture -Four components capture shell output to disk. Disabling +Five components capture shell output to disk. Disabling __fish_config_op_logging skips all capture and removes the logging wrappers. Component What it captures @@ -1687,6 +1687,8 @@ __fish_config_op_logging skips all capture and removes the logging wrappers. ~/.terminal_history/scrollback_YYYY-MM-DD_HH-MM-SS.log tmux pane capture Continuous pane stream via pipe-pane, saved to: ~/.terminal_history/tmux_-w-p_YYYY-MM-DD_HH-MM-SS.log + zellij pane capture Pane scrollback snapshot on shell exit, saved to: + ~/.terminal_history/zellij_-p_YYYY-MM-DD_HH-MM-SS.log paru wrapper All paru/AUR output captured to: ~/.terminal_history/paru_YYYY-MM-DD_HH-MM-SS.log yay wrapper All yay/AUR output captured to: @@ -1701,6 +1703,15 @@ session gets its own log file; a new log is created on each shell start tmux_*.log files are pruned (by modification time) to keep the total within SCROLLBACK_HISTORY_MAX_FILES, matching the paru/yay wrapper behaviour. +The zellij capture works differently: Zellij has no live output-streaming +facility like pipe-pane, so the log is taken as a one-shot snapshot when the +shell exits, via `zellij action dump-screen --full`. A fish_exit handler +(registered whenever $ZELLIJ is set) writes the pane's full scrollback and +then prunes old zellij_*.log files the same way. Because the capture happens +at exit, toggling __fish_config_op_logging takes effect on the next exit with +no restart or sentinel coordination needed — the C5 guard is re-checked when +the handler fires. + Logging coordination via sentinel file C5 uses a sentinel file to synchronize state between the shell and diff --git a/functions/_prune_terminal_logs.fish b/functions/_prune_terminal_logs.fish new file mode 100644 index 0000000..f121fbb --- /dev/null +++ b/functions/_prune_terminal_logs.fish @@ -0,0 +1,45 @@ +# Copyright (C) 2026 Rootiest +# SPDX-License-Identifier: AGPL-3.0-or-later + +# SYNOPSIS +# _prune_terminal_logs +# +# DESCRIPTION +# Removes the oldest "_*.log" files in SCROLLBACK_HISTORY_DIR +# (default ~/.terminal_history) so the total count for that prefix stays +# within SCROLLBACK_HISTORY_MAX_FILES (default 100). Files are sorted by +# modification time, so the most recently written logs are kept — actively +# appended logs (e.g. a tmux pipe-pane stream) survive. +# +# Uses `command ls`/`command rm` to bypass the C1 shadows: the bare `ls` is +# the eza wrapper, which injects OSC-8 hyperlink escapes into paths, and the +# bare `rm` is the trash wrapper. The glob is expanded via `set` first so a +# no-match (empty dir / first run) yields an empty list instead of a hard +# "No matches for wildcard" error. +# +# ARGUMENTS +# prefix Log-name prefix to prune (e.g. tmux, zellij) +# +# RETURNS +# 0 Always +# +# EXAMPLE +# _prune_terminal_logs tmux +function _prune_terminal_logs --argument-names prefix --description 'Prune oldest _*.log files beyond the configured max' + set -q prefix[1]; or return 0 + set -l log_dir (set -q SCROLLBACK_HISTORY_DIR; and echo $SCROLLBACK_HISTORY_DIR; or echo "$HOME/.terminal_history") + set -l max_files (set -q SCROLLBACK_HISTORY_MAX_FILES; and echo $SCROLLBACK_HISTORY_MAX_FILES; or echo 100) + test -d $log_dir; or return 0 + + # Glob via `set` so a no-match yields an empty list rather than an error. + set -l logs $log_dir/$prefix"_"*.log + set -q logs[1]; or return 0 + + # Re-sort by mtime, oldest first. Pass explicit files (no glob) so ls can + # never fail on no-match; `command ls` avoids the eza OSC-8 corruption. + set logs (command ls -1tr $logs 2>/dev/null) + if test (count $logs) -gt $max_files + set -l excess (math (count $logs) - $max_files) + command rm -f $logs[1..$excess] + end +end diff --git a/functions/_tmux_pipe_log.fish b/functions/_tmux_pipe_log.fish index 1693af7..feb3594 100644 --- a/functions/_tmux_pipe_log.fish +++ b/functions/_tmux_pipe_log.fish @@ -7,11 +7,10 @@ # DESCRIPTION # Starts tmux pipe-pane capture for the current pane, streaming its output # to a timestamped log in SCROLLBACK_HISTORY_DIR (default ~/.terminal_history). -# Before starting, prunes the oldest tmux_*.log files so that the total stays -# within SCROLLBACK_HISTORY_MAX_FILES (default 100), leaving room for the new -# log. No-op when not inside a tmux session ($TMUX unset) or tmux is missing. -# Shared by conf.d/tmux-logging.fish (shell startup) and -# __fish_config_sync_logging (C5 re-enable) so both stay in sync. +# Old tmux_*.log files are pruned via _prune_terminal_logs to stay within +# SCROLLBACK_HISTORY_MAX_FILES. No-op when not inside a tmux session ($TMUX +# unset) or tmux is missing. Shared by conf.d/tmux-logging.fish (shell +# startup) and __fish_config_sync_logging (C5 re-enable) so both stay in sync. # # RETURNS # 0 Always @@ -23,23 +22,12 @@ function _tmux_pipe_log --description 'Start tmux pipe-pane capture for the curr type -q tmux; or return 0 set -l log_dir (set -q SCROLLBACK_HISTORY_DIR; and echo $SCROLLBACK_HISTORY_DIR; or echo "$HOME/.terminal_history") - set -l max_files (set -q SCROLLBACK_HISTORY_MAX_FILES; and echo $SCROLLBACK_HISTORY_MAX_FILES; or echo 100) set -l pane_id (tmux display-message -p '#{session_name}-w#{window_index}-p#{pane_index}' 2>/dev/null) set -l timestamp (date "+%Y-%m-%d_%H-%M-%S") set -l log_file "$log_dir/tmux_"$pane_id"_"$timestamp".log" mkdir -p $log_dir - - # Prune oldest logs, leaving room for the one we're about to create. - # Names carry a per-pane prefix, so sort by mtime (command ls -tr = oldest - # first). Use `command ls`: the bare `ls` is shadowed by the eza wrapper, - # which injects OSC-8 escapes into paths and corrupts the filenames. - set -l keep (math max 0, $max_files - 1) - set -l logs (command ls -1tr $log_dir/tmux_*.log 2>/dev/null) - if test (count $logs) -gt $keep - set -l excess (math (count $logs) - $keep) - command rm -f $logs[1..$excess] - end + _prune_terminal_logs tmux tmux pipe-pane "cat >> $log_file" 2>/dev/null end diff --git a/functions/_zellij_dump_log.fish b/functions/_zellij_dump_log.fish new file mode 100644 index 0000000..3f4e822 --- /dev/null +++ b/functions/_zellij_dump_log.fish @@ -0,0 +1,40 @@ +# Copyright (C) 2026 Rootiest +# SPDX-License-Identifier: AGPL-3.0-or-later + +# SYNOPSIS +# _zellij_dump_log +# +# DESCRIPTION +# Captures the current Zellij pane's scrollback to a timestamped log in +# SCROLLBACK_HISTORY_DIR (default ~/.terminal_history). Zellij has no live +# output-streaming facility like tmux's pipe-pane, so this performs a one-shot +# `zellij action dump-screen --full` — intended to run on shell exit. Old +# zellij_*.log files are pruned via _prune_terminal_logs to stay within +# SCROLLBACK_HISTORY_MAX_FILES. +# +# The C5 logging guard (__fish_config_op_logging) is evaluated here, at call +# time, so toggling logging takes effect on the next exit without a restart. +# No-op when logging is disabled, not inside Zellij ($ZELLIJ unset), or the +# zellij binary is missing. +# +# RETURNS +# 0 Always +# +# EXAMPLE +# _zellij_dump_log +function _zellij_dump_log --description 'Dump the current Zellij pane scrollback to a log file, with pruning' + __fish_config_op_enabled __fish_config_op_logging; or return 0 + set -q ZELLIJ; or return 0 + type -q zellij; or return 0 + + set -l log_dir (set -q SCROLLBACK_HISTORY_DIR; and echo $SCROLLBACK_HISTORY_DIR; or echo "$HOME/.terminal_history") + set -l session (set -q ZELLIJ_SESSION_NAME; and echo $ZELLIJ_SESSION_NAME; or echo unknown) + set -l pane_id (set -q ZELLIJ_PANE_ID; and echo $ZELLIJ_PANE_ID; or echo unknown) + set -l timestamp (date "+%Y-%m-%d_%H-%M-%S") + set -l log_file "$log_dir/zellij_"$session"-p"$pane_id"_"$timestamp".log" + + mkdir -p $log_dir + zellij action dump-screen --full "$log_file" 2>/dev/null + + _prune_terminal_logs zellij +end