Skip to content
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

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

Merged
merged 4 commits into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
37 changes: 35 additions & 2 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 @@ -369,6 +370,8 @@ def get_page(

LEADING_SPACES_NUM = 2

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

Expand Down Expand Up @@ -414,6 +417,11 @@ def colors_of(key: str) -> Tuple[str, str, List[str]]:


def output(page: str, plain: bool = False) -> None:
def emphasise_example(x: str) -> str:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opted for a def to avoid a lambda exceeding the line length.

# Use ANSI escapes to enable italics at the start and disable at the end
# Also use the color yellow to differentiate from the default green
return "\x1B[3m" + colored(x.group('example'), 'yellow') + "\x1B[23m"

if not plain:
print()
for line in page:
Expand All @@ -422,23 +430,48 @@ 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 yellow italics
if '`' in line:
elements = ['\n', ' ' * LEADING_SPACES_NUM]

for item in EXAMPLE_SPLIT_REGEX.split(line):
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Followed the pattern for example commands below, using regex to pull out the substrings using backticks.

item, replaced = EXAMPLE_REGEX.subn(emphasise_example, item)
if not replaced:
item = colored(item, *colors_of('example'))
elements.append(item)

line = ''.join(elements)

# Otherwise, use the same colour for the whole line
else:
line = '\n' + ' ' * LEADING_SPACES_NUM + \
colored(line, *colors_of('example'))

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