Browse Source

Add support for cursorless menus

When a highlighter is used instead of a cursor it looks much better when
all the options have the same length. With right_pad_options all options
are right-padded with spaces so they have the same length. Also, an
example for a cursorless menu is added.
Sebastian Lohff 1 year ago
parent
commit
775c50ed6f
2 changed files with 18 additions and 1 deletions
  1. 9
    1
      clintermission/climenu.py
  2. 9
    0
      examples/cursorless.py

+ 9
- 1
clintermission/climenu.py View File

@@ -93,7 +93,7 @@ class CliMenu:
93 93
 
94 94
     def __init__(self, options=None, header=None, cursor=None, style=None,
95 95
                  indent=2, dedent_selection=False, initial_pos=0,
96
-                 option_prefix=' ', option_suffix=''):
96
+                 option_prefix=' ', option_suffix='', right_pad_options=False):
97 97
         self._items = []
98 98
         self._item_num = 0
99 99
         self._ran = False
@@ -105,6 +105,7 @@ class CliMenu:
105 105
         self._option_indent = indent
106 106
         self._header_indent = indent
107 107
         self._dedent_selection = dedent_selection
108
+        self._right_pad_options = right_pad_options
108 109
 
109 110
         self._cursor = cursor if cursor is not None else self.default_cursor
110 111
         self._style = style if style is not None else self.default_style
@@ -259,6 +260,13 @@ class CliMenu:
259 260
             raise ValueError("Initial position {} is out of range, needs to be in range of [0, {})"
260 261
                              .format(self._initial_pos, self._item_num))
261 262
 
263
+        if self._right_pad_options:
264
+            # pad all item labels with spaces to have the same length
265
+            max_item_len = max([len(_item.text) for _item in self._items if _item.focusable])
266
+            for item in self._items:
267
+                if item.focusable:
268
+                    item.text += " " * (max_item_len - len(item.text))
269
+
262 270
     def _accept(self, event):
263 271
         self._success = True
264 272
         event.app.exit()

+ 9
- 0
examples/cursorless.py View File

@@ -0,0 +1,9 @@
1
+#!/usr/bin/env python3
2
+from clintermission import CliMenu, CliMenuTheme
3
+
4
+
5
+q = ["Foo", "Bar", "Baz baz baz baz baz"]
6
+m = CliMenu(q, "Time to choose:\n", style=CliMenuTheme.BOLD_HIGHLIGHT,
7
+            cursor='', option_prefix=' ', option_suffix=' ', right_pad_options=True)
8
+m.get_selection()
9
+print("You selected", m.get_selection())

Loading…
Cancel
Save