Skip to content

Commit d8b4f62

Browse files
committed
Ensure example styles are still applied and not completely reset
1 parent 74718de commit d8b4f62

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
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: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ def get_page(
370370

371371
LEADING_SPACES_NUM = 2
372372

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

@@ -415,6 +417,11 @@ def colors_of(key: str) -> Tuple[str, str, List[str]]:
415417

416418

417419
def output(page: str, plain: bool = False) -> None:
420+
def emphasise_example(x: str) -> str:
421+
# Use ANSI escapes to enable italics at the start and disable at the end
422+
# Also use the color yellow to differentiate from the default green
423+
return "\x1B[3m" + colored(x.group('example'), 'yellow') + "\x1B[23m"
424+
418425
if not plain:
419426
print()
420427
for line in page:
@@ -444,16 +451,24 @@ def output(page: str, plain: bool = False) -> None:
444451

445452
# Handle an example description
446453
elif line[0] == '-':
447-
line = '\n' + ' ' * LEADING_SPACES_NUM + \
448-
colored(line, *colors_of('example'))
449-
# Stylize text within backticks using italics
454+
455+
# Stylize text within backticks using yellow italics
450456
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
457+
elements = ['\n', ' ' * LEADING_SPACES_NUM]
458+
459+
for item in EXAMPLE_SPLIT_REGEX.split(line):
460+
item, replaced = EXAMPLE_REGEX.subn(emphasise_example, item)
461+
if not replaced:
462+
item = colored(item, *colors_of('example'))
463+
elements.append(item)
464+
465+
line = ''.join(elements)
466+
467+
# Otherwise, use the same colour for the whole line
468+
else:
469+
line = '\n' + ' ' * LEADING_SPACES_NUM + \
470+
colored(line, *colors_of('example'))
471+
457472
sys.stdout.buffer.write(line.encode('utf-8'))
458473

459474
# Handle an example command

0 commit comments

Comments
 (0)