From 6bc44363dec8a54582b6f57623bb4ae88e6095b1 Mon Sep 17 00:00:00 2001 From: Guillaume Raffy Date: Wed, 17 Jun 2026 10:32:07 +0200 Subject: [PATCH] refactored cssh (added ITerminalLauncher) this actract class better organises the code, and will allow a clear separation between macosx functions and gnome related functions work related to [https://bugzilla.ipr.univ-rennes.fr/show_bug.cgi?id=4405] --- home/bin/cssh | 58 +++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/home/bin/cssh b/home/bin/cssh index ce04dc7..8bf1cb4 100755 --- a/home/bin/cssh +++ b/home/bin/cssh @@ -4,6 +4,7 @@ from typing import Optional, List import argparse import re import subprocess +import abc class RgbColor(): @@ -40,6 +41,20 @@ class SshTarget(): target_as_str = f'{self.user_id}@{target_as_str}' return target_as_str + +TerminalLabel: str # label used as the title and the color profile of the terminal + +class ITerminalLauncher(abc.ABC): + + @abc.abstractmethod + def launch_terminal(self, terminal_label: TerminalLabel, bg_color: RgbColor, command: List[str]): + ''' + terminal_label: eg 'alambix50.ipr.univ-rennes.fr' + command: the command to execute in the terminal eg ['/usr/bin/ssh', 'root@alambix50.ipr.univ-rennes.fr'] + ''' + + + # ''' # function getHostRealFqdn() # { @@ -262,6 +277,23 @@ class GnomeTerminal(): subprocess.run(['dconf', 'write', f"{GnomeTerminal.DCONF_PROFILES_PATH}/{profile_uuid}/background-color", f'({bg_color.red}, {bg_color.green}, {bg_color.blue})'], check=True, stdout=subprocess.PIPE) +class GnomeTerminalLauncher(ITerminalLauncher): + + def launch_terminal(self, terminal_label: TerminalLabel, bg_color: RgbColor, command: List[str]): + ''' + command: the command to execute in the terminal eg ['/usr/bin/ssh', f'{str(ssh_target)}'] + ''' + + GnomeTerminal.set_terminal_profile_bg_color(terminal_label, bg_color) + + profile_uuid = GnomeTerminal.get_terminal_profile_uuid(terminal_label) + + launch_term_command = ['gnome-terminal', f'--window-with-profile={profile_uuid}', f'--title={terminal_label}', '--'] + command + logging.debug('launch_term_command = %s', launch_term_command) + subprocess.run(launch_term_command, check=True) + + + def main(): logging.basicConfig(level=logging.DEBUG) arg_parser = argparse.ArgumentParser('cssh (colored ssh) opens a terminal window with a background color that depends on the fully qualified domain name of the target machine') @@ -277,10 +309,12 @@ def main(): # AS_ROOT='true' # fi + color_saturation = 0.3 color_value = 0.2 proc = subprocess.run(['uname'], check=True, stdout=subprocess.PIPE) os_name = proc.stdout.decode('utf8').strip() + terminal_launcher: Optional[ITerminalLauncher] = None if os_name == 'Darwin': raise NotImplementedError() # 'Darwin') @@ -292,22 +326,20 @@ def main(): # /usr/bin/ssh "$@" # ;; elif os_name == 'Linux': - proc = subprocess.run(['make_color.py', ssh_target.host_id, str(color_value), str(color_saturation), 'linux'], check=True, stdout=subprocess.PIPE) - bg_color_as_str = proc.stdout.decode('utf8').strip() - match = re.match(r'\((?P[0-9]+), (?P[0-9]+), (?P[0-9]+)\)', bg_color_as_str) - assert match, f'unexpected color as string: {bg_color_as_str}' - bg_color = RgbColor(match['red'], match['green'], match['blue']) - logging.debug('bg_color = %s', str(bg_color)) # eg (35, 43, 51) - - GnomeTerminal.set_terminal_profile_bg_color(ssh_target.host_id, bg_color) - profile_uuid = GnomeTerminal.get_terminal_profile_uuid(ssh_target.host_id) - - launch_term_command = ['gnome-terminal', f'--window-with-profile={profile_uuid}', f'--title={ssh_target.host_id}', '--', '/usr/bin/ssh', f'{str(ssh_target)}'] - logging.debug('launch_term_command = %s', launch_term_command) - subprocess.run(launch_term_command, check=True) + terminal_launcher = GnomeTerminalLauncher() else: raise NotImplementedError(f'unhandled os : "{os_name}"') + proc = subprocess.run(['make_color.py', ssh_target.host_id, str(color_value), str(color_saturation), os_name.lower()], check=True, stdout=subprocess.PIPE) + bg_color_as_str = proc.stdout.decode('utf8').strip() + match = re.match(r'\((?P[0-9]+), (?P[0-9]+), (?P[0-9]+)\)', bg_color_as_str) + assert match, f'unexpected color as string: {bg_color_as_str}' + bg_color = RgbColor(match['red'], match['green'], match['blue']) + logging.debug('bg_color = %s', str(bg_color)) # eg (35, 43, 51) + + + command = ['/usr/bin/ssh', f'{str(ssh_target)}'] + terminal_launcher.launch_terminal(terminal_label=ssh_target.host_id, bg_color=bg_color, command=command) main() \ No newline at end of file