Add session -p <project> flag
This flag sets the session name to `<project>` where `<project>` is a git repository to make working with multiple worktrees more streamlined.
This commit is contained in:
@@ -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'
|
||||
|
||||
@@ -1,12 +1,48 @@
|
||||
session() {
|
||||
if [[ "$1" == "" ]]; then
|
||||
echo "usage: session [-h] <name> [<host>]"
|
||||
echo "usage: session [-h] <name> [<host>]
|
||||
session -p <project>"
|
||||
elif [[ "$1" == "-h" ]] || [[ "$1" == "--help" ]]; then
|
||||
echo "usage: session [-h] <name> [<host>]
|
||||
session -p <project>
|
||||
|
||||
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 <project>, 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 <project> 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
|
||||
|
||||
Reference in New Issue
Block a user