-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew-window
executable file
·95 lines (79 loc) · 2.95 KB
/
new-window
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
83
84
85
86
87
88
89
90
91
92
93
94
95
#!/bin/bash
# To use, put something like this in your .tmux.conf:
# bind M-0 command-prompt -p Repository: 'run "~/tools/tmux-new-window.sh %1 0"'
### Determine configuration ####################################################
PROJECT=$1
WINDOW=$2
COLS=$(tmux display -p '#{window_width}')
USER_PROJECT_DIRS=$(bash --login -c "echo \${USER_PROJECT_DIRS:-$HOME $HOME/tools $HOME/code}")
PROJECT_DIR=~/$PROJECT
for DIR in ${USER_PROJECT_DIRS} ; do
if [ -d "$DIR/$PROJECT" ]; then
PROJECT_DIR=$DIR/$PROJECT
fi
done
function option { # option OPTION_NAME [DEFAULT]
VALUE=""
if [ -f "$PROJECT_DIR/.tmuxtools.toml" ]; then
# Don't worry too much about toml format yet
VALUE=$(grep -o "^$1 *= *.*$" "$PROJECT_DIR/.tmuxtools.toml" 2> /dev/null | sed -e "s/^$1 = \\(.*\\)\$/\\1/" | sed -e "s/^\"\\(.*\\)\"\$/\\1/")
fi
echo ${VALUE:-$2}
}
# Determine shell command
CMD=$(option "shell")
if [[ -n "$CMD" ]]; then
CMD="bash -l -c $(printf "%q" "$CMD")"
fi
# Add utility panes if there's enough room
UTILITY_PANES=()
UTILITY_COLUMNS=0
if [[ $COLS -gt 160 ]]; then
if ! [[ "$PROJECT_DIR" == /mnt/* ]]; then
if [ -d "$PROJECT_DIR/.docker" ]; then
UTILITY_PANES+=( "/usr/local/bin/docker stats --format \"table {{.Name}}\t{{.CPUPerc}}\t{{.MemPerc}}\"" )
fi
if [ -d "$PROJECT_DIR/.git" -o -d "$PROJECT_DIR/../.git" ]; then
UTILITY_PANES+=( "git watch-status" "git graph-branch --watch" )
fi
fi
if [ ${#UTILITY_PANES[@]} -ne 0 ]; then
UTILITY_COLUMNS=1
fi
fi
# Determine number of shell columns and panes to display
# Each shell column takes 100 characters; the utility column takes 60;
# and there is a 1-character divider between each
SHELL_COLUMNS=$(( ($COLS + 1 - ($UTILITY_COLUMNS * 61)) / 101 ))
if [ $SHELL_COLUMNS -lt 1 ]; then
SHELL_COLUMNS=1 # Always have at least one, no matter how small the window
fi
SHELL_PANES=$(option "shell_panes" $SHELL_COLUMNS)
if [ $SHELL_PANES -lt $SHELL_COLUMNS ]; then
SHELL_PANES=$SHELL_COLUMNS
fi
### Create new window ##########################################################
cd $PROJECT_DIR
TEMP_PANE_ID=$(tmux new-window -PF "#{pane_id}" ${WINDOW:+-t$WINDOW} -n $PROJECT "echo Spawning... && sleep 100")
# Display shell panes
PANE_IDS=()
for (( idx=0 ; idx<$SHELL_PANES ; idx++ )); do
if [ $idx == 0 ] || [ $idx -lt $(( $SHELL_COLUMNS - 1 )) ]; then
# Spawn a new pane to the left
TMUX_ARGS="-dhb -l 100"
elif [ $idx -eq $(( $SHELL_COLUMNS - 1 )) ]; then
# Spawn a new pane to the right
TMUX_ARGS="-dh -l 100"
else
# Spawn a new pane below the top pane in the column
PARENT_ID=$(( $idx % $SHELL_COLUMNS ))
TMUX_ARGS="-dv -t ${PANE_IDS[$PARENT_ID]}"
fi
PANE_ID=$(tmux splitw -PF "#{pane_id}" $TMUX_ARGS ${CMD:+"$CMD"})
PANE_IDS+=( $PANE_ID )
done
# Display utility panes if there's enough room
for (( idx=${#UTILITY_PANES[@]}-1 ; idx >= 0 ; idx-- )); do
tmux splitw -dv -p 50 "source ~/.profile ; ${UTILITY_PANES[idx]}"
done
tmux kill-pane -t $TEMP_PANE_ID