Skip to content

Commit 7209bbc

Browse files
authored
Merge branch 'main' into colorama-win-only
2 parents 476ded3 + 1d36280 commit 7209bbc

File tree

7 files changed

+86
-22
lines changed

7 files changed

+86
-22
lines changed

.github/dependabot.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ updates:
33
- package-ecosystem: "github-actions"
44
directory: "/"
55
schedule:
6-
interval: "monthly"
6+
interval: "weekly"

.github/workflows/publish.yml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# This workflow will upload a Python Package using Twine when a release is created
1+
# This workflow will upload a Python Package using Trusted Publishers automatically when a release is created
22
# For more information see: https://help.github.com/en/actions/language-and-framework-guides/using-python-with-github-actions#publishing-to-package-registries
3+
# and https://docs.pypi.org/trusted-publishers/using-a-publisher/.
34

45
name: Upload Python Package
56

@@ -8,14 +9,22 @@ on:
89
types: [created]
910

1011
jobs:
11-
deploy:
12-
12+
pypi-publish:
1313
runs-on: ubuntu-latest
14+
15+
environment:
16+
name: pypi
17+
url: https://pypi.org/project/tldr/
18+
19+
permissions:
20+
contents: read
21+
id-token: write # Required for accessing OpenID Connect (OIDC) token for PyPI trusted publisher
22+
1423
steps:
15-
- uses: actions/checkout@v4
24+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
1625

1726
- name: Set up Python
18-
uses: actions/setup-python@v5
27+
uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55 # v5.5.0
1928
with:
2029
python-version: '3.8'
2130

@@ -56,7 +65,4 @@ jobs:
5665
.
5766
5867
- name: Publish package
59-
uses: pypa/gh-action-pypi-publish@release/v1
60-
with:
61-
user: __token__
62-
password: ${{ secrets.PYPI_PASSWORD }}
68+
uses: pypa/gh-action-pypi-publish@76f52bc884231f62b9a034ebfe128415bbaabdfc # v1.12.4

.github/workflows/test.yml

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ on: ['push', 'pull_request']
44

55
jobs:
66
build:
7-
runs-on: ubuntu-latest
7+
runs-on: ${{ matrix.os }}
88

99
strategy:
1010
matrix:
11+
os: ['ubuntu-latest', 'ubuntu-24.04-arm']
1112
python-version: ['3.8', '3.9', '3.10', '3.11', '3.12', 'pypy3.9', 'pypy3.10']
1213

1314
steps:
14-
- uses: actions/checkout@v4
15+
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
1516

1617
- name: Set up Python ${{ matrix.python-version }}
17-
uses: actions/setup-python@v5
18+
uses: actions/setup-python@8d9ed9ac5c53483de85588cdf95a591a75ab9f55 # v5.5.0
1819
with:
1920
python-version: ${{ matrix.python-version }}
2021

@@ -55,15 +56,22 @@ jobs:
5556
tldr --version
5657
5758
build-snap:
58-
runs-on: ubuntu-latest
59+
runs-on: ${{ matrix.os }}
60+
needs: ['build']
61+
62+
strategy:
63+
matrix:
64+
os: ['ubuntu-latest', 'ubuntu-24.04-arm']
5965

6066
steps:
6167
- uses: actions/checkout@v4
62-
- uses: snapcore/action-build@v1
68+
69+
- uses: canonical/action-build@3bdaa03e1ba6bf59a65f84a751d943d549a54e79 # v1.3.0
6370
id: snapcraft-build
6471
with:
6572
snapcraft-args: "-v"
66-
- uses: actions/upload-artifact@v4
73+
74+
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4.6.2
6775
with:
6876
name: ${{ steps.snapcraft-build.outputs.snap }}
6977
path: ${{ steps.snapcraft-build.outputs.snap }}

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ ENV/
114114
env.bak/
115115
venv.bak/
116116

117+
# direnv
118+
.envrc
119+
.direnv/
120+
117121
# Spyder project settings
118122
.spyderproject
119123
.spyproject

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ export TLDR_LANGUAGE="es"
8585
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"
88-
export TLDR_DOWNLOAD_CACHE_LOCATION="https://tldr-pages.github.io/assets/tldr.zip"
88+
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
@@ -136,6 +137,10 @@ For networks that sit behind a proxy, it may be necessary to disable SSL verific
136137

137138
will disable SSL certificate inspection. This __should be avoided__ unless absolutely necessary.
138139

140+
Alternatively, It is possible to use a different certificate store/bundle by setting:
141+
142+
* `TLDR_CERT=/path/to/certificates.crt`
143+
139144
### Colors
140145

141146
Values of the `TLDR_COLOR_x` variables may consist of three parts:
@@ -180,3 +185,7 @@ can either use the `--source` flag when using tldr or by specifying the followin
180185
- it can also point to a local directory using `file:///path/to/directory`.
181186
- `TLDR_DOWNLOAD_CACHE_LOCATION` to control where to pull a zip of all pages from.
182187
- defaults to `https://github.com/tldr-pages/tldr/releases/latest/download/tldr.zip`.
188+
189+
### Command options
190+
191+
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@
3333
USE_NETWORK = int(os.environ.get('TLDR_NETWORK_ENABLED', '1')) > 0
3434
USE_CACHE = int(os.environ.get('TLDR_CACHE_ENABLED', '1')) > 0
3535
MAX_CACHE_AGE = int(os.environ.get('TLDR_CACHE_MAX_AGE', 24*7))
36+
CAFILE = None if os.environ.get('TLDR_CERT', None) is None else \
37+
Path(os.environ.get('TLDR_CERT')).expanduser()
3638

3739
URLOPEN_CONTEXT = None
3840
if int(os.environ.get('TLDR_ALLOW_INSECURE', '0')) == 1:
3941
URLOPEN_CONTEXT = ssl.create_default_context()
4042
URLOPEN_CONTEXT.check_hostname = False
4143
URLOPEN_CONTEXT.verify_mode = ssl.CERT_NONE
44+
elif CAFILE:
45+
URLOPEN_CONTEXT = ssl.create_default_context(cafile=CAFILE)
4246

4347
OS_DIRECTORIES = {
4448
"android": "android",
@@ -204,7 +208,7 @@ def get_platform() -> str:
204208

205209

206210
def get_platform_list() -> List[str]:
207-
platforms = ['common'] + list(OS_DIRECTORIES.values())
211+
platforms = ['common'] + list(set(OS_DIRECTORIES.values()))
208212
current_platform = get_platform()
209213
platforms.remove(current_platform)
210214
platforms.insert(0, current_platform)
@@ -414,7 +418,7 @@ def colors_of(key: str) -> Tuple[str, str, List[str]]:
414418
return (color, on_color, attrs)
415419

416420

417-
def output(page: str, plain: bool = False) -> None:
421+
def output(page: str, display_option_length: str, plain: bool = False) -> None:
418422
def emphasise_example(x: str) -> str:
419423
# Use ANSI escapes to enable italics at the start and disable at the end
420424
# Also use the color yellow to differentiate from the default green
@@ -477,6 +481,12 @@ def emphasise_example(x: str) -> str:
477481
line = line.replace(r'\{\{', '__ESCAPED_OPEN__')
478482
line = line.replace(r'\}\}', '__ESCAPED_CLOSE__')
479483

484+
# Extract long or short options from placeholders
485+
if display_option_length == "short":
486+
line = re.sub(r'{{\[([^|]+)\|[^|]+?\]}}', r'\1', line)
487+
elif display_option_length == "long":
488+
line = re.sub(r'{{\[[^|]+\|([^|]+?)\]}}', r'\1', line)
489+
480490
elements = [' ' * 2 * LEADING_SPACES_NUM]
481491
for item in COMMAND_SPLIT_REGEX.split(line):
482492
item, replaced = PARAM_REGEX.subn(
@@ -602,6 +612,16 @@ def create_parser() -> ArgumentParser:
602612
action='store_true',
603613
help='Just print the plain page file.')
604614

615+
parser.add_argument('--short-options',
616+
default=False,
617+
action="store_true",
618+
help='Display shortform options over longform')
619+
620+
parser.add_argument('--long-options',
621+
default=False,
622+
action="store_true",
623+
help='Display longform options over shortform')
624+
605625
parser.add_argument(
606626
'command', type=str, nargs='*', help="command to lookup", metavar='command'
607627
).complete = {"bash": "shtab_tldr_cmd_list", "zsh": "shtab_tldr_cmd_list"}
@@ -623,9 +643,25 @@ def main() -> None:
623643

624644
options = parser.parse_args()
625645

646+
display_option_length = "long"
647+
if not (options.short_options or options.long_options):
648+
if os.environ.get('TLDR_OPTIONS') == "short":
649+
display_option_length = "short"
650+
elif os.environ.get('TLDR_OPTIONS') == "long":
651+
display_option_length = "long"
652+
elif os.environ.get('TLDR_OPTIONS') == "both":
653+
display_option_length = "both"
654+
if options.short_options:
655+
display_option_length = "short"
656+
if options.long_options:
657+
display_option_length = "long"
658+
if options.short_options and options.long_options:
659+
display_option_length = "both"
660+
626661
if sys.platform == "win32":
627662
import colorama
628663
colorama.init(strip=options.color)
664+
629665
if options.color is False:
630666
os.environ["FORCE_COLOR"] = "true"
631667

@@ -643,6 +679,7 @@ def main() -> None:
643679
if file_path.exists():
644680
with file_path.open(encoding='utf-8') as open_file:
645681
output(open_file.read().encode('utf-8').splitlines(),
682+
display_option_length,
646683
plain=options.markdown)
647684
elif options.search:
648685
search_term = options.search.lower()
@@ -676,7 +713,7 @@ def main() -> None:
676713
" send a pull request to: https://github.com/tldr-pages/tldr"
677714
).format(cmd=command))
678715
else:
679-
output(results[0][0], plain=options.markdown)
716+
output(results[0][0], display_option_length, plain=options.markdown)
680717
if results[1:]:
681718
platforms_str = [result[1] for result in results[1:]]
682719
are_multiple_platforms = len(platforms_str) > 1

0 commit comments

Comments
 (0)