Redo tmux-sessionizer in python + add small readme + small update

This commit is contained in:
Loïc Gremaud 2024-04-11 13:47:53 +02:00
parent fd029a8ce2
commit 6cc86d7fdf
5 changed files with 57 additions and 44 deletions

20
README.md Normal file
View File

@ -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
```

View File

@ -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

View File

@ -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

30
tmux_sessionizer.py Normal file
View File

@ -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)

View File

@ -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")