1818]
1919
2020import builtins
21+ import shlex
2122import shutil
2223from collections .abc import Collection
2324from functools import partial
2728
2829import wraptext
2930from adorable import ansi , color
31+ from adorable .color import Color , RGB
3032from adorable .common import BLACK , BOLD , MAROON , NAVY
3133
3234from .static import Static
4042PROMPT_PREFIX = "$"
4143
4244
45+ def _color_for_category (category : Category | None ) -> Color [Any ]:
46+ if category is None :
47+ return color .Colorless .from_rgb (0 )
48+ elif isinstance (category .color , Color ):
49+ return category .color
50+ else :
51+ return color .from_rgb (
52+ category .color or blake2s (category .title .encode ()).digest ()[0 ]
53+ )
54+
55+
4356# See also: https://phoenixr-codes.github.io/adorable/caution-ansi-strings.html#getting-the-visible-length-of-a-string
4457def len (x : Any , / ) -> int :
4558 """Returns the length of an object.
@@ -196,9 +209,8 @@ def examples(cmd: Command[Any, Any]) -> str:
196209 lines .append (
197210 f"{ BOLD :{_indent_text (example .description , indent = ' ' , indent_initial = True )}} "
198211 )
199- # TODO: shlex escape args
200212 command_line = (
201- f"{ PROMPT_PREFIX } { cmd ._subcommand_path ()} { ' ' .join (example .args )} "
213+ f"{ PROMPT_PREFIX } { cmd ._subcommand_path ()} { ' ' .join (shlex . quote ( arg ) for arg in example .args )} "
202214 )
203215 # TODO: syntax highlighting
204216 lines .append (_indent_text (command_line , indent = " " , indent_initial = True ))
@@ -210,13 +222,7 @@ def pretty_flag(cmd: Command[Any, Any], flag: Flag[Any, Any, Any]) -> str:
210222 """Returns a descriptive information for a flag."""
211223 parts = []
212224 category = flag .category
213- clr = (
214- ansi .empty ()
215- if category is None
216- else color .from_rgb (
217- category .color or blake2s (category .title .encode ()).digest ()[0 ]
218- )
219- )
225+ clr = _color_for_category (category )
220226 if flag .short is not None and cmd .prefix_short is not None :
221227 parts .extend (
222228 [f"{ clr :{cmd .prefix_short + name }} " for name in flag .visible_short_names ()]
@@ -264,13 +270,7 @@ def commands(cmd: Command[Any, Any]) -> str:
264270 cmds = categories .setdefault (command .category , [])
265271 cmds .append (command )
266272 for category , commands in categories .items ():
267- clr = (
268- ansi .empty ()
269- if category is None
270- else color .from_rgb (
271- category .color or blake2s (category .title .encode ()).digest ()[0 ]
272- )
273- )
273+ clr = _color_for_category (category )
274274 for command in commands :
275275 lines .extend (
276276 _add_description (
0 commit comments