Skip to content

Commit 1869338

Browse files
committed
Limited support for languages, missing index.json for other languages.
Some bugsquashing, updated screenshots where mandated.
1 parent 4562dd7 commit 1869338

6 files changed

+48
-39
lines changed

README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# tldr-bash-client
22

3-
* version 0.43
3+
* version 0.44
44

55
### Bash client for tldr: community driven man-by-example
66
**A fully-functional [bash](https://tiswww.case.edu/php/chet/bash/bashtop.html)
@@ -28,7 +28,7 @@ If the location is not in $PATH, you need to specify the path to run it.
2828
### Prerequisites
2929
coreutils, grep, unzip, curl / wget, less (optional)
3030

31-
<img alt="tldr find screenshot" src="tldr-find.jpg" title="tldr find" width="600" />
31+
<img alt="tldr search screenshot" src="tldr-search.jpg" title="tldr search" width="600" />
3232

3333
## Output customisation
3434
<img alt="tldr customize screenshot" src="tldr-customize.jpg" title="tldr customize" width="600" />
@@ -64,13 +64,15 @@ Color/BG (Newline and Space also allowed) for error and info messages
6464
* TLDR_INFO_COLOR (defaults to: Newline Space Green)
6565

6666
How many days before freshly downloading a potentially stale page
67-
* TLDR_EXPIRY (defaults to: 60)
68-
67+
* TLDR_EXPIRY (defaults to 7)
6968
Alternative location of pages cache
7069
* TLDR_CACHE (not set by default)
71-
72-
Instead of `less`, use `cat` for output (automatic if less not available)
73-
* TLDR_LESS (not set by default; if set to *0* `cat` will be used)
70+
Usage of 'less' or 'cat' for output (set to '0' for cat)
71+
* TLDR_LESS (*1* by default; if set to *0* `cat` will be used)
72+
Force current OS
73+
* TLDR_OS (overrides what is read by `uname -s`)
74+
Force preferred language: ISO639 format (2 lowercase letters)
75+
* TLDR_LANG (not set by default, $LANG is used from environment)
7476

7577
<img alt="tldr list screenshot" src="tldr-list.jpg" title="tldr list" width="600" />
7678

tldr

+39-32
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/usr/bin/env bash
22
set +vx -o pipefail
33
[[ $- = *i* ]] && echo "Don't source this script!" && return 1
4-
version='0.43'
5-
# tldr-bash-client version 0.43
4+
version='0.44'
5+
# tldr-bash-client version 0.44
66
# Bash client for tldr: community driven man-by-example
77
# - forked from Ray Lee, https://github.com/raylee/tldr
88
# - modified and expanded by pepa65: https://gitlab.com/pepa65/tldr-bash-client
@@ -36,16 +36,15 @@ version='0.43'
3636
: ${TLDR_INFO_COLOR:="Newline Space Green"}
3737

3838
# How many days before freshly downloading a potentially stale page
39-
: ${TLDR_EXPIRY:=60}
40-
39+
: ${TLDR_EXPIRY:=7}
4140
# Alternative location of pages cache
4241
: ${TLDR_CACHE_LOCATION:=""}
43-
4442
# Usage of 'less' or 'cat' for output (set to '0' for cat)
4543
: ${TLDR_LESS:=1}
46-
4744
# Force current OS
4845
: ${TLDR_OS:=""}
46+
# Force preferred language: ISO639 format (2 lowercase letters)
47+
: ${TLDR_LANG:=""}
4948

5049
## Function definitions
5150

@@ -72,7 +71,7 @@ Usage(){
7271
7372
${HDE}Element styling:$XHDE ${T}Title$XT ${D}Description$XD ${E}Example$XE ${C}Code$XC ${V}Value$XV
7473
${HDE}All pages and the index are cached locally under $HUR$cachedir$XHUR.
75-
${HDE}By default, the cached copies will be freshly downloaded after $HUR$TLDR_EXPIRY$XHUR days.
74+
${HDE}Cached pages will be freshly downloaded after $HUR$TLDR_EXPIRY$XHUR days. Preferred language: $lang.
7675
EOF
7776
)"
7877
exit "${1:-0}"
@@ -183,10 +182,10 @@ Init_term(){
183182
}
184183

185184
# $1: page
186-
Recent(){ find "$1" -mtime -"$TLDR_EXPIRY" >/dev/null 2>&1;}
185+
Recent(){ [[ $(find "$1" -mtime -"$TLDR_EXPIRY") ]];}
187186

188187
# Initialize globals, check the environment; Uses: config cachedir version
189-
# Sets: stdout os version dl
188+
# Sets: stdout os version dl pages
190189
Config(){
191190
# Don't use less if no stdout or less not available
192191
[[ ! -t 1 ]] && ! type -P less >/dev/null && TLDR_LESS=0
@@ -201,8 +200,10 @@ Config(){
201200
CYGWIN*|MINGW*) os='windows' ;;
202201
*) os=
203202
esac
203+
lang=${TLDR_LANG:-$LANG} lang=${lang:0:2} lang=${lang,,}
204+
[[ $lang = en ]] && pages=pages || pages=pages.$lang
204205
Init_term
205-
[[ $TLDR_LESS = 0 ]] &&
206+
[[ $TLDR_LESS = 0 ]] &&
206207
trap 'cat <<<"$stdout"' EXIT ||
207208
trap 'less -~RXQFP"Browse up/down, press Q to exit " <<<"$stdout"' EXIT
208209

@@ -214,7 +215,7 @@ Config(){
214215
Err "tldr requires ${I}curl$XI or ${I}wget$XI installed in your path" &&
215216
exit 3
216217

217-
pages_url='https://raw.githubusercontent.com/tldr-pages/tldr/master/pages'
218+
repo_url='https://raw.githubusercontent.com/tldr-pages/tldr/master'
218219
zip_url='http://tldr.sh/assets/tldr.zip'
219220

220221
cachedir=$TLDR_CACHE_LOCATION
@@ -227,10 +228,11 @@ Config(){
227228
! mkdir -p "$cachedir" &&
228229
Err "Can't create the pages cache location $cachedir" &&
229230
exit 4
230-
index=$cachedir/index.json
231+
# Indexes for every language should be available, $pages instead of pages
232+
index=$cachedir/pages/index.json
231233
# update if the file doesn't exists, or if it's older than $TLDR_EXPIRY
232234
[[ -f $index ]] && Recent "$index" || Cache_fill
233-
platforms=$(cd "$cachedir"; ls -d -- */ |tr -d /)
235+
platforms=$(cd "$cachedir/pages"; ls -d -- */ |tr -d /)
234236
platforms=${platforms//$'\n'/,}
235237
}
236238

@@ -240,7 +242,7 @@ Unlinted(){
240242
exit 5
241243
}
242244

243-
# $1: page; Uses: index cachedir pages_url platform os dl cached md
245+
# $1: page; Uses: index cachedir repo_url platform os dl cached md
244246
# Sets: cached md
245247
Get_tldr(){
246248
local desc err=0 notfound
@@ -263,8 +265,7 @@ Get_tldr(){
263265
fi
264266
else # no platform specified: check common
265267
if [[ $desc =~ \"common\" ]]
266-
then
267-
md=common/$1.md
268+
then md=common/$1.md
268269
else # not in common either
269270
[[ $notfound ]] && notfound+=" or "
270271
notfound+=${I}common$XI
@@ -282,12 +283,14 @@ Get_tldr(){
282283
((err)) && Err "tldr page $I$1$XI not found in $notfound, from platform $U${md%/*}$XU instead"
283284

284285
# return local cached copy of tldr-page, or retrieve and cache from github
285-
cached=$cachedir/$md
286+
cached=$cachedir/$pages/$md
287+
[[ $cached ]] || cached=$cachedir/pages/$md
286288
if ! Recent "$cached"
287289
then
288290
mkdir -p "${cached%/*}"
289-
$dl "$cached" "$pages_url/$md" ||
290-
Err "Could not download page $I$cached$XI with $dl"
291+
$dl "$cached" "$repo_url/$pages/$md" &&
292+
Inf "Downloaded page '$pages/$md'" ||
293+
Err "Could not download $repo_url/$pages/$md"
291294
fi
292295
}
293296

@@ -364,20 +367,20 @@ List_pages(){
364367
pregex=^ ptext="${I}all$XI platforms"
365368
if [[ $platform = current ]]
366369
then [[ $os ]] &&
367-
pregex="-e $os -e common" ptext="$I$os$XI platform and ${I}common$XI" ||
368-
pregex=common ptext="platform not detected, ${I}common$XI"
369-
Inf "Known tldr pages from $ptext:"
370+
pregex="-e $os -e common" ptext="$I$os$XI platform and ${I}common$XI" ||
371+
pregex=common ptext="platform not detected, ${I}common$XI"
370372
fi
373+
Inf "Known tldr pages from $ptext:"
371374
Out "$(tr '{' '\n' <"$index" |grep $pregex |cut -d "$Q" -f4 |column)"
372375
exit "$1"
373376
}
374377

375378
# $1: regex(es); Uses: cachedir
376379
Find_regex(){
377-
local regex=$* list=$cachedir/*/*.md
380+
local regex=$* list=$cachedir/*/*/*.md
378381
while (($#))
379382
do
380-
list=$(grep $1 $list |cut -d: -f1)
383+
list=$(grep "$1" $list |cut -d: -f1)
381384
shift
382385
done
383386
n=$(wc -l <<<"$list")
@@ -402,16 +405,20 @@ Cache_fill(){
402405
Err "tldr requires ${I}unzip$XI to fill the cache" &&
403406
exit 7
404407
tmp=$(mktemp -d)
405-
! $dl "$tmp/pages.zip" "$zip_url" &&
406-
rm -- "$tmp" &&
407-
Err "Could not download pages archive from $U$zip_url$XU with $dl" &&
408+
if ! $dl "$tmp/pages.zip" "$zip_url"
409+
then
410+
rm -- "$tmp" || Err "Error deleting temporary directory $tmp"
411+
Err "Could not download pages archive from $U$zip_url$XU with $dl"
408412
exit 8
409-
! $unzip "$tmp/pages.zip" -d "$tmp" 'pages/**' &&
410-
rm -- "$tmp" &&
411-
Err "Couldn't unzip the cache archive on $tmp/pages.zip" &&
413+
fi
414+
if ! $unzip "$tmp/pages.zip" -d "$tmp"
415+
then
416+
rm -- "$tmp" || Err "Error deleting temporary directory $tmp"
417+
Err "Couldn't unzip the cache archive on $tmp/pages.zip"
412418
exit 9
419+
fi
413420
rm -rf -- "${cachedir:?}/"*
414-
mv -- "$tmp/pages/"* "${cachedir:?}/"
421+
mv -- "$tmp"/pages* "${cachedir:?}/"
415422
rm -rf -- "$tmp"
416423
Inf "Pages cached in $U$cachedir$XU"
417424
updated=1
@@ -424,7 +431,7 @@ Main(){
424431
case "$1" in
425432
-s|--search) [[ -z $2 ]] && Err "Search term(s) [regex] needed" && Usage 10
426433
shift
427-
Find_regex $* ;;
434+
Find_regex "$@" ;;
428435
-l|--list)
429436
if [[ $2 ]]
430437
then

tldr-customize.jpg

6.5 KB
Loading

tldr-find.jpg

-12.7 KB
Binary file not shown.

tldr-search.jpg

22.1 KB
Loading

tldr-usage.jpg

1.16 KB
Loading

0 commit comments

Comments
 (0)