-
Notifications
You must be signed in to change notification settings - Fork 479
/
Copy pathresize-adaptable.sh
executable file
·82 lines (75 loc) · 2.44 KB
/
resize-adaptable.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/sh
# Adaptable tmux resize script by percentage
#
# Layout types supported (-l):
#
# main-horizontal: top pane is main pane, panes split left to right on the bottom
# main-vertical: left pane is maine pane, right panes split top to bottom on the
# right side
#
# Options:
#
# -l layout-name (required): the name of the layout, "main-horizontal" or "main-vertical"
# -p percentage (required): an integer of the percentage of the client width/height to set
# -t target-window (optional): the tmux target for the window, can be an fnmatch(1) of the
# window name, index, or id
#
# Example usage:
#
# Case 1: Resize to a main-horizontal, main pane 66% of client height
# $ ./scripts/resize-adaptable.sh -p 66 -l main-horizontal
#
# Case 2: Same as Case 1, target "devel" window
# $ ./scripts/resize-adaptable.sh -p 66 -l main-horizontal -t devel
#
# Case 3: Resize to a main-horizontal, main pane half width
# $ ./scripts/resize-adaptable.sh -p 50 -l main-vertical
#
# Case 4: Same as Case 3, target "mywindow"
# $ ./scripts/resize-adaptable.sh -p 50 -l main-vertical -t mywindow
#
# Author: Tony Narlock
# Website: https://devel.tech
# License: MIT
lflag=
pflag=
tflag=
while getopts l:p:t: name;
do
case $name in
l) lflag=1
layout_name=$OPTARG;;
p) pflag=1
percentage="$OPTARG";;
t) tflag=1
target="$OPTARG";;
?) printf "Usage: %s: [-l layout-name] [-p percentage] [-t target-window]\n" $0
exit 2;;
esac
done
if [ ! -z "$pflag" ]; then
if ! [ "$percentage" -eq "$percentage" ] 2> /dev/null; then
printf "Percentage (-p) must be an integer" >&2
exit 1
fi
fi
if [ ! -z "$lflag" ]; then
if [ $layout_name != 'main-horizontal' ] && [ $layout_name != 'main-vertical' ] ; then
printf "layout name must be main-horizontal or main-vertical" >&2
exit 1
fi
fi
if [ "$layout_name" = "main-vertical" ]; then
MAIN_PANE_SIZE=$(expr $(tmux display -p '#{window_width}') \* $percentage \/ 100)
MAIN_SIZE_OPTION='main-pane-width'
fi
if [ "$layout_name" = "main-horizontal" ]; then
MAIN_PANE_SIZE=$(expr $(tmux display -p '#{window_height}') \* $percentage \/ 100)
MAIN_SIZE_OPTION='main-pane-height'
fi
if [ ! -z "$target" ]; then
tmux setw -t $target $MAIN_SIZE_OPTION $MAIN_PANE_SIZE; tmux select-layout -t $target $layout_name
else
tmux setw $MAIN_SIZE_OPTION $MAIN_PANE_SIZE; tmux select-layout $layout_name
fi
exit 0