diff --git a/.tmux.conf b/.tmux.conf index 9c49e90..5766e05 100644 --- a/.tmux.conf +++ b/.tmux.conf @@ -276,20 +276,20 @@ set-option -g status-right "#(~/.config/tmux/tmux-status/right.sh)" # session index switching -bind -n C-1 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 1" -bind -n C-2 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 2" -bind -n C-3 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 3" -bind -n C-4 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 4" -bind -n C-5 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 5" -bind -n C-6 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 6" -bind -n C-7 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 7" -bind -n C-8 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 8" -bind -n C-9 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 9" -bind -n F1 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 1" -bind -n F2 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 2" -bind -n F3 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 3" -bind -n F4 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 4" -bind -n F5 run-shell "~/.config/tmux/scripts/switch_session_by_index.sh 5" +bind -n C-1 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 1" +bind -n C-2 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 2" +bind -n C-3 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 3" +bind -n C-4 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 4" +bind -n C-5 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 5" +bind -n C-6 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 6" +bind -n C-7 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 7" +bind -n C-8 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 8" +bind -n C-9 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 9" +bind -n F1 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 1" +bind -n F2 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 2" +bind -n F3 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 3" +bind -n F4 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 4" +bind -n F5 run-shell -b "~/.config/tmux/scripts/switch_session_by_index.sh 5" # True color support set -g default-terminal "tmux-256color" set -as terminal-features ",*256col*:RGB" diff --git a/tmux/scripts/session_manager.py b/tmux/scripts/session_manager.py index 4357810..9a4546d 100755 --- a/tmux/scripts/session_manager.py +++ b/tmux/scripts/session_manager.py @@ -86,6 +86,7 @@ def command_switch(index_str: str) -> None: if index > len(sessions): return run_tmux(["switch-client", "-t", sessions[index - 1]["id"]], check=False) + run_tmux(["refresh-client", "-S"], check=False) def command_rename(label: str) -> None: diff --git a/tmux/scripts/switch_session_by_index.sh b/tmux/scripts/switch_session_by_index.sh index e72b9de..093aa6b 100755 --- a/tmux/scripts/switch_session_by_index.sh +++ b/tmux/scripts/switch_session_by_index.sh @@ -6,4 +6,10 @@ if [[ -z "$index" || ! "$index" =~ ^[0-9]+$ ]]; then exit 0 fi -python3 "$HOME/.config/tmux/scripts/session_manager.py" switch "$index" +# Get session matching index prefix (e.g., "1-" for index 1) +target=$(tmux list-sessions -F '#{session_id} #{session_name}' 2>/dev/null | awk -v idx="$index" '$2 ~ "^"idx"-" {print $1; exit}') + +if [[ -n "$target" ]]; then + tmux switch-client -t "$target" + tmux refresh-client -S +fi diff --git a/tmux/tmux-status/left.sh b/tmux/tmux-status/left.sh index cb59bd3..2b272bd 100755 --- a/tmux/tmux-status/left.sh +++ b/tmux/tmux-status/left.sh @@ -4,21 +4,15 @@ set -euo pipefail current_session_id="${1:-}" current_session_name="${2:-}" -detect_current_session_id=$(tmux display-message -p '#{session_id}') -detect_current_session_name=$(tmux display-message -p '#{session_name}') +# Single tmux call to get all needed info +IFS=$'\t' read -r detect_session_id detect_session_name term_width status_bg < <( + tmux display-message -p '#{session_id} #{session_name} #{client_width} #{status-bg}' 2>/dev/null || echo "" +) -if [[ -z "$current_session_id" ]]; then - current_session_id="$detect_current_session_id" -fi - -if [[ -z "$current_session_name" ]]; then - current_session_name="$detect_current_session_name" -fi - -status_bg=$(tmux show -gqv status-bg) -if [[ -z "$status_bg" || "$status_bg" == "default" ]]; then - status_bg=black -fi +[[ -z "$current_session_id" ]] && current_session_id="$detect_session_id" +[[ -z "$current_session_name" ]] && current_session_name="$detect_session_name" +[[ -z "$status_bg" || "$status_bg" == "default" ]] && status_bg=black +term_width="${term_width:-100}" inactive_bg="#373b41" inactive_fg="#c5c8c6" @@ -28,22 +22,9 @@ separator="" left_cap="█" max_width=18 -# width-based label policy: when narrow (<80 cols by default), -# show title for active session and only the numeric index for inactive ones. left_narrow_width=${TMUX_LEFT_NARROW_WIDTH:-80} -term_width=$(tmux display-message -p '#{client_width}' 2>/dev/null || true) -if [[ -z "${term_width:-}" || "$term_width" == "0" ]]; then - term_width=$(tmux display-message -p '#{window_width}' 2>/dev/null || true) -fi -if [[ -z "${term_width:-}" || "$term_width" == "0" ]]; then - term_width=${COLUMNS:-} -fi is_narrow=0 -if [[ -n "${term_width:-}" && "$term_width" =~ ^[0-9]+$ ]]; then - if (( term_width < left_narrow_width )); then - is_narrow=1 - fi -fi +[[ "$term_width" =~ ^[0-9]+$ ]] && (( term_width < left_narrow_width )) && is_narrow=1 normalize_session_id() { local value="$1" @@ -69,124 +50,7 @@ extract_index() { fi } -tracker_pending_sessions="" -tracker_running_sessions="" -TRACKER_CACHE_DIR="$HOME/.config/tmux/cache" -TRACKER_CACHE_FILE="$TRACKER_CACHE_DIR/tracker_state_lines" -TRACKER_CACHE_TTL=1 -file_mtime() { - local path="$1" - [[ ! -e "$path" ]] && return 1 - - local mtime - mtime=$(stat -f %m "$path" 2>/dev/null || true) - if [[ -n "$mtime" ]]; then - printf '%s\n' "$mtime" - return 0 - fi - - mtime=$(stat -c %Y "$path" 2>/dev/null || true) - if [[ -n "$mtime" ]]; then - printf '%s\n' "$mtime" - return 0 - fi - - return 1 -} - -write_tracker_cache() { - mkdir -p "$TRACKER_CACHE_DIR" - local tmp_file="$TRACKER_CACHE_FILE.$$" - printf '%s\n' "$1" > "$tmp_file" - mv "$tmp_file" "$TRACKER_CACHE_FILE" -} - -reset_tracker_state() { - tracker_pending_sessions="" - tracker_running_sessions="" -} - -load_tracker_info() { - reset_tracker_state - - if ! command -v jq >/dev/null 2>&1; then - return - fi - - local tracker_lines="" - local now - now=$(date +%s 2>/dev/null || true) - - if [[ -f "$TRACKER_CACHE_FILE" && -n "$now" ]]; then - local cache_mtime - cache_mtime=$(file_mtime "$TRACKER_CACHE_FILE" 2>/dev/null || true) - if [[ -n "$cache_mtime" ]]; then - local age=$(( now - cache_mtime )) - if (( age <= TRACKER_CACHE_TTL )); then - tracker_lines=$(cat "$TRACKER_CACHE_FILE" 2>/dev/null || true) - fi - fi - fi - - if [[ -z "$tracker_lines" ]]; then - local tracker_bin="$HOME/.config/agent-tracker/bin/tracker-client" - if [[ ! -x "$tracker_bin" ]]; then - rm -f "$TRACKER_CACHE_FILE" 2>/dev/null || true - return - fi - local tracker_state - tracker_state=$("$tracker_bin" state 2>/dev/null || true) - if [[ -z "$tracker_state" ]]; then - rm -f "$TRACKER_CACHE_FILE" 2>/dev/null || true - return - fi - - tracker_lines=$(printf '%s\n' "$tracker_state" | jq -r 'select(.kind == "state") | .tasks[] | "\(.session_id)|\(.status)|\(.acknowledged)"' 2>/dev/null || true) - if [[ -n "$tracker_lines" ]]; then - write_tracker_cache "$tracker_lines" - else - rm -f "$TRACKER_CACHE_FILE" 2>/dev/null || true - return - fi - fi - - while IFS='|' read -r session_id status acknowledged; do - [[ -z "$session_id" ]] && continue - case "$status" in - in_progress) - tracker_running_sessions+="$session_id"$'\n' - ;; - completed) - if [[ "$acknowledged" == "false" ]]; then - tracker_pending_sessions+="$session_id"$'\n' - fi - ;; - esac - done <<< "$tracker_lines" -} - -value_in_list() { - local needle="$1" - local list="$2" - if [[ -z "$needle" || -z "$list" ]]; then - return 1 - fi - while IFS= read -r line; do - [[ "$line" == "$needle" ]] && return 0 - done <<< "$list" - return 1 -} - -session_has_pending() { - value_in_list "$1" "$tracker_pending_sessions" -} - -session_has_running() { - value_in_list "$1" "$tracker_running_sessions" -} - -load_tracker_info sessions=$(tmux list-sessions -F '#{session_id}::#{session_name}' 2>/dev/null || true) if [[ -z "$sessions" ]]; then @@ -232,19 +96,12 @@ while IFS= read -r entry; do label="${label:0:max_width-1}…" fi - indicator="" - if session_has_pending "$session_id"; then - indicator=" #[fg=#a6e3a1,bg=${segment_bg}]●#[fg=${segment_fg},bg=${segment_bg}]" - elif session_has_running "$session_id"; then - indicator=" #[fg=#f9e2af,bg=${segment_bg}]●#[fg=${segment_fg},bg=${segment_bg}]" - fi - if [[ -z "$prev_bg" ]]; then rendered+="#[fg=${segment_bg},bg=${status_bg}]${left_cap}" else rendered+="#[fg=${prev_bg},bg=${segment_bg}]${separator}" fi - rendered+="#[fg=${segment_fg},bg=${segment_bg}] ${label}${indicator} " + rendered+="#[fg=${segment_fg},bg=${segment_bg}] ${label} " prev_bg="$segment_bg" done <<< "$sessions"