From 6cc86d7fdffb5c0506bb7ec425ccbe689957934e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Gremaud?= Date: Thu, 11 Apr 2024 13:47:53 +0200 Subject: [PATCH] Redo tmux-sessionizer in python + add small readme + small update --- README.md | 20 ++++++++++++++++++++ tmux-cht.sh | 14 -------------- tmux-sessionizer | 26 -------------------------- tmux_sessionizer.py | 30 ++++++++++++++++++++++++++++++ tmux_ssh_group.py | 11 +++++++---- 5 files changed, 57 insertions(+), 44 deletions(-) create mode 100644 README.md delete mode 100755 tmux-cht.sh delete mode 100755 tmux-sessionizer create mode 100644 tmux_sessionizer.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..f592c32 --- /dev/null +++ b/README.md @@ -0,0 +1,20 @@ +# Scripts + +## tmux-sessionizer +Inspired by [ThePrimeagen](https://github.com/ThePrimeagen/.dotfiles/blob/master/bin/.local/scripts/tmux-sessionizer), but in python + +Create/load tmux session based on the path of a project, use fzf for selection + +## tmux-ssh-group +Open multiple ssh connection to multiple servers as sudo, can pass extra commands also. +Will open a new window in tiled mode on all this servers at the same times + +Read from config files located under `~/.ssh-tmux/`, with the format: +``` +[Gestion controller] +servers = myserver1.fqdn + myserver2.fqdn + myserver3.fqdn + myserver4.fqdn +commands = ls -l +``` diff --git a/tmux-cht.sh b/tmux-cht.sh deleted file mode 100755 index d639c24..0000000 --- a/tmux-cht.sh +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env bash -selected=`cat ~/.tmux-cht-languages ~/.tmux-cht-command | fzf` -if [[ -z $selected ]]; then - exit 0 -fi - -read -p "Enter Query: " query - -if grep -qs "$selected" ~/.tmux-cht-languages; then - query=`echo $query | tr ' ' '+'` - tmux neww bash -c "echo \"curl cht.sh/$selected/$query/\" & curl cht.sh/$selected/$query & while [ : ]; do sleep 1; done" -else - tmux neww bash -c "curl -s cht.sh/$selected~$query | less" -fi diff --git a/tmux-sessionizer b/tmux-sessionizer deleted file mode 100755 index 6fdb71f..0000000 --- a/tmux-sessionizer +++ /dev/null @@ -1,26 +0,0 @@ -#!/usr/bin/env bash - -if [[ $# -eq 1 ]]; then - selected=$1 -else - selected=$(find ~/Documents/Arcanite/ ~/Documents/PolyLAN/ ~/Documents/Python ~/Documents/ -mindepth 1 -maxdepth 1 -type d | fzf) -fi - -if [[ -z $selected ]]; then - exit 0 -fi - -selected_name=$(basename "$selected" | tr . _) -tmux_running=$(pgrep tmux) - -if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then - tmux new-session -s $selected_name -c $selected - exit 0 -fi - -if ! tmux has-session -t=$selected_name 2> /dev/null; then - tmux new-session -ds $selected_name -c $selected -fi - -tmux switch-client -t $selected_name - diff --git a/tmux_sessionizer.py b/tmux_sessionizer.py new file mode 100644 index 0000000..9342972 --- /dev/null +++ b/tmux_sessionizer.py @@ -0,0 +1,30 @@ +import libtmux +import sh +import sys + + +from pathlib import Path +from pyfzf.pyfzf import FzfPrompt + +fzf = FzfPrompt() +folders = [ + "~/Documents/Arcanite/", + "~/Documents/PolyLAN/", + "~/Documents/Python/", + "~/Documents/", +] + +available_folders = sh.find(*[Path(f).expanduser() for f in folders] + "-mindepth 1 -maxdepth 1 -type d".split(" ")).strip().split("\n") +selected = fzf.prompt(available_folders, "--cycle") + +if not selected: + sys.exit(1) + +selected = selected[0] +session_name = selected.split("/")[-1] +srv = libtmux.Server() + +if not srv.has_session(session_name): + srv.new_session(session_name, attach=False, start_directory=selected) + +srv.switch_client(session_name) diff --git a/tmux_ssh_group.py b/tmux_ssh_group.py index c139555..e04ccd2 100644 --- a/tmux_ssh_group.py +++ b/tmux_ssh_group.py @@ -51,10 +51,14 @@ else: else: active_session = srv.sessions[0] -window = active_session.new_window(f"ssh-multig {','.join(servers)}", window_shell=f"ssh {servers[0]}") +window_name = f"ssh-multig {selected_group.replace(' ', '_')}" +if windows := active_session.windows.filter(name=window_name): + windows[0].select() + sys.exit(0) + +window = active_session.new_window(window_name, window_shell=f"ssh {servers[0]}") for server in servers[1:]: - window.select_layout("tiled") pane = window.split(shell=f"ssh {server}") # Wait until tmux finished working @@ -75,6 +79,5 @@ for command in extra_commands: pane.send_keys(command) window.set_window_option("synchronize-panes", "off") - window.select() -srv.cmd("select-layout", "tiled") +window.select_layout("tiled")