2 files changed,
88 insertions(+),
13 deletions(-)
Author:
Oleksandr Smirnov
olexsmir@gmail.com
Committed at:
2026-02-09 02:08:42 +0200
Authored at:
2026-02-08 22:56:46 +0200
Change ID:
lvxxlykuymptqvzmqvzzstutqyqpwsnz
Parent:
0a4900f
jump to
| M | scripts/pick.sh |
| M | scripts/sessions.sh |
M
scripts/pick.sh
··· 32 32 done 33 33 } 34 34 35 +strip_session_name() { 36 + local name="${1#● }" 37 + name="${name#○ }" 38 + name="${name% (active)}" 39 + echo "$name" 40 +} 41 + 35 42 pick() { 36 - local current_session="$(get_current_session_name)" 37 - local selected="$(get_all_sessions "$current_session" | fzf)" 43 + local selected=$(get_all_sessions "$(get_current_session_name)" | fzf \ 44 + --footer="C-x: unload/kill | C-r: rename | C-n: new" \ 45 + --bind "ctrl-x:execute($0 unload {})+reload($0 list)" \ 46 + --bind "ctrl-r:execute($0 rename {})+reload($0 list)" \ 47 + --bind "ctrl-n:execute($0 new)+reload($0 list)" \ 48 + --bind "enter:accept") 49 + 38 50 [[ -z "$selected" ]] && return 0 39 51 40 - local session_name 41 - session_name=${selected#● } 42 - session_name=${session_name#○ } 43 - session_name=${session_name% (active)} 44 - 52 + local session_name="$(strip_session_name "$selected")" 45 53 if tmux has-session -t "$session_name" 2>/dev/null; then 46 54 tmux switch-client -t "$session_name" 47 55 else ··· 49 57 fi 50 58 } 51 59 52 -# TODO: unload session (save before killing) 53 -# TODO: remove unload session 54 -# TODO: rename session (rename old saves too to remove the duplicates) 55 -# TODO: create new session 60 +unload_or_kill() { 61 + local session_name="$(strip_session_name "$1")" 62 + if tmux has-session -t "$session_name" 2>/dev/null; then 63 + save_session "$session_name" 64 + tmux kill-session -t "$session_name" 65 + else 66 + confirm_delete "$session_name" 67 + fi 68 +} 69 + 70 +confirm_delete() { 71 + local session_name="$1" 72 + local save_dir="$(get_opt_dir)" 73 + local session_file="$save_dir/${session_name}_last" 74 + 75 + local result 76 + result=$(printf "Delete\nCancel" | fzf --header="Delete saved session '$session_name'?") 77 + if [[ "$result" == "Delete" ]]; then 78 + rm -f "$session_file" 79 + fi 80 +} 81 + 82 +rename() { 83 + local old_name="$(strip_session_name "$1")" 84 + local new_name=$(printf "" | fzf --prompt="Rename ""$old_name"" to " --print-query) 85 + [[ -z "$new_name" ]] && return 1 86 + rename_session "$old_name" "$new_name" 87 +} 88 + 89 +new_session() { 90 + local name=$(printf "" | fzf --prompt="New session name: " --print-query) 91 + [[ -z "$name" ]] && return 1 92 + 93 + if tmux has-session -t "$name" 2>/dev/null; then 94 + tmux switch-client -t "$name" 95 + else 96 + local cwd="$(tmux display-message -p "#{pane_current_path}")" 97 + tmux new-session -ds "$name" -c "$cwd" 98 + tmux switch-client -t "$name" 99 + fi 100 +} 101 + 56 102 main() { 57 103 case "${1:-}" in 58 104 "") pick ;; 105 + unload) unload_or_kill "$2" ;; 106 + rename) rename "$2" ;; 107 + new) new_session ;; 108 + list) get_all_sessions "$(get_current_session_name)" ;; 59 109 esac 60 110 } 61 -main 111 +main "$@"
M
scripts/sessions.sh
··· 4 4 5 5 declare S=$'\t' 6 6 7 -# === common 7 +rename_session() { 8 + local old="$1" 9 + local new="$2" 10 + local dir="$(get_opt_dir)" 11 + 12 + [[ -z "$new" || "$old" == "$new" ]] && return 1 13 + [[ -e "${dir}/${new}_last" ]] && return 1 14 + 15 + tmux has-session -t "$new" 2>/dev/null && return 1 16 + tmux rename-session -t "$old" "$new" 2>/dev/null 17 + 18 + local old_last="${dir}/${old}_last" 19 + [[ -L "$old_last" ]] && { 20 + local actual="$(readlink "$old_last")" 21 + local new_actual="${dir}/${new}_$(basename "$actual" | cut -d_ -f2-)" 22 + mv "$actual" "$new_actual" 23 + ln -sf "$new_actual" "${dir}/${new}_last" 24 + rm "$old_last" 25 + } 26 +} 8 27 9 28 # === save 10 29 save_cwd() { ··· 81 100 tmux list-sessions -F "#{session_name}" | while read -r session; do 82 101 save_session "$session" 83 102 done 103 +} 104 + 105 +unload_session() { 106 + local session_name="$1" 107 + save_session "$session_name" 108 + tmux kill-session -t "$session_name" 84 109 } 85 110 86 111 # === restore