Browse Source

Always use text as item if no item was specified

When an option is passed without an item attached we now always use the
label as item. This was the behaviour for __init__() and is now also the
behaviour of add_option().
Sebastian Lohff 1 year ago
parent
commit
22307bc4ae
1 changed files with 9 additions and 3 deletions
  1. 9
    3
      clintermission/climenu.py

+ 9
- 3
clintermission/climenu.py View File

@@ -70,6 +70,10 @@ class CliMenuTheme:
70 70
     BOLD_HIGHLIGHT = CliMenuStyle(header_style='bold', highlight_style='bold fg:black bg:white')
71 71
 
72 72
 
73
+class _EmptyParameter:
74
+    pass
75
+
76
+
73 77
 class CliMenu:
74 78
     default_style = CliMenuTheme.BASIC
75 79
     default_cursor = CliMenuCursor.TRIANGLE
@@ -110,7 +114,7 @@ class CliMenu:
110 114
                 elif isinstance(option, dict):
111 115
                     self.add_option(**option)
112 116
                 elif isinstance(option, str):
113
-                    self.add_option(option, option)
117
+                    self.add_option(option)
114 118
                 else:
115 119
                     raise ValueError("Option needs to be either tuple, dict or string, found '{}' of type {}"
116 120
                                      .format(option, type(option)))
@@ -122,7 +126,9 @@ class CliMenu:
122 126
         for text in title.split('\n'):
123 127
             self._items.append(_CliMenuHeader(text, indent=indent))
124 128
 
125
-    def add_option(self, text, item=None):
129
+    def add_option(self, text, item=_EmptyParameter):
130
+        if item == _EmptyParameter:
131
+            item = text
126 132
         self._items.append(_CliMenuOption(text, self._item_num, item=item))
127 133
         self._item_num += 1
128 134
 
@@ -334,7 +340,7 @@ class CliMultiMenu(CliMenu):
334 340
         self._selection_icons = selection_icons if selection_icons is not None else self.default_selection_icons
335 341
         super().__init__(*args, **kwargs)
336 342
 
337
-    def add_option(self, text, item=None, selected=False):
343
+    def add_option(self, text, item=_EmptyParameter, selected=False):
338 344
         super().add_option(text, item)
339 345
         if selected:
340 346
             self._multi_selected.append(len(self._items) - 1)

Loading…
Cancel
Save