From d2825814b2ba93933960f31d27b303f5acc2b0a9 Mon Sep 17 00:00:00 2001 From: RIOUX Guilhem Date: Wed, 31 Jul 2024 17:11:48 +0200 Subject: [PATCH 1/3] Arsenal without tags shown --- arsenal/app.py | 3 ++ arsenal/modules/gui.py | 95 +++++++++++++++++++++++++++--------------- 2 files changed, 65 insertions(+), 33 deletions(-) diff --git a/arsenal/app.py b/arsenal/app.py index 65a37c3..445d59a 100644 --- a/arsenal/app.py +++ b/arsenal/app.py @@ -49,6 +49,7 @@ def get_args(self): group_out.add_argument('-t', '--tmux', action='store_true', help='Send command to tmux panel') group_out.add_argument('-c', '--check', action='store_true', help='Check the existing commands') group_out.add_argument('-f', '--prefix', action='store_true', help='command prefix') + group_out.add_argument('--no-tags', action='store_false', help='Whether or not to show the tags when drawing the cheats') parser.add_argument('-V', '--version', action='version', version='%(prog)s (version {})'.format(__version__)) return parser.parse_args() @@ -66,6 +67,8 @@ def run(self): self.start(args, cheatsheets) def start(self, args, cheatsheets): + arsenal_gui.Gui.with_tags = args.no_tags + # create gui object gui = arsenal_gui.Gui() while True: diff --git a/arsenal/modules/gui.py b/arsenal/modules/gui.py index e4fe63b..b07efaf 100644 --- a/arsenal/modules/gui.py +++ b/arsenal/modules/gui.py @@ -7,7 +7,7 @@ from os import sep import glob -#  local +# local from . import config from . import command @@ -85,46 +85,51 @@ def draw_cheat(win, cheat, selected): prompt = '> ' max_width = win_width - len(prompt) - len("\n") - col4_size = math.floor(max_width * 14 / 100) - col3_size = math.floor(max_width * 8 / 100) - col1_size = math.floor(max_width * 23 / 100) - col2_size = math.floor(max_width * 55 / 100) - # col0_size = math.floor(max_width * 20 / 100) - title = cheat.tags if cheat.tags != '' else cheat.str_title tags = cheat.get_tags() + columns_list = ["title", "name", "description"] + if Gui.with_tags: + columns_list = ["tags"] + columns_list + + get_col_size = lambda ratio: math.floor( (max_width * ratio) / 100) + ratios = Gui.get_ratios_for_column(columns_list) + + columns = { + "tags": { + "width": get_col_size(ratios.get("tags", 0)), + "val": tags, + "color": Gui.COL4_COLOR_SELECT if selected else Gui.COL4_COLOR + }, + "title": { + "width": get_col_size(ratios.get("title", 0)), + "val": cheat.str_title, + "color": Gui.COL3_COLOR_SELECT if selected else Gui.COL1_COLOR + }, + "name": { + "width": get_col_size(ratios.get("name", 0)), + "val": cheat.name, + "color": Gui.COL2_COLOR_SELECT if selected else Gui.COL2_COLOR + }, + "description": { + "width": get_col_size(ratios.get("description", 0)), + "val": cheat.printable_command, + "color": Gui.COL3_COLOR_SELECT if selected else Gui.COL3_COLOR + } + } + if selected: win.addstr(prompt, curses.color_pair(Gui.CURSOR_COLOR_SELECT)) - win.addstr("{:{}s}".format(Gui.draw_string(tags, col4_size), col4_size), - curses.color_pair(Gui.COL4_COLOR_SELECT)) - win.addstr("{:{}s}".format(Gui.draw_string(cheat.str_title, col3_size), col3_size), - curses.color_pair(Gui.COL3_COLOR_SELECT)) - win.addstr("{:{}s}".format(Gui.draw_string(cheat.name, col1_size), col1_size), - curses.color_pair(Gui.COL2_COLOR_SELECT)) - win.addstr("{:{}s}".format(Gui.draw_string(cheat.printable_command, col2_size), col2_size), - curses.color_pair(Gui.COL3_COLOR_SELECT)) - # win.addstr("{:{}s}".format(Gui.draw_string(title, col0_size), col0_size), - # curses.color_pair(Gui.COL1_COLOR_SELECT)) - win.addstr("\n") else: win.addstr(' ' * len(prompt), curses.color_pair(Gui.BASIC_COLOR)) - if tags.startswith('[W]'): - win.addstr("{:{}s}".format(Gui.draw_string(tags, col4_size), col4_size), - curses.color_pair(Gui.COL5_COLOR)) - else: - win.addstr("{:{}s}".format(Gui.draw_string(tags, col4_size), col4_size), - curses.color_pair(Gui.COL4_COLOR)) - win.addstr("{:{}s}".format(Gui.draw_string(cheat.str_title, col3_size), col3_size), - curses.color_pair(Gui.COL1_COLOR)) - win.addstr("{:{}s}".format(Gui.draw_string(cheat.name, col1_size), col1_size), - curses.color_pair(Gui.COL2_COLOR)) - win.addstr("{:{}s}".format(Gui.draw_string(cheat.printable_command, col2_size), col2_size), - curses.color_pair(Gui.COL3_COLOR)) - # win.addstr("{:{}s}".format(Gui.draw_string(title, col0_size), col0_size), - # curses.color_pair(Gui.COL1_COLOR)) - win.addstr("\n") + + for column_name in columns_list: + win.addstr("{:{}s}".format(Gui.draw_string(columns[column_name]["val"], + columns[column_name]["width"]), + columns[column_name]["width"]), + curses.color_pair(columns[column_name]["color"])) + win.addstr("\n") def draw_cheatslistbox(self): """ @@ -784,6 +789,9 @@ class Gui: INFO_CMD_COLOR = 0 ARG_NAME_COLOR = 5 loaded_menu = False + with_tags = False + + DEFAULT_RATIOS = {"tags": 14, "title": 8, "name": 23, "description": 55} def __init__(self): self.cheats_menu = None @@ -796,6 +804,27 @@ def init_colors(): for i in range(0, 255): curses.init_pair(i + 1, i, -1) + @classmethod + def get_ratios_for_column(cls, columns_in_use): + """ + Calculate the column size from the column to print + + :param columns_in_use: List of the column to print when drawing the + cheat + :return: The updated ratios size of each columns + """ + missing_ratio = 0 + for col in cls.DEFAULT_RATIOS.keys(): + if col not in columns_in_use: + missing_ratio += cls.DEFAULT_RATIOS.get(col) + if not missing_ratio: + return cls.DEFAULT_RATIOS + + new_ratio = {} + for column in columns_in_use: + new_ratio[column] = math.floor(cls.DEFAULT_RATIOS[column] + missing_ratio / len(columns_in_use)) + return new_ratio + @staticmethod def draw_string(str_value, max_size): """ From a1f02b739654f5cf1a5f25062d29d925f712b398 Mon Sep 17 00:00:00 2001 From: RIOUX Guilhem Date: Wed, 31 Jul 2024 17:38:57 +0200 Subject: [PATCH 2/3] Add linting syntax --- arsenal/app.py | 3 ++- arsenal/modules/config.py | 2 +- arsenal/modules/gui.py | 36 +++++++++++++++++++----------------- 3 files changed, 22 insertions(+), 19 deletions(-) diff --git a/arsenal/app.py b/arsenal/app.py index 445d59a..780bb66 100644 --- a/arsenal/app.py +++ b/arsenal/app.py @@ -49,7 +49,8 @@ def get_args(self): group_out.add_argument('-t', '--tmux', action='store_true', help='Send command to tmux panel') group_out.add_argument('-c', '--check', action='store_true', help='Check the existing commands') group_out.add_argument('-f', '--prefix', action='store_true', help='command prefix') - group_out.add_argument('--no-tags', action='store_false', help='Whether or not to show the tags when drawing the cheats') + group_out.add_argument('--no-tags', action='store_false', help='Whether or not to show the' + ' tags when drawing the cheats') parser.add_argument('-V', '--version', action='version', version='%(prog)s (version {})'.format(__version__)) return parser.parse_args() diff --git a/arsenal/modules/config.py b/arsenal/modules/config.py index 94dfcf8..b40f90e 100644 --- a/arsenal/modules/config.py +++ b/arsenal/modules/config.py @@ -24,4 +24,4 @@ savevarfile = join(HOMEPATH, ".arsenal.json") -PREFIX_GLOBALVAR_NAME = "arsenal_prefix_cmd" \ No newline at end of file +PREFIX_GLOBALVAR_NAME = "arsenal_prefix_cmd" diff --git a/arsenal/modules/gui.py b/arsenal/modules/gui.py index b07efaf..832bb48 100644 --- a/arsenal/modules/gui.py +++ b/arsenal/modules/gui.py @@ -93,31 +93,34 @@ def draw_cheat(win, cheat, selected): if Gui.with_tags: columns_list = ["tags"] + columns_list - get_col_size = lambda ratio: math.floor( (max_width * ratio) / 100) + def get_col_size(max_width, ratio): + """ + Return the column size from the given ratio + + :param max_width: The width maximal of the screen + :param ratio: The ratio of the column + """ + return math.floor((max_width * ratio) / 100) + ratios = Gui.get_ratios_for_column(columns_list) - columns = { - "tags": { - "width": get_col_size(ratios.get("tags", 0)), + columns = {"tags": {"width": get_col_size(max_width, ratios.get("tags", 0)), "val": tags, "color": Gui.COL4_COLOR_SELECT if selected else Gui.COL4_COLOR - }, - "title": { - "width": get_col_size(ratios.get("title", 0)), + }, + "title": {"width": get_col_size(max_width, ratios.get("title", 0)), "val": cheat.str_title, "color": Gui.COL3_COLOR_SELECT if selected else Gui.COL1_COLOR - }, - "name": { - "width": get_col_size(ratios.get("name", 0)), + }, + "name": {"width": get_col_size(max_width, ratios.get("name", 0)), "val": cheat.name, "color": Gui.COL2_COLOR_SELECT if selected else Gui.COL2_COLOR - }, - "description": { - "width": get_col_size(ratios.get("description", 0)), + }, + "description": {"width": get_col_size(max_width, ratios.get("description", 0)), "val": cheat.printable_command, "color": Gui.COL3_COLOR_SELECT if selected else Gui.COL3_COLOR - } - } + } + } if selected: win.addstr(prompt, curses.color_pair(Gui.CURSOR_COLOR_SELECT)) @@ -809,8 +812,7 @@ def get_ratios_for_column(cls, columns_in_use): """ Calculate the column size from the column to print - :param columns_in_use: List of the column to print when drawing the - cheat + :param columns_in_use: List of the column to print when drawing :return: The updated ratios size of each columns """ missing_ratio = 0 From 9b24d6e62a2308893c38518d5a7697c90c8a3f4e Mon Sep 17 00:00:00 2001 From: RIOUX Guilhem Date: Wed, 31 Jul 2024 17:46:20 +0200 Subject: [PATCH 3/3] Match flake8 wishes --- arsenal/modules/gui.py | 45 +++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/arsenal/modules/gui.py b/arsenal/modules/gui.py index 832bb48..3957842 100644 --- a/arsenal/modules/gui.py +++ b/arsenal/modules/gui.py @@ -106,21 +106,16 @@ def get_col_size(max_width, ratio): columns = {"tags": {"width": get_col_size(max_width, ratios.get("tags", 0)), "val": tags, - "color": Gui.COL4_COLOR_SELECT if selected else Gui.COL4_COLOR - }, + "color": Gui.COL4_COLOR_SELECT if selected else Gui.COL4_COLOR}, "title": {"width": get_col_size(max_width, ratios.get("title", 0)), "val": cheat.str_title, - "color": Gui.COL3_COLOR_SELECT if selected else Gui.COL1_COLOR - }, + "color": Gui.COL3_COLOR_SELECT if selected else Gui.COL1_COLOR}, "name": {"width": get_col_size(max_width, ratios.get("name", 0)), "val": cheat.name, - "color": Gui.COL2_COLOR_SELECT if selected else Gui.COL2_COLOR - }, + "color": Gui.COL2_COLOR_SELECT if selected else Gui.COL2_COLOR}, "description": {"width": get_col_size(max_width, ratios.get("description", 0)), "val": cheat.printable_command, - "color": Gui.COL3_COLOR_SELECT if selected else Gui.COL3_COLOR - } - } + "color": Gui.COL3_COLOR_SELECT if selected else Gui.COL3_COLOR}} if selected: win.addstr(prompt, curses.color_pair(Gui.CURSOR_COLOR_SELECT)) @@ -809,23 +804,23 @@ def init_colors(): @classmethod def get_ratios_for_column(cls, columns_in_use): - """ - Calculate the column size from the column to print + """ + Calculate the column size from the column to print - :param columns_in_use: List of the column to print when drawing - :return: The updated ratios size of each columns - """ - missing_ratio = 0 - for col in cls.DEFAULT_RATIOS.keys(): - if col not in columns_in_use: - missing_ratio += cls.DEFAULT_RATIOS.get(col) - if not missing_ratio: - return cls.DEFAULT_RATIOS - - new_ratio = {} - for column in columns_in_use: - new_ratio[column] = math.floor(cls.DEFAULT_RATIOS[column] + missing_ratio / len(columns_in_use)) - return new_ratio + :param columns_in_use: List of the column to print when drawing + :return: The updated ratios size of each columns + """ + missing_ratio = 0 + for col in cls.DEFAULT_RATIOS.keys(): + if col not in columns_in_use: + missing_ratio += cls.DEFAULT_RATIOS.get(col) + if not missing_ratio: + return cls.DEFAULT_RATIOS + + new_ratio = {} + for column in columns_in_use: + new_ratio[column] = math.floor(cls.DEFAULT_RATIOS[column] + missing_ratio / len(columns_in_use)) + return new_ratio @staticmethod def draw_string(str_value, max_size):