Skip to content

Commit 9e10f56

Browse files
A little more color
1 parent f549345 commit 9e10f56

File tree

6 files changed

+99
-36
lines changed

6 files changed

+99
-36
lines changed

data/toolbox/command.py

Lines changed: 83 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -157,75 +157,139 @@ 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=None):
161161
"""Generate help text from usage specification.
162162
163163
Args:
164164
command_name: Name of the command (e.g., "rb-object-print")
165+
terminal: Optional 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 if we have a terminal
171+
if terminal:
172+
import format as fmt
173+
else:
174+
fmt = None
175+
# Summary with color
176+
if terminal:
177+
lines = [terminal.print(fmt.bold, self.summary, fmt.reset), ""]
178+
else:
179+
lines = [self.summary, ""]
170180

171181
# Build usage line
172-
usage_parts = [command_name]
182+
usage_parts = [command_name] if not terminal else [terminal.print(fmt.bold, command_name, fmt.reset)]
183+
173184
for param_name, _ in self.parameters:
174-
usage_parts.append(f"<{param_name}>")
185+
if terminal:
186+
usage_parts.append(terminal.print(fmt.dim, f"<{param_name}>", fmt.reset))
187+
else:
188+
usage_parts.append(f"<{param_name}>")
175189

176190
# Add option placeholders
177191
for option_name in self.options.keys():
178-
usage_parts.append(f"[--{option_name} N]")
192+
placeholder = f"[--{option_name} N]"
193+
if terminal:
194+
usage_parts.append(terminal.print(fmt.dim, placeholder, fmt.reset))
195+
else:
196+
usage_parts.append(placeholder)
179197

180198
# Add flag placeholders
181199
for flag_name, _ in self.flags:
182-
usage_parts.append(f"[--{flag_name}]")
200+
placeholder = f"[--{flag_name}]"
201+
if terminal:
202+
usage_parts.append(terminal.print(fmt.dim, placeholder, fmt.reset))
203+
else:
204+
usage_parts.append(placeholder)
183205

184206
lines.append(f"Usage: {' '.join(usage_parts)}")
185207
lines.append("")
186208

187209
# Parameter descriptions
188210
if self.parameters:
189-
lines.append("Parameters:")
211+
if terminal:
212+
lines.append(terminal.print(fmt.title, "Parameters:", fmt.reset))
213+
else:
214+
lines.append("Parameters:")
215+
190216
for param_name, param_desc in self.parameters:
191-
if param_desc:
192-
lines.append(f" {param_name:<15} {param_desc}")
217+
if terminal:
218+
param_str = terminal.print(fmt.symbol, param_name, fmt.reset)
219+
if param_desc:
220+
lines.append(f" {param_str:<15} {param_desc}")
221+
else:
222+
lines.append(f" {param_str}")
193223
else:
194-
lines.append(f" {param_name}")
224+
if param_desc:
225+
lines.append(f" {param_name:<15} {param_desc}")
226+
else:
227+
lines.append(f" {param_name}")
195228
lines.append("")
196229

197230
# Option descriptions
198231
if self.options:
199-
lines.append("Options:")
232+
if terminal:
233+
lines.append(terminal.print(fmt.title, "Options:", fmt.reset))
234+
else:
235+
lines.append("Options:")
236+
200237
for option_name, opt_spec in self.options.items():
201238
opt_type, opt_default = opt_spec[0], opt_spec[1]
202239
opt_desc = opt_spec[2] if len(opt_spec) > 2 else None
203240

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

244+
if terminal:
245+
opt_str = terminal.print(fmt.symbol, f"--{option_name}", fmt.reset)
246+
type_part = terminal.print(fmt.dim, f" <{type_str}>", fmt.reset)
247+
else:
248+
opt_str = f"--{option_name}"
249+
type_part = f" <{type_str}>"
250+
207251
if opt_desc:
208-
lines.append(f" --{option_name} <{type_str}>{default_str}")
252+
lines.append(f" {opt_str}{type_part}{default_str}")
209253
lines.append(f" {opt_desc}")
210254
else:
211-
lines.append(f" --{option_name} <{type_str}>{default_str}")
255+
lines.append(f" {opt_str}{type_part}{default_str}")
212256
lines.append("")
213257

214258
# Flag descriptions
215259
if self.flags:
216-
lines.append("Flags:")
260+
if terminal:
261+
lines.append(terminal.print(fmt.title, "Flags:", fmt.reset))
262+
else:
263+
lines.append("Flags:")
264+
217265
for flag_name, flag_desc in self.flags:
218-
if flag_desc:
219-
lines.append(f" --{flag_name:<15} {flag_desc}")
266+
if terminal:
267+
flag_str = terminal.print(fmt.symbol, f"--{flag_name}", fmt.reset)
268+
if flag_desc:
269+
lines.append(f" {flag_str:<15} {flag_desc}")
270+
else:
271+
lines.append(f" {flag_str}")
220272
else:
221-
lines.append(f" --{flag_name}")
273+
if flag_desc:
274+
lines.append(f" --{flag_name:<15} {flag_desc}")
275+
else:
276+
lines.append(f" --{flag_name}")
222277
lines.append("")
223278

224279
# Examples section
225280
if self.examples:
226-
lines.append("Examples:")
281+
if terminal:
282+
lines.append(terminal.print(fmt.title, "Examples:", fmt.reset))
283+
else:
284+
lines.append("Examples:")
285+
227286
for example_cmd, example_desc in self.examples:
228-
lines.append(f" {example_cmd}")
287+
if terminal:
288+
cmd_str = terminal.print(fmt.dim, f" {example_cmd}", fmt.reset)
289+
lines.append(cmd_str)
290+
else:
291+
lines.append(f" {example_cmd}")
292+
229293
if example_desc:
230294
lines.append(f" {example_desc}")
231295
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: 5 additions & 6 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
@@ -372,6 +373,8 @@ def __init__(self):
372373
self.usage_spec = usage
373374

374375
def invoke(self, arg, from_tty):
376+
terminal = format.create_terminal(from_tty)
377+
375378
try:
376379
# Parse and validate arguments
377380
if self.usage_spec:
@@ -381,23 +384,19 @@ def invoke(self, arg, from_tty):
381384
import command as cmd_module
382385
arguments = cmd_module.parse_arguments(arg if arg else "")
383386

384-
# Create terminal for formatting
385-
import format
386-
terminal = format.create_terminal(from_tty)
387-
388387
# Lazy instantiate command class
389388
if self.command_instance is None:
390389
self.command_instance = command_class()
391390

392391
# Call command's invoke with parsed Arguments object and terminal
393-
self.command_instance.invoke(arguments, terminal, from_tty)
392+
self.command_instance.invoke(arguments, terminal)
394393

395394
except ValueError as e:
396395
# Validation error - show usage
397396
print(f"Error: {e}")
398397
if self.usage_spec:
399398
print()
400-
print(self.usage_spec.help_text(command_name))
399+
print(self.usage_spec.help_text(command_name, terminal))
401400
except Exception as e:
402401
print(f"Error: {e}")
403402
import traceback

data/toolbox/debugger/lldb_backend.py

Lines changed: 5 additions & 6 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
@@ -497,6 +498,8 @@ def __init__(self):
497498
self.usage_spec = usage
498499

499500
def invoke(self, arg, from_tty):
501+
terminal = format.create_terminal(from_tty)
502+
500503
try:
501504
# Parse and validate arguments
502505
if self.usage_spec:
@@ -506,23 +509,19 @@ def invoke(self, arg, from_tty):
506509
import command as cmd_module
507510
arguments = cmd_module.parse_arguments(arg if arg else "")
508511

509-
# Create terminal for formatting
510-
import format
511-
terminal = format.create_terminal(from_tty)
512-
513512
# Lazy instantiate command class
514513
if self.command_instance is None:
515514
self.command_instance = command_class()
516515

517516
# Call command's invoke with parsed Arguments object and terminal
518-
self.command_instance.invoke(arguments, terminal, from_tty)
517+
self.command_instance.invoke(arguments, terminal)
519518

520519
except ValueError as e:
521520
# Validation error - show usage
522521
print(f"Error: {e}")
523522
if self.usage_spec:
524523
print()
525-
print(self.usage_spec.help_text(command_name))
524+
print(self.usage_spec.help_text(command_name, terminal))
526525
except Exception as e:
527526
print(f"Error: {e}")
528527
import traceback

data/toolbox/format.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def __repr__(self):
2828
error = Style('error')
2929
bold = Style('bold')
3030
dim = Style('dim')
31+
title = Style('title') # For section headers in help text
3132

3233
class Text:
3334
"""Plain text output without any formatting."""
@@ -127,6 +128,7 @@ def __init__(self):
127128
error: self.RED,
128129
bold: self.BOLD,
129130
dim: self.DIM,
131+
title: self.BOLD, # Section headers in help text
130132
}
131133

132134
def print(self, *args):

data/toolbox/print.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,12 @@ class RubyObjectPrinter:
3737
]
3838
)
3939

40-
def invoke(self, arguments, terminal, from_tty):
41-
"""Execute the inspect command.
40+
def invoke(self, arguments, terminal):
41+
"""Execute the print command.
4242
4343
Args:
4444
arguments: Parsed Arguments object
45-
terminal: Terminal formatter
46-
from_tty: True if called from TTY
45+
terminal: Terminal formatter (already configured for TTY/non-TTY)
4746
"""
4847
# Get options
4948
max_depth = arguments.get_option('depth', 1)

0 commit comments

Comments
 (0)