2024-09-25 20:33:55 +00:00
|
|
|
{ pkgs }:
|
|
|
|
|
2024-12-06 12:38:40 +00:00
|
|
|
pkgs.writeShellApplication {
|
2024-12-15 10:16:25 +00:00
|
|
|
name = "tmux-sessionizer";
|
2024-09-25 20:33:55 +00:00
|
|
|
|
2024-12-06 12:18:38 +00:00
|
|
|
runtimeInputs = with pkgs; [ tmux ];
|
2024-09-25 20:33:55 +00:00
|
|
|
|
|
|
|
text = ''
|
2024-12-06 12:38:40 +00:00
|
|
|
set +o nounset
|
|
|
|
|
|
|
|
# Based on https://github.com/ThePrimeagen/tmux-sessionizer.
|
2024-12-06 12:18:38 +00:00
|
|
|
|
|
|
|
switch_to() {
|
|
|
|
if [[ -z $TMUX ]]; then
|
|
|
|
tmux attach-session -t "$1"
|
|
|
|
else
|
|
|
|
tmux switch-client -t "$1"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
has_session() {
|
|
|
|
tmux list-sessions | grep -q "^$1:"
|
|
|
|
}
|
|
|
|
|
|
|
|
hydrate() {
|
2024-12-16 17:09:03 +00:00
|
|
|
if [ -f "$2/.tmux-sessionizer" ]; then
|
2024-12-06 12:18:38 +00:00
|
|
|
tmux send-keys -t "$1" "source $2/.tmux-sessionizer" Enter
|
|
|
|
elif [ -f "$HOME/.tmux-sessionizer" ]; then
|
|
|
|
tmux send-keys -t "$1" "source $HOME/.tmux-sessionizer" Enter
|
|
|
|
fi
|
|
|
|
}
|
2024-10-28 16:29:22 +00:00
|
|
|
|
|
|
|
if [[ $# -eq 1 ]]; then
|
2024-12-06 12:18:38 +00:00
|
|
|
selected=$1
|
2024-10-28 16:29:22 +00:00
|
|
|
else
|
2024-12-16 17:59:37 +00:00
|
|
|
selected=$(find -L ~/ ~/Code ~/Code/personal ~/Code/os ~/Documents -mindepth 1 -maxdepth 1 -type d ! -name "*.old" | fzf)
|
2024-10-28 16:29:22 +00:00
|
|
|
fi
|
|
|
|
|
2024-12-06 12:18:38 +00:00
|
|
|
if [[ -z "$selected" ]]; then
|
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
|
|
|
selected_name="$(basename "$selected" | tr . _)"
|
|
|
|
tmux_running="$(pgrep tmux)"
|
2024-10-28 16:29:22 +00:00
|
|
|
|
2024-12-06 12:18:38 +00:00
|
|
|
if [[ -z $TMUX ]] && [[ -z "$tmux_running" ]]; then
|
|
|
|
tmux new-session -s "$selected_name" -c "$selected"
|
|
|
|
hydrate "$selected_name" "$selected"
|
2024-10-28 16:29:22 +00:00
|
|
|
exit 0
|
|
|
|
fi
|
|
|
|
|
2024-12-06 12:18:38 +00:00
|
|
|
if ! has_session "$selected_name"; then
|
|
|
|
tmux new-session -ds "$selected_name" -c "$selected"
|
|
|
|
hydrate "$selected_name" "$selected"
|
|
|
|
fi
|
|
|
|
|
|
|
|
switch_to "$selected_name"
|
2024-09-25 20:33:55 +00:00
|
|
|
'';
|
|
|
|
}
|