dotfiles

My collection of dotfiles
git clone git://git.noxz.tech/dotfiles
Log | Files | Refs

nrowgrid.tmux (2483B)


      1 #!/bin/sh -eu
      2 
      3 # make sure 'nrows' variable exist
      4 tmux showenv nrows 1>&2 2>/dev/null || tmux setenv nrows 1
      5 rows=$(tmux showenv nrows | sed 's|^.*=||')
      6 rows=${rows:-1}
      7 
      8 # read number of desired rows from input
      9 input=${@:-}
     10 if [ -n "$input" ]; then
     11 	case "$input" in
     12 	# increase row number
     13 	'++') rows=$((rows + 1)) ;;
     14 
     15 	# decrease row number
     16 	'--') rows=$((rows - 1)) ;;
     17 
     18 	# allow numeric input for row count
     19 	*) [ $input -eq $input 2>/dev/null ] && rows=$input ;;
     20 	esac
     21 fi
     22 
     23 # get some initial data
     24 set -- $(tmux list-panes -F '#{pane_id}')
     25 spane=$(tmux display -p '#{pane_id}')
     26 hpane=$(tmux last-pane 2>/dev/null \;display -p '#{pane_id}' \;last-pane) || :
     27 wd=$(tmux display -p '#{window_width}x#{window_height}')
     28 ww=${wd%%x*}
     29 wh=${wd##*x}
     30 
     31 exec tmux $({
     32 	n=$#
     33 
     34 	[ $rows -lt 1 ] && rows=1                       # at least one row, please
     35 	[ $n -lt $rows ] && rows=$n                     # never allow empty rows
     36 
     37 	tmux setenv nrows $rows
     38 
     39 	# counters
     40 	ri=0; ci=0; uc=0; uw=0; uh=0
     41 
     42 	# define first row
     43 	cols=$((n / rows))                              # number of columns
     44 	ch=$((wh / rows))                               # client/pane height
     45 	uc=$cols                                        # utilized columns
     46 	uh=$ch                                          # utilized height
     47 	rs=""                                           # resolutions tracker
     48 
     49 	# stack panes
     50 	echo select-layout even-vertical
     51 
     52 	# format panes in a grid
     53 	for pid
     54 	do
     55 	if [ $ci -eq $cols ]; then
     56 		uw=0                                    # utilized width
     57 		ci=0                                    # column index
     58 		ri=$((ri + 1))                          # row index
     59 
     60 		# next row
     61 		cols=$(((n - uc) / (rows - ri)))
     62 		uc=$((uc + cols))
     63 		ch=$(((wh - uh) / (rows - ri)))
     64 		uh=$((uh + ch))
     65 	fi
     66 
     67 		cw=$(((ww - uw) / (cols - ci)))             # client/pane width
     68 		uw=$((uw + cw))
     69 		rs="$rs$cw:$ch;"
     70 
     71 		echo select-pane -t $pid
     72 
     73 		# move pane if it's not first in the row
     74 		if [ $ci -gt 0 ]
     75 		then
     76 			echo move-pane -d -s . -t .-1 -h
     77 			echo resize-pane -t . -x $((cw - 1))
     78 		fi
     79 
     80 		ci=$((ci+1))
     81 	done
     82 
     83 	# resize panes in the new grid layout
     84 	for pid; do
     85 		r="${rs%%;*}"                               # resolution
     86 		w="${r%%:*}"                                # width
     87 		h="${r##*:}"                                # height
     88 		rs="${rs#*;}"
     89 		echo resize-pane -t $pid -x $((w - 1)) -y $((h - 1))
     90 	done
     91 
     92 	# restore selection
     93 	[ -n "$hpane" ] &&  echo select-pane -t $hpane
     94 	echo select-pane -t $spane
     95 
     96 } | sed 's/$/ ;/')