Skip to content

Commit d078665

Browse files
Managoracuteenvy
andauthored
Implement option length selection with options and environment variables (#259)
* Initial work on option lenght selection * Document functionality with a comment * Stricter regex * Allow for multi-option placeholders like in rsync.md * Set display with options * Fix issues * Fix tests * Fix comma * Add env variable support * shuffle order * whitespace * longform by default * New syntax and default behavior * Leave the brackets if both are requested * -o and -O for options * Update README.md * merge * Apply official option naming * Update README.md * env variables * Make it work * fix default state * Fix --short-options * Update README.md Co-authored-by: Lena <[email protected]> * Use strings instead of bools --------- Co-authored-by: Lena <[email protected]>
1 parent 4e2e125 commit d078665

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export TLDR_CACHE_ENABLED=1
8686
export TLDR_CACHE_MAX_AGE=720
8787
export TLDR_PAGES_SOURCE_LOCATION="https://raw.githubusercontent.com/tldr-pages/tldr/main/pages"
8888
export TLDR_DOWNLOAD_CACHE_LOCATION="https://github.com/tldr-pages/tldr/releases/latest/download/tldr.zip"
89+
export TLDR_OPTIONS=short
8990
```
9091

9192
### Cache
@@ -180,3 +181,7 @@ can either use the `--source` flag when using tldr or by specifying the followin
180181
- it can also point to a local directory using `file:///path/to/directory`.
181182
- `TLDR_DOWNLOAD_CACHE_LOCATION` to control where to pull a zip of all pages from.
182183
- defaults to `https://github.com/tldr-pages/tldr/releases/latest/download/tldr.zip`.
184+
185+
### Command options
186+
187+
Pages might contain `{{[*|*]}}` patterns to let the client decide whether to show shortform or longform versions of options. This can be configured with `TLDR_OPTIONS`, which accepts values `short`, `long` and `both`

tests/test_tldr.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def test_whole_page(page_name, monkeypatch):
2121
sys.stdout = io.StringIO()
2222
sys.stdout.buffer = types.SimpleNamespace()
2323
sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8"))
24-
tldr.output(f_original)
24+
tldr.output(f_original, "both")
2525

2626
sys.stdout.seek(0)
2727
tldr_output = sys.stdout.read().encode("utf-8")
@@ -39,7 +39,7 @@ def test_markdown_mode(page_name):
3939
sys.stdout = io.StringIO()
4040
sys.stdout.buffer = types.SimpleNamespace()
4141
sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8"))
42-
tldr.output(d_original.splitlines(), plain=True)
42+
tldr.output(d_original.splitlines(), "both", plain=True)
4343

4444
sys.stdout.seek(0)
4545
tldr_output = sys.stdout.read().encode("utf-8")

tldr.py

+33-3
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ def colors_of(key: str) -> Tuple[str, str, List[str]]:
415415
return (color, on_color, attrs)
416416

417417

418-
def output(page: str, plain: bool = False) -> None:
418+
def output(page: str, display_option_length: str, plain: bool = False) -> None:
419419
def emphasise_example(x: str) -> str:
420420
# Use ANSI escapes to enable italics at the start and disable at the end
421421
# Also use the color yellow to differentiate from the default green
@@ -478,6 +478,12 @@ def emphasise_example(x: str) -> str:
478478
line = line.replace(r'\{\{', '__ESCAPED_OPEN__')
479479
line = line.replace(r'\}\}', '__ESCAPED_CLOSE__')
480480

481+
# Extract long or short options from placeholders
482+
if display_option_length == "short":
483+
line = re.sub(r'{{\[([^|]+)\|[^|]+?\]}}', r'\1', line)
484+
elif display_option_length == "long":
485+
line = re.sub(r'{{\[[^|]+\|([^|]+?)\]}}', r'\1', line)
486+
481487
elements = [' ' * 2 * LEADING_SPACES_NUM]
482488
for item in COMMAND_SPLIT_REGEX.split(line):
483489
item, replaced = PARAM_REGEX.subn(
@@ -603,6 +609,16 @@ def create_parser() -> ArgumentParser:
603609
action='store_true',
604610
help='Just print the plain page file.')
605611

612+
parser.add_argument('--short-options',
613+
default=False,
614+
action="store_true",
615+
help='Display shortform options over longform')
616+
617+
parser.add_argument('--long-options',
618+
default=False,
619+
action="store_true",
620+
help='Display longform options over shortform')
621+
606622
parser.add_argument(
607623
'command', type=str, nargs='*', help="command to lookup", metavar='command'
608624
).complete = {"bash": "shtab_tldr_cmd_list", "zsh": "shtab_tldr_cmd_list"}
@@ -623,7 +639,20 @@ def main() -> None:
623639
parser = create_parser()
624640

625641
options = parser.parse_args()
626-
642+
display_option_length = "long"
643+
if not (options.short_options or options.long_options):
644+
if os.environ.get('TLDR_OPTIONS') == "short":
645+
display_option_length = "short"
646+
elif os.environ.get('TLDR_OPTIONS') == "long":
647+
display_option_length = "long"
648+
elif os.environ.get('TLDR_OPTIONS') == "both":
649+
display_option_length = "both"
650+
if options.short_options:
651+
display_option_length = "short"
652+
if options.long_options:
653+
display_option_length = "long"
654+
if options.short_options and options.long_options:
655+
display_option_length = "both"
627656
colorama.init(strip=options.color)
628657
if options.color is False:
629658
os.environ["FORCE_COLOR"] = "true"
@@ -642,6 +671,7 @@ def main() -> None:
642671
if file_path.exists():
643672
with file_path.open(encoding='utf-8') as open_file:
644673
output(open_file.read().encode('utf-8').splitlines(),
674+
display_option_length,
645675
plain=options.markdown)
646676
elif options.search:
647677
search_term = options.search.lower()
@@ -675,7 +705,7 @@ def main() -> None:
675705
" send a pull request to: https://github.com/tldr-pages/tldr"
676706
).format(cmd=command))
677707
else:
678-
output(results[0][0], plain=options.markdown)
708+
output(results[0][0], display_option_length, plain=options.markdown)
679709
if results[1:]:
680710
platforms_str = [result[1] for result in results[1:]]
681711
are_multiple_platforms = len(platforms_str) > 1

0 commit comments

Comments
 (0)