Skip to content

Commit 74718de

Browse files
committed
Stylize italics in helptext using ANSI escapes
1 parent bad9f77 commit 74718de

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
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: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env python3
22
# PYTHON_ARGCOMPLETE_OK
33

4+
import itertools
45
import sys
56
import os
67
import re
@@ -422,23 +423,40 @@ def output(page: str, plain: bool = False) -> None:
422423
if plain:
423424
print(line)
424425
continue
426+
425427
elif len(line) == 0:
426428
continue
429+
430+
# Handle the command name
427431
elif line[0] == '#':
428432
line = ' ' * LEADING_SPACES_NUM + \
429433
colored(line.replace('# ', ''), *colors_of('name')) + '\n'
430434
sys.stdout.buffer.write(line.encode('utf-8'))
435+
436+
# Handle the command description
431437
elif line[0] == '>':
432438
line = ' ' * (LEADING_SPACES_NUM - 1) + \
433439
colored(
434440
line.replace('>', '').replace('<', ''),
435441
*colors_of('description')
436442
)
437443
sys.stdout.buffer.write(line.encode('utf-8'))
444+
445+
# Handle an example description
438446
elif line[0] == '-':
439447
line = '\n' + ' ' * LEADING_SPACES_NUM + \
440448
colored(line, *colors_of('example'))
449+
# Stylize text within backticks using italics
450+
if '`' in line:
451+
# Backticks should occur in pairs, so str.split results in an odd number of elements
452+
*parts, last_part = line.split('`')
453+
# Setup an infinite cycle of italic and reset ANSI escape pairs
454+
italics_escapes = itertools.cycle(('\x1B[3m', '\x1B[0m'))
455+
# Rejoin the original string parts with the matching escape pairs
456+
line = "".join(itertools.chain.from_iterable(zip(parts, italics_escapes))) + last_part
441457
sys.stdout.buffer.write(line.encode('utf-8'))
458+
459+
# Handle an example command
442460
elif line[0] == '`':
443461
line = line[1:-1] # Remove backticks for parsing
444462

0 commit comments

Comments
 (0)