Skip to content

Commit 38fe118

Browse files
kylucakbdharun
andauthored
feat: stylize text inside backticks when appearing in example description (#254)
* Stylize italics in helptext using ANSI escapes * Ensure example styles are still applied and not completely reset * Remove unused import --------- Co-authored-by: K.B.Dharun Krishna <[email protected]>
1 parent 784225e commit 38fe118

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

tests/data/jq_rendered

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- Output all elements from arrays (or all the values from objects) in a JSON file:
1111
jq '.[]' file.json
1212

13-
- Read JSON objects from a file into an array, and output it (inverse of `jq .[]`):
13+
- Read JSON objects from a file into an array, and output it (inverse of jq .[]):
1414
jq --slurp . file.json
1515

1616
- Output the first element in a JSON file:
@@ -19,7 +19,7 @@
1919
- Output the value of a given key of each element in a JSON text from stdin:
2020
cat file.json | jq 'map(.key_name)'
2121

22-
- Output the value of multiple keys as a new JSON object (assuming the input JSON has the keys `key_name` and `other_key_name`):
22+
- Output the value of multiple keys as a new JSON object (assuming the input JSON has the keys key_name and other_key_name):
2323
cat file.json | jq '{my_new_key: .key_name, my_other_key: .other_key_name}'
2424

2525
- Combine multiple filters:

tldr.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,8 @@ def get_page(
369369

370370
LEADING_SPACES_NUM = 2
371371

372+
EXAMPLE_SPLIT_REGEX = re.compile(r'(?P<example>`.+?`)')
373+
EXAMPLE_REGEX = re.compile(r'(?:`)(?P<example>.+?)(?:`)')
372374
COMMAND_SPLIT_REGEX = re.compile(r'(?P<param>{{.+?}*}})')
373375
PARAM_REGEX = re.compile(r'(?:{{)(?P<param>.+?)(?:}})')
374376

@@ -414,6 +416,11 @@ def colors_of(key: str) -> Tuple[str, str, List[str]]:
414416

415417

416418
def output(page: str, plain: bool = False) -> None:
419+
def emphasise_example(x: str) -> str:
420+
# Use ANSI escapes to enable italics at the start and disable at the end
421+
# Also use the color yellow to differentiate from the default green
422+
return "\x1B[3m" + colored(x.group('example'), 'yellow') + "\x1B[23m"
423+
417424
if not plain:
418425
print()
419426
for line in page:
@@ -422,23 +429,48 @@ def output(page: str, plain: bool = False) -> None:
422429
if plain:
423430
print(line)
424431
continue
432+
425433
elif len(line) == 0:
426434
continue
435+
436+
# Handle the command name
427437
elif line[0] == '#':
428438
line = ' ' * LEADING_SPACES_NUM + \
429439
colored(line.replace('# ', ''), *colors_of('name')) + '\n'
430440
sys.stdout.buffer.write(line.encode('utf-8'))
441+
442+
# Handle the command description
431443
elif line[0] == '>':
432444
line = ' ' * (LEADING_SPACES_NUM - 1) + \
433445
colored(
434446
line.replace('>', '').replace('<', ''),
435447
*colors_of('description')
436448
)
437449
sys.stdout.buffer.write(line.encode('utf-8'))
450+
451+
# Handle an example description
438452
elif line[0] == '-':
439-
line = '\n' + ' ' * LEADING_SPACES_NUM + \
440-
colored(line, *colors_of('example'))
453+
454+
# Stylize text within backticks using yellow italics
455+
if '`' in line:
456+
elements = ['\n', ' ' * LEADING_SPACES_NUM]
457+
458+
for item in EXAMPLE_SPLIT_REGEX.split(line):
459+
item, replaced = EXAMPLE_REGEX.subn(emphasise_example, item)
460+
if not replaced:
461+
item = colored(item, *colors_of('example'))
462+
elements.append(item)
463+
464+
line = ''.join(elements)
465+
466+
# Otherwise, use the same colour for the whole line
467+
else:
468+
line = '\n' + ' ' * LEADING_SPACES_NUM + \
469+
colored(line, *colors_of('example'))
470+
441471
sys.stdout.buffer.write(line.encode('utf-8'))
472+
473+
# Handle an example command
442474
elif line[0] == '`':
443475
line = line[1:-1] # Remove backticks for parsing
444476

0 commit comments

Comments
 (0)