Last active
April 19, 2024 22:59
-
-
Save afair/3716075 to your computer and use it in GitHub Desktop.
Tmux rails session wrapper script
This file contains hidden or 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
#!/usr/bin/env bash | |
################################################################################ | |
# Script to initiate (or connect to) a tmux session | |
############################################################################### | |
# Starts session and returns, or attaches to existing session name and exits. | |
function tm_session { # session name | |
#echo tmux start-server | |
tmux start-server | |
#echo tmux has-session -t $1 | |
tmux has-session -t $1 | |
if [ $? -eq 0 ]; then | |
echo "Session $1 already exists.exists Attaching..." | |
#sleep 1 | |
tmux attach -t $1 | |
exit 0; | |
else | |
echo "new session: $?" | |
fi | |
#echo tmux new-session -d -s $1 | |
tmux new-session -d -s $1 | |
} | |
################################################################################ | |
# w1 (3split): vim, zsh, guard | |
# w2 (vsplit): console, dbconsole | |
# w3 (hsplit): server, log | |
function tm_rails { # setup_rails appname | |
# Setup Windows. Note `cd $APPDIR` is necessary to fire .rvmrc | |
APPDIR=~/src/$1 | |
echo tmux new-window -t $1:0 -k -n edit "cd $APPDIR;$0 tm_command 'source ~/.zshrc && cd $APPDIR && vim .'" | |
tmux new-window -t $1:0 -k -n edit "cd $APPDIR;$0 tm_command 'source ~/.zshrc && cd $APPDIR && vim .'" | |
echo tmux split-window -t $1:0 -h "cd $APPDIR;$0 tm_command 'source ~/.zshrc && cd $APPDIR && bundle exec guard'" | |
tmux split-window -t $1:0 -h "cd $APPDIR;$0 tm_command 'source ~/.zshrc && cd $APPDIR && bundle exec guard'" | |
echo tmux split-window -t $1:0.1 "cd $APPDIR;zsh" | |
tmux split-window -t $1:0.1 "cd $APPDIR;zsh" | |
echo tmux new-window -t $1:1 -n console -d "cd $APPDIR;$0 tm_command 'source ~/.zshrc && cd $APPDIR && bundle exec rails console'" | |
tmux new-window -t $1:1 -n console -d "cd $APPDIR;$0 tm_command 'source ~/.zshrc && cd $APPDIR && bundle exec rails console'" | |
echo tmux split-window -t $1:1.0 -h "cd $APPDIR;$0 tm_command 'source ~/.zshrc && cd $APPDIR && bundle exec rails dbconsole'" | |
tmux split-window -t $1:1.0 -h "cd $APPDIR;$0 tm_command 'source ~/.zshrc && cd $APPDIR && bundle exec rails dbconsole'" | |
echo tmux new-window -t $1:2 -n server -d "cd $APPDIR;$0 tm_command 'source ~/.zshrc && cd $APPDIR && bundle exec rails server'" | |
tmux new-window -t $1:2 -n server -d "cd $APPDIR;$0 tm_command 'source ~/.zshrc && cd $APPDIR && bundle exec rails server'" | |
echo tmux split-window -t $1:2.0 "cd $APPDIR;$0 tm_command 'source ~/.zshrc && cd $APPDIR && tail -f log/*.log'" | |
tmux split-window -t $1:2.0 "cd $APPDIR;$0 tm_command 'source ~/.zshrc && cd $APPDIR && tail -f log/*.log'" | |
echo tmux next-window -t $1 | |
tmux next-window -t $1 | |
tmux attach -t $1 | |
exit | |
} | |
function tm_command { # runs zsh -c $1 in a loop until ^C | |
go=yes | |
echo Running zsh -c \"$1\" | |
# while [[ "$go" -ge "" ]]; do | |
zsh -c "$1" | |
# echo "Press Enter to restart, ^C to exit" | |
# read go | |
# done | |
echo complete $1 | |
zsh | |
} | |
################################################################################ | |
if [[ "$1" = "" ]]; then | |
echo "Usage: tm session_name [format] [dir]" | |
echo "" | |
echo "Current tmux sessions:" | |
tmux ls | |
exit | |
fi | |
if [[ "$1" = "tm_command" ]]; then | |
tm_command "$2" | |
exit | |
fi | |
sname=$1;shift; | |
format=$1;shift; | |
if [[ "$format" = "" ]]; then format='1'; fi | |
dir=$1;shift; | |
if [[ "$dir" = "" ]]; then dir='.'; fi | |
tm_session $sname | |
# tmux target: -t session:window.pane | |
case $format in | |
1) | |
;; | |
2) | |
tmux split-window -t $sname -h | |
;; | |
3) | |
tmux split-window -t $sname -h \; split-window -v | |
;; | |
4) | |
tmux split-window -t $sname -h \; split-window -v \; | |
tmux split-window -t $sname:0.0 -v | |
;; | |
rails) | |
tm_rails $sname | |
;; | |
kill) | |
tmux kill-session -t $sname | |
;; | |
*) | |
echo "Hey, I don't know how to configure with $format" | |
esac | |
tmux attach -t $sname | |
exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment