1 import readline, textwrap
3 from shlex import split as shsplit
4 from rlcompleter import Completer
5 from lcfg2.config import ConfigEnvironment
6 from .common import ShconfigException, get_config_name
7 from .commands import Commands
9 class ConfigNameCompleter(Completer):
10 def __init__(self, env: ConfigEnvironment):
11 super().__init__(None)
15 get_config_name(x._name)
19 def complete(self, text, state):
21 text = text if text else ""
23 x for x in self.__config_set if x.startswith(text)]
25 return None if not self.__options else self.__options[state]
28 def next_input(cmds: Commands):
29 line = input("shconfig> ")
35 name, args = parts[0], parts[1:]
37 if name in ['q', 'exit']:
41 raise KeyboardInterrupt()
43 if name.startswith("CONFIG_"):
44 cmds.call("opt", name)
47 cmds.call(name, *args)
50 def shconfig(env: ConfigEnvironment):
56 Lunaix Interactive Configurator (shconfig)
58 Type "help" to see all commands avaliables
59 Type "q" or "exit" to confirm and exit
60 Type "q!" or use ^C to discard and abort
66 cmpleter = ConfigNameCompleter(env)
67 readline.parse_and_bind('tab: complete')
68 readline.set_completer(cmpleter.complete)
72 if not next_input(cmds):
74 except ShconfigException as e:
77 except KeyboardInterrupt as e:
79 except Exception as e: