Skip to content

Commit 41054af

Browse files
Respect language settings when geting a list of commands (#239)
Fixes: #238
1 parent 68acf05 commit 41054af

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

tests/test_tldr.py

+21
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,24 @@ def test_get_cache_dir_default(monkeypatch):
135135
monkeypatch.delenv("HOME", raising=False)
136136
monkeypatch.setattr(Path, 'home', lambda: Path('/tmp/expanduser'))
137137
assert tldr.get_cache_dir() == Path("/tmp/expanduser/.cache/tldr")
138+
139+
140+
def test_get_commands(monkeypatch, tmp_path):
141+
cache_default = tmp_path / ".cache" / "tldr" / "pages" / "linux"
142+
Path.mkdir(cache_default, parents=True)
143+
Path.touch(cache_default / "lspci.md")
144+
145+
monkeypatch.setenv("HOME", tmp_path)
146+
147+
result = tldr.get_commands(platforms=["linux"])
148+
149+
assert isinstance(result, list)
150+
assert "lspci (en)" in result
151+
152+
cache_zh = tmp_path / ".cache" / "tldr" / "pages.zh" / "linux"
153+
Path.mkdir(cache_zh, parents=True)
154+
Path.touch(cache_zh / "lspci.md")
155+
156+
result = tldr.get_commands(platforms=["linux"], language=["zh_CN"])
157+
158+
assert "lspci (zh)" in result

tldr.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -305,17 +305,27 @@ def get_page(
305305
PARAM_REGEX = re.compile(r'(?:{{)(?P<param>.+?)(?:}})')
306306

307307

308-
def get_commands(platforms: Optional[List[str]] = None) -> List[str]:
308+
def get_commands(platforms: Optional[List[str]] = None,
309+
language: Optional[str] = None) -> List[str]:
309310
if platforms is None:
310311
platforms = get_platform_list()
311312

313+
if language:
314+
languages = [get_language_code(language[0])]
315+
else:
316+
languages = get_language_list()
317+
312318
commands = []
313319
if get_cache_dir().exists():
314320
for platform in platforms:
315-
path = get_cache_dir() / 'pages' / platform
316-
if not path.exists():
317-
continue
318-
commands += [file.stem for file in path.iterdir() if file.suffix == '.md']
321+
for language in languages:
322+
pages_dir = f'pages.{language}' if language != 'en' else 'pages'
323+
path = get_cache_dir() / pages_dir / platform
324+
if not path.exists():
325+
continue
326+
commands += [f"{file.stem} ({language})"
327+
for file in path.iterdir()
328+
if file.suffix == '.md']
319329
return commands
320330

321331

@@ -511,7 +521,7 @@ def main() -> None:
511521
parser.print_help(sys.stderr)
512522
sys.exit(1)
513523
if options.list:
514-
print('\n'.join(get_commands(options.platform)))
524+
print('\n'.join(get_commands(options.platform, options.language)))
515525
elif options.render:
516526
for command in options.command:
517527
if Path(command).exists():

0 commit comments

Comments
 (0)