From 74718dea23adfcd1ade2e9abb7ff706624c05c2b Mon Sep 17 00:00:00 2001 From: kyluca Date: Wed, 2 Oct 2024 16:46:26 +1000 Subject: [PATCH 1/3] Stylize italics in helptext using ANSI escapes --- tests/data/jq_rendered | 4 ++-- tldr.py | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/tests/data/jq_rendered b/tests/data/jq_rendered index f850dbf..4e40cab 100644 --- a/tests/data/jq_rendered +++ b/tests/data/jq_rendered @@ -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: @@ -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: diff --git a/tldr.py b/tldr.py index 02bcaf5..39247f5 100755 --- a/tldr.py +++ b/tldr.py @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # PYTHON_ARGCOMPLETE_OK +import itertools import sys import os import re @@ -422,12 +423,17 @@ 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( @@ -435,10 +441,22 @@ def output(page: str, plain: bool = False) -> None: *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 From d8b4f620ef573364dd8abe8137e19e936ea8ccde Mon Sep 17 00:00:00 2001 From: kyluca Date: Wed, 2 Oct 2024 23:44:45 +1000 Subject: [PATCH 2/3] Ensure example styles are still applied and not completely reset --- tests/data/jq_rendered | 4 ++-- tldr.py | 33 ++++++++++++++++++++++++--------- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/tests/data/jq_rendered b/tests/data/jq_rendered index 4e40cab..82b274c 100644 --- a/tests/data/jq_rendered +++ b/tests/data/jq_rendered @@ -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: @@ -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: diff --git a/tldr.py b/tldr.py index 39247f5..e59332c 100755 --- a/tldr.py +++ b/tldr.py @@ -370,6 +370,8 @@ def get_page( LEADING_SPACES_NUM = 2 +EXAMPLE_SPLIT_REGEX = re.compile(r'(?P`.+?`)') +EXAMPLE_REGEX = re.compile(r'(?:`)(?P.+?)(?:`)') COMMAND_SPLIT_REGEX = re.compile(r'(?P{{.+?}*}})') PARAM_REGEX = re.compile(r'(?:{{)(?P.+?)(?:}})') @@ -415,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: + # 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: @@ -444,16 +451,24 @@ def output(page: str, plain: bool = False) -> None: # Handle an example description elif line[0] == '-': - line = '\n' + ' ' * LEADING_SPACES_NUM + \ - colored(line, *colors_of('example')) - # Stylize text within backticks using italics + + # Stylize text within backticks using yellow 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 + elements = ['\n', ' ' * LEADING_SPACES_NUM] + + for item in EXAMPLE_SPLIT_REGEX.split(line): + 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 From 141e7a18cf0b2a6e00193d3486b53e263840db8a Mon Sep 17 00:00:00 2001 From: kyluca Date: Thu, 3 Oct 2024 06:38:30 +1000 Subject: [PATCH 3/3] Remove unused import --- tldr.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tldr.py b/tldr.py index e59332c..6713ae8 100755 --- a/tldr.py +++ b/tldr.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # PYTHON_ARGCOMPLETE_OK -import itertools import sys import os import re