Skip to content

Commit e2c4068

Browse files
ItamarShalevItamar Shalevkbdharun
authored
feat: add TLDR_PLATFORM env variable to override platform detection (#292)
Signed-off-by: Itamar Shalev <[email protected]> Signed-off-by: Dharun Krishna K B <[email protected]> Co-authored-by: Itamar Shalev <[email protected]> Co-authored-by: K.B.Dharun Krishna <[email protected]>
1 parent 9d15270 commit e2c4068

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
pip install tldr
2525
```
2626

27+
Install the Python Client in an isolated environment using [`pipx`](https://pipx.pypa.io/stable/):
28+
29+
```bash
30+
pipx install tldr
31+
```
32+
2733
### from Arch Linux repository
2834

2935
```bash
@@ -91,8 +97,15 @@ export TLDR_CACHE_MAX_AGE=720
9197
export TLDR_PAGES_SOURCE_LOCATION="https://raw.githubusercontent.com/tldr-pages/tldr/main/pages"
9298
export TLDR_DOWNLOAD_CACHE_LOCATION="https://github.com/tldr-pages/tldr/releases/latest/download/tldr.zip"
9399
export TLDR_OPTIONS=short
100+
export TLDR_PLATFORM=linux
94101
```
95102
103+
### Platform
104+
105+
Determines the platform that tldr will use based on the custom `TLDR_PLATFORM` environment variable or automatically via system platform detection.
106+
107+
For a complete list of supported platform values for the `--platform` option flag, refer to the [help page](#usage).
108+
96109
### Cache
97110
98111
Cache is downloaded from `TLDR_DOWNLOAD_CACHE_LOCATION` (defaults to the one described in [the client specification](https://github.com/tldr-pages/tldr/blob/main/CLIENT-SPECIFICATION.md#caching)), unzipped and extracted into the [local cache directory](#cache-location). Pages are loaded directly from `TLDR_PAGES_SOURCE_LOCATION` if `tldr <command>` is used.

tldr.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@
5656
"osx": "osx",
5757
"sunos": "sunos",
5858
"win32": "windows",
59-
"windows": "windows"
59+
"windows": "windows",
60+
"common": "common",
6061
}
6162

6263

@@ -209,7 +210,7 @@ def get_platform() -> str:
209210

210211

211212
def get_platform_list() -> List[str]:
212-
platforms = ['common'] + list(set(OS_DIRECTORIES.values()))
213+
platforms = list(set(OS_DIRECTORIES.values()))
213214
current_platform = get_platform()
214215
platforms.remove(current_platform)
215216
platforms.insert(0, current_platform)
@@ -592,17 +593,17 @@ def create_parser() -> ArgumentParser:
592593
action='store_true',
593594
help="Delete the local cache of pages and exit")
594595

596+
all_platforms = sorted(set(OS_DIRECTORIES.values()))
597+
platforms_str = "[" + ", ".join(all_platforms) + "]"
598+
595599
parser.add_argument(
596600
'-p', '--platform',
597601
nargs=1,
598602
default=None,
599603
type=str,
600-
choices=['android', 'freebsd', 'linux', 'netbsd', 'openbsd', 'osx', 'sunos',
601-
'windows', 'common'],
604+
choices=all_platforms,
602605
metavar='PLATFORM',
603-
help="Override the operating system "
604-
"[android, freebsd, linux, netbsd, openbsd,"
605-
" osx, sunos, windows, common]"
606+
help=f"Override the operating system {platforms_str}"
606607
)
607608

608609
parser.add_argument('-l', '--list',
@@ -669,6 +670,21 @@ def main() -> None:
669670

670671
options = parser.parse_args()
671672

673+
if sys.platform == "win32":
674+
import colorama
675+
colorama.init(strip=options.color)
676+
677+
if options.platform is None:
678+
platform_env = os.environ.get('TLDR_PLATFORM', '').strip().lower()
679+
if platform_env in OS_DIRECTORIES:
680+
options.platform = [platform_env]
681+
elif platform_env:
682+
warning_msg = (
683+
f"Warning: '{platform_env}' is not a supported TLDR_PLATFORM environment value."
684+
"\nFalling back to auto-detection."
685+
)
686+
print(colored(warning_msg, 'yellow'))
687+
672688
display_option_length = "long"
673689
if os.environ.get('TLDR_OPTIONS') == "short":
674690
display_option_length = "short"
@@ -683,10 +699,6 @@ def main() -> None:
683699
if options.short_options and options.long_options:
684700
display_option_length = "both"
685701

686-
if sys.platform == "win32":
687-
import colorama
688-
colorama.init(strip=options.color)
689-
690702
if options.color is False:
691703
os.environ["FORCE_COLOR"] = "true"
692704

0 commit comments

Comments
 (0)