Browse Source

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.
Sebastian Lohff 1 year ago
parent
commit
9ef6532c1f
2 changed files with 16 additions and 4 deletions
  1. 9
    4
      clintermission/climenu.py
  2. 7
    0
      examples/basic_usage.py

+ 9
- 4
clintermission/climenu.py View File

@@ -83,13 +83,16 @@ class CliMenu:
83 83
         cls.default_cursor = cursor
84 84
 
85 85
     def __init__(self, options=None, header=None, cursor=None, style=None,
86
-                 indent=2, dedent_selection=False, initial_pos=0):
86
+                 indent=2, dedent_selection=False, initial_pos=0,
87
+                 option_prefix=' ', option_suffix=''):
87 88
         self._items = []
88 89
         self._item_num = 0
89 90
         self._ran = False
90 91
         self._success = None
91 92
         self._pos = 0
92 93
         self._initial_pos = initial_pos
94
+        self._option_prefix = option_prefix
95
+        self._option_suffix = option_suffix
93 96
         self._option_indent = indent
94 97
         self._header_indent = indent
95 98
         self._dedent_selection = dedent_selection
@@ -163,14 +166,16 @@ class CliMenu:
163 166
         # cursor
164 167
         indent = ''
165 168
         prefix = ''
169
+        suffix = ''
166 170
         if item.focusable:
167 171
             indent += ' ' * self._option_indent
172
+            suffix = self._option_suffix
168 173
 
169 174
             if ti.lineno == self._pos:
170
-                prefix += '{} '.format(self._cursor)
175
+                prefix += '{}{}'.format(self._cursor, self._option_prefix)
171 176
                 style = s.highlight_style
172 177
             else:
173
-                prefix += ' ' * (len(self._cursor) + 1 + 1 * self._dedent_selection)
178
+                prefix += ' ' * len(self._cursor) + self._option_prefix + ' ' * self._dedent_selection
174 179
                 style = s.option_style
175 180
         else:
176 181
             if item.indent:
@@ -180,7 +185,7 @@ class CliMenu:
180 185
         items = [(s if s else style, t) for s, t in ti.fragments]
181 186
         prefix = self._transform_prefix(item, ti.lineno, prefix)
182 187
 
183
-        return Transformation([('', indent), (style, prefix)] + items)
188
+        return Transformation([('', indent), (style, prefix)] + items + [(style, suffix)])
184 189
 
185 190
     def next_item(self, direction):
186 191
         if not any(item.focusable for item in self._items):

+ 7
- 0
examples/basic_usage.py View File

@@ -68,3 +68,10 @@ print()
68 68
 # --- with shortcut, shows no menu when only a single option is provided (can be disabled with return_single=False) ---
69 69
 result = cli_select_item(["Single Foo"])
70 70
 print("Directly selected for you as it was the only option:", result)
71
+print()
72
+
73
+# --- prefix/suffix ---
74
+q = ["Foo", "Bar", "Baz"]
75
+m = CliMenu(q, "Time to choose:\n", option_prefix=' <<<', option_suffix='>>>')
76
+print("You selected", m.get_selection())
77
+print()

Loading…
Cancel
Save