diff options
Diffstat (limited to 'dotfiles/common')
| -rwxr-xr-x | dotfiles/common/.local/bin/tmux-util | 54 |
1 files changed, 53 insertions, 1 deletions
diff --git a/dotfiles/common/.local/bin/tmux-util b/dotfiles/common/.local/bin/tmux-util index 10c6fbb..2fa861d 100755 --- a/dotfiles/common/.local/bin/tmux-util +++ b/dotfiles/common/.local/bin/tmux-util @@ -36,6 +36,55 @@ EOF } # ----------------------------------------------------------------------------- +# ls +# ----------------------------------------------------------------------------- + +# Render seconds as a short human duration (e.g. "5s", "12m", "3h", "2d"). +format_idle() { + local s="$1" + if [ "$s" -lt 60 ]; then + echo "${s}s" + elif [ "$s" -lt 3600 ]; then + echo "$((s / 60))m" + elif [ "$s" -lt 86400 ]; then + echo "$((s / 3600))h" + else + echo "$((s / 86400))d" + fi +} + +cmd_ls() { + local sessions + sessions=$(tmux list-sessions \ + -F '#{session_attached} #{session_name} #{session_activity} #{session_windows}' \ + 2>/dev/null || true) + + if [ -z "$sessions" ]; then + echo "No tmux sessions." + return 0 + fi + + local now="${TMUX_UTIL_NOW:-$(date +%s)}" + + { + printf 'STATE\tNAME\tIDLE\tWINDOWS\tCWD\n' + local attached name activity windows state idle_str cwd + while IFS=' ' read -r attached name activity windows; do + [ -n "$name" ] || continue + state="detached" + [ "$attached" -gt 0 ] && state="attached" + idle_str=$(format_idle "$((now - activity))") + cwd=$(tmux display -p -t "$name" '#{pane_current_path}' 2>/dev/null || echo "?") + # Replace leading $HOME with ~ for display. + if [ -n "${HOME:-}" ] && [ "${cwd#$HOME}" != "$cwd" ]; then + cwd="~${cwd#$HOME}" + fi + printf '%s\t%s\t%s\t%s\t%s\n' "$state" "$name" "$idle_str" "$windows" "$cwd" + done <<<"$sessions" + } | column -t -s $'\t' +} + +# ----------------------------------------------------------------------------- # Reap # ----------------------------------------------------------------------------- @@ -92,7 +141,10 @@ main() { reap) cmd_reap "$@" ;; - go|pick|ls|find|rename) + ls) + cmd_ls "$@" + ;; + go|pick|find|rename) echo "tmux-util: subcommand '$sub' is not implemented yet" >&2 return 2 ;; |
