-
Notifications
You must be signed in to change notification settings - Fork 0
/
workspace-lib.sh
148 lines (125 loc) · 4.02 KB
/
workspace-lib.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env bash
# Tmux workspace library - routines available inside scripts.
# stores the last session / window / pane defined by the open functions
_TMUX_SESSION=
_TMUX_WINDOW=
_TMUX_PANE=
# set to non-null to log the tmux functions executed
DEBUG=
# extra arguments injected to window / pane creation routines
TMUX_WINDOW_ARGS=(-d -P -F "#{session_name}:#{window_index}")
TMUX_SPLIT_ARGS=(-P -F "#{session_name}:#{window_index}")
# Cached properties
_TMUX_WORKDIR=
_TMUX_WORKDIR_WINDOW=
_TMUX_ENVS=()
# Sets the working directory to be used for the text workspace object
function @working-dir() {
_TMUX_WORKDIR="$1"
}
# Tmux object creation routines
function @new-session() {
local ARGS=(-P -d)
# append working directory if not specified
if ! is_option_present "-c" "$@"; then
[[ -z "$_TMUX_WORKDIR" ]] || ARGS+=(-c "$_TMUX_WORKDIR")
elif [[ -z "$_TMUX_WORKDIR" ]]; then # set the default workdir
local WORKDIR=$(get_option_value "-c" "$@")
[[ -z "$WORKDIR" ]] || _TMUX_WORKDIR="$WORKDIR"
fi
ARGS+=("$@" "${_TMUX_ENVS[@]}")
[[ -z "$DEBUG" ]] || echo "tmux new-session ${ARGS[@]}"
_TMUX_SESSION=$(tmux new-session "${ARGS[@]}")
_TMUX_WINDOW=$_TMUX_SESSION
_TMUX_PANE=$_TMUX_WINDOW
}
function @new-window() {
local ARGS=("${TMUX_WINDOW_ARGS[@]}")
# append target if not specified
is_option_present "-t" "$@" || ARGS+=(-t "$_TMUX_SESSION")
# append working directory if not specified
if ! is_option_present "-c" "$@" && [[ -n "$_TMUX_WORKDIR" ]]; then
ARGS+=(-c "$_TMUX_WORKDIR")
fi
ARGS+=("$@")
[[ -z "$DEBUG" ]] || echo "tmux new-window ${ARGS[@]}"
_TMUX_WINDOW=$(tmux new-window "${ARGS[@]}")
_TMUX_PANE=$_TMUX_WINDOW
}
function @split-window() {
local ARGS=("${TMUX_SPLIT_ARGS[@]}")
# append target if not specified
is_option_present "-t" "$@" || ARGS+=(-t "$_TMUX_WINDOW")
# append working directory if not specified
if ! is_option_present "-c" "$@" && [[ -n "$_TMUX_WORKDIR" ]]; then
ARGS+=(-c "$_TMUX_WORKDIR")
fi
ARGS+=("$@")
[[ -z "$DEBUG" ]] || echo "tmux split-window ${ARGS[@]}"
_TMUX_PANE=$(tmux split-window "${ARGS[@]}")
}
# Other tmux wrapper routines
function @send-keys() {
[[ -z "$DEBUG" ]] || echo "tmux send-keys -t $_TMUX_PANE $@"
tmux send-keys -t "$_TMUX_PANE" "$@"
}
function @select-window() {
[[ -z "$DEBUG" ]] || echo "tmux select-window $@"
tmux select-window "$@"
}
function @set-option() {
# determine option's target (window / session / pane)
local _ARGS=(-t "$_TMUX_SESSION")
for opt in "$@"; do
if [[ "$opt" == "-g" || "$opt" == "-t" ]]; then
# no target required
ARGS=(); break;
elif [[ "$opt" == "-w" ]]; then
ARGS=(-t "$_TMUX_WINDOW"); break;
elif [[ "$opt" == "-p" ]]; then
ARGS=(-t "$_TMUX_PANE"); break;
fi # defaults to session target
done
[[ -z "$DEBUG" ]] || echo "tmux set-option ${_ARGS[@]} $@"
tmux set-option "${_ARGS[@]}" "$@"
}
function @temporary-option() {
local SLEEP="$1"; shift
local VALUE="${@: -1}";
local _SAVED_OPTION=$(tmux show-option -v "${@:1:$#-1}")
@set-option "${@:1:$#-1}" off
{ sleep "$SLEEP"; @set-option "${@:1:$#-1}" "$_SAVED_OPTION"; }&
}
# sets an environment variable
# sets either a global (-g) or session (default) value
function @set-environment() {
local ARGS=("$@")
local VAR=
local VALUE=
for opt in "$@"; do
if [[ "$opt" == "-"* ]]; then continue; fi
if [[ -z "$VAR" ]]; then VAR="$opt"
else VALUE="$opt"; break; fi
done
_TMUX_ENVS+=(-e "$VAR=$VALUE")
if _check_needs_target "$@"; then ARGS=(-t "$_TMUX_SESSION" "${ARGS[@]}"); fi
tmux set-environment "${ARGS[@]}"
}
# add a path to $PATH
# works as either global (-g) or session (default) path variable
function @add-to-path() {
local ARGS=("$@")
if _check_needs_target "$@"; then ARGS+=(-t "$_TMUX_SESSION"); fi
tmux_add_to_path "${ARGS[@]}"
}
# internal functions
# returns 0 if the command needs the implicit target (-t) argument
# (for most tmux functions, no "-g" or "-t" given)
function _check_needs_target() {
local GLOBAL=
for opt in "$@"; do
if [[ "$opt" == "-g" ]]; then return 1; fi
if [[ "$opt" == "-t" ]]; then return 1; fi
done
return 0
}