diff --git a/tmux_firefox_groups.py b/tmux_firefox_groups.py new file mode 100644 index 0000000..2f43ea5 --- /dev/null +++ b/tmux_firefox_groups.py @@ -0,0 +1,65 @@ +import argparse +import configparser +import os +import subprocess +import sys +from pathlib import Path + +from pyfzf.pyfzf import FzfPrompt + +parser = argparse.ArgumentParser() +parser.add_argument( + "--no-open", + action="store_true", + help="Do not open firefox window. Mostly for debugging", +) +parser_args = parser.parse_args() + + +BASE_PATH = Path("~/.firefox/").expanduser() + +configs = {} +sections = {} +for subconffile in os.listdir(BASE_PATH): + if not subconffile.endswith(".ini"): + continue + + prefix = subconffile.replace(".ini", "").capitalize() + config = configparser.ConfigParser() + config.read(BASE_PATH / subconffile) + + configs[subconffile] = config + + for section in config.sections(): + section_name = f"[{prefix}] {section}" + sections[section_name] = { + "section": section, + "config": subconffile, + } + +fzf = FzfPrompt() +selections = fzf.prompt( + ["Select one or more group to open"] + list(sections.keys()), + "--cycle --multi --header-lines 1", +) + +if not selections: + sys.exit(0) + + +for selected_group in selections: + info = sections[selected_group] + config = configs[info["config"]] + + prefix = config[info["section"]].get("prefix", "") + suffix = config[info["section"]].get("suffix", "") + + endpoints = [ + f"{prefix}{e}{suffix}" for e in config[info["section"]]["endpoints"].split("\n") + ] + + data = ["firefox"] + for endpoint in endpoints: + data.extend(["--new-tab", endpoint]) + + subprocess.Popen(data) diff --git a/tmux_multi_ssh.py b/tmux_multi_ssh.py index ff52e34..a7a410d 100644 --- a/tmux_multi_ssh.py +++ b/tmux_multi_ssh.py @@ -1,12 +1,11 @@ +import argparse import configparser import sys import time -import libtmux -import argparse - from pathlib import Path -from pyfzf.pyfzf import FzfPrompt +import libtmux +from pyfzf.pyfzf import FzfPrompt parser = argparse.ArgumentParser() parser.add_argument("--servers-file", "-f", help="Servers file to connect to") @@ -39,7 +38,9 @@ def get_all_known_ssh_hosts(): servers.add(line.split(" ")[0]) # Check previous ssh command on histfile - with open(Path("~/.histfile").expanduser(), "r", encoding="utf-8", errors="ignore") as file: + with open( + Path("~/.histfile").expanduser(), "r", encoding="utf-8", errors="ignore" + ) as file: lines = file.read().strip().split("\n") for line in lines: @@ -59,14 +60,16 @@ def get_favourite_servers_first(): order = {} for server in servers: - order[server] = '0' + order[server] = "0" for server, uses in config[fav_key].items(): if server in order: order[server] = uses # Return the most used servers first - return [x[0] for x in sorted(order.items(), key=lambda x: int(x[1]), reverse=True)] + for count, value in order.items(): + print(count, value) + return [x[0] for x in sorted(order.items(), key=lambda x: x[1], reverse=True)] def write_choosen_servers(servers): @@ -80,13 +83,17 @@ def write_choosen_servers(servers): config[fav_key][server] = str(int(config[fav_key][server]) + 1) else: - config[fav_key][server] = '1' + config[fav_key][server] = "1" with open(config_path, "w") as file: config.write(file) + 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") + servers = fzf.prompt( + ["Select server to connect to"] + get_favourite_servers_first(), + "--cycle --multi --print-query --header-lines 1 --tmux center", + ) # Strip from query if found else, use the query if len(servers) > 1: @@ -98,8 +105,10 @@ if not servers: write_choosen_servers(servers) srv = libtmux.Server() -active_session = srv.sessions.filter(session_attached='1')[0] -window = active_session.new_window(f"ssh-multis {','.join(servers)}", window_shell=f"ssh {servers[0]}") +active_session = srv.sessions.filter(session_attached="1")[0] +window = active_session.new_window( + f"ssh-multis {','.join(servers)}", window_shell=f"ssh {servers[0]}" +) for server in servers[1:]: window.select_layout("tiled") @@ -109,7 +118,9 @@ for server in servers[1:]: time.sleep(0.1) # Confirm connection on asking panes -confirmation_needed_text = "Are you sure you want to continue connecting (yes/no/[fingerprint])?" +confirmation_needed_text = ( + "Are you sure you want to continue connecting (yes/no/[fingerprint])?" +) for pane in window.panes: pane_content = pane.capture_pane() if pane_content and confirmation_needed_text == pane_content[-1]: diff --git a/tmux_select_pane.py b/tmux_select_pane.py index 75181dc..983ae59 100644 --- a/tmux_select_pane.py +++ b/tmux_select_pane.py @@ -1,15 +1,18 @@ -import libtmux -import sys -import sh import argparse -from pprint import pprint +import sys from collections import defaultdict +from pprint import pprint - +import libtmux +import sh from pyfzf.pyfzf import FzfPrompt parser = argparse.ArgumentParser() -parser.add_argument("--no-open", action="store_true", help="Do not open tmux window. Mostly for debugging") +parser.add_argument( + "--no-open", + action="store_true", + help="Do not open tmux window. Mostly for debugging", +) parser_args = parser.parse_args() @@ -21,9 +24,9 @@ commands = defaultdict(list) all_tty = [p.pane_tty for p in srv.panes] -cmd = f"-t {' -t '.join(all_tty)} -o pid:10 -o tty:10 -o command -ww" # -f +cmd = f"-t {' -t '.join(all_tty)} -o pid:10 -o tty:10 -o command -ww" # -f -sh_commands = sh.ps(cmd.split(' ')).stdout.decode().strip().split("\n") +sh_commands = sh.ps(cmd.split(" ")).stdout.decode().strip().split("\n") # Ignore first lines (i.e: table headers) for cmd in sh_commands[1:]: @@ -58,13 +61,18 @@ def format_pane(pane): path = pane.pane_current_path.replace("/home/legrems/Documents/Arcanite", "~/D/A") path = path.replace("/home/legrems/Documents", "~/D") path = path.replace("/home/legrems", "~") - return [f"{pane.pane_tty}: [{pane.session_name}: {pane.window_name}, {path}]: {cmd['command']}"] + return [ + f"{pane.pane_tty}: [{pane.session_name}: {pane.window_name}, {path}]: {cmd['command']}" + ] panes = [] for pane in srv.panes: panes.extend(format_pane(pane)) -selections = fzf.prompt(["Select one pane you want to switch to"] + panes, "--cycle --header-lines 1") +selections = fzf.prompt( + ["Select one pane you want to switch to"] + panes, + "--cycle --header-lines 1 --tmux center", +) if not selections: sys.exit(0) diff --git a/tmux_select_session.py b/tmux_select_session.py index 970c885..392b79e 100644 --- a/tmux_select_session.py +++ b/tmux_select_session.py @@ -1,20 +1,31 @@ -import libtmux import sys - +import libtmux 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") + +mapping = {} +lm = max(len(s.name) for s in srv.sessions if s.name) +for session in srv.sessions: + string = f"({session.name: <{lm}}) {session.session_path}" + mapping[string] = session + +selections = fzf.prompt( + ["Select one session you want to switch to"] + list(mapping.keys()), + "--cycle --header-lines 1 --tmux center", +) if not selections: sys.exit(0) -session_name = selections[0] - -session = srv.sessions.filter(name=session_name)[0] +sess_str = selections[0] +session = mapping[sess_str] session.switch_client() -session.active_window.active_pane.display_message(f"Switched to session: {session_name}") + +session.active_window.active_pane.display_message( + f"Switched to session: {session.name}" +) diff --git a/tmux_sessionizer.py b/tmux_sessionizer.py index 2e2fd40..a516b3a 100644 --- a/tmux_sessionizer.py +++ b/tmux_sessionizer.py @@ -1,9 +1,8 @@ +import sys +from pathlib import Path + import libtmux import sh -import sys - - -from pathlib import Path from pyfzf.pyfzf import FzfPrompt fzf = FzfPrompt() @@ -15,8 +14,18 @@ folders = [ "~/Documents/Games/Minecraft/modded/", ] -available_folders = sh.find(*[Path(f).expanduser() for f in folders] + "-mindepth 1 -maxdepth 1 -type d".split(" ")).strip().split("\n") -selected = fzf.prompt(["Select a folder to create or switch session to"] + available_folders, "--cycle --header-lines 1") +available_folders = ( + sh.find( + *[Path(f).expanduser() for f in folders] + + "-mindepth 1 -maxdepth 1 -type d".split(" ") + ) + .strip() + .split("\n") +) +selected = fzf.prompt( + ["Select a folder to create or switch session to"] + available_folders, + "--cycle --header-lines 1 --tmux center", +) if not selected: sys.exit(1) diff --git a/tmux_ssh_group.py b/tmux_ssh_group.py index 4a5cb36..41ae969 100644 --- a/tmux_ssh_group.py +++ b/tmux_ssh_group.py @@ -1,18 +1,25 @@ -import libtmux -import configparser import argparse -import time -import sys +import configparser import os - - +import sys +import time from pathlib import Path + +import libtmux from pyfzf.pyfzf import FzfPrompt - parser = argparse.ArgumentParser() -parser.add_argument("--no-open", action="store_true", help="Do not open tmux window. Mostly for debugging") -parser.add_argument("--separate-sessions", "-s", action="store_true", help="Use a separate sessions for all of the groups") +parser.add_argument( + "--no-open", + action="store_true", + help="Do not open tmux window. Mostly for debugging", +) +parser.add_argument( + "--separate-sessions", + "-s", + action="store_true", + help="Use a separate sessions for all of the groups", +) parser_args = parser.parse_args() @@ -20,6 +27,7 @@ def get_window_name(session_name): name = session_name.replace("-", "").replace(" ", " ").replace(" ", "_") return f"{'' if parser_args.separate_sessions else 'ssh-multig '}{name}" + BASE_PATH = Path("~/.ssh-tmux/").expanduser() configs = {} @@ -42,7 +50,10 @@ for subconffile in os.listdir(BASE_PATH): } fzf = FzfPrompt() -selections = fzf.prompt(["Select one or more servers group to open"] + list(sections.keys()), "--cycle --multi --header-lines 1") +selections = fzf.prompt( + ["Select one or more servers group to open"] + list(sections.keys()), + "--cycle --multi --header-lines 1 --tmux center", +) if not selections: sys.exit(0) @@ -62,7 +73,13 @@ if parser_args.separate_sessions: else: if len(active_sessions) > 1: - session_choice = fzf.prompt(["You have multiple active tmux sessions open. Choose one to create the window on"] + [sess.name for sess in active_sessions], "--cycle --header-lines 1") + session_choice = fzf.prompt( + [ + "You have multiple active tmux sessions open. Choose one to create the window on" + ] + + [sess.name for sess in active_sessions], + "--cycle --header-lines 1", + ) if not session_choice: sys.exit(0) @@ -122,7 +139,9 @@ for selected_group in selections: time.sleep(0.05) # Confirm connection on asking panes - confirmation_needed_text = "Are you sure you want to continue connecting (yes/no/[fingerprint])?" + confirmation_needed_text = ( + "Are you sure you want to continue connecting (yes/no/[fingerprint])?" + ) for pane in window.panes: pane_content = pane.capture_pane() if pane_content and confirmation_needed_text == pane_content[-1]: