Last active
December 10, 2017 02:30
-
-
Save wolph/3163775 to your computer and use it in GitHub Desktop.
Tmux start script which automatically reconnects or starts a new session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function auto_activate(){ | |
name=$(basename "$PWD") | |
project_dir="$HOME/workspace/$name" | |
env_dir="$HOME/envs/$name" | |
if [ -d "$env_dir" ]; then | |
workon "$name" | |
fi | |
} | |
auto_activate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
# If we have workon available, load it :) | |
which workon > /dev/null || . "$(which virtualenvwrapper_lazy.sh)" | |
# Rename the virtualenvwrapper version of workon so we can overwrite the behaviour | |
eval "$(echo "original_workon() {"; declare -f workon | tail -n +2)" | |
# Mount the file to that directory | |
# Since OS X doesn't like millions of files that much I had to resort to images with my projects | |
function mount_file(){ | |
dir="$1" | |
image="$dir.sparseimage" | |
# If there's an image and not a directory yet, mount the image | |
if [ -f "$image" ] && [ ! -d "$dir" ]; then | |
echo "Attaching $image to $dir" | |
hdiutil attach -mountroot $(dirname "$dir") "$image" | |
fi | |
} | |
# Wrap workon to mount the needed images and activate the virtualenv if available | |
function workon(){ | |
project_dir="$HOME/workspace/$1" | |
env_dir="$HOME/envs/$1" | |
# Mount the images | |
mount_file "$project_dir" | |
mount_file "$env_dir" | |
# No project, no virtualenv, new project I suppose? | |
if [ ! -d "$project_dir" ] && [ ! -d "$env_dir" ]; then | |
mkproject "$1" | |
cd "$project_dir" | |
# Virtualenv found, activate! | |
elif [ -d "$env_dir" ]; then | |
original_workon "$1" | |
# No virtualenv, probably not a python project | |
else | |
echo "No virtualenv $1, simply doing a cd to $project_dir" | |
test -d "$project_dir" && cd "$project_dir" | |
fi | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/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 && tmux ls) | |
if [[ -z "$1" ]]; then | |
echo "Specify session name as the first argument" | |
echo | |
echo "Available sessions:" | |
echo "$sessions" | |
exit | |
fi | |
# Only because I often issue `ls` to this script by accident | |
if [[ "$1" == "ls" ]]; then | |
echo "Available sessions:" | |
echo "$sessions" | |
exit | |
fi | |
base_session="$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 [ -d "$project_path" ] || [ -f "$project_image" ]; then | |
. ~/bin/mount_workon.sh | |
workon $1 | |
cd "$PROJECT_HOME$1" | |
fi | |
echo "Launching tmux base session $base_session ..." | |
tmux -2 new-session -s $base_session | |
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=`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 | |
tmux -2 attach-session -t $session_id | |
# When we detach from it, kill the session | |
tmux kill-session -t $session_id | |
fi | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment