Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
should probably use one of the tmux plugins, but chatgpt helped with me with this one and it's easy to understand
- Loading branch information
1 parent
820ff0b
commit 68e26ed
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#!/usr/bin/env zsh | ||
|
||
# initial session name | ||
session_name=${PWD:t} | ||
|
||
# session counter | ||
counter=0 | ||
|
||
while [[ $counter -lt 10 ]]; do | ||
if [[ $counter -gt 0 ]]; then | ||
session="${session_name} $counter" | ||
else | ||
session="${session_name}" | ||
fi | ||
|
||
# if the session doesn't exist, create it | ||
if ! tmux has-session -t "$session" 2>/dev/null; then | ||
tmux new -ADs "$session" | ||
break | ||
fi | ||
|
||
# if the session exists but isn't attached, attach it | ||
if [ -z "$(tmux list-sessions -F "#{session_name} #{session_attached}" | grep "^$session 1")" ]; then | ||
tmux attach -t "$session" | ||
break | ||
fi | ||
|
||
counter=$((counter + 1)) | ||
done | ||
|
||
if [[ $counter -ge 10 ]]; then | ||
echo "Could not find an unattached session after 10 attempts." | ||
fi |