diff --git a/conf.d/tmux-logging.fish b/conf.d/tmux-logging.fish index bbf54fc..cc416b4 100644 --- a/conf.d/tmux-logging.fish +++ b/conf.d/tmux-logging.fish @@ -11,10 +11,4 @@ status is-interactive; or exit type -q tmux; or exit set -q TMUX; or exit -set -l log_dir (set -q SCROLLBACK_HISTORY_DIR; and echo $SCROLLBACK_HISTORY_DIR; or echo "$HOME/.terminal_history") -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 -tmux pipe-pane "cat >> $log_file" 2>/dev/null +_tmux_pipe_log diff --git a/docs/fish-config.md b/docs/fish-config.md index 766b0e9..4c827e5 100644 --- a/docs/fish-config.md +++ b/docs/fish-config.md @@ -1697,7 +1697,9 @@ The tmux capture starts automatically when fish launches inside any tmux pane ($TMUX is set). It uses tmux's native pipe-pane to stream all pane output directly to disk without an intermediate process. Each fish shell session gets its own log file; a new log is created on each shell start -(including exec fish and new splits). +(including exec fish and new splits). Before each new log, the oldest +tmux_*.log files are pruned (by modification time) to keep the total within +SCROLLBACK_HISTORY_MAX_FILES, matching the paru/yay wrapper behaviour. Logging coordination via sentinel file diff --git a/functions/__fish_config_sync_logging.fish b/functions/__fish_config_sync_logging.fish index 0ee0e08..efc9f41 100644 --- a/functions/__fish_config_sync_logging.fish +++ b/functions/__fish_config_sync_logging.fish @@ -34,14 +34,7 @@ function __fish_config_sync_logging --description 'Sync C5 logging state: sentin rm -f $sentinel # Restart tmux pipe-pane for the current pane if inside tmux - if set -q TMUX - set -l log_dir (set -q SCROLLBACK_HISTORY_DIR; and echo $SCROLLBACK_HISTORY_DIR; or echo "$HOME/.terminal_history") - 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 - tmux pipe-pane "cat >> $log_file" 2>/dev/null - end + _tmux_pipe_log if test -x /usr/bin/paru mkdir -p (dirname $paru_wrapper) diff --git a/functions/_tmux_pipe_log.fish b/functions/_tmux_pipe_log.fish new file mode 100644 index 0000000..1693af7 --- /dev/null +++ b/functions/_tmux_pipe_log.fish @@ -0,0 +1,45 @@ +# Copyright (C) 2026 Rootiest +# SPDX-License-Identifier: AGPL-3.0-or-later + +# SYNOPSIS +# _tmux_pipe_log +# +# 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. +# +# RETURNS +# 0 Always +# +# EXAMPLE +# _tmux_pipe_log +function _tmux_pipe_log --description 'Start tmux pipe-pane capture for the current pane, with pruning' + set -q TMUX; or return 0 + 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 + + tmux pipe-pane "cat >> $log_file" 2>/dev/null +end