From 85c3c5c94766e8d1aedc752931f493dcff940ff2 Mon Sep 17 00:00:00 2001 From: Sebastian Lohff Date: Thu, 27 Aug 2020 01:57:02 +0200 Subject: [PATCH] Make option prefix/suffix configurable This can be used to add some text surrounding options or to remove the space between cursor and option item. --- clintermission/climenu.py | 13 +++++++++---- examples/basic_usage.py | 7 +++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/clintermission/climenu.py b/clintermission/climenu.py index eeb9f93..2f24632 100644 --- a/clintermission/climenu.py +++ b/clintermission/climenu.py @@ -83,13 +83,16 @@ class CliMenu: cls.default_cursor = cursor def __init__(self, options=None, header=None, cursor=None, style=None, - indent=2, dedent_selection=False, initial_pos=0): + indent=2, dedent_selection=False, initial_pos=0, + option_prefix=' ', option_suffix=''): self._items = [] self._item_num = 0 self._ran = False self._success = None self._pos = 0 self._initial_pos = initial_pos + self._option_prefix = option_prefix + self._option_suffix = option_suffix self._option_indent = indent self._header_indent = indent self._dedent_selection = dedent_selection @@ -163,14 +166,16 @@ class CliMenu: # cursor indent = '' prefix = '' + suffix = '' if item.focusable: indent += ' ' * self._option_indent + suffix = self._option_suffix if ti.lineno == self._pos: - prefix += '{} '.format(self._cursor) + prefix += '{}{}'.format(self._cursor, self._option_prefix) style = s.highlight_style else: - prefix += ' ' * (len(self._cursor) + 1 + 1 * self._dedent_selection) + prefix += ' ' * len(self._cursor) + self._option_prefix + ' ' * self._dedent_selection style = s.option_style else: if item.indent: @@ -180,7 +185,7 @@ class CliMenu: items = [(s if s else style, t) for s, t in ti.fragments] prefix = self._transform_prefix(item, ti.lineno, prefix) - return Transformation([('', indent), (style, prefix)] + items) + return Transformation([('', indent), (style, prefix)] + items + [(style, suffix)]) def next_item(self, direction): if not any(item.focusable for item in self._items): diff --git a/examples/basic_usage.py b/examples/basic_usage.py index b14b2ef..08882e9 100644 --- a/examples/basic_usage.py +++ b/examples/basic_usage.py @@ -68,3 +68,10 @@ print() # --- with shortcut, shows no menu when only a single option is provided (can be disabled with return_single=False) --- result = cli_select_item(["Single Foo"]) print("Directly selected for you as it was the only option:", result) +print() + +# --- prefix/suffix --- +q = ["Foo", "Bar", "Baz"] +m = CliMenu(q, "Time to choose:\n", option_prefix=' <<<', option_suffix='>>>') +print("You selected", m.get_selection()) +print()