From 9cd3e9f94efa272c204b08ff2dcd1071b382dd5b Mon Sep 17 00:00:00 2001 From: tonybanters Date: Tue, 2 Sep 2025 23:35:33 -0700 Subject: [PATCH] initial commit --- bk-tmux-dmenu | 46 ++++++++++++++++++++++++++++++++++++++++++++++ configs-dmenu.sh | 27 +++++++++++++++++++++++++++ tmux-dmenu.sh | 37 +++++++++++++++++++++++++++++++++++++ tmux-rofi.sh | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+) create mode 100755 bk-tmux-dmenu create mode 100755 configs-dmenu.sh create mode 100755 tmux-dmenu.sh create mode 100755 tmux-rofi.sh diff --git a/bk-tmux-dmenu b/bk-tmux-dmenu new file mode 100755 index 0000000..add0602 --- /dev/null +++ b/bk-tmux-dmenu @@ -0,0 +1,46 @@ +#!/bin/sh +# dmenu-driven tmux session switcher/attacher for dwm + st +# deps: tmux, dmenu, st + +set -eu + +# List available sessions (quiet if no server/sessions) +sessions="$(tmux list-sessions -F '#S' 2>/dev/null || true)" +[ -n "${sessions}" ] || exit 0 + +# prefer rofi, fall back to dmenu +pick_session() { + if command -v rofi >/dev/null 2>&1; then + rofi -dmenu -i -p 'tmux sessions:' -lines 10 \ + -kb-row-down "Down,Control+n,j" -kb-row-up "Up,Control+p,k" + else + dmenu -i -vi -c -bw 3 -l 10 -h 30 -F -p 'tmux sessions:' + fi +} +chosen="$(printf '%s\n' "$sessions" | pick_session)" + +# DMENU="dmenu -i -vi -c -bw 3 -l 10 -h 30 -F -p 'tmux sessions:'" +# chosen="$(printf '%s\n' "$sessions" | eval "$DMENU")" + +[ -n "$chosen" ] || exit 0 + +# 1) If this script is run from inside a tmux client, switch in-place. +if [ "${TMUX-}" ]; then + exec tmux switch-client -t "${chosen}" +fi + +# 2) If any st-based tmux client exists, switch that client to the chosen session. +# (client_termname is usually 'st-256color' for st) +client_tty="$( + tmux list-clients -F '#{client_tty} #{client_termname}' 2>/dev/null \ + | awk '$2 ~ /^st/ { print $1; exit }' || true +)" + +if [ -n "${client_tty}" ]; then + # switch that client to the chosen session + tmux switch-client -c "${client_tty}" -t "${chosen}" + exit 0 +fi + +# 3) Fallback: launch a new st and attach. +exec st -e tmux attach -t "${chosen}" diff --git a/configs-dmenu.sh b/configs-dmenu.sh new file mode 100755 index 0000000..49afddf --- /dev/null +++ b/configs-dmenu.sh @@ -0,0 +1,27 @@ +#!/bin/sh +set -eu + +# Grab only directories inside ~/debian-dotfiles +configs="$(ls -1d ~/debian-dotfiles/*/ 2>/dev/null | xargs -n1 basename)" +[ -n "$configs" ] || exit 0 + +chosen="$(printf '%s\n' $configs | dmenu -i -vi -c -bw 3 -l 10 -h 30 -F -p 'configs:')" +[ -n "$chosen" ] || exit 0 + +dir="$HOME/debian-dotfiles/$chosen" + +# If inside tmux, just switch session (create it if missing) +if [ "${TMUX-}" ]; then + if tmux has-session -t "$chosen" 2>/dev/null; then + exec tmux switch-client -t "$chosen" + else + exec tmux new-session -s "$chosen" -c "$dir" "nvim ." + fi +fi + +# Outside tmux: open st, create or attach session running nvim +if tmux has-session -t "$chosen" 2>/dev/null; then + exec st -e tmux attach -t "$chosen" +else + exec st -e tmux new-session -s "$chosen" -c "$dir" "nvim ." +fi diff --git a/tmux-dmenu.sh b/tmux-dmenu.sh new file mode 100755 index 0000000..ebf1eb6 --- /dev/null +++ b/tmux-dmenu.sh @@ -0,0 +1,37 @@ +#!/bin/sh +set -eu + +sessions="$(tmux list-sessions -F '#S' 2>/dev/null || true)" +[ -n "${sessions}" ] || exit 0 + +rofi_pick_session() { + if command -v rofi >/dev/null 2>&1; then + rofi -dmenu -i -p 'tmux sessions:' -lines 10 \ + -kb-row-down "Down,Control+n,j" -kb-row-up "Up,Control+p,k" + else + dmenu -i -vi -c -bw 3 -l 10 -h 30 -F -p 'tmux sessions:' + fi +} +dmenu_pick_session() { + dmenu -i -vi -c -bw 3 -l 10 -h 30 -F -p 'tmux sessions:' +} +chosen="$(printf '%s\n' "$sessions" | dmenu_pick_session)" + +[ -n "$chosen" ] || exit 0 + +if [ "${TMUX-}" ]; then + exec tmux switch-client -t "${chosen}" +fi + +client_tty="$( + tmux list-clients -F '#{client_tty} #{client_termname}' 2>/dev/null \ + | awk '$2 ~ /^st/ { print $1; exit }' || true +)" + +if [ -n "${client_tty}" ]; then + # switch that client to the chosen session + tmux switch-client -c "${client_tty}" -t "${chosen}" + exit 0 +fi + +exec st -e tmux attach -t "${chosen}" diff --git a/tmux-rofi.sh b/tmux-rofi.sh new file mode 100755 index 0000000..c01d81a --- /dev/null +++ b/tmux-rofi.sh @@ -0,0 +1,38 @@ +#!/bin/sh +set -eu + +sessions="$(tmux list-sessions -F '#S' 2>/dev/null || true)" +[ -n "${sessions}" ] || exit 0 + +rofi_pick_session() { + if command -v rofi >/dev/null 2>&1; then + rofi -dmenu -i -p 'tmux sessions:' -lines 10 \ + -kb-row-down "Down,Control+n,j" -kb-row-up "Up,Control+p,k" + else + dmenu -i -vi -c -bw 3 -l 10 -h 30 -F -p 'tmux sessions:' + fi +} +dmenu_pick_session() { + dmenu -i -vi -c -bw 3 -l 10 -h 30 -F -p 'tmux sessions:' +} +chosen="$(printf '%s\n' "$sessions" | rofi_pick_session)" + +[ -n "$chosen" ] || exit 0 + +if [ "${TMUX-}" ]; then + exec tmux switch-client -t "${chosen}" +fi + +client_tty="$( + tmux list-clients -F '#{client_tty} #{client_termname}' 2>/dev/null \ + | awk '$2 ~ /^st/ { print $1; exit }' || true +)" + +if [ -n "${client_tty}" ]; then + # switch that client to the chosen session + tmux switch-client -c "${client_tty}" -t "${chosen}" + exit 0 +fi + +exec st -e tmux attach -t "${chosen}" +