Skip to content

Instantly share code, notes, and snippets.

@RichardBronosky
Forked from dmckean/tmux-panes.sh
Last active August 17, 2023 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RichardBronosky/f4ea9e5e62426b8d61ef40ed34b09a43 to your computer and use it in GitHub Desktop.
Save RichardBronosky/f4ea9e5e62426b8d61ef40ed34b09a43 to your computer and use it in GitHub Desktop.
A combination of the tmux commands: list-sessions list-windows and list-panes
#! /usr/bin/env bash
hi_color=$(tput setaf 4) #blue
bold=$(tput bold)
underline=$(tput smul)
normal=$(tput sgr0)
read -r -d '' usage << DOC
Prints out a table of tmux sessons, windows, and panels.
Active panes appear in ${hi_color}color${normal}.
Attached panes are ${bold}${hi_color}bolded${normal}.
The current pane will be ${underline}${bold}${hi_color}underlined${normal} if this shell is in a tmux session.
DOC
if [ "$1" = "-h" -o "$1" = "--help" ]
then
printf "%s\n" "$usage"
exit
fi
# Exit if tmux is not running
tmux has || exit 1
pane_arr=()
format_arr=()
format="%s %s %s %s %s %s"
pane_arr+=( "$(printf "$format" ID "Session Start" Session Window Pane Path)" )
# See FORMAT section of tmux's man page
tmux_args="'#{session_id}:#{window_id}:#{pane_id}' '#{session_name}' "
tmux_args+="#{session_created} '#{window_index}/#{session_windows}:#{window_name}#{window_flags}' "
tmux_args+="'#{pane_index}/#{window_panes}:#{pane_current_command}' '#{pane_current_path}' "
tmux_args+="#{session_attached} #{window_active} #{pane_active}"
# Parse output line by line
{ while read -r str_args
do
# Parse quoted arguments
declare -a "args=($str_args)"
TMUX_ID=${args[0]}
SESSION=${args[1]}
SESSION_TIME=${args[2]}
WINDOW=${args[3]}
PANE=${args[4]}
PANE_PATH=${args[5]}
SESSION_ATTACHED=${args[6]}
WINDOW_ACTIVE=${args[7]}
PANE_ACTIVE=${args[8]}
SESSION_TIME=$(date -d @$SESSION_TIME +"%d %b %R") # Jan 01 hh:mm format
color=""
# Highlight only the active panes in active windows
if [ "$PANE_ACTIVE" == "1" -a "$WINDOW_ACTIVE" == "1" ]
then
color="${hi_color}$color"
# Bold (embolden?) active panes in attached sessions
if [ "$SESSION_ATTACHED" == "1" ]
then
color="${bold}$color"
# Underline the pane that is active in this terminal
if [[ "$TMUX_ID" == *":$(tmux display -p "#D")" ]] && [ -n "$TMUX" ]
then
color="${underline}$color"
fi
fi
fi
format_arr+=( "$color" )
pane_arr+=( "$(printf "$format" "$TMUX_ID" "$SESSION_TIME" "$SESSION" "$WINDOW" "$PANE" "$PANE_PATH")" )
done
} < <(tmux list-panes -aF "$tmux_args")
output=()
OIFS=$IFS
IFS=$'\n'
{ while read -r line; do output+=( "$line" ); done } < <({ for line in ${pane_arr[@]}; do echo "$line"; done } | column -t -s $'\t')
IFS=$OIFS
{ for i in $(seq 0 "${#output[@]}"); do
closing_format=""
if [ -n "${format_arr[$i]}" ]
then closing_format="${normal}"
fi
printf "%s%s%s\n" "${format_arr[$i]}" "${output[$i]}" "$closing_format"
done }

tmux-panes.sh

A combination of the tmux commands: list-sessions list-windows and list-panes

Sample Output

~ $ tmux-panes.sh
ID        Session Start  Session  Window     Pane      Path
$0:@0:%0  22 Dec 17:46   bruno    0/2:bash-  0/1:bash  /home/pi/src/dotfiles
$0:@1:%1  22 Dec 17:46   bruno    1/2:top*   0/2:top   /etc/systemd/system
$0:@1:%2  22 Dec 17:46   bruno    1/2:bash*  1/2:bash  /home/pi
$1:@2:%3  22 Dec 19:22   shared   0/1:bash*  0/1:bash  /root
@RichardBronosky
Copy link
Author

I'd like to make it a little more "graphical" like the output of tmux choose-tree, but this is a start.

~ $ tmux-panes.sh
ID        Session Start  Session  Window     Pane      Path
$0:@0:%0  22 Dec 17:46   bruno    0/2:vim-   0/1:vim   /home/pi/src/dotfiles
$0:@1:%1  22 Dec 17:46   bruno    1/2:top*   0/2:top   /etc/systemd/system
$0:@1:%2  22 Dec 17:46   bruno    1/2:bash*  1/2:bash  /home/pi
$1:@2:%3  22 Dec 19:22   shared   0/1:vim*   0/1:vim   /root
~ $ tmux choose-tree
(0)  - all_others: 1 windows (attached)
(1)  └─> 0: vim* (1 panes) "robopi.local"
(2)  - bruno: 2 windows
(3)  ├─>   0: vim- (1 panes) "robopi.local"
(4)  └─> - 1: bash* (2 panes)
(5)      ├─> 0: top "robopi.local"
(6)      └─> 1: bash "robopi.local"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment