Skip to content

Commit b77e188

Browse files
A little more color
1 parent f549345 commit b77e188

File tree

6 files changed

+77
-185
lines changed

6 files changed

+77
-185
lines changed

data/toolbox/command.py

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -157,75 +157,92 @@ def parse(self, argument_string):
157157
# Return new Arguments with converted options
158158
return Arguments(arguments.expressions, arguments.flags, converted_options)
159159

160-
def help_text(self, command_name):
160+
def help_text(self, command_name, terminal):
161161
"""Generate help text from usage specification.
162162
163163
Args:
164-
command_name: Name of the command (e.g., "rb-object-print")
164+
command_name: Name of the command (e.g., "rb-print")
165+
terminal: Terminal for colored output
165166
166167
Returns:
167168
Formatted help string with usage, parameters, options, and flags
168169
"""
169-
lines = [self.summary, ""]
170+
import format as fmt
171+
172+
# Summary with color
173+
lines = [terminal.print(fmt.bold, self.summary, fmt.reset), ""]
170174

171175
# Build usage line
172-
usage_parts = [command_name]
176+
usage_parts = [terminal.print(fmt.bold, command_name, fmt.reset)]
177+
173178
for param_name, _ in self.parameters:
174-
usage_parts.append(f"<{param_name}>")
179+
usage_parts.append(terminal.print(fmt.placeholder, f"<{param_name}>", fmt.reset))
175180

176181
# Add option placeholders
177182
for option_name in self.options.keys():
178-
usage_parts.append(f"[--{option_name} N]")
183+
opt_placeholder = f"[--{option_name} N]"
184+
usage_parts.append(terminal.print(fmt.placeholder, opt_placeholder, fmt.reset))
179185

180186
# Add flag placeholders
181187
for flag_name, _ in self.flags:
182-
usage_parts.append(f"[--{flag_name}]")
188+
flag_placeholder = f"[--{flag_name}]"
189+
usage_parts.append(terminal.print(fmt.placeholder, flag_placeholder, fmt.reset))
183190

184191
lines.append(f"Usage: {' '.join(usage_parts)}")
185192
lines.append("")
186193

187194
# Parameter descriptions
188195
if self.parameters:
189-
lines.append("Parameters:")
196+
lines.append(terminal.print(fmt.title, "Parameters:", fmt.reset))
197+
190198
for param_name, param_desc in self.parameters:
199+
param_str = terminal.print(fmt.symbol, param_name, fmt.reset)
191200
if param_desc:
192-
lines.append(f" {param_name:<15} {param_desc}")
201+
lines.append(f" {param_str:<15} {param_desc}")
193202
else:
194-
lines.append(f" {param_name}")
203+
lines.append(f" {param_str}")
195204
lines.append("")
196205

197206
# Option descriptions
198207
if self.options:
199-
lines.append("Options:")
208+
lines.append(terminal.print(fmt.title, "Options:", fmt.reset))
209+
200210
for option_name, opt_spec in self.options.items():
201211
opt_type, opt_default = opt_spec[0], opt_spec[1]
202212
opt_desc = opt_spec[2] if len(opt_spec) > 2 else None
203213

204214
type_str = opt_type.__name__ if hasattr(opt_type, '__name__') else str(opt_type)
205215
default_str = f" (default: {opt_default})" if opt_default is not None else ""
206216

217+
opt_str = terminal.print(fmt.symbol, f"--{option_name}", fmt.reset)
218+
type_part = terminal.print(fmt.placeholder, f" <{type_str}>", fmt.reset)
219+
207220
if opt_desc:
208-
lines.append(f" --{option_name} <{type_str}>{default_str}")
221+
lines.append(f" {opt_str}{type_part}{default_str}")
209222
lines.append(f" {opt_desc}")
210223
else:
211-
lines.append(f" --{option_name} <{type_str}>{default_str}")
224+
lines.append(f" {opt_str}{type_part}{default_str}")
212225
lines.append("")
213226

214227
# Flag descriptions
215228
if self.flags:
216-
lines.append("Flags:")
229+
lines.append(terminal.print(fmt.title, "Flags:", fmt.reset))
230+
217231
for flag_name, flag_desc in self.flags:
232+
flag_str = terminal.print(fmt.symbol, f"--{flag_name}", fmt.reset)
218233
if flag_desc:
219-
lines.append(f" --{flag_name:<15} {flag_desc}")
234+
lines.append(f" {flag_str:<15} {flag_desc}")
220235
else:
221-
lines.append(f" --{flag_name}")
236+
lines.append(f" {flag_str}")
222237
lines.append("")
223238

224239
# Examples section
225240
if self.examples:
226-
lines.append("Examples:")
241+
lines.append(terminal.print(fmt.title, "Examples:", fmt.reset))
242+
227243
for example_cmd, example_desc in self.examples:
228-
lines.append(f" {example_cmd}")
244+
cmd_str = terminal.print(fmt.example, f" {example_cmd}", fmt.reset)
245+
lines.append(cmd_str)
229246
if example_desc:
230247
lines.append(f" {example_desc}")
231248
lines.append("")

data/toolbox/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ def invoke(self, arg, from_tty):
385385
flags_set = {'debug'} if debug else set()
386386
args_for_printer = command.Arguments([storage_val], flags_set, {'depth': depth})
387387

388-
printer.invoke(args_for_printer, terminal, from_tty)
388+
printer.invoke(args_for_printer, terminal)
389389

390390
except Exception as e:
391391
print(f"Error: {e}")

data/toolbox/debugger/gdb_backend.py

Lines changed: 12 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"""
44

55
import gdb
6+
import format
67

78
# Command categories
89
COMMAND_DATA = gdb.COMMAND_DATA
@@ -341,71 +342,6 @@ def invoke(self, arg, from_tty):
341342
raise NotImplementedError("Subclasses must implement invoke()")
342343

343344

344-
def register(command_name, command_class, usage=None, category=COMMAND_USER):
345-
"""Register a command with GDB using declarative interface.
346-
347-
Creates a wrapper that handles argument parsing, validation, and delegation.
348-
349-
Args:
350-
command_name: Name of the command (e.g., "rb-object-print")
351-
command_class: Class to instantiate (must have invoke(arguments, from_tty) method)
352-
usage: Optional command.Usage specification for validation and help
353-
category: Command category (COMMAND_USER or COMMAND_DATA)
354-
355-
Returns:
356-
The registered command wrapper instance
357-
358-
Example:
359-
class RubyObjectPrinter:
360-
def invoke(self, arguments, from_tty):
361-
value_expr = arguments.expressions[0]
362-
depth = arguments.get_option('depth', 1)
363-
# ... print logic
364-
365-
debugger.register("rb-object-print", RubyObjectPrinter,
366-
usage=RubyObjectPrinter.USAGE)
367-
"""
368-
class RegisteredCommand(Command):
369-
def __init__(self):
370-
super().__init__(command_name, category)
371-
self.command_instance = None
372-
self.usage_spec = usage
373-
374-
def invoke(self, arg, from_tty):
375-
try:
376-
# Parse and validate arguments
377-
if self.usage_spec:
378-
arguments = self.usage_spec.parse(arg if arg else "")
379-
else:
380-
# Fallback: use raw parsing
381-
import command as cmd_module
382-
arguments = cmd_module.parse_arguments(arg if arg else "")
383-
384-
# Create terminal for formatting
385-
import format
386-
terminal = format.create_terminal(from_tty)
387-
388-
# Lazy instantiate command class
389-
if self.command_instance is None:
390-
self.command_instance = command_class()
391-
392-
# Call command's invoke with parsed Arguments object and terminal
393-
self.command_instance.invoke(arguments, terminal, from_tty)
394-
395-
except ValueError as e:
396-
# Validation error - show usage
397-
print(f"Error: {e}")
398-
if self.usage_spec:
399-
print()
400-
print(self.usage_spec.help_text(command_name))
401-
except Exception as e:
402-
print(f"Error: {e}")
403-
import traceback
404-
traceback.print_exc()
405-
406-
return RegisteredCommand()
407-
408-
409345
def parse_and_eval(expression):
410346
"""Evaluate an expression in the debugger.
411347
@@ -690,29 +626,29 @@ def __init__(self):
690626

691627
def invoke(self, arg, from_tty):
692628
"""GDB entry point - parses arguments and delegates to handler."""
629+
# Create terminal first (needed for help text)
630+
import format
631+
terminal = format.create_terminal(from_tty)
632+
693633
try:
694634
# Parse and validate arguments
695635
if self.usage_spec:
696-
try:
697-
arguments = self.usage_spec.parse(arg if arg else "")
698-
except ValueError as e:
699-
print(f"Error: {e}")
700-
print()
701-
print(self.usage_spec.help_text(name))
702-
return
636+
arguments = self.usage_spec.parse(arg if arg else "")
703637
else:
704638
# Fallback to basic parsing without validation
705639
import command
706640
arguments = command.parse_arguments(arg if arg else "")
707641

708-
# Create terminal for formatting
709-
import format
710-
terminal = format.create_terminal(from_tty)
711-
712642
# Instantiate handler and invoke
713643
handler = self.handler_class()
714644
handler.invoke(arguments, terminal)
715645

646+
except ValueError as e:
647+
# Validation error - show colored help
648+
print(f"Error: {e}")
649+
if self.usage_spec:
650+
print()
651+
print(self.usage_spec.help_text(name, terminal))
716652
except Exception as e:
717653
print(f"Error: {e}")
718654
import traceback

data/toolbox/debugger/lldb_backend.py

Lines changed: 12 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import lldb
9+
import format
910

1011
# Command categories (LLDB doesn't have exact equivalents, using symbolic constants)
1112
COMMAND_DATA = 0
@@ -464,74 +465,6 @@ def get_command(cls, name):
464465
return cls._commands.get(name)
465466

466467

467-
def register(command_name, command_class, usage=None, category=COMMAND_USER):
468-
print(f"DEBUG: Registering command: {command_name} category: {category}")
469-
"""Register a command with LLDB using declarative interface.
470-
471-
Creates a wrapper that handles argument parsing, validation, and delegation.
472-
473-
Args:
474-
command_name: Name of the command (e.g., "rb-object-print")
475-
command_class: Class to instantiate (must have invoke(arguments, from_tty) method)
476-
usage: Optional command.Usage specification for validation and help
477-
category: Command category (COMMAND_USER or COMMAND_DATA)
478-
479-
Returns:
480-
The registered command wrapper instance
481-
482-
Example:
483-
class RubyObjectPrinter:
484-
def invoke(self, arguments, from_tty):
485-
value_expr = arguments.expressions[0]
486-
depth = arguments.get_option('depth', 1)
487-
# ... print logic
488-
489-
debugger.register("rb-object-print", RubyObjectPrinter,
490-
usage=RubyObjectPrinter.USAGE)
491-
"""
492-
class RegisteredCommand(Command):
493-
def __init__(self):
494-
print(f"Registering command: {command_name} category: {category}")
495-
super().__init__(command_name, category)
496-
self.command_instance = None
497-
self.usage_spec = usage
498-
499-
def invoke(self, arg, from_tty):
500-
try:
501-
# Parse and validate arguments
502-
if self.usage_spec:
503-
arguments = self.usage_spec.parse(arg if arg else "")
504-
else:
505-
# Fallback: use raw parsing
506-
import command as cmd_module
507-
arguments = cmd_module.parse_arguments(arg if arg else "")
508-
509-
# Create terminal for formatting
510-
import format
511-
terminal = format.create_terminal(from_tty)
512-
513-
# Lazy instantiate command class
514-
if self.command_instance is None:
515-
self.command_instance = command_class()
516-
517-
# Call command's invoke with parsed Arguments object and terminal
518-
self.command_instance.invoke(arguments, terminal, from_tty)
519-
520-
except ValueError as e:
521-
# Validation error - show usage
522-
print(f"Error: {e}")
523-
if self.usage_spec:
524-
print()
525-
print(self.usage_spec.help_text(command_name))
526-
except Exception as e:
527-
print(f"Error: {e}")
528-
import traceback
529-
traceback.print_exc()
530-
531-
print(f"DEBUG: Returning RegisteredCommand for: {command_name}")
532-
return RegisteredCommand()
533-
534-
535468
def parse_and_eval(expression):
536469
"""Evaluate an expression in the debugger.
537470
@@ -990,29 +923,29 @@ def __init__(self):
990923

991924
def invoke(self, arg, from_tty):
992925
"""LLDB entry point - parses arguments and delegates to handler."""
926+
# Create terminal first (needed for help text)
927+
import format
928+
terminal = format.create_terminal(from_tty)
929+
993930
try:
994931
# Parse and validate arguments
995932
if self.usage_spec:
996-
try:
997-
arguments = self.usage_spec.parse(arg if arg else "")
998-
except ValueError as e:
999-
print(f"Error: {e}")
1000-
print()
1001-
print(self.usage_spec.help_text(name))
1002-
return
933+
arguments = self.usage_spec.parse(arg if arg else "")
1003934
else:
1004935
# Fallback to basic parsing without validation
1005936
import command
1006937
arguments = command.parse_arguments(arg if arg else "")
1007938

1008-
# Create terminal for formatting
1009-
import format
1010-
terminal = format.create_terminal(from_tty)
1011-
1012939
# Instantiate handler and invoke
1013940
handler = self.handler_class()
1014941
handler.invoke(arguments, terminal)
1015942

943+
except ValueError as e:
944+
# Validation error - show colored help
945+
print(f"Error: {e}")
946+
if self.usage_spec:
947+
print()
948+
print(self.usage_spec.help_text(name, terminal))
1016949
except Exception as e:
1017950
print(f"Error: {e}")
1018951
import traceback

0 commit comments

Comments
 (0)