auroraff7bd95
tms: fix attach outside tmux, quote paths, list running sessions
+17 −11
1 file changed
- M .local/bin/tms +17 −11
| @@ -1,25 +1,31 @@ | |||
| 1 | − | #!/bin/env bash | |
| 1 | + | #!/usr/bin/env bash | |
| 2 | 2 | ||
| 3 | 3 | if [[ $# -eq 1 ]]; then | |
| 4 | − | selected=$1; | |
| 4 | + | selected=$(realpath -- "$1") || exit 1 | |
| 5 | 5 | else | |
| 6 | − | selected=$(find ~/code/dev ~/code/repos ~/code/rust -mindepth 1 -maxdepth 1 -type d | fzf); | |
| 6 | + | selected=$( { | |
| 7 | + | tmux list-sessions -F '#S' 2>/dev/null | |
| 8 | + | find ~/code/dev ~/code/repos ~/code/rust -mindepth 1 -maxdepth 1 -type d | |
| 9 | + | } | fzf) | |
| 7 | 10 | fi | |
| 8 | 11 | ||
| 9 | 12 | if [[ -z $selected ]]; then | |
| 10 | 13 | exit 0 | |
| 11 | 14 | fi | |
| 12 | 15 | ||
| 13 | − | selected_name=$(basename "$selected" | tr . _) | |
| 14 | − | tmux_running=$(pgrep tmux) | |
| 16 | + | # an existing session picked from the list: just go there | |
| 17 | + | if [[ ! -d $selected ]]; then | |
| 18 | + | selected_name=$selected | |
| 19 | + | else | |
| 20 | + | selected_name=$(basename -- "$selected" | tr '.:' '__') | |
| 21 | + | fi | |
| 15 | 22 | ||
| 16 | − | if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then | |
| 17 | − | tmux new-session -s $selected_name -c $selected | |
| 18 | − | exit 0 | |
| 23 | + | if [[ -z $TMUX ]]; then | |
| 24 | + | exec tmux new-session -A -s "$selected_name" -c "$selected" | |
| 19 | 25 | fi | |
| 20 | 26 | ||
| 21 | − | if ! tmux has-session -t=$selected_name 2> /dev/null; then | |
| 22 | − | tmux new-session -ds $selected_name -c $selected | |
| 27 | + | if ! tmux has-session -t="$selected_name" 2> /dev/null; then | |
| 28 | + | tmux new-session -ds "$selected_name" -c "$selected" | |
| 23 | 29 | fi | |
| 24 | 30 | ||
| 25 | − | tmux switch-client -t $selected_name | |
| 31 | + | tmux switch-client -t="$selected_name" | |