#!/bin/bash # # A lightweight session manager. # Open a work session, which might consist of VIM, tmux, etc sessions. # # Parameters NAME="$0" BASENAME="`basename $0`" IGNORE="$HOME/.config/session-ignore" function __session_not_ignored { if [ -z "$1" ]; then return 1 fi if grep -q "^$1$" $IGNORE; then return 1 fi return 0 } function __session_query { # VIM if __session_not_ignored vim && [ -e "$HOME/.local/share/vim/sessions" ]; then ls -1 $HOME/.local/share/vim/sessions | sed -e 's/\.vim//g' fi # Tmux if __session_not_ignored tmux && [ -d "$HOME/.tmux" ]; then ls -1 $HOME/.tmux fi # Screen #if __session_not_ignored screen && [ -d "$HOME/.screen" ]; then # ls -1 $HOME/.screen #fi # Luakit if __session_not_ignored luakit && [ -d "$HOME/.local/share/luakit/sessions" ]; then ls -1 $HOME/.local/share/luakit/sessions fi # Qutebrowser if __session_not_ignored qutebrowser && [ -d "$HOME/.local/share/qutebrowser" ]; then ls -1 $HOME/.local/share/qutebrowser fi # Chromium if __session_not_ignored chromium && [ -d "$HOME/.config/chromium-profiles" ]; then ls -1 $HOME/.config/chromium-profiles fi # Firefox if __session_not_ignored firefox && [ -d "$HOME/.mozilla/firefox/profiles" ]; then ls -1 $HOME/.mozilla/firefox/profiles fi } function __session_list { n="0" __session_query | sort | uniq | while read session; do echo -en "$n. $session:" # Check VIM session if __session_not_ignored vim && [ -e "$HOME/.local/share/vim/sessions/$session.vim" ]; then echo -n " vim" #else # echo " -" fi # Check tmux session if __session_not_ignored tmux && [ -e "$HOME/.tmux/$session" ]; then echo -n " tmux" #else # echo " -" fi # Screen if __session_not_ignored screen && [ -e "$HOME/.screen/$session" ]; then echo -n " screen" #else # echo " -" fi # Luakit if __session_not_ignored luakit && [ -e "$HOME/.local/share/luakit/sessions/$session" ]; then echo -n " luakit" #else # echo " -" fi # Qutebrowser if __session_not_ignored qutebrowser && [ -d "$HOME/.local/share/qutebrowser/$session" ]; then echo -n " qutebrowser" #else # echo " -" fi # Chromium if __session_not_ignored chromium && [ -d "$HOME/.config/chromium-profiles/$session" ]; then echo -n " chromium" #else # echo " -" fi # Firefox if __session_not_ignored firefox && [ -d "$HOME/.mozilla/firefox/profiles/$session" ]; then echo -n " firefox" #else # echo " -" fi echo "" let ++n done | column -t -c 6 #| sed -e 's/^/\t/' } function __session_chooser { __session_list read -rep "Choose session: " n if [ ! -z "$n" ]; then session="$(__session_list | grep "^$n." | sed -e "s/^$n. //" | cut -d : -f 1)" if [ ! -z "$session" ]; then $NAME $session fi fi } function __session_open { SESSION="$1" # Check VIM session if __session_not_ignored vim && [ -e "$HOME/.local/share/vim/sessions/$SESSION.vim" ]; then if which vim &> /dev/null; then terminal vim -S $HOME/.local/share/vim/sessions/$SESSION.vim & fi fi # Check tmux session if __session_not_ignored tmux && [ -e "$HOME/.tmux/$SESSION" ]; then if which tmux &> /dev/null; then terminal shell $SESSION & fi fi # Screen #if __session_not_ignored screen && [ -e "$HOME/.screen/$SESSION" ]; then # if which screen &> /dev/null; then # terminal shell $SESSION & # fi #fi # Luakit # TODO: how to open luakit in a given session from the command line? #if __session_not_ignored luakit && [ -e "$HOME/.local/share/luakit/sessions/$SESSION" ]; then # if which luakit &> /dev/null; then # fi #fi # Qutebrowser if __session_not_ignored qutebrowser && [ -d "$HOME/.local/share/qutebrowser/$SESSION" ]; then if which qutebrowser &> /dev/null; then qutebrowser -r $SESSION fi fi # Chromium if __session_not_ignored chromium && [ -d "$HOME/.config/chromium-profiles/$SESSION" ]; then if which chromium-profile &> /dev/null; then chromium-profile $SESSION fi fi # Firefox if __session_not_ignored firefox && [ -d "$HOME/.mozilla/firefox/profiles" ]; then firefox-profile $SESSION fi } # Dispatch if [ "$BASENAME" == "session-list" ]; then __session_list elif [ ! -z "$1" ] && [ "$BASENAME" == "session-chooser" ]; then __session_chooser elif [ ! -z "$1" ] && echo "$1" | grep -q -- '--list'; then __session_list elif [ ! -z "$1" ] && echo "$1" | grep -q -- '--chooser'; then __session_chooser elif [ -z "$1" ]; then echo "usage: $BASENAME [--list|--chooser] " echo "available sessions:" echo "" __session_list exit 1 else __session_open $* fi