浏览代码

[O] Allow abbreviation in selection

Azalea (on HyDEV-Daisy) 2 年之前
父节点
当前提交
17a28c0495
共有 1 个文件被更改,包括 20 次插入4 次删除
  1. 20 4
      hyfetch/main.py

+ 20 - 4
hyfetch/main.py

@@ -55,13 +55,29 @@ def literal_input(prompt: str, options: Iterable[str], default: str, show_ops: b
     else:
         printc(f'{prompt} (default: {default})')
 
-    selection = input('> ') or default
-    while not selection.lower() in lows:
+    def find_selection(sel: str):
+        if not sel:
+            return None
+
+        # Find exact match
+        if sel in lows:
+            return options[lows.index(sel)]
+
+        # Find starting abbreviation
+        for i, op in enumerate(lows):
+            if op.startswith(sel):
+                return options[i]
+
+        return None
+
+    selection = input('> ').lower() or default
+    while not find_selection(selection):
         print(f'Invalid selection! {selection} is not one of {"|".join(options)}')
-        selection = input('> ') or default
+        selection = input('> ').lower() or default
+
     print()
 
-    return options[lows.index(selection)]
+    return find_selection(selection)
 
 
 def create_config() -> Config: