mirror of
https://codeberg.org/leana8959/.files.git
synced 2025-12-06 14:49:14 +00:00
127 lines
3.2 KiB
Nix
127 lines
3.2 KiB
Nix
{
|
|
writeShellApplication,
|
|
tmux,
|
|
procps,
|
|
fzf,
|
|
gnused,
|
|
lib,
|
|
symlinkJoin,
|
|
}: let
|
|
tmux-register-session = writeShellApplication {
|
|
name = "__tmux-register-session";
|
|
runtimeInputs = [tmux];
|
|
text = ''
|
|
last=/tmp/TMUX_LAST
|
|
|
|
# bail if tmux not running
|
|
if ! pgrep tmux >/dev/null 2>&1; then
|
|
exit 1
|
|
fi
|
|
|
|
this="$(tmux display-message -p '#S')"
|
|
if [ ! -f "$last" ] || [ "$(cat "$last")" != "$this" ]; then
|
|
echo "$this" >"$last"
|
|
fi
|
|
'';
|
|
};
|
|
|
|
tmux-maybe-create = writeShellApplication {
|
|
name = "__tmux-maybe-create";
|
|
runtimeInputs = [procps tmux];
|
|
text = ''
|
|
session_name="$1"
|
|
session_dir="$2"
|
|
|
|
if ! pgrep tmux >/dev/null 2>&1 || ! tmux has -t="$session_name" 2>/dev/null; then
|
|
tmux new-session -ds "$session_name" -c "$session_dir"
|
|
fi
|
|
'';
|
|
};
|
|
|
|
tmux-attach-or-switch = writeShellApplication {
|
|
name = "__tmux-attach-or-switch";
|
|
runtimeInputs = [tmux];
|
|
text = ''
|
|
session_name="$1"
|
|
TMUX=''${TMUX:-}
|
|
|
|
if [ -z "$TMUX" ]; then
|
|
tmux attach-session -t "$session_name"
|
|
else
|
|
tmux switch-client -t "$session_name"
|
|
fi
|
|
'';
|
|
};
|
|
|
|
tmux-last = writeShellApplication {
|
|
name = "tmux-last";
|
|
text = ''
|
|
tmux_last=/tmp/TMUX_LAST
|
|
if [ ! -f $tmux_last ]; then
|
|
echo "Last session is not yet set"
|
|
return 1
|
|
fi
|
|
|
|
session_name="$(cat $tmux_last)"
|
|
session_path="$session_name"
|
|
if [ ! -d "$session_path" ]; then
|
|
session_path="/tmp"
|
|
fi
|
|
|
|
# bail if not in tmux, nothing to register
|
|
${lib.getExe tmux-register-session} || :
|
|
${lib.getExe tmux-maybe-create} "$session_name" "$session_path"
|
|
${lib.getExe tmux-attach-or-switch} "$session_name"
|
|
'';
|
|
};
|
|
|
|
tmux-sessionizer = writeShellApplication {
|
|
name = "tmux-sessionizer";
|
|
runtimeInputs = [fzf gnused];
|
|
text = ''
|
|
selected=$(
|
|
{
|
|
fd . "$REPO_PATH" --exact-depth 2 --hidden --type d
|
|
fd . "$WORKTREE_PATH" --exact-depth 4 --hidden --type d
|
|
fd . "$PLAYGROUND_PATH" --exact-depth 1 --hidden --type d
|
|
echo "dotfiles"
|
|
echo "password-store"
|
|
} 2> /dev/null | sed -e "s|^$HOME|~|" | fzf)
|
|
selected=''${selected//\~/$HOME}
|
|
|
|
if [ -z "$selected" ]; then
|
|
return 0
|
|
fi
|
|
|
|
# derive session name based on selected path
|
|
# dots are meaningful in tmux, remove them
|
|
session_name=''${selected//./_}
|
|
|
|
# fixup special cases, override derived session_name if necessary
|
|
# this should be id for non-special cases
|
|
case "$selected" in
|
|
dotfiles)
|
|
selected=~/.dotfiles
|
|
;;
|
|
password-store)
|
|
selected="$PASSWORD_STORE_DIR"
|
|
;;
|
|
esac
|
|
|
|
# effects
|
|
${lib.getExe tmux-register-session} || :
|
|
${lib.getExe tmux-maybe-create} "$session_name" "$selected"
|
|
${lib.getExe tmux-attach-or-switch} "$session_name"
|
|
'';
|
|
};
|
|
in
|
|
symlinkJoin {
|
|
name = "tmux-sessionizer";
|
|
paths = [
|
|
tmux-register-session
|
|
tmux-maybe-create
|
|
tmux-attach-or-switch
|
|
tmux-last
|
|
tmux-sessionizer
|
|
];
|
|
}
|