Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
executable file 123 lines (99 sloc) 3.27 KB
#!/usr/bin/env zsh
#
# Tmux launcher
#
# See: https://gist.github.com/WoLpH/3163775
#
# Modified version of a script orginally found at:
# http://github.com/brandur/tmux-extra
# http://forums.gentoo.org/viewtopic-t-836006-start-0.html
#
# Works because zsh automatically trims by assigning to variables and passing
# arguments
trim() { echo $1; }
# List all sessions only if there is a server running
sessions=$(tmux ls 2> /dev/null)
# Clean up old sessions
old_sessions=$(echo "$sessions" \
| grep -v '(attached)' \
| awk -F ':' '{print $1}' \
| grep -E -- '-[0-9]{14}$')
if [ -n "$old_sessions" ]; then
echo "$old_sessions" | xargs -L1 tmux kill-session -t
fi
function available_sessions(){
sessions=$(tmux ls 2> /dev/null | grep -Ei "^[a-z]")
echo "Available sessions:"
echo "$sessions" | grep -vE -- '-[0-9]{14}:'
exit
}
if [[ -z "$1" ]]; then
echo "Specify session name as the first argument"
echo
available_sessions
exit
fi
# Only because I often issue `ls` to this script by accident
if [[ "$1" == "ls" ]]; then
available_sessions
fi
base_session="$1"
test -n "$ITERM_PROFILE" && echo -ne "\e]1;$base_session\a"
# Set terminal name
title "$1"
# This actually works without the trim() on all systems except OSX
tmux_nb=$(trim `echo "$sessions" | grep "^$base_session:" | wc -l`)
if [[ "$tmux_nb" == "0" ]]; then
# Virtualenvwrapper stores the projects in the $PROJECT_HOME directory so
# see if there's a project to activate :)
project_path="$PROJECT_HOME$1"
project_image="$project_path.sparseimage"
if [ -f "$project_image.bz2" ]; then
pbzip2 -dvv "$project_image.bz2"
fi
if [ -d "$project_path" ] || [ -f "$project_image" ]; then
. ~/bin/mount_workon
workon "$1"
cd "$PROJECT_HOME$1"
fi
if [ -d "$project_path" ]; then
export HISTFILE="$PROJECT_HOME$1/.zshhistory"
if [ ! -f "$HISTFILE" ]; then
cp ~/.zshhistory "$HISTFILE"
fi
fi
echo "Launching tmux base session $base_session ..."
rc="$project_path/.tmuxrc"
if [ -f $rc ]; then
. $rc
else
tmux -2 new-session -s $base_session
fi
else
# Make sure we are not already in a tmux session
if [[ -z "$TMUX" ]]; then
# Kill defunct sessions first
old_sessions=$(tmux ls 2>/dev/null | egrep "^[0-9]{14}.*[0-9]+\)$" | cut -f 1 -d:)
for old_session_id in $old_sessions; do
tmux kill-session -t $old_session_id
done
echo "Launching copy of base session $base_session ..."
# Session is is date and time to prevent conflict
session_id="${base_session}-$(date +%Y%m%d%H%M%S)"
# Create a new session (without attaching it) and link to base session
# to share windows
tmux new-session -d -t $base_session -s $session_id
# Attach to the new session
echo "Attaching tmux -2 attach-session -t $session_id"
tmux -2 attach-session -t $session_id
# When we detach from it, kill the session
#tmux kill-session -t $session_id
fi
fi
sessions=$(tmux ls 2> /dev/null && tmux ls)
sessions=$(echo "$sessions" | grep "$1")
if [[ -z "$sessions" ]]; then
. ~/bin/mount_workon
workoff "$1"
fi
test -n "$ITERM_PROFILE" && echo -ne "\e]0;\a"