Browse Source

Allow passing an item to each option

Sebastian Lohff 1 year ago
parent
commit
b18372e3b8
1 changed files with 13 additions and 4 deletions
  1. 13
    4
      clintermission/climenu.py

+ 13
- 4
clintermission/climenu.py View File

@@ -19,9 +19,10 @@ class CliMenuHeader:
19 19
 
20 20
 class CliMenuOption:
21 21
     """Hold a menu option"""
22
-    def __init__(self, text, num):
22
+    def __init__(self, text, num, item=None):
23 23
         self.text = text
24 24
         self.num = num
25
+        self.item = item
25 26
         self.focusable = True
26 27
 
27 28
 
@@ -100,15 +101,23 @@ class CliMenu:
100 101
         for text in title.split('\n'):
101 102
             self._items.append(CliMenuHeader(text))
102 103
 
103
-    def add_option(self, text):
104
-        self._items.append(CliMenuOption(text, self._item_num))
104
+    def add_option(self, text, item=None):
105
+        self._items.append(CliMenuOption(text, self._item_num, item=item))
105 106
         self._item_num += 1
106 107
 
107 108
     def get_selection(self):
108 109
         if not self._ran:
109 110
             self._run()
110 111
 
111
-        return self._items[self._pos].num
112
+        item = self._items[self._pos]
113
+
114
+        return (item.num, item.item)
115
+
116
+    def get_selection_num(self):
117
+        return self.get_selection()[0]
118
+
119
+    def get_selection_item(self):
120
+        return self.get_selection()[1]
112 121
 
113 122
     def cursor(self):
114 123
         return '{} '.format(self._cursor)

Loading…
Cancel
Save