4 from .common import CmdTable, ShconfigException
5 from .common import select, cmd
7 from lcfg2.config import ConfigEnvironment
8 from lcfg2.common import NodeProperty, NodeDependency, ConfigNodeError
10 class Commands(CmdTable):
11 def __init__(self, env: ConfigEnvironment):
16 def __get_node(self, name: str):
17 node_name = name.removeprefix("CONFIG_").lower()
18 node = self.__env.get_node(node_name)
20 raise ShconfigException(f"no such config: {name}")
23 def __get_opt_line(self, node, color_hint = False):
25 name = f"CONFIG_{node._name.upper()}"
26 value = NodeProperty.Value[node]
27 enabled = NodeProperty.Enabled[node]
28 hidden = NodeProperty.Hidden[node]
29 ro = NodeProperty.Readonly[node]
31 status = f"{select(not enabled, 'x', '.')}" \
32 f"{select(ro, 'r', '.')}" \
33 f"{select(hidden, 'h', '.')}" \
35 val_txt = f"{value if value is not None else '<?>'}"
37 line = f"[{status}] {name}"
38 to_pad = max(aligned - len(line), 4)
39 line = f"{line} {'.' * to_pad} {val_txt}"
41 if color_hint and not enabled:
42 line = f"\x1b[90;49m{line}\x1b[0m"
52 for exe in self._cmd_map:
58 Show all configurable options
64 " (flags) CONFIG_NAME ..... VALUE",
67 " x Config is disabled",
68 " r Read-Only config",
72 "Defined configuration terms",
76 for node in self.__env.terms():
77 lines.append(self.__get_opt_line(node, True))
79 pydoc.pager("\n".join(lines))
82 def __fn_set(self, name: str, value):
84 Update a configurable option's value
87 node = self.__get_node(name)
89 raise ShconfigException(f"no such config: {name}")
91 if NodeProperty.Readonly[node]:
92 raise ShconfigException(f"node is read only")
95 NodeProperty.Value[node] = value
97 except ConfigNodeError as e:
101 def __fn_dep(self, name: str):
103 Show the dependency chain and boolean conditionals
106 def __print_dep_recursive(env, node, inds = 0):
108 dep: NodeDependency = NodeProperty.Dependency[node]
110 state = 'enabled' if NodeProperty.Value[node] else 'disabled'
111 print(f"{indent}* {node._name} (currently {state})")
115 print(f" {indent}predicate: {dep._expr}")
116 print(f" {indent}dependents:")
117 for name in dep._names:
118 n = env.get_node(name)
119 __print_dep_recursive(env, n, inds + 6)
121 node = self.__get_node(name)
122 __print_dep_recursive(self.__env, node)
124 @cmd("opt", "val", "v")
125 def __fn_opt(self, name: str):
127 Show the current value and flags of selected options
130 node = self.__get_node(name)
131 print(self.__get_opt_line(node))
134 def __fn_what(self, name: str):
136 Show the documentation associated with the option
139 node = self.__get_node(name)
140 help = NodeProperty.HelpText[node]
141 help = "<no help message>" if not help else help
144 print(textwrap.indent(help.strip(), " |\t", lambda _:True))
148 @cmd("effect", "link")
149 def __fn_effect(self, name: str):
151 Show the effects of this option on other options
154 node = self.__get_node(name)
155 link = NodeProperty.Linkage[node]
160 for other, exprs in link.linkages():
163 print(f" > when {expr}")