Skip to content

Commit

Permalink
Merge branch 'FEATURE-wenv-start-behavior-change' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
dgrisham committed Dec 29, 2020
2 parents 91f5063 + 4c09fb1 commit 576912a
Showing 1 changed file with 28 additions and 36 deletions.
64 changes: 28 additions & 36 deletions wenv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env zsh
#!/usr/bin/zsh

wenv_def() {
WENV_DIR="$SRC/wenv"
Expand Down Expand Up @@ -92,24 +92,19 @@ Run \`wenv <cmd> -h\` for more information on a given subcommand <cmd>.
wenv_start() {
local usage="\
USAGE
wenv start [-t] [-i] [-q] [-h] <wenv> - Start working environemnt <wenv>. If <wenv> is already running, attach to it.
wenv start [-i] [-q] [-h] <wenv> - Start working environemnt <wenv>. If <wenv> is already running, attach to it.
OPTIONS
-t Open the wenv in tmux. Default: true.
-i Run <wenv>'s startup function. Default: true.
-d Don't attach to the wenv's tmux session after starting. Default: false.
-h Display this help message.
"

local flag_t=1
local flag_q=0
local flag_i=1
local flag_d=0
while getopts ":tqidh" opt; do
while getopts ":qidh" opt; do
case $opt in
t)
flag_t=0
;;
q)
flag_q=1
;;
Expand All @@ -136,37 +131,34 @@ OPTIONS
fi
local wenv="$1"

# check if wenv is already running
tmux list-sessions | grep "^$wenv:" >/dev/null && {
echo "wenv $wenv already running"
((flag_d == 0)) &&
tmux attach-session -t "$wenv" \; display-message 'attached to existing session for wenv '"'$wenv'"
return 0
}

if ((flag_t == 0)); then
wenv_stop
wenv_exec $@ || return 1
((flag_i == 1)) && startup_wenv
return 0
if ! tmux list-sessions | grep "^$wenv:" >/dev/null; then # wenv isn't running yet, start it
# create tmux session for wenv
tmux new-session -d -s "$wenv"

mkdir -p /tmp/wenv
# replace / with - for wenvs in subdirectories
local tmp_start_file="/tmp/wenv/start-$(echo $wenv | perl -pe 's|/|-|')"
[[ -f "$tmp_start_file" ]] && rm -f "$tmp_start_file"
echo -e '
wenv_exec '"$@"' || return 1
tmux set-environment WENV "$WENV"
(('"$flag_i"' == 1)) && startup_wenv
clear
'> "$tmp_start_file"

tmux send -t "$wenv" "source $tmp_start_file && rm -f $tmp_start_file" ENTER

((flag_d == 1)) && { echo "started wenv '$wenv' " ; return 0 }
else # wenv is already running
((flag_d == 1)) && { echo "wenv '$wenv' already running" ; return 0 }
fi

tmux new-session -d -s "$wenv"

mkdir -p /tmp/wenv
# replace / with - for wenvs in subdirectories
local tmp_start_file="/tmp/wenv/start-$(echo $wenv | perl -pe 's|/|-|')"
[[ -f "$tmp_start_file" ]] && rm -f "$tmp_start_file"
echo -e '
wenv_exec '"$@"' || return 1
tmux set-environment WENV "$WENV"
(('"$flag_i"' == 1)) && startup_wenv
clear
'> "$tmp_start_file"

tmux send -t "$wenv" "source $tmp_start_file && rm -f $tmp_start_file" ENTER
if [[ -n $TMUX ]]; then # we're in tmux, switch to specified wenv's session
tmux switch -t "$wenv"
else # not in tmux, attach to specified wenv's session
tmux attach-session -t "$wenv"
fi

((flag_d == 0)) && tmux attach-session -t "$wenv"
return 0
}

Expand Down

0 comments on commit 576912a

Please sign in to comment.