From 6307f0173cbe26d33531783bd3913fdb7586dd26 Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Fri, 13 Dec 2024 18:16:43 +0200 Subject: [PATCH 01/25] Initial work on option lenght selection --- tldr.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/tldr.py b/tldr.py index 3749988..be9a128 100755 --- a/tldr.py +++ b/tldr.py @@ -415,7 +415,7 @@ def colors_of(key: str) -> Tuple[str, str, List[str]]: return (color, on_color, attrs) -def output(page: str, plain: bool = False) -> None: +def output(page: str, plain: bool = False, longform: bool = True, shortform: bool = True) -> 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 @@ -477,6 +477,11 @@ def emphasise_example(x: str) -> str: # Handle escaped placeholders first line = line.replace(r'\{\{', '__ESCAPED_OPEN__') line = line.replace(r'\}\}', '__ESCAPED_CLOSE__') + if not (shortform and longform): + if shortform: + line = re.sub(r'{{(-.)\|--.+?}}', r'{{\1}}', line) + elif longform: + line = re.sub(r'{{-.\|(--.+?)}}', r'{{\1}}', line) elements = [' ' * 2 * LEADING_SPACES_NUM] for item in COMMAND_SPLIT_REGEX.split(line): From 8239aef61ef64db603af1ec33bfbd26878397d3a Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Fri, 13 Dec 2024 18:34:34 +0200 Subject: [PATCH 02/25] Document functionality with a comment --- tldr.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tldr.py b/tldr.py index be9a128..9116e6c 100755 --- a/tldr.py +++ b/tldr.py @@ -477,6 +477,8 @@ def emphasise_example(x: str) -> str: # Handle escaped placeholders first line = line.replace(r'\{\{', '__ESCAPED_OPEN__') line = line.replace(r'\}\}', '__ESCAPED_CLOSE__') + + # Extract long or short options from placeholders if not (shortform and longform): if shortform: line = re.sub(r'{{(-.)\|--.+?}}', r'{{\1}}', line) From fb452eb956be04a71e3e5928f821988796965edc Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Fri, 13 Dec 2024 20:34:38 +0200 Subject: [PATCH 03/25] Stricter regex --- tldr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tldr.py b/tldr.py index 9116e6c..c84a1ca 100755 --- a/tldr.py +++ b/tldr.py @@ -481,9 +481,9 @@ def emphasise_example(x: str) -> str: # Extract long or short options from placeholders if not (shortform and longform): if shortform: - line = re.sub(r'{{(-.)\|--.+?}}', r'{{\1}}', line) + line = re.sub(r'{{(-.)\|--[^|]+?}}', r'{{\1}}', line) elif longform: - line = re.sub(r'{{-.\|(--.+?)}}', r'{{\1}}', line) + line = re.sub(r'{{-.\|(--[^|]+?)}}', r'{{\1}}', line) elements = [' ' * 2 * LEADING_SPACES_NUM] for item in COMMAND_SPLIT_REGEX.split(line): From 9926aff29770a0beeea9894a3e941c047ea6f0e5 Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Sat, 14 Dec 2024 10:26:45 +0200 Subject: [PATCH 04/25] Allow for multi-option placeholders like in rsync.md --- tldr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tldr.py b/tldr.py index c84a1ca..ceb53c5 100755 --- a/tldr.py +++ b/tldr.py @@ -481,9 +481,9 @@ def emphasise_example(x: str) -> str: # Extract long or short options from placeholders if not (shortform and longform): if shortform: - line = re.sub(r'{{(-.)\|--[^|]+?}}', r'{{\1}}', line) + line = re.sub(r'{{(-[^|]+)\|--[^|]+?}}', r'{{\1}}', line) elif longform: - line = re.sub(r'{{-.\|(--[^|]+?)}}', r'{{\1}}', line) + line = re.sub(r'{{-[^|]+\|(--[^|]+?)}}', r'{{\1}}', line) elements = [' ' * 2 * LEADING_SPACES_NUM] for item in COMMAND_SPLIT_REGEX.split(line): From 5d99ef6947f9d4d23f3d08185ca3f0a2b956fd3c Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Sun, 15 Dec 2024 22:56:42 +0200 Subject: [PATCH 05/25] Set display with options --- tldr.py | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/tldr.py b/tldr.py index ceb53c5..bc82619 100755 --- a/tldr.py +++ b/tldr.py @@ -415,7 +415,7 @@ def colors_of(key: str) -> Tuple[str, str, List[str]]: return (color, on_color, attrs) -def output(page: str, plain: bool = False, longform: bool = True, shortform: bool = True) -> None: +def output(page: str, short: bool, long: bool, 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 @@ -479,10 +479,10 @@ def emphasise_example(x: str) -> str: line = line.replace(r'\}\}', '__ESCAPED_CLOSE__') # Extract long or short options from placeholders - if not (shortform and longform): - if shortform: + if not (short and long): + if short: line = re.sub(r'{{(-[^|]+)\|--[^|]+?}}', r'{{\1}}', line) - elif longform: + elif long: line = re.sub(r'{{-[^|]+\|(--[^|]+?)}}', r'{{\1}}', line) elements = [' ' * 2 * LEADING_SPACES_NUM] @@ -610,6 +610,18 @@ def create_parser() -> ArgumentParser: action='store_true', help='Just print the plain page file.') + parser.add_argument('-V', '--longform', + default=False, + action="store_true", + help='Display longform options over shortform', + ) + + parser.add_argument('-S', '--shortform', + default=False, + action="store_true", + help='Display shortform options over longform', + ) + parser.add_argument( 'command', type=str, nargs='*', help="command to lookup", metavar='command' ).complete = {"bash": "shtab_tldr_cmd_list", "zsh": "shtab_tldr_cmd_list"} @@ -631,6 +643,8 @@ def main() -> None: options = parser.parse_args() + short=options.shortform + long=options.longform colorama.init(strip=options.color) if options.color is False: os.environ["FORCE_COLOR"] = "true" @@ -682,7 +696,7 @@ def main() -> None: " send a pull request to: https://github.com/tldr-pages/tldr" ).format(cmd=command)) else: - output(results[0][0], plain=options.markdown) + output(results[0][0], short, long, plain=options.markdown) if results[1:]: platforms_str = [result[1] for result in results[1:]] are_multiple_platforms = len(platforms_str) > 1 From f7928a4e0c998a8bd3fff674356a47c30f064b13 Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Sun, 15 Dec 2024 23:05:44 +0200 Subject: [PATCH 06/25] Fix issues --- tldr.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/tldr.py b/tldr.py index bc82619..d405fe0 100755 --- a/tldr.py +++ b/tldr.py @@ -613,14 +613,12 @@ def create_parser() -> ArgumentParser: parser.add_argument('-V', '--longform', default=False, action="store_true", - help='Display longform options over shortform', - ) + help='Display longform options over shortform') parser.add_argument('-S', '--shortform', default=False, action="store_true", - help='Display shortform options over longform', - ) + help='Display shortform options over longform') parser.add_argument( 'command', type=str, nargs='*', help="command to lookup", metavar='command' @@ -643,8 +641,8 @@ def main() -> None: options = parser.parse_args() - short=options.shortform - long=options.longform + short = options.shortform + long = options.longform colorama.init(strip=options.color) if options.color is False: os.environ["FORCE_COLOR"] = "true" @@ -663,6 +661,8 @@ def main() -> None: if file_path.exists(): with file_path.open(encoding='utf-8') as open_file: output(open_file.read().encode('utf-8').splitlines(), + short, + long, plain=options.markdown) elif options.search: search_term = options.search.lower() From 9571e3415ab3b5be9c615a109b5e03a0ad65ba38 Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Sun, 15 Dec 2024 23:16:53 +0200 Subject: [PATCH 07/25] Fix tests --- tests/test_tldr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_tldr.py b/tests/test_tldr.py index 772735c..1c6ddde 100644 --- a/tests/test_tldr.py +++ b/tests/test_tldr.py @@ -21,7 +21,7 @@ def test_whole_page(page_name, monkeypatch): sys.stdout = io.StringIO() sys.stdout.buffer = types.SimpleNamespace() sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8")) - tldr.output(f_original) + tldr.output(f_original, True, True) sys.stdout.seek(0) tldr_output = sys.stdout.read().encode("utf-8") @@ -39,7 +39,7 @@ def test_markdown_mode(page_name): sys.stdout = io.StringIO() sys.stdout.buffer = types.SimpleNamespace() sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8")) - tldr.output(d_original.splitlines(), plain=True) + tldr.output(d_original.splitlines(), True, True plain=True) sys.stdout.seek(0) tldr_output = sys.stdout.read().encode("utf-8") From ea973a3fe7cf63aa9d33a83451609fdbc441d935 Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Sun, 15 Dec 2024 23:19:49 +0200 Subject: [PATCH 08/25] Fix comma --- tests/test_tldr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_tldr.py b/tests/test_tldr.py index 1c6ddde..35f1add 100644 --- a/tests/test_tldr.py +++ b/tests/test_tldr.py @@ -39,7 +39,7 @@ def test_markdown_mode(page_name): sys.stdout = io.StringIO() sys.stdout.buffer = types.SimpleNamespace() sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8")) - tldr.output(d_original.splitlines(), True, True plain=True) + tldr.output(d_original.splitlines(), True, True, plain=True) sys.stdout.seek(0) tldr_output = sys.stdout.read().encode("utf-8") From e1fc48b45738f58633bcd57b535a66a50ae0a8b4 Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Sun, 15 Dec 2024 23:44:34 +0200 Subject: [PATCH 09/25] Add env variable support --- tldr.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tldr.py b/tldr.py index d405fe0..de58893 100755 --- a/tldr.py +++ b/tldr.py @@ -640,9 +640,15 @@ def main() -> None: parser = create_parser() options = parser.parse_args() - - short = options.shortform - long = options.longform + short = False + long = False + if not (options.shortform or options.longform): + short = int(os.environ.get('TLDR_SHORTFORM', '0')) > 0 + long = int(os.environ.get('TLDR_LONGFORM', '0')) > 0 + elif options.shortform: + short = True + elif options.longform: + long = True colorama.init(strip=options.color) if options.color is False: os.environ["FORCE_COLOR"] = "true" From 1b5395a72d9540174fd64c62cd4e09a2cfbdf42b Mon Sep 17 00:00:00 2001 From: Managor <42655600+Managor@users.noreply.github.com> Date: Fri, 20 Dec 2024 10:34:59 +0200 Subject: [PATCH 10/25] shuffle order --- tldr.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tldr.py b/tldr.py index de58893..58b51cd 100755 --- a/tldr.py +++ b/tldr.py @@ -610,15 +610,15 @@ def create_parser() -> ArgumentParser: action='store_true', help='Just print the plain page file.') - parser.add_argument('-V', '--longform', - default=False, - action="store_true", - help='Display longform options over shortform') - parser.add_argument('-S', '--shortform', default=False, action="store_true", help='Display shortform options over longform') + + parser.add_argument('-V', '--longform', + default=False, + action="store_true", + help='Display longform options over shortform') parser.add_argument( 'command', type=str, nargs='*', help="command to lookup", metavar='command' From a78108ccec2465162fc2d0c466ce478d68dc51b2 Mon Sep 17 00:00:00 2001 From: Managor <42655600+Managor@users.noreply.github.com> Date: Fri, 20 Dec 2024 10:41:25 +0200 Subject: [PATCH 11/25] whitespace --- tldr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tldr.py b/tldr.py index 58b51cd..f35c3e8 100755 --- a/tldr.py +++ b/tldr.py @@ -614,7 +614,7 @@ def create_parser() -> ArgumentParser: default=False, action="store_true", help='Display shortform options over longform') - + parser.add_argument('-V', '--longform', default=False, action="store_true", From 68f36a00a9ba48146306acd2ebf6366bc0c917b0 Mon Sep 17 00:00:00 2001 From: Managor <42655600+Managor@users.noreply.github.com> Date: Tue, 28 Jan 2025 21:18:54 +0200 Subject: [PATCH 12/25] longform by default --- tldr.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tldr.py b/tldr.py index f35c3e8..ba5d6ff 100755 --- a/tldr.py +++ b/tldr.py @@ -644,7 +644,10 @@ def main() -> None: long = False if not (options.shortform or options.longform): short = int(os.environ.get('TLDR_SHORTFORM', '0')) > 0 - long = int(os.environ.get('TLDR_LONGFORM', '0')) > 0 + if short: + long = int(os.environ.get('TLDR_LONGFORM', '0')) > 0 + else: + long = int(os.environ.get('TLDR_LONGFORM', '1')) > 0 elif options.shortform: short = True elif options.longform: From 09fe404d1e2f7bf97af8fc513b507e14d97d6da2 Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Tue, 28 Jan 2025 21:43:12 +0200 Subject: [PATCH 13/25] New syntax and default behavior --- tldr.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tldr.py b/tldr.py index ba5d6ff..d47ec6b 100755 --- a/tldr.py +++ b/tldr.py @@ -481,9 +481,10 @@ def emphasise_example(x: str) -> str: # Extract long or short options from placeholders if not (short and long): if short: - line = re.sub(r'{{(-[^|]+)\|--[^|]+?}}', r'{{\1}}', line) + line = re.sub(r'{{\[([^|]+)\|[^|]+?\]}}', r'\1', line) elif long: - line = re.sub(r'{{-[^|]+\|(--[^|]+?)}}', r'{{\1}}', line) + line = re.sub(r'{{\[[^|]+\|([^|]+?)\]}}', r'\1', line) + line = re.sub(r'{{\[([^|]+\|[^|]+?)\]}}', r'{{\1}}', line) elements = [' ' * 2 * LEADING_SPACES_NUM] for item in COMMAND_SPLIT_REGEX.split(line): @@ -648,9 +649,9 @@ def main() -> None: long = int(os.environ.get('TLDR_LONGFORM', '0')) > 0 else: long = int(os.environ.get('TLDR_LONGFORM', '1')) > 0 - elif options.shortform: + if options.shortform: short = True - elif options.longform: + if options.longform: long = True colorama.init(strip=options.color) if options.color is False: From e3a41269748129cfc90b8b003e3a55c5f5e93151 Mon Sep 17 00:00:00 2001 From: Managor <42655600+Managor@users.noreply.github.com> Date: Wed, 29 Jan 2025 18:55:19 +0200 Subject: [PATCH 14/25] Leave the brackets if both are requested --- tldr.py | 1 - 1 file changed, 1 deletion(-) diff --git a/tldr.py b/tldr.py index d47ec6b..4371e2c 100755 --- a/tldr.py +++ b/tldr.py @@ -484,7 +484,6 @@ def emphasise_example(x: str) -> str: line = re.sub(r'{{\[([^|]+)\|[^|]+?\]}}', r'\1', line) elif long: line = re.sub(r'{{\[[^|]+\|([^|]+?)\]}}', r'\1', line) - line = re.sub(r'{{\[([^|]+\|[^|]+?)\]}}', r'{{\1}}', line) elements = [' ' * 2 * LEADING_SPACES_NUM] for item in COMMAND_SPLIT_REGEX.split(line): From d156238fe64b82d24983e1b296ab7ce1ea1524f6 Mon Sep 17 00:00:00 2001 From: Managor <42655600+Managor@users.noreply.github.com> Date: Fri, 31 Jan 2025 19:20:59 +0200 Subject: [PATCH 15/25] -o and -O for options --- tldr.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tldr.py b/tldr.py index 4371e2c..e177c2c 100755 --- a/tldr.py +++ b/tldr.py @@ -610,12 +610,12 @@ def create_parser() -> ArgumentParser: action='store_true', help='Just print the plain page file.') - parser.add_argument('-S', '--shortform', + parser.add_argument('-o', '--shortform', default=False, action="store_true", help='Display shortform options over longform') - parser.add_argument('-V', '--longform', + parser.add_argument('-O', '--longform', default=False, action="store_true", help='Display longform options over shortform') From 8564a75be3ed4f54492068bd2ba30ee98c85e615 Mon Sep 17 00:00:00 2001 From: Managor <42655600+Managor@users.noreply.github.com> Date: Tue, 11 Feb 2025 12:48:03 +0200 Subject: [PATCH 16/25] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 546e45a..54311f0 100644 --- a/README.md +++ b/README.md @@ -86,6 +86,8 @@ export TLDR_CACHE_ENABLED=1 export TLDR_CACHE_MAX_AGE=720 export TLDR_PAGES_SOURCE_LOCATION="https://raw.githubusercontent.com/tldr-pages/tldr/main/pages" export TLDR_DOWNLOAD_CACHE_LOCATION="https://tldr-pages.github.io/assets/tldr.zip" +export TLDR_SHORTFORM=1 +export TLDR_LONGFORM=1 ``` ### Cache From ddcad3402c632bcbc7a795f9cdd8eb87142fee62 Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Fri, 7 Mar 2025 21:38:17 +0200 Subject: [PATCH 17/25] merge --- tldr.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tldr.py b/tldr.py index e177c2c..db96aec 100755 --- a/tldr.py +++ b/tldr.py @@ -610,12 +610,12 @@ def create_parser() -> ArgumentParser: action='store_true', help='Just print the plain page file.') - parser.add_argument('-o', '--shortform', + parser.add_argument('--short-options', default=False, action="store_true", help='Display shortform options over longform') - parser.add_argument('-O', '--longform', + parser.add_argument('--long-options', default=False, action="store_true", help='Display longform options over shortform') @@ -642,15 +642,15 @@ def main() -> None: options = parser.parse_args() short = False long = False - if not (options.shortform or options.longform): + if not (options.short-options or options.long-options): short = int(os.environ.get('TLDR_SHORTFORM', '0')) > 0 if short: long = int(os.environ.get('TLDR_LONGFORM', '0')) > 0 else: long = int(os.environ.get('TLDR_LONGFORM', '1')) > 0 - if options.shortform: + if options.short-options: short = True - if options.longform: + if options.long-options: long = True colorama.init(strip=options.color) if options.color is False: From f4e2ac2b8e72367d579fe81547c9e993f7e56efc Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Fri, 7 Mar 2025 22:02:41 +0200 Subject: [PATCH 18/25] Apply official option naming --- README.md | 4 ++-- tldr.py | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 54311f0..bfdf6c7 100644 --- a/README.md +++ b/README.md @@ -86,8 +86,8 @@ export TLDR_CACHE_ENABLED=1 export TLDR_CACHE_MAX_AGE=720 export TLDR_PAGES_SOURCE_LOCATION="https://raw.githubusercontent.com/tldr-pages/tldr/main/pages" export TLDR_DOWNLOAD_CACHE_LOCATION="https://tldr-pages.github.io/assets/tldr.zip" -export TLDR_SHORTFORM=1 -export TLDR_LONGFORM=1 +export TLDR_SHORT_OPTIONS=1 +export TLDR_LONG_OPTIONS=1 ``` ### Cache diff --git a/tldr.py b/tldr.py index db96aec..6c9c97e 100755 --- a/tldr.py +++ b/tldr.py @@ -642,15 +642,15 @@ def main() -> None: options = parser.parse_args() short = False long = False - if not (options.short-options or options.long-options): - short = int(os.environ.get('TLDR_SHORTFORM', '0')) > 0 + if not (options.short_options or options.long_options): + short = int(os.environ.get('TLDR_SHORT_OPTIONS', '0')) > 0 if short: - long = int(os.environ.get('TLDR_LONGFORM', '0')) > 0 + long = int(os.environ.get('TLDR_LONG_OPTIONS', '0')) > 0 else: - long = int(os.environ.get('TLDR_LONGFORM', '1')) > 0 - if options.short-options: + long = int(os.environ.get('TLDR_LONG_OPTIONS', '1')) > 0 + if options.short_options: short = True - if options.long-options: + if options.long_options: long = True colorama.init(strip=options.color) if options.color is False: From 7607aa6c48886ae2225af350cb4c5bbd4065e98a Mon Sep 17 00:00:00 2001 From: Managor <42655600+Managor@users.noreply.github.com> Date: Sat, 8 Mar 2025 00:16:25 +0200 Subject: [PATCH 19/25] Update README.md --- README.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bfdf6c7..52b3f3b 100644 --- a/README.md +++ b/README.md @@ -86,8 +86,7 @@ export TLDR_CACHE_ENABLED=1 export TLDR_CACHE_MAX_AGE=720 export TLDR_PAGES_SOURCE_LOCATION="https://raw.githubusercontent.com/tldr-pages/tldr/main/pages" export TLDR_DOWNLOAD_CACHE_LOCATION="https://tldr-pages.github.io/assets/tldr.zip" -export TLDR_SHORT_OPTIONS=1 -export TLDR_LONG_OPTIONS=1 +export TLDR_OPTIONS=short ``` ### Cache @@ -182,3 +181,7 @@ can either use the `--source` flag when using tldr or by specifying the followin - it can also point to a local directory using `file:///path/to/directory`. - `TLDR_DOWNLOAD_CACHE_LOCATION` to control where to pull a zip of all pages from. - defaults to `https://github.com/tldr-pages/tldr/releases/latest/download/tldr.zip`. + +### Command options + +Pages might contain `{{[*|*]}}` pattern to let the client decice whether to show shortfrom or longform versions of options. This can be configured with `TLDR_OPTIONS` and it accepts values `short`, `long` and `both` From bbb827053588f5bda5b521779c0c06951d51f178 Mon Sep 17 00:00:00 2001 From: Managor <42655600+Managor@users.noreply.github.com> Date: Sat, 8 Mar 2025 00:26:37 +0200 Subject: [PATCH 20/25] env variables --- tldr.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/tldr.py b/tldr.py index 6c9c97e..dddf6c8 100755 --- a/tldr.py +++ b/tldr.py @@ -643,11 +643,13 @@ def main() -> None: short = False long = False if not (options.short_options or options.long_options): - short = int(os.environ.get('TLDR_SHORT_OPTIONS', '0')) > 0 - if short: - long = int(os.environ.get('TLDR_LONG_OPTIONS', '0')) > 0 - else: - long = int(os.environ.get('TLDR_LONG_OPTIONS', '1')) > 0 + if TLDR_OPTIONS = short: + short = True + elif TLDR_OPTIONS = long: + long = True + elif TLDR_OPTION = both: + short = True + long = True if options.short_options: short = True if options.long_options: From efc9e5a2e139ed1b914d02f31d7d394c5659e247 Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Sat, 8 Mar 2025 00:31:33 +0200 Subject: [PATCH 21/25] Make it work --- tldr.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tldr.py b/tldr.py index dddf6c8..b0c953f 100755 --- a/tldr.py +++ b/tldr.py @@ -643,11 +643,11 @@ def main() -> None: short = False long = False if not (options.short_options or options.long_options): - if TLDR_OPTIONS = short: + if os.environ.get('TLDR_OPTIONS') == "short": short = True - elif TLDR_OPTIONS = long: + elif os.environ.get('TLDR_OPTIONS') == "long": long = True - elif TLDR_OPTION = both: + elif os.environ.get('TLDR_OPTIONS') == "both": short = True long = True if options.short_options: From a8deec24d957a4ab976e47f62793b4cfa8ebdc56 Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Sat, 8 Mar 2025 02:27:25 +0200 Subject: [PATCH 22/25] fix default state --- tldr.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tldr.py b/tldr.py index b0c953f..ff9a24d 100755 --- a/tldr.py +++ b/tldr.py @@ -641,11 +641,13 @@ def main() -> None: options = parser.parse_args() short = False - long = False + long = True if not (options.short_options or options.long_options): if os.environ.get('TLDR_OPTIONS') == "short": short = True + long = False elif os.environ.get('TLDR_OPTIONS') == "long": + short = False long = True elif os.environ.get('TLDR_OPTIONS') == "both": short = True From d18a113f25d7e0da75cb2ad731910aded2e51cd3 Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Sat, 8 Mar 2025 13:04:27 +0200 Subject: [PATCH 23/25] Fix --short-options --- tldr.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tldr.py b/tldr.py index ff9a24d..e36fbb9 100755 --- a/tldr.py +++ b/tldr.py @@ -654,6 +654,7 @@ def main() -> None: long = True if options.short_options: short = True + long = False if options.long_options: long = True colorama.init(strip=options.color) From f2c5406af2885c0cbb72b3b189821e9251451727 Mon Sep 17 00:00:00 2001 From: Managor <42655600+Managor@users.noreply.github.com> Date: Thu, 13 Mar 2025 04:40:09 +0200 Subject: [PATCH 24/25] Update README.md Co-authored-by: Lena <126529524+acuteenvy@users.noreply.github.com> --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 52b3f3b..8c03169 100644 --- a/README.md +++ b/README.md @@ -184,4 +184,4 @@ can either use the `--source` flag when using tldr or by specifying the followin ### Command options -Pages might contain `{{[*|*]}}` pattern to let the client decice whether to show shortfrom or longform versions of options. This can be configured with `TLDR_OPTIONS` and it accepts values `short`, `long` and `both` +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` From 7e77cef49bc4900ed6fe01fd40463a09b93ec004 Mon Sep 17 00:00:00 2001 From: Antti Savolainen Date: Mon, 24 Mar 2025 14:44:05 +0200 Subject: [PATCH 25/25] Use strings instead of bools --- tests/test_tldr.py | 4 ++-- tldr.py | 35 +++++++++++++++-------------------- 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/tests/test_tldr.py b/tests/test_tldr.py index 35f1add..9fd713a 100644 --- a/tests/test_tldr.py +++ b/tests/test_tldr.py @@ -21,7 +21,7 @@ def test_whole_page(page_name, monkeypatch): sys.stdout = io.StringIO() sys.stdout.buffer = types.SimpleNamespace() sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8")) - tldr.output(f_original, True, True) + tldr.output(f_original, "both") sys.stdout.seek(0) tldr_output = sys.stdout.read().encode("utf-8") @@ -39,7 +39,7 @@ def test_markdown_mode(page_name): sys.stdout = io.StringIO() sys.stdout.buffer = types.SimpleNamespace() sys.stdout.buffer.write = lambda x: sys.stdout.write(x.decode("utf-8")) - tldr.output(d_original.splitlines(), True, True, plain=True) + tldr.output(d_original.splitlines(), "both", plain=True) sys.stdout.seek(0) tldr_output = sys.stdout.read().encode("utf-8") diff --git a/tldr.py b/tldr.py index e36fbb9..e4d2b28 100755 --- a/tldr.py +++ b/tldr.py @@ -415,7 +415,7 @@ def colors_of(key: str) -> Tuple[str, str, List[str]]: return (color, on_color, attrs) -def output(page: str, short: bool, long: bool, plain: bool = False) -> None: +def output(page: str, display_option_length: 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 @@ -479,11 +479,10 @@ def emphasise_example(x: str) -> str: line = line.replace(r'\}\}', '__ESCAPED_CLOSE__') # Extract long or short options from placeholders - if not (short and long): - if short: - line = re.sub(r'{{\[([^|]+)\|[^|]+?\]}}', r'\1', line) - elif long: - line = re.sub(r'{{\[[^|]+\|([^|]+?)\]}}', r'\1', line) + if display_option_length == "short": + line = re.sub(r'{{\[([^|]+)\|[^|]+?\]}}', r'\1', line) + elif display_option_length == "long": + line = re.sub(r'{{\[[^|]+\|([^|]+?)\]}}', r'\1', line) elements = [' ' * 2 * LEADING_SPACES_NUM] for item in COMMAND_SPLIT_REGEX.split(line): @@ -640,23 +639,20 @@ def main() -> None: parser = create_parser() options = parser.parse_args() - short = False - long = True + display_option_length = "long" if not (options.short_options or options.long_options): if os.environ.get('TLDR_OPTIONS') == "short": - short = True - long = False + display_option_length = "short" elif os.environ.get('TLDR_OPTIONS') == "long": - short = False - long = True + display_option_length = "long" elif os.environ.get('TLDR_OPTIONS') == "both": - short = True - long = True + display_option_length = "both" if options.short_options: - short = True - long = False + display_option_length = "short" if options.long_options: - long = True + display_option_length = "long" + if options.short_options and options.long_options: + display_option_length = "both" colorama.init(strip=options.color) if options.color is False: os.environ["FORCE_COLOR"] = "true" @@ -675,8 +671,7 @@ def main() -> None: if file_path.exists(): with file_path.open(encoding='utf-8') as open_file: output(open_file.read().encode('utf-8').splitlines(), - short, - long, + display_option_length, plain=options.markdown) elif options.search: search_term = options.search.lower() @@ -710,7 +705,7 @@ def main() -> None: " send a pull request to: https://github.com/tldr-pages/tldr" ).format(cmd=command)) else: - output(results[0][0], short, long, plain=options.markdown) + output(results[0][0], display_option_length, plain=options.markdown) if results[1:]: platforms_str = [result[1] for result in results[1:]] are_multiple_platforms = len(platforms_str) > 1