1
1
#! /usr/bin/env bash
2
2
set +vx -o pipefail
3
3
[[ $- = * 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
6
6
# Bash client for tldr: community driven man-by-example
7
7
# - forked from Ray Lee, https://github.com/raylee/tldr
8
8
# - modified and expanded by pepa65: https://gitlab.com/pepa65/tldr-bash-client
@@ -36,16 +36,15 @@ version='0.43'
36
36
: ${TLDR_INFO_COLOR:= " Newline Space Green" }
37
37
38
38
# How many days before freshly downloading a potentially stale page
39
- : ${TLDR_EXPIRY:= 60}
40
-
39
+ : ${TLDR_EXPIRY:= 7}
41
40
# Alternative location of pages cache
42
41
: ${TLDR_CACHE_LOCATION:= " " }
43
-
44
42
# Usage of 'less' or 'cat' for output (set to '0' for cat)
45
43
: ${TLDR_LESS:= 1}
46
-
47
44
# Force current OS
48
45
: ${TLDR_OS:= " " }
46
+ # Force preferred language: ISO639 format (2 lowercase letters)
47
+ : ${TLDR_LANG:= " " }
49
48
50
49
# # Function definitions
51
50
@@ -72,7 +71,7 @@ Usage(){
72
71
73
72
${HDE} Element styling:$XHDE ${T} Title$XT ${D} Description$XD ${E} Example$XE ${C} Code$XC ${V} Value$XV
74
73
${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 .
76
75
EOF
77
76
) "
78
77
exit " ${1:- 0} "
@@ -183,10 +182,10 @@ Init_term(){
183
182
}
184
183
185
184
# $1: page
186
- Recent (){ find " $1 " -mtime -" $TLDR_EXPIRY " > /dev/null 2>&1 ;}
185
+ Recent (){ [[ $( find " $1 " -mtime -" $TLDR_EXPIRY " ) ]] ;}
187
186
188
187
# Initialize globals, check the environment; Uses: config cachedir version
189
- # Sets: stdout os version dl
188
+ # Sets: stdout os version dl pages
190
189
Config (){
191
190
# Don't use less if no stdout or less not available
192
191
[[ ! -t 1 ]] && ! type -P less > /dev/null && TLDR_LESS=0
@@ -201,8 +200,10 @@ Config(){
201
200
CYGWIN* |MINGW* ) os=' windows' ;;
202
201
* ) os=
203
202
esac
203
+ lang=${TLDR_LANG:- $LANG } lang=${lang: 0: 2} lang=${lang,,}
204
+ [[ $lang = en ]] && pages=pages || pages=pages.$lang
204
205
Init_term
205
- [[ $TLDR_LESS = 0 ]] &&
206
+ [[ $TLDR_LESS = 0 ]] &&
206
207
trap ' cat <<<"$stdout"' EXIT ||
207
208
trap ' less -~RXQFP"Browse up/down, press Q to exit " <<<"$stdout"' EXIT
208
209
@@ -214,7 +215,7 @@ Config(){
214
215
Err " tldr requires ${I} curl$XI or ${I} wget$XI installed in your path" &&
215
216
exit 3
216
217
217
- pages_url =' https://raw.githubusercontent.com/tldr-pages/tldr/master/pages '
218
+ repo_url =' https://raw.githubusercontent.com/tldr-pages/tldr/master'
218
219
zip_url=' http://tldr.sh/assets/tldr.zip'
219
220
220
221
cachedir=$TLDR_CACHE_LOCATION
@@ -227,10 +228,11 @@ Config(){
227
228
! mkdir -p " $cachedir " &&
228
229
Err " Can't create the pages cache location $cachedir " &&
229
230
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
231
233
# update if the file doesn't exists, or if it's older than $TLDR_EXPIRY
232
234
[[ -f $index ]] && Recent " $index " || Cache_fill
233
- platforms=$( cd " $cachedir " ; ls -d -- * / | tr -d /)
235
+ platforms=$( cd " $cachedir /pages " ; ls -d -- * / | tr -d /)
234
236
platforms=${platforms// $' \n ' / ,}
235
237
}
236
238
@@ -240,7 +242,7 @@ Unlinted(){
240
242
exit 5
241
243
}
242
244
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
244
246
# Sets: cached md
245
247
Get_tldr (){
246
248
local desc err=0 notfound
@@ -263,8 +265,7 @@ Get_tldr(){
263
265
fi
264
266
else # no platform specified: check common
265
267
if [[ $desc =~ \" common\" ]]
266
- then
267
- md=common/$1 .md
268
+ then md=common/$1 .md
268
269
else # not in common either
269
270
[[ $notfound ]] && notfound+=" or "
270
271
notfound+=${I} common$XI
@@ -282,12 +283,14 @@ Get_tldr(){
282
283
(( err)) && Err " tldr page $I $1 $XI not found in $notfound , from platform $U ${md%/* } $XU instead"
283
284
284
285
# 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
286
288
if ! Recent " $cached "
287
289
then
288
290
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 "
291
294
fi
292
295
}
293
296
@@ -364,20 +367,20 @@ List_pages(){
364
367
pregex=^ ptext=" ${I} all$XI platforms"
365
368
if [[ $platform = current ]]
366
369
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 "
370
372
fi
373
+ Inf " Known tldr pages from $ptext :"
371
374
Out " $( tr ' {' ' \n' < " $index " | grep $pregex | cut -d " $Q " -f4 | column) "
372
375
exit " $1 "
373
376
}
374
377
375
378
# $1: regex(es); Uses: cachedir
376
379
Find_regex (){
377
- local regex=$* list=$cachedir /* /* .md
380
+ local regex=$* list=$cachedir /* /* / * .md
378
381
while (( $# ))
379
382
do
380
- list=$( grep $1 $list | cut -d: -f1)
383
+ list=$( grep " $1 " $list | cut -d: -f1)
381
384
shift
382
385
done
383
386
n=$( wc -l <<< " $list" )
@@ -402,16 +405,20 @@ Cache_fill(){
402
405
Err " tldr requires ${I} unzip$XI to fill the cache" &&
403
406
exit 7
404
407
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 "
408
412
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"
412
418
exit 9
419
+ fi
413
420
rm -rf -- " ${cachedir:? } /" *
414
- mv -- " $tmp /pages/ " * " ${cachedir:? } /"
421
+ mv -- " $tmp " /pages* " ${cachedir:? } /"
415
422
rm -rf -- " $tmp "
416
423
Inf " Pages cached in $U$cachedir$XU "
417
424
updated=1
@@ -424,7 +431,7 @@ Main(){
424
431
case " $1 " in
425
432
-s|--search) [[ -z $2 ]] && Err " Search term(s) [regex] needed" && Usage 10
426
433
shift
427
- Find_regex $* ;;
434
+ Find_regex " $@ " ;;
428
435
-l|--list)
429
436
if [[ $2 ]]
430
437
then
0 commit comments