1
1
#! /bin/bash
2
2
set +vx -o pipefail
3
3
[[ $- = * i* ]] && echo " Don't source this script!" && return 1
4
- version=' 0.3 '
5
- # tldr-bash-client version 0.3
4
+ version=' 0.33 '
5
+ # tldr-bash-client version 0.33
6
6
# Bash client for tldr: community driven man-by-example
7
7
# - forked from Ray Lee, http://github.com/raylee/tldr
8
8
# - modified and expanded by pepa65: http://github.com/pepa65/tldr-bash-client
@@ -176,7 +176,7 @@ Recent(){ find "$1" -mtime -"${TLDR_EXPIRY// /}" >/dev/null 2>&1;}
176
176
# Download index.json; Uses: index index_url base_url zip_url dl
177
177
Update_index (){
178
178
$dl " $index " " $index_url " && Inf " Index file $I$index$XI freshly downloaded" || {
179
- Err " Could not download index from $I$index_url$XI "
179
+ Err " Could not download index from $I$index_url$XI with $dl "
180
180
exit 2
181
181
}
182
182
}
@@ -196,8 +196,8 @@ Config(){
196
196
version=" tldr-bash-client version $version $XB$URL http://github.com/pepa65/tldr-bash-client$XURL "
197
197
198
198
# Select download method
199
- dl=" $( type -p curl) -sfo " || {
200
- dl=" $( type -p wget) -qNO" || {
199
+ dl=" $( type -p curl) -sLfo " || {
200
+ dl=" $( type -p wget) --max-redirect=20 - qNO" || {
201
201
Err " tldr requires$I curl$XI or$I wget $XI installed in your path"
202
202
exit 3
203
203
}
@@ -221,10 +221,10 @@ Config(){
221
221
[[ -f $index ]] && Recent " $index " || Update_index
222
222
}
223
223
224
- # $1: error message; Uses: md line ln
224
+ # $1: error message; Uses: md REPLY ln
225
225
Unlinted (){
226
- Err " Page $I$md$XI not properly linted!$N ${ERRSP}${ERR} Line $I$ln$XI [$XERR$U$line $XU$ERR ]$N$ERRSP$ERR $1 "
227
- exit 4
226
+ Err " Page $I$md$XI not properly linted!$N ${ERRSP}${ERR} Line $I$ln$XI [$XERR$U$REPLY $XU$ERR ]$N$ERRSP$ERR $1 "
227
+ exit 5
228
228
}
229
229
230
230
# $1: page; Uses: index index_url cachedir base_url platform os dl cached md
@@ -238,10 +238,10 @@ Get_tldr(){
238
238
239
239
[[ $desc ]] || return # just not found
240
240
241
- err =0
241
+ error =0
242
242
if [[ $platform ]]
243
243
then # platform given on commandline
244
- [[ ! $desc =~ \" $platform \" ]] && notfound=$I$platform$XI && err =1 || md=$platform /$1 .md
244
+ [[ ! $desc =~ \" $platform \" ]] && notfound=$I$platform$XI && error =1 || md=$platform /$1 .md
245
245
else # check common
246
246
[[ $desc =~ \" common\" ]] && md=common/$1 .md || { # not in common either
247
247
[[ $notfound ]] && notfound+=" or "
@@ -253,78 +253,80 @@ Get_tldr(){
253
253
[[ $desc =~ \" $os \" ]] && md=$os /$1 .md
254
254
} || {
255
255
notfound+=" or $I$os$XI "
256
- err =1
256
+ error =1
257
257
}
258
258
# if still no page found, get the first entry in index
259
259
[[ $md ]] || md=$( cut -d " $Q " -f 8 <<< " $desc" ) /" $1 .md"
260
- (( err )) && Err " tldr page $I $1 $XI not found in $notfound , from platform $U ${md%/* } $XU instead"
260
+ (( error )) && Err " tldr page $I $1 $XI not found in $notfound , from platform $U ${md%/* } $XU instead"
261
261
262
262
# return the local cached copy of the tldrpage, or retrieve and cache from github
263
263
cached=$cachedir /$md
264
264
Recent " $cached " || {
265
265
mkdir -p " ${cached%/* } "
266
- $dl " $cached " " $base_url /$md " || Err " Could not download page $I$cached$XI from index $U$index_url$XU "
266
+ $dl " $cached " " $base_url /$md " || Err " Could not download page $I$cached$XI from index $U$index_url$XU with $dl "
267
267
}
268
268
}
269
269
270
- # $1: text; Uses: page stdout; Sets: ln line
270
+ # $1: text; Uses: page stdout; Sets: ln REPLY
271
271
Display_tldr (){
272
272
local newfmt len val
273
- ln=0 line =' '
273
+ ln=0 REPLY =' '
274
274
[[ $md ]] || md=$1
275
275
# Read full lines, and process even when no newline at the end
276
- while read -r line || [[ $line ]]
276
+ while read -r || [[ $REPLY ]]
277
277
do
278
278
(( ++ ln))
279
279
(( ln== 1 )) && {
280
- [[ ${line : 0: 1} = ' #' ]] && newfmt=0 || newfmt=1
280
+ [[ ${REPLY : 0: 1} = ' #' ]] && newfmt=0 || newfmt=1
281
281
(( newfmt)) && {
282
- [[ $line ]] || Unlinted " No title"
283
- Out " $TNL$TSP$T$line$XT "
284
- len=${# line}
285
- read -r; (( ++ ln))
286
- [[ $line =~ [^= ] ]] && Unlinted " Title underline must be equal signs"
287
- (( len!= ${# line} )) && Unlinted " Underline length not equal to title's"
288
- read -r; (( ++ ln))
282
+ [[ $REPLY ]] || Unlinted " Empty title"
283
+ Out " $TNL$TSP$T$REPLY$XT "
284
+ len=${# REPLY} # title length
285
+ read -r
286
+ (( ++ ln))
287
+ [[ $REPLY =~ [^= ] ]] && Unlinted " Title underline must be equal signs"
288
+ (( len!= ${# REPLY} )) && Unlinted " Underline length not equal to title's"
289
+ read -r
290
+ (( ++ ln))
289
291
}
290
292
}
291
- case " ${line : 0: 1} " in # first character
293
+ case " ${REPLY : 0: 1} " in # first character
292
294
' #' ) (( newfmt)) && Unlinted " Bad first character"
293
- (( ${# line } <= 2 )) && Unlinted " No title"
294
- [[ ! ${line : 1: 1} = ' ' ]] && Unlinted " 2nd character no space"
295
- Out " $TNL$TSP$T ${line : 2} $XT " ;;
296
- ' >' ) (( ${# line } <= 3 )) && Unlinted " No valid desciption"
297
- [[ ! ${line : 1: 1} = ' ' ]] && Unlinted " 2nd character no space"
298
- [[ ! ${line : -1} = ' .' ]] && Unlinted " Description doesn't end in full stop"
299
- Out " $DNL$DSP$D ${line : 2} $XD "
295
+ (( ${# REPLY } <= 2 )) && Unlinted " No title"
296
+ [[ ! ${REPLY : 1: 1} = ' ' ]] && Unlinted " 2nd character no space"
297
+ Out " $TNL$TSP$T ${REPLY : 2} $XT " ;;
298
+ ' >' ) (( ${# REPLY } <= 3 )) && Unlinted " No valid desciption"
299
+ [[ ! ${REPLY : 1: 1} = ' ' ]] && Unlinted " 2nd character no space"
300
+ [[ ! ${REPLY : -1} = ' .' ]] && Unlinted " Description doesn't end in full stop"
301
+ Out " $DNL$DSP$D ${REPLY : 2} $XD "
300
302
DNL=' ' ;;
301
303
' -' ) (( newfmt)) && Unlinted " Bad first character"
302
- (( ${# line } <= 2 )) && Unlinted " No example content"
303
- [[ ! ${line : 1: 1} = ' ' ]] && Unlinted " 2nd character no space"
304
- Out " $ENL$ESP$E ${line : 2} $XE " ;;
304
+ (( ${# REPLY } <= 2 )) && Unlinted " No example content"
305
+ [[ ! ${REPLY : 1: 1} = ' ' ]] && Unlinted " 2nd character no space"
306
+ Out " $ENL$ESP$E ${REPLY : 2} $XE " ;;
305
307
' ' ) (( newfmt)) || Unlinted " Bad first character"
306
- (( ${# line } <= 4 )) && Unlinted " No valid code content"
307
- [[ ${line : 0: 4} = ' ' ]] || Unlinted " No four spaces before code"
308
- val=${line : 4}
308
+ (( ${# REPLY } <= 4 )) && Unlinted " No valid code content"
309
+ [[ ${REPLY : 0: 4} = ' ' ]] || Unlinted " No four spaces before code"
310
+ val=${REPLY : 4}
309
311
# Value: convert {{value}}
310
312
val=${val// \{\{ / $CX$V }
311
313
val=${val// \}\} / $XV$C }
312
314
Out " $CNL$CSP$C$val$XC " ;;
313
315
' `' ) (( newfmt)) && Unlinted " Bad first character"
314
- (( ${# line } <= 2 )) && Unlinted " No valid code content"
315
- [[ ! ${line : -1} = ' `' ]] && Unlinted " Code doesn't end in backtick"
316
- val=${line : 1:- 1}
316
+ (( ${# REPLY } <= 2 )) && Unlinted " No valid code content"
317
+ [[ ! ${REPLY : -1} = ' `' ]] && Unlinted " Code doesn't end in backtick"
318
+ val=${REPLY : 1:- 1}
317
319
# Value: convert {{value}}
318
320
val=${val// \{\{ / $CX$V }
319
321
val=${val// \}\} / $XV$C }
320
322
Out " $CNL$CSP$C$val$XC " ;;
321
323
' ' ) continue ;;
322
324
* ) (( newfmt)) || Unlinted " Bad first character"
323
- [[ -z $line ]] && Unlinted " No example content"
324
- Out " $ENL$EPS $E$line $XE " ;;
325
+ [[ -z $REPLY ]] && Unlinted " No example content"
326
+ Out " $ENL$ESP $E$REPLY $XE " ;;
325
327
esac
326
328
done < " $1 "
327
- trap ' less + Gg -~RXQFP"%pB\% tldr $I$page$XI - browse up/down, press Q to exit" <<<"$stdout"' EXIT
329
+ trap ' less - Gg -~RXQFP"%pB\% tldr $I$page$XI - browse up/down, press Q to exit" <<<"$stdout"' EXIT
328
330
}
329
331
330
332
# $1: exit code; Uses: platform index
@@ -340,18 +342,21 @@ List_pages(){
340
342
# $1: exit code; Uses: dl cachedir zip_url
341
343
Cache_fill (){
342
344
local tmp unzip
343
- tmp=$( mktemp -d)
344
- $dl " $tmp /pages.zip" " $zip_url " || {
345
- rm -- " $tmp "
346
- Err " Could not download pages archive from $U$zip_url$XU "
345
+ unzip=" $( type -p unzip) -q" || {
346
+ Err " Unzip is necessary to fill the cache"
347
347
exit 6
348
348
}
349
- unzip=" $( type -p unzip) -q" || {
349
+ tmp=$( mktemp -d)
350
+ $dl " $tmp /pages.zip" " $zip_url " || {
350
351
rm -- " $tmp "
351
- Err " Unzip is necessary to fill the cache "
352
+ Err " Could not download pages archive from $U$zip_url$XU with $dl "
352
353
exit 7
353
354
}
354
- $unzip " $tmp /pages.zip" -d " $tmp " ' pages/*'
355
+ $unzip " $tmp /pages.zip" -d " $tmp " ' pages/*' || {
356
+ rm -- " $tmp "
357
+ Err " Couldn't unzip the cache archive on $tmp /pages.zip"
358
+ exit 8
359
+ }
355
360
rm -rf -- " ${cachedir:? } /" *
356
361
mv -- " $tmp /pages/" * " ${cachedir:? } /"
357
362
rm -rf -- " $tmp "
@@ -361,56 +366,56 @@ Cache_fill(){
361
366
362
367
# $@: commandline parameters; Uses: version cached; Sets: platform page
363
368
Main (){
364
- local markdown err
369
+ local markdown err nomore= ' No more command line arguments allowed '
365
370
Config
366
371
markdown=0 err=0
367
372
case " $1 " in
368
373
-l|--list) [[ $2 ]] && {
369
374
platform=$2
370
375
[[ ,common,linux,osx,sunos, = * ,$platform ,* ]] || {
371
376
Err " Unknown platform $I$platform$XI "
372
- Usage 8
377
+ Usage 9
373
378
}
374
- [[ $3 ]] && Err " No more command line arguments allowed " && err=9
379
+ [[ $3 ]] && Err " $nomore " && err=10
375
380
}
376
381
List_pages " $err " ;;
377
- -c|--cache) [[ $2 ]] && Err " No more command line arguments allowed " && err=10
382
+ -c|--cache) [[ $2 ]] && Err " $nomore " && err=11
378
383
Cache_fill " $err " ;;
379
- -v|--version) [[ $2 ]] && Err " No more command line arguments allowed " && err=11
384
+ -v|--version) [[ $2 ]] && Err " $nomore " && err=12
380
385
Inf " $version "
381
386
exit " $err " ;;
382
- -u|--update) [[ $2 ]] && Err " No more command line arguments allowed " && err=12
387
+ -u|--update) [[ $2 ]] && Err " $nomore " && err=13
383
388
Update_index
384
389
exit " $err " ;;
385
- -r|--render) [[ -z $2 ]] && Err " Specify a file to render" && Usage 13
386
- [[ $3 ]] && Err " No more command line arguments allowed " && err=14
390
+ -r|--render) [[ -z $2 ]] && Err " Specify a file to render" && Usage 14
391
+ [[ $3 ]] && Err " $nomore " && err=15
387
392
[[ -f " $2 " ]] && {
388
393
Display_tldr " $2 " && exit " $err "
389
394
Err " A file error occured"
390
- exit 15
391
- } || Err " No file:$I $2 $XI " && exit 16 ;;
395
+ exit 16
396
+ } || Err " No file:$I $2 $XI " && exit 17 ;;
392
397
-m|--markdown) shift
393
398
page=$*
394
- [[ -z $page ]] && Err " Specify a page to display" && Usage 17
399
+ [[ -z $page ]] && Err " Specify a page to display" && Usage 18
395
400
[[ -f " $page " && ${page: -3: 3} = .md ]] && Out " $( cat " $page " ) " && exit 0
396
401
markdown=1 ;;
397
- ' ' |-h|-\? |--help) [[ $2 ]] && Err " No more command line arguments allowed " && err=18
402
+ ' ' |-h|-\? |--help) [[ $2 ]] && Err " $nomore " && err=19
398
403
Usage " $err " ;;
399
- -* ) Err " Unrecognized option $I $1 $XI " ; Usage 19 ;;
404
+ -* ) Err " Unrecognized option $I $1 $XI " ; Usage 20 ;;
400
405
* ) page=$* ;;
401
406
esac
402
407
403
- [[ -z $page ]] && Err " No command specified" && Usage 20
404
- [[ $page =~ ' -' || ${page: 0: 1} = ' -' ]] && Err " Only one option allowed" && Usage 21
408
+ [[ -z $page ]] && Err " No command specified" && Usage 21
409
+ [[ $page =~ ' -' || ${page: 0: 1} = ' -' ]] && Err " Only one option allowed" && Usage 22
405
410
[[ $page = * /* ]] && platform=${page%/* } && page=${page##*/ }
406
411
[[ $platform && ,common,linux,osx,sunos, != * ,$platform ,* ]] && {
407
412
Err " Unknown platform $I$platform$XI "
408
- Usage 22
413
+ Usage 23
409
414
}
410
415
411
416
Get_tldr " ${page// / -} "
412
417
[[ ! -s $cached ]] && Err " tldr page for command $I$page$XI not found" \
413
- && Inf " Contribute new pages at:$XB$URL https://github.com/tldr-pages/tldr$XURL " && exit 23
418
+ && Inf " Contribute new pages at:$XB$URL https://github.com/tldr-pages/tldr$XURL " && exit 24
414
419
(( markdown)) && Out " $( cat " $cached " ) " || Display_tldr " $cached "
415
420
}
416
421
0 commit comments