diff --git a/session/_session b/session/_session index c1603f9..c5df710 100644 --- a/session/_session +++ b/session/_session @@ -4,6 +4,8 @@ __session_sessions() { list() { for item in $HOME/.local/share/tmux/layouts/session-*; do item=${item#$HOME/.local/share/tmux/layouts/} + # session-project is parameterized by -p, not a session of its own + [ "$item" = session-project ] && continue echo ${item#session-} done } @@ -12,6 +14,10 @@ __session_sessions() { _describe 'session' sessions } +__session_projects() { + _files -W $HOME/Projects -/ +} + __session_hosts() { list() { declare -A hosts @@ -28,5 +34,7 @@ __session_hosts() { } _arguments \ + '(- *)'{-h,--help}'[Show usage information]' \ + '(- *)'{-p,--project}'[Create or attach to a session for a project in ~/Projects]:project:__session_projects' \ ':session:__session_sessions' \ ':host:__session_hosts' diff --git a/session/session.plugin.zsh b/session/session.plugin.zsh index 658b21f..2d8feb1 100644 --- a/session/session.plugin.zsh +++ b/session/session.plugin.zsh @@ -1,12 +1,48 @@ session() { if [[ "$1" == "" ]]; then - echo "usage: session [-h] []" + echo "usage: session [-h] [] + session -p " elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then echo "usage: session [-h] [] + session -p Create or attach to a tmux session by name either locally on on a remote host via ssh. + +With -p create or attach to a session for , a path in ~/Projects with +the prefix removed, which must be a git repository. The session is named after +the project and holds a home window plus a window at the project, named after +its default branch. " + elif [[ "$1" == "-p" ]] || [[ "$1" == "--project" ]]; then + local project=${2%/} + if [[ "$project" == "" ]]; then + echo "$fg[red]error:$reset_color missing argument" + return 1 + fi + if [[ "$3" != "" ]]; then + echo "$fg[red]error:$reset_color invalid argument: $3" + return 1 + fi + local repo=$HOME/Projects/$project + if [ ! -d "$repo" ]; then + echo "$fg[red]error:$reset_color no such project: $project" + return 1 + fi + if ! git -C "$repo" rev-parse --git-dir &>/dev/null; then + echo "$fg[red]error:$reset_color not a git repository: $repo" + return 1 + fi + # tmux rewrites `.` and `:` in session names, so mirror that to keep the + # name we create and the name we target in sync. + local name=${project//[.:]/_} + if [[ "$TMUX" == "" ]]; then + tmux new-session -As "$name" -c $HOME -e "TMUX_PROJECT=$project" + else + tmux has-session -t "=$name" &> /dev/null || \ + tmux new-session -Ads "$name" -c $HOME -e "TMUX_PROJECT=$project" + tmux switch-client -t "=$name" + fi else local name=$1 local host=$2