|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +session_list() { |
| 4 | + tmux list-sessions -F "#{session_name}" |
| 5 | +} |
| 6 | + |
| 7 | +output_height() { |
| 8 | + local list="$1" |
| 9 | + echo "$list" | |
| 10 | + wc -l | |
| 11 | + tr -d ' ' |
| 12 | +} |
| 13 | + |
| 14 | +# calculates required number of columns for the multi-column output |
| 15 | +get_column_number() { |
| 16 | + local output_height="$1" |
| 17 | + local pane_height="$2" |
| 18 | + local columns="$(expr $output_height / $pane_height )" |
| 19 | + if [ $(expr $output_height % $pane_height ) -gt 0 ]; then |
| 20 | + columns=$(expr $columns + 1) |
| 21 | + fi |
| 22 | + echo "$columns" |
| 23 | +} |
| 24 | + |
| 25 | +get_column_width() { |
| 26 | + declare -a array=("${!1}") |
| 27 | + local width=0 |
| 28 | + # searching for the longest session name |
| 29 | + for session_name in "${array[@]}"; do |
| 30 | + if [ ${#session_name} -gt $width ]; then |
| 31 | + width="${#session_name}" |
| 32 | + fi |
| 33 | + done |
| 34 | + # add some padding to the right |
| 35 | + echo $((width + 2)) |
| 36 | +} |
| 37 | + |
| 38 | +print_multi_column_output() { |
| 39 | + local output_height="$1" |
| 40 | + local pane_height="$2" |
| 41 | + local pane_width="$3" |
| 42 | + local columns="$(get_column_number "$output_height" "$pane_height")" |
| 43 | + eval session_array=( $(tmux list-sessions -F "'#{session_name}'") ) |
| 44 | + local width="$(get_column_width session_array[@])" |
| 45 | + |
| 46 | + # limit number of displayed columns |
| 47 | + local max_columns=$(( $((pane_width + 2)) / $width)) |
| 48 | + if [ $columns -gt $max_columns ]; then |
| 49 | + # there's more columns than it fits on the display |
| 50 | + columns="$max_columns" |
| 51 | + # last displayed element indicates there's more |
| 52 | + session_array[$((pane_height * columns - 1))]="..." |
| 53 | + fi |
| 54 | + |
| 55 | + # Composing a string that prints variable number of columns. |
| 56 | + # Example print_string for 2 columns: |
| 57 | + # printf "%-17s %s\n" "${session_array[$index]}" "${session_array[$((index + 54 ))]}" |
| 58 | + local first_arg='' |
| 59 | + local arg_list='' |
| 60 | + local i=1 |
| 61 | + while [ $i -lt $columns ]; do |
| 62 | + first_arg+="%-${width}s" |
| 63 | + arg_list+=" \"\${session_array[\$((index + $((i * $pane_height)) ))]}\"" |
| 64 | + i=$((i + 1)) |
| 65 | + done |
| 66 | + |
| 67 | + local print_string='' |
| 68 | + print_string+='printf "' |
| 69 | + print_string+=$first_arg |
| 70 | + print_string+='%s\n" "${session_array[$index]}"' |
| 71 | + print_string+=$arg_list |
| 72 | + |
| 73 | + local index=0 |
| 74 | + while [ $index -lt $pane_height ]; do |
| 75 | + eval "$print_string" |
| 76 | + index=$((index + 1)) |
| 77 | + done |
| 78 | +} |
| 79 | + |
| 80 | +main() { |
| 81 | + local pane_height=$(tmux display-message -p -F "#{pane_height}") |
| 82 | + local pane_width=$(tmux display-message -p -F "#{pane_width}") |
| 83 | + local session_list="$(session_list)" |
| 84 | + local output_height=$(output_height "$session_list") |
| 85 | + if [ $output_height -gt $pane_height ]; then |
| 86 | + print_multi_column_output "$output_height" "$pane_height" "$pane_width" |
| 87 | + else |
| 88 | + echo "$session_list" |
| 89 | + fi |
| 90 | +} |
| 91 | +main |
0 commit comments