|
|
|
@ -12,23 +12,20 @@ from prompt_toolkit import search
|
|
|
|
|
from prompt_toolkit.widgets import SearchToolbar
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _CliMenuHeader:
|
|
|
|
|
class CliMenuHeader:
|
|
|
|
|
"""Hold a menu header"""
|
|
|
|
|
def __init__(self, text, indent=False, style=None):
|
|
|
|
|
def __init__(self, text, indent=False):
|
|
|
|
|
self.text = text
|
|
|
|
|
self.indent = indent
|
|
|
|
|
self.style = style
|
|
|
|
|
self.focusable = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class _CliMenuOption:
|
|
|
|
|
class CliMenuOption:
|
|
|
|
|
"""Hold a menu option"""
|
|
|
|
|
def __init__(self, text, num, item=None, style=None, highlighted_style=None):
|
|
|
|
|
def __init__(self, text, num, item=None):
|
|
|
|
|
self.text = text
|
|
|
|
|
self.num = num
|
|
|
|
|
self.item = item
|
|
|
|
|
self.style = style
|
|
|
|
|
self.highlighted_style = highlighted_style
|
|
|
|
|
self.focusable = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@ -36,9 +33,9 @@ class CliMenuCursor:
|
|
|
|
|
"""Collection of cursors pointing at the active menu item"""
|
|
|
|
|
BULLET = '●'
|
|
|
|
|
TRIANGLE = '▶'
|
|
|
|
|
ASCII_STAR = '*'
|
|
|
|
|
ASCII_ARROW = '-->'
|
|
|
|
|
ASCII_CAT = '=^.^='
|
|
|
|
|
CLI_STAR = '*'
|
|
|
|
|
CLI_ARROW = '-->'
|
|
|
|
|
CLI_CAT = '=^.^='
|
|
|
|
|
CAT = '😸'
|
|
|
|
|
ARROW = '→'
|
|
|
|
|
|
|
|
|
@ -48,86 +45,71 @@ class CliMenuStyle:
|
|
|
|
|
|
|
|
|
|
Allows to select header, option and selected option color
|
|
|
|
|
"""
|
|
|
|
|
def __init__(self, option='', highlighted='', text=''):
|
|
|
|
|
self.option = option
|
|
|
|
|
self.highlighted = highlighted
|
|
|
|
|
self.text = text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CliSelectionStyle:
|
|
|
|
|
SQUARE_BRACKETS = ('[x]', '[ ]')
|
|
|
|
|
ROUND_BRACKETS = ('(x)', '( )')
|
|
|
|
|
CHECKMARK = ('✔', '✖')
|
|
|
|
|
THUMBS = ('👍', '👎')
|
|
|
|
|
SMILEY = ('🙂', '🙁')
|
|
|
|
|
SMILEY_EXTREME = ('😁', '😨')
|
|
|
|
|
def __init__(self, option_style='', highlight_style='', header_style=''):
|
|
|
|
|
self.option_style = option_style
|
|
|
|
|
self.highlight_style = highlight_style
|
|
|
|
|
self.header_style = header_style
|
|
|
|
|
|
|
|
|
|
# self.option_color = '#aa0000'
|
|
|
|
|
# #self.highlight_color = 'fg:ansiblue bg:ansired bold'
|
|
|
|
|
# self.highlight_color = 'bold'
|
|
|
|
|
# self.cursor = ' --> '
|
|
|
|
|
# self.cursor = ' ● '
|
|
|
|
|
# self.no_cursor = ' '
|
|
|
|
|
# self.header_color = '#aa22cc'
|
|
|
|
|
# self.option_indent = 4
|
|
|
|
|
# self.header_indent = 4
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CliMenuTheme:
|
|
|
|
|
BASIC = CliMenuStyle()
|
|
|
|
|
BASIC_BOLD = CliMenuStyle(text='bold', highlighted='bold')
|
|
|
|
|
BASIC_BOLD = CliMenuStyle(header_style='bold', highlight_style='bold')
|
|
|
|
|
RED = CliMenuStyle('#aa0000', '#ee0000', '#aa0000')
|
|
|
|
|
CYAN = CliMenuStyle('cyan', 'lightcyan', 'cyan')
|
|
|
|
|
BLUE = CliMenuStyle('ansiblue', 'ansired', 'ansiblue')
|
|
|
|
|
ANSI_CYAN = CliMenuStyle('ansicyan', 'ansibrightcyan', 'ansicyan')
|
|
|
|
|
BOLD_HIGHLIGHT = CliMenuStyle(text='bold', highlighted='bold fg:black bg:white')
|
|
|
|
|
BOLD_HIGHLIGHT = CliMenuStyle(header_style='bold', highlight_style='bold fg:black bg:white')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CliMenu:
|
|
|
|
|
default_style = CliMenuTheme.BASIC
|
|
|
|
|
default_cursor = CliMenuCursor.TRIANGLE
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def set_default_style(cls, style):
|
|
|
|
|
cls.default_style = style
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def set_default_cursor(cls, cursor):
|
|
|
|
|
cls.default_cursor = cursor
|
|
|
|
|
|
|
|
|
|
def __init__(self, options=None, header=None, cursor=None, style=None,
|
|
|
|
|
indent=2, dedent_selection=False, initial_pos=0,
|
|
|
|
|
option_prefix=' ', option_suffix=''):
|
|
|
|
|
indent=2, dedent_selection=False):
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
self._cursor = cursor if cursor is not None else self.default_cursor
|
|
|
|
|
self._style = style if style is not None else self.default_style
|
|
|
|
|
self._cursor = cursor
|
|
|
|
|
if not self._cursor:
|
|
|
|
|
self._cursor = self.default_cursor
|
|
|
|
|
|
|
|
|
|
self._style = style
|
|
|
|
|
if not self._style:
|
|
|
|
|
self._style = self.default_style
|
|
|
|
|
|
|
|
|
|
if header:
|
|
|
|
|
self.add_text(header, indent=False)
|
|
|
|
|
self.add_header(header, indent=False)
|
|
|
|
|
|
|
|
|
|
if options:
|
|
|
|
|
for option in options:
|
|
|
|
|
if isinstance(option, tuple):
|
|
|
|
|
if isinstance(option, tuple) and len(option) == 2:
|
|
|
|
|
self.add_option(*option)
|
|
|
|
|
elif isinstance(option, dict):
|
|
|
|
|
self.add_option(**option)
|
|
|
|
|
elif isinstance(option, str):
|
|
|
|
|
self.add_option(option, option)
|
|
|
|
|
else:
|
|
|
|
|
raise ValueError("Option needs to be either tuple, dict or string, found '{}' of type {}"
|
|
|
|
|
.format(option, type(option)))
|
|
|
|
|
|
|
|
|
|
def add_header(self, *args, **kwargs):
|
|
|
|
|
return self.add_text(*args, **kwargs)
|
|
|
|
|
self.add_option(option, option)
|
|
|
|
|
|
|
|
|
|
def add_text(self, title, indent=True, style=None):
|
|
|
|
|
def add_header(self, title, indent=True):
|
|
|
|
|
for text in title.split('\n'):
|
|
|
|
|
self._items.append(_CliMenuHeader(text, indent=indent, style=style))
|
|
|
|
|
self._items.append(CliMenuHeader(text, indent=indent))
|
|
|
|
|
|
|
|
|
|
def add_option(self, text, item=None, style=None, highlighted_style=None):
|
|
|
|
|
self._items.append(_CliMenuOption(text, self._item_num, item=item,
|
|
|
|
|
style=style, highlighted_style=highlighted_style))
|
|
|
|
|
def add_option(self, text, item=None):
|
|
|
|
|
self._items.append(CliMenuOption(text, self._item_num, item=item))
|
|
|
|
|
self._item_num += 1
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
@ -138,7 +120,7 @@ class CliMenu:
|
|
|
|
|
return self._success
|
|
|
|
|
|
|
|
|
|
def get_options(self):
|
|
|
|
|
return [_item for _item in self._items if isinstance(_item, _CliMenuOption)]
|
|
|
|
|
return [_item for _item in self._items if isinstance(_item, CliMenuOption)]
|
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def num_options(self):
|
|
|
|
@ -147,6 +129,7 @@ class CliMenu:
|
|
|
|
|
def get_selection(self):
|
|
|
|
|
if self.success:
|
|
|
|
|
item = self._items[self._pos]
|
|
|
|
|
|
|
|
|
|
return (item.num, item.item)
|
|
|
|
|
else:
|
|
|
|
|
return (None, None)
|
|
|
|
@ -157,46 +140,41 @@ class CliMenu:
|
|
|
|
|
def get_selection_item(self):
|
|
|
|
|
return self.get_selection()[1]
|
|
|
|
|
|
|
|
|
|
def _transform_prefix(self, item, lineno, prefix):
|
|
|
|
|
return prefix
|
|
|
|
|
def cursor(self):
|
|
|
|
|
return '{} '.format(self._cursor)
|
|
|
|
|
|
|
|
|
|
def _get_style(self, item, lineno, highlighted):
|
|
|
|
|
s = self._style
|
|
|
|
|
if item.focusable:
|
|
|
|
|
if highlighted:
|
|
|
|
|
return item.highlighted_style if item.highlighted_style is not None else s.highlighted
|
|
|
|
|
else:
|
|
|
|
|
return item.style if item.style is not None else s.option
|
|
|
|
|
else:
|
|
|
|
|
return item.style if item.style is not None else s.text
|
|
|
|
|
@property
|
|
|
|
|
def no_cursor(self):
|
|
|
|
|
# cursor with spaces minus dedent
|
|
|
|
|
return ' ' * (len(self._cursor) + 1 * self._dedent_selection)
|
|
|
|
|
|
|
|
|
|
def _transform_line(self, ti):
|
|
|
|
|
if len(list(ti.fragments)) == 0:
|
|
|
|
|
return Transformation(ti.fragments)
|
|
|
|
|
style, text = list(ti.fragments)[0]
|
|
|
|
|
item = self._items[ti.lineno]
|
|
|
|
|
style = self._get_style(item, ti.lineno, ti.lineno == self._pos)
|
|
|
|
|
s = self._style
|
|
|
|
|
|
|
|
|
|
# cursor
|
|
|
|
|
indent = ''
|
|
|
|
|
prefix = ''
|
|
|
|
|
suffix = ''
|
|
|
|
|
if item.focusable:
|
|
|
|
|
indent += ' ' * self._option_indent
|
|
|
|
|
suffix = self._option_suffix
|
|
|
|
|
|
|
|
|
|
if ti.lineno == self._pos:
|
|
|
|
|
prefix += '{}{}'.format(self._cursor, self._option_prefix)
|
|
|
|
|
prefix += '{} '.format(self._cursor)
|
|
|
|
|
style = s.highlight_style
|
|
|
|
|
else:
|
|
|
|
|
prefix += ' ' * len(self._cursor) + self._option_prefix + ' ' * self._dedent_selection
|
|
|
|
|
prefix += ' ' * (len(self._cursor) + 1 + 1 * self._dedent_selection)
|
|
|
|
|
style = s.option_style
|
|
|
|
|
else:
|
|
|
|
|
if item.indent:
|
|
|
|
|
indent += ' ' * (self._header_indent + len(self._cursor) + 1)
|
|
|
|
|
style = s.header_style
|
|
|
|
|
|
|
|
|
|
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 + [(style, suffix)])
|
|
|
|
|
return Transformation([('', indent), (style, prefix)] + items)
|
|
|
|
|
|
|
|
|
|
def next_item(self, direction):
|
|
|
|
|
if not any(item.focusable for item in self._items):
|
|
|
|
@ -243,21 +221,7 @@ class CliMenu:
|
|
|
|
|
|
|
|
|
|
return lines
|
|
|
|
|
|
|
|
|
|
def _register_extra_kb_cbs(self, kb):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def _preflight(self):
|
|
|
|
|
if self._initial_pos < 0 or self._initial_pos >= self._item_num:
|
|
|
|
|
raise ValueError("Initial position {} is out of range, needs to be in range of [0, {})"
|
|
|
|
|
.format(self._initial_pos, self._item_num))
|
|
|
|
|
|
|
|
|
|
def _accept(self, event):
|
|
|
|
|
self._success = True
|
|
|
|
|
event.app.exit()
|
|
|
|
|
|
|
|
|
|
def _run(self):
|
|
|
|
|
self._preflight()
|
|
|
|
|
|
|
|
|
|
class MenuColorizer(Processor):
|
|
|
|
|
def apply_transformation(_self, ti):
|
|
|
|
|
return self._transform_line(ti)
|
|
|
|
@ -282,7 +246,7 @@ class CliMenu:
|
|
|
|
|
|
|
|
|
|
@self._kb.add('N', filter=~is_searching)
|
|
|
|
|
@self._kb.add('n', filter=~is_searching)
|
|
|
|
|
def search_inc(event):
|
|
|
|
|
def search_inc(event, filter=is_searching):
|
|
|
|
|
if not self._bufctrl.search_state.text:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
@ -294,8 +258,10 @@ class CliMenu:
|
|
|
|
|
|
|
|
|
|
@self._kb.add('c-m', filter=~is_searching)
|
|
|
|
|
@self._kb.add('right', filter=~is_searching)
|
|
|
|
|
@self._kb.add('c-space', filter=~is_searching)
|
|
|
|
|
def accept(event):
|
|
|
|
|
self._accept(event)
|
|
|
|
|
self._success = True
|
|
|
|
|
event.app.exit()
|
|
|
|
|
|
|
|
|
|
@self._kb.add('c-m', filter=is_searching)
|
|
|
|
|
def accept_search(event):
|
|
|
|
@ -303,8 +269,6 @@ class CliMenu:
|
|
|
|
|
new_line, _ = self._doc.translate_index_to_position(self._buf.cursor_position)
|
|
|
|
|
self.sync_cursor_to_line(new_line)
|
|
|
|
|
|
|
|
|
|
self._register_extra_kb_cbs(self._kb)
|
|
|
|
|
|
|
|
|
|
self._searchbar = SearchToolbar(ignore_case=True)
|
|
|
|
|
|
|
|
|
|
text = '\n'.join(map(lambda _x: _x.text, self._items))
|
|
|
|
@ -320,8 +284,7 @@ class CliMenu:
|
|
|
|
|
self._searchbar])
|
|
|
|
|
|
|
|
|
|
# set initial pos
|
|
|
|
|
for _ in range(self._initial_pos + 1):
|
|
|
|
|
self.next_item(1)
|
|
|
|
|
self.sync_cursor_to_line(0)
|
|
|
|
|
|
|
|
|
|
app = Application(layout=Layout(split),
|
|
|
|
|
key_bindings=self._kb,
|
|
|
|
@ -332,72 +295,6 @@ class CliMenu:
|
|
|
|
|
self._ran = True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class CliMultiMenu(CliMenu):
|
|
|
|
|
default_selection_icons = CliSelectionStyle.SQUARE_BRACKETS
|
|
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
|
def set_default_selector_icons(cls, selection_icons):
|
|
|
|
|
cls.default_selection_icons = selection_icons
|
|
|
|
|
|
|
|
|
|
def __init__(self, *args, selection_icons=None, min_selection_count=0, **kwargs):
|
|
|
|
|
self._multi_selected = []
|
|
|
|
|
self._min_selection_count = min_selection_count
|
|
|
|
|
self._selection_icons = selection_icons if selection_icons is not None else self.default_selection_icons
|
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
|
|
def add_option(self, text, item=None, selected=False):
|
|
|
|
|
super().add_option(text, item)
|
|
|
|
|
if selected:
|
|
|
|
|
self._multi_selected.append(len(self._items) - 1)
|
|
|
|
|
|
|
|
|
|
def get_selection(self):
|
|
|
|
|
if self.success:
|
|
|
|
|
return [(self._items[n].num, self._items[n].item) for n in self._multi_selected]
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def get_selection_num(self):
|
|
|
|
|
if self.success:
|
|
|
|
|
return [self._items[n].num for n in self._multi_selected]
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def get_selection_item(self):
|
|
|
|
|
if self.success:
|
|
|
|
|
return [self._items[n].item for n in self._multi_selected]
|
|
|
|
|
else:
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
def _register_extra_kb_cbs(self, kb):
|
|
|
|
|
@kb.add('space', filter=~is_searching)
|
|
|
|
|
@kb.add('right', filter=~is_searching)
|
|
|
|
|
def mark(event):
|
|
|
|
|
if self._pos not in self._multi_selected:
|
|
|
|
|
self._multi_selected.append(self._pos)
|
|
|
|
|
else:
|
|
|
|
|
self._multi_selected.remove(self._pos)
|
|
|
|
|
|
|
|
|
|
def _transform_prefix(self, item, lineno, prefix):
|
|
|
|
|
if item.focusable:
|
|
|
|
|
if lineno in self._multi_selected:
|
|
|
|
|
icon = self._selection_icons[0]
|
|
|
|
|
else:
|
|
|
|
|
icon = self._selection_icons[1]
|
|
|
|
|
return "{}{} ".format(prefix, icon)
|
|
|
|
|
else:
|
|
|
|
|
return prefix
|
|
|
|
|
|
|
|
|
|
def _preflight(self):
|
|
|
|
|
super()._preflight()
|
|
|
|
|
if self._min_selection_count > self._item_num:
|
|
|
|
|
raise ValueError("A minimum of {} items was requested for successful selection but only {} exist"
|
|
|
|
|
.format(self._min_selection_count, self._item_num))
|
|
|
|
|
|
|
|
|
|
def _accept(self, event):
|
|
|
|
|
if len(self._multi_selected) >= self._min_selection_count:
|
|
|
|
|
super()._accept(event)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def cli_select_item(options, header=None, abort_exc=ValueError, abort_text="Selection aborted.", style=None,
|
|
|
|
|
return_single=True):
|
|
|
|
|
"""Helper function to quickly get a selection with just a few arguments"""
|
|
|
|
|