Skip to content

feat: stylize text inside backticks when appearing in example description #254

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions tests/data/jq_rendered
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- Output all elements from arrays (or all the values from objects) in a JSON file:
jq '.[]' file.json

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

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

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

- Combine multiple filters:
Expand Down
18 changes: 18 additions & 0 deletions tldr.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#!/usr/bin/env python3
# PYTHON_ARGCOMPLETE_OK

import itertools
import sys
import os
import re
Expand Down Expand Up @@ -422,23 +423,40 @@ def output(page: str, plain: bool = False) -> None:
if plain:
print(line)
continue

elif len(line) == 0:
continue

# Handle the command name
elif line[0] == '#':
line = ' ' * LEADING_SPACES_NUM + \
colored(line.replace('# ', ''), *colors_of('name')) + '\n'
sys.stdout.buffer.write(line.encode('utf-8'))

# Handle the command description
elif line[0] == '>':
line = ' ' * (LEADING_SPACES_NUM - 1) + \
colored(
line.replace('>', '').replace('<', ''),
*colors_of('description')
)
sys.stdout.buffer.write(line.encode('utf-8'))

# Handle an example description
elif line[0] == '-':
line = '\n' + ' ' * LEADING_SPACES_NUM + \
colored(line, *colors_of('example'))
# Stylize text within backticks using italics
if '`' in line:
# Backticks should occur in pairs, so str.split results in an odd number of elements
*parts, last_part = line.split('`')
# Setup an infinite cycle of italic and reset ANSI escape pairs
italics_escapes = itertools.cycle(('\x1B[3m', '\x1B[0m'))
# Rejoin the original string parts with the matching escape pairs
line = "".join(itertools.chain.from_iterable(zip(parts, italics_escapes))) + last_part
sys.stdout.buffer.write(line.encode('utf-8'))

# Handle an example command
elif line[0] == '`':
line = line[1:-1] # Remove backticks for parsing

Expand Down
Loading