Make option prefix/suffix configurable
This can be used to add some test surrounding options or to remove the space between cursor and option item.
This commit is contained in:
parent
68e6ecbe4f
commit
9ef6532c1f
|
@ -83,13 +83,16 @@ class CliMenu:
|
||||||
cls.default_cursor = cursor
|
cls.default_cursor = cursor
|
||||||
|
|
||||||
def __init__(self, options=None, header=None, cursor=None, style=None,
|
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._items = []
|
||||||
self._item_num = 0
|
self._item_num = 0
|
||||||
self._ran = False
|
self._ran = False
|
||||||
self._success = None
|
self._success = None
|
||||||
self._pos = 0
|
self._pos = 0
|
||||||
self._initial_pos = initial_pos
|
self._initial_pos = initial_pos
|
||||||
|
self._option_prefix = option_prefix
|
||||||
|
self._option_suffix = option_suffix
|
||||||
self._option_indent = indent
|
self._option_indent = indent
|
||||||
self._header_indent = indent
|
self._header_indent = indent
|
||||||
self._dedent_selection = dedent_selection
|
self._dedent_selection = dedent_selection
|
||||||
|
@ -163,14 +166,16 @@ class CliMenu:
|
||||||
# cursor
|
# cursor
|
||||||
indent = ''
|
indent = ''
|
||||||
prefix = ''
|
prefix = ''
|
||||||
|
suffix = ''
|
||||||
if item.focusable:
|
if item.focusable:
|
||||||
indent += ' ' * self._option_indent
|
indent += ' ' * self._option_indent
|
||||||
|
suffix = self._option_suffix
|
||||||
|
|
||||||
if ti.lineno == self._pos:
|
if ti.lineno == self._pos:
|
||||||
prefix += '{} '.format(self._cursor)
|
prefix += '{}{}'.format(self._cursor, self._option_prefix)
|
||||||
style = s.highlight_style
|
style = s.highlight_style
|
||||||
else:
|
else:
|
||||||
prefix += ' ' * (len(self._cursor) + 1 + 1 * self._dedent_selection)
|
prefix += ' ' * len(self._cursor) + self._option_prefix + ' ' * self._dedent_selection
|
||||||
style = s.option_style
|
style = s.option_style
|
||||||
else:
|
else:
|
||||||
if item.indent:
|
if item.indent:
|
||||||
|
@ -180,7 +185,7 @@ class CliMenu:
|
||||||
items = [(s if s else style, t) for s, t in ti.fragments]
|
items = [(s if s else style, t) for s, t in ti.fragments]
|
||||||
prefix = self._transform_prefix(item, ti.lineno, prefix)
|
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):
|
def next_item(self, direction):
|
||||||
if not any(item.focusable for item in self._items):
|
if not any(item.focusable for item in self._items):
|
||||||
|
|
|
@ -68,3 +68,10 @@ print()
|
||||||
# --- with shortcut, shows no menu when only a single option is provided (can be disabled with return_single=False) ---
|
# --- 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"])
|
result = cli_select_item(["Single Foo"])
|
||||||
print("Directly selected for you as it was the only option:", result)
|
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()
|
||||||
|
|
Loading…
Reference in New Issue