Browse Source

Add initial_pos to choose initially selected item

Sebastian Lohff 1 year ago
parent
commit
d01024d1be
1 changed files with 8 additions and 2 deletions
  1. 8
    2
      clintermission/climenu.py

+ 8
- 2
clintermission/climenu.py View File

@@ -83,12 +83,13 @@ 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):
86
+                 indent=2, dedent_selection=False, initial_pos=0):
87 87
         self._items = []
88 88
         self._item_num = 0
89 89
         self._ran = False
90 90
         self._success = None
91 91
         self._pos = 0
92
+        self._initial_pos = initial_pos
92 93
         self._option_indent = indent
93 94
         self._header_indent = indent
94 95
         self._dedent_selection = dedent_selection
@@ -230,6 +231,10 @@ class CliMenu:
230 231
         pass
231 232
 
232 233
     def _run(self):
234
+        if self._initial_pos < 0 or self._initial_pos >= self._item_num:
235
+            raise ValueError("Initial position {} is out of range, needs to be in range of [0, {})"
236
+                             .format(self._initial_pos, self._item_num))
237
+
233 238
         class MenuColorizer(Processor):
234 239
             def apply_transformation(_self, ti):
235 240
                 return self._transform_line(ti)
@@ -293,7 +298,8 @@ class CliMenu:
293 298
                         self._searchbar])
294 299
 
295 300
         # set initial pos
296
-        self.sync_cursor_to_line(0)
301
+        for _ in range(self._initial_pos + 1):
302
+            self.next_item(1)
297 303
 
298 304
         app = Application(layout=Layout(split),
299 305
                           key_bindings=self._kb,

Loading…
Cancel
Save