Add tmux session select, update other scripts

This commit is contained in:
Loïc Gremaud 2024-06-09 03:14:24 +02:00
parent d6e0fd4025
commit 953b9a791c
Signed by: Legrems
GPG Key ID: D4620E6DF3E0121D
3 changed files with 36 additions and 3 deletions

View File

@ -2,17 +2,29 @@ import configparser
import sys import sys
import time import time
import libtmux import libtmux
import argparse
from pathlib import Path from pathlib import Path
from pyfzf.pyfzf import FzfPrompt from pyfzf.pyfzf import FzfPrompt
parser = argparse.ArgumentParser()
parser.add_argument("--servers-file", "-f", help="Servers file to connect to")
parser_args = parser.parse_args()
if parser_args.servers_file:
with open(parser_args.servers_file, "r") as file:
data = file.read()
servers = []
for line in data.strip().split("\n"):
servers.append(line.strip())
config = configparser.ConfigParser() config = configparser.ConfigParser()
config_path = Path("~/.ssh-tmux-multi.ini").expanduser() config_path = Path("~/.ssh-tmux-multi.ini").expanduser()
config.read(config_path) config.read(config_path)
fav_key = "Favourite servers" fav_key = "Favourite servers"
fzf = FzfPrompt() fzf = FzfPrompt()
@ -73,7 +85,8 @@ def write_choosen_servers(servers):
with open(config_path, "w") as file: with open(config_path, "w") as file:
config.write(file) config.write(file)
servers = fzf.prompt(get_favourite_servers_first(), "--cycle --multi --print-query") if not parser_args.servers_file:
servers = fzf.prompt(["Select server to connect to"] + get_favourite_servers_first(), "--cycle --multi --print-query --header-lines 1")
# Strip from query if found else, use the query # Strip from query if found else, use the query
if len(servers) > 1: if len(servers) > 1:

20
tmux_select_session.py Normal file
View File

@ -0,0 +1,20 @@
import libtmux
import sys
from pyfzf.pyfzf import FzfPrompt
# Get active tmux sessions
srv = libtmux.Server()
fzf = FzfPrompt()
selections = fzf.prompt(["Select one session you want to switch to"] + [s.name for s in srv.sessions], "--cycle --header-lines 1")
if not selections:
sys.exit(0)
session_name = selections[0]
session = srv.sessions.filter(name=session_name)[0]
session.switch_client()
session.active_window.active_pane.display_message(f"Switched to session: {session_name}")

View File

@ -15,7 +15,7 @@ folders = [
] ]
available_folders = sh.find(*[Path(f).expanduser() for f in folders] + "-mindepth 1 -maxdepth 1 -type d".split(" ")).strip().split("\n") 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") selected = fzf.prompt(["Select a folder to create or switch session to"] + available_folders, "--cycle --header-lines 1")
if not selected: if not selected:
sys.exit(1) sys.exit(1)