Rewrite the lunabuild toolchain with enhanced feature (#60)
[lunaix-os.git] / lunaix-os / scripts / build-tools / shared / shconfig / main.py
diff --git a/lunaix-os/scripts/build-tools/shared/shconfig/main.py b/lunaix-os/scripts/build-tools/shared/shconfig/main.py
new file mode 100644 (file)
index 0000000..216e8e6
--- /dev/null
@@ -0,0 +1,80 @@
+import readline, textwrap
+
+from shlex          import split as shsplit
+from rlcompleter    import Completer
+from lcfg2.config   import ConfigEnvironment
+from .common        import ShconfigException, get_config_name
+from .commands      import Commands
+
+class ConfigNameCompleter(Completer):
+    def __init__(self, env: ConfigEnvironment):
+        super().__init__(None)
+
+        self.__options = []
+        self.__config_set= [
+            get_config_name(x._name)
+            for x in env.terms()
+        ]
+
+    def complete(self, text, state):
+        if state == 0:
+            text = text if text else ""
+            self.__options = [
+                x for x in self.__config_set if x.startswith(text)]
+        
+        return None if not self.__options else self.__options[state]
+
+
+def next_input(cmds: Commands):
+    line = input("shconfig> ")
+
+    if len(line) == 0:
+        return True
+    
+    parts = shsplit(line)
+    name, args = parts[0], parts[1:]
+
+    if name in ['q', 'exit']:
+        return False
+    
+    if name == "q!":
+        raise KeyboardInterrupt()
+    
+    if name.startswith("CONFIG_"):
+        cmds.call("opt", name)
+        return True
+
+    cmds.call(name, *args)
+    return True
+
+def shconfig(env: ConfigEnvironment):
+    print(
+        "\n",
+        textwrap.dedent(
+            
+            """
+            Lunaix Interactive Configurator (shconfig)
+            
+            Type "help" to see all commands avaliables
+            Type "q" or "exit" to confirm and exit
+            Type "q!" or use ^C to discard and abort
+
+            """
+        ).strip(), "\n")
+
+    cmds = Commands(env)
+    cmpleter = ConfigNameCompleter(env)
+    readline.parse_and_bind('tab: complete')
+    readline.set_completer(cmpleter.complete)
+
+    while True:
+        try:
+            if not next_input(cmds):
+                return True
+        except ShconfigException as e:
+            print(str(e))
+            continue
+        except KeyboardInterrupt as e:
+            return False
+        except Exception as e:
+            raise e
\ No newline at end of file