Skip to content

Instantly share code, notes, and snippets.

@kchr
Created December 8, 2016 15:27
Show Gist options
  • Save kchr/aa9e66a300b99eab2628d7a9ab4919ce to your computer and use it in GitHub Desktop.
Save kchr/aa9e66a300b99eab2628d7a9ab4919ce to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
#
# Switch tmux session with rofi menu
ROFI_SCRIPT=$( realpath $0 )
ROFI_ARGS="-bc #0A8A8A -bw 3 -fuzzy"
_filter_dupes () {
perl -ne 'if (!defined $x{$_}) { print $_; $x{$_} = 1; }'
}
_tmux_list_sessions () {
tmux list-sessions 2>/dev/null
}
tmux_sessions () {
_tmux_list_sessions | cut -d ':' -f 1
}
tmux_switch_client () {
local session; session="$1"
tmux switch-client -t $session
}
_list_projects_by_modified () {
local tempfile; tempfile="$( mktemp )"
_list_projects > $tempfile
ls -1t /data/noor/work/ | grep -f $tempfile
}
_list_projects () {
find /data/noor/work -maxdepth 1 -mindepth 1 -type d \
| sed -e 's,.*\/,,' \
| sort
}
CMD=$1
case $CMD in
choose-project)
TMUX_SESSION=$( $ROFI_SCRIPT list-projects | _filter_dupes | rofi $ROFI_ARGS -dmenu -p "Select project:" )
if [ -z "$TMUX_SESSION" ]; then
exit 0
fi
# Open project in terminal or start a new one
#/home/noor/bin/txdev $TMUX_SESSION
/home/noor/scripts/awesome/tmux_run_or_raise.sh $TMUX_SESSION
;;
choose-session)
TMUX_SESSION=$( $ROFI_SCRIPT list-sessions | rofi $ROFI_ARGS -dmenu -p "Select session:" )
if [ -z "$TMUX_SESSION" ]; then
exit 1
fi
tmux_sessions | egrep -q "^${TMUX_SESSION}$"
if [ $? -eq 0 ]; then
exit 2
fi
# Switch to existing session
tmux_switch_client $TMUX_SESSION
;;
list-project-session)
# Get all running tmux sessions
_tmux_sessions="$( tmux_sessions )"
# List all tmux sessions that belong to a project
_list_projects_by_modified | while read name; do
for session in $_tmux_sessions; do
echo $session | egrep "^${name}\$"
done
done
;;
list-projects)
# Get all active tmux sessions
_tmux_sessions="$( tmux_sessions )"
# List all tmux sessions that belong to a project
_list_projects_by_modified | while read name; do
for session in $_tmux_sessions; do
echo $session | egrep "^${name}\$"
done
done
# List all projects that did not have a tmux session
_list_projects_by_modified | while read name; do
echo $sessions | egrep -q -v "^${name}\$" && echo $name
done
;;
list-sessions)
tmux_sessions
;;
*)
echo "Unknown command: $CMD"
exit 1
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment