From f5f795d6601688cf4711b006ea1d11012f95b231 Mon Sep 17 00:00:00 2001 From: Steve Beaulac Date: Sat, 6 Jun 2026 17:09:52 -0400 Subject: [PATCH] feat(tmux): new tmux session selector using rofi --- config/hypr/hyprland.lua | 40 +++++++++++++++++++++++++++--------- config/tmux/tmux.conf | 1 + script/hyprland-tmux-session | 26 +++++++---------------- 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/config/hypr/hyprland.lua b/config/hypr/hyprland.lua index d50ba46..ba0a603 100644 --- a/config/hypr/hyprland.lua +++ b/config/hypr/hyprland.lua @@ -227,6 +227,36 @@ function focus_or_launch(opts) hl.exec_cmd(cmd) end +function fetch_or_launch(opts) + local match = opts.match + local cmd = opts.cmd + local field = opts.field or "class" -- class, title, initialClass, initialTitle + local use_workspace = opts.use_workspace ~= false + + local clients = hl.get_windows() + + local active_workspace = hl.get_active_workspace() + + for _, client in ipairs(clients) do + local value = client[field] + if value and string.find(string.lower(value), string.lower(match), 1, true) then + if use_workspace and client.workspace and client.workspace.id then + hl.dispatch(hl.dsp.window.move({ + workspace = active_workspace.id, + on_current_monitor = true, + })) + end + + hl.dispatch(hl.dsp.focus({ + window = "address:" .. client.address + })) + return + end + end + + hl.exec_cmd(cmd) +end + function toggle_master() local ws = hl.get_active_workspace() if not ws then @@ -259,16 +289,6 @@ hl.bind(LEADER .. "return", function() }) end, { description = "Open terminal" }) ---hl.bind(LEADER .. "SHIFT + return", hl.dsp.exec_raw(terminal .. "tmux attach-session -t $(tmux list-windows -a -F '#S:#I' | rofi -dmenu -P Session)")) -hl.bind(LEADER .. "SHIFT + return", function() - local session = io.popen("tmux list-sessions -F '#S:#I' | rofi -dmenu -P Session"):read("*a") - hl.notification.create({ text = "Attaching to tmux session: " .. session, icon = "hint", duration = 2000 }) - focus_or_launch({ - match = "session", - field = "title", - cmd = terminal .. " -e tmux attach-session -t " .. session - }) -end, { description = "Attach to tmux session" }) hl.bind(LEADER .. "B", hl.dsp.exec_raw(browser), { description = "Open browser" }) hl.bind(LEADER .. "T", hl.dsp.exec_raw(terminal .. " -e btop"), { description = "Open system monitor" }) diff --git a/config/tmux/tmux.conf b/config/tmux/tmux.conf index 0ab5674..499605a 100644 --- a/config/tmux/tmux.conf +++ b/config/tmux/tmux.conf @@ -23,6 +23,7 @@ # auto rename windows set -g automatic-rename on + set -g automatic-rename-format '#{pane_current_command} #{b:pane_current_path}' set -g allow-rename on # start with window 1 (instead of 0) diff --git a/script/hyprland-tmux-session b/script/hyprland-tmux-session index 4bf1445..365666d 100755 --- a/script/hyprland-tmux-session +++ b/script/hyprland-tmux-session @@ -1,7 +1,9 @@ #!/bin/sh set -eu -sessions="$(tmux list-sessions -F '#S' 2>/dev/null || true)" +#sessions="$(tmux list-sessions -F '#S' 2>/dev/null || true)" +sessions="$(tmux list-windows -a -F '#S #W::#S:#I' 2>/dev/null || true)" + [ -n "${sessions}" ] || exit 0 walker_pick_session() { @@ -12,11 +14,11 @@ walker_pick_session() { rofi_pick_session() { if command -v rofi >/dev/null 2>&1; then - rofi -dmenu -p 'tmux sessions:' + rofi -i -dmenu -display-columns 1 -display-column-separator '::' -p 'tmux sessions:' fi } -chosen="$(printf '%s\n' "$sessions" | rofi_pick_session)" +chosen="$(printf '%s\n' "$sessions" | rofi_pick_session | awk -F'::' 'NR==1{print $NF}')" [ -n "$chosen" ] || exit 0 @@ -24,19 +26,5 @@ 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 ~ /xterm/ { print $1; exit }' || true -)" - -if [ -n "${client_tty}" ]; then - # switch that client to the chosen session - address=$(hyprctl clients -j | jq -r "to_entries | .[] | select(.value.class == \"foot\") | \"\(.value.address)\"") - workspace=$(hyprctl clients -j | jq -r ".[] | select(.address == \"$address\") | \"(.workspace.id)\"") - hyprctl dispatch "hl.dsp.focus({workspace = $workspace , on_current_monitor = true})" - hyprctl dispatch "hl.dsp.focus({ window = \"address:$address\" })" - tmux switch-client -c "${client_tty}" -t "${chosen}" - exit 0 -fi - - +session_name="${chosen%%:*}" +tmux select-window -t $chosen && hyprctl dispatch "focus_or_launch({match = \"$session_name\", field = \"title\", cmd = \"foot -e tmux attach-session -t $chosen\"})"