Skip to content

Commit 27582a4

Browse files
committed
2 parents 8ce2b67 + 2e17a43 commit 27582a4

File tree

4 files changed

+258
-75
lines changed

4 files changed

+258
-75
lines changed

README.md

+24-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
# tldr-bash-client
22

3-
* version 0.3
4-
* https://github.com/pepa65/tldr-bash-client
3+
* version 0.33
54

65
### Bash client for tldr: community driven man-by-example
76
**A fully-functional [bash](https://tiswww.case.edu/php/chet/bash/bashtop.html)
87
client for the [tldr](https://github.com/tldr-pages/tldr) project, providing
98
poignant examples of terminal commands.**
109

11-
<img alt="tldr list screenshot" src="tldr-list.jpg" title="tldr list" width="600" />
10+
<img alt="tldr page gif" src="tldr-page.gif" title="tldr page" />
1211

1312
This client can render both the old and the new tldr markup format.
1413

@@ -25,8 +24,8 @@ If the location is not in $PATH, you need to specify the path to run it.
2524
Alternately, you can do `sudo bpkg-install pepa65/tldr` if you have
2625
[bpkg](https://github.com/bpkg/bpkg) installed.
2726

28-
<img alt="tldr page gif" src="tldr-page.gif" title="tldr page" />
29-
[](<img alt="tldr page screenshot" src="tldr-page.jpg" title="tldr page" width="600" />)
27+
<img alt="tldr page screenshot" src="tldr-page.jpg" title="tldr page" width="600" />
28+
<img alt="tldr list screenshot" src="tldr-list.jpg" title="tldr list" width="600" />
3029

3130
### Prerequisites
3231
coreutils, less, grep, unzip, curl / wget
@@ -73,6 +72,24 @@ Alternative location of pages cache
7372

7473
<img alt="tldr customize screenshot" src="tldr-customize.jpg" title="tldr customize" width="600" />
7574

75+
# tldr-lint
76+
77+
* version 0.11
78+
79+
### Linter for new syntax tldr source files
80+
81+
```
82+
Usage: tldr-lint [-h|--help] [-V|--version] [-q|--quiet] [<dir>] [<file>]
83+
- All *.md files under <dir> and subdirectories are checked
84+
- <file> is checked regardless of extension
85+
-q, --quiet: No output means check is OK
86+
-V, --version: Display version
87+
-h, --help: Display this help text
88+
```
89+
90+
### Prerequisites
91+
coreutils, sed, grep, find
92+
7693
## Contributing
7794

7895
Please file an issue for a question, a bug or a feature request.
@@ -84,8 +101,8 @@ Or even better, send a pull request!
84101

85102
### License
86103

87-
Original client by Ray Lee http://github.com/raylee/tldr (MIT license)
104+
Original tldr client in bash by Ray Lee http://github.com/raylee/tldr (MIT license)
88105

89-
Relicensed under GPLv3+
106+
The tldr-bash-client is relicensed under GPLv3+ and tldr-lint is GPLv3+ as well.
90107

91108
<img alt="tldr new markdown screenshot" src="tldr-markdown-new.jpg" title="tldr new markdown" width="600" />

tldr

+73-68
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#!/bin/bash
22
set +vx -o pipefail
33
[[ $- = *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
66
# Bash client for tldr: community driven man-by-example
77
# - forked from Ray Lee, http://github.com/raylee/tldr
88
# - 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;}
176176
# Download index.json; Uses: index index_url base_url zip_url dl
177177
Update_index(){
178178
$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"
180180
exit 2
181181
}
182182
}
@@ -196,8 +196,8 @@ Config(){
196196
version="tldr-bash-client version $version $XB$URL http://github.com/pepa65/tldr-bash-client$XURL"
197197

198198
# 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" || {
201201
Err "tldr requires$I curl$XI or$I wget $XI installed in your path"
202202
exit 3
203203
}
@@ -221,10 +221,10 @@ Config(){
221221
[[ -f $index ]] && Recent "$index" || Update_index
222222
}
223223

224-
# $1: error message; Uses: md line ln
224+
# $1: error message; Uses: md REPLY ln
225225
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
228228
}
229229

230230
# $1: page; Uses: index index_url cachedir base_url platform os dl cached md
@@ -238,10 +238,10 @@ Get_tldr(){
238238

239239
[[ $desc ]] || return # just not found
240240

241-
err=0
241+
error=0
242242
if [[ $platform ]]
243243
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
245245
else # check common
246246
[[ $desc =~ \"common\" ]] && md=common/$1.md || { # not in common either
247247
[[ $notfound ]] && notfound+=" or "
@@ -253,78 +253,80 @@ Get_tldr(){
253253
[[ $desc =~ \"$os\" ]] && md=$os/$1.md
254254
} || {
255255
notfound+=" or $I$os$XI"
256-
err=1
256+
error=1
257257
}
258258
# if still no page found, get the first entry in index
259259
[[ $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"
261261

262262
# return the local cached copy of the tldrpage, or retrieve and cache from github
263263
cached=$cachedir/$md
264264
Recent "$cached" || {
265265
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"
267267
}
268268
}
269269

270-
# $1: text; Uses: page stdout; Sets: ln line
270+
# $1: text; Uses: page stdout; Sets: ln REPLY
271271
Display_tldr(){
272272
local newfmt len val
273-
ln=0 line=''
273+
ln=0 REPLY=''
274274
[[ $md ]] || md=$1
275275
# Read full lines, and process even when no newline at the end
276-
while read -r line || [[ $line ]]
276+
while read -r || [[ $REPLY ]]
277277
do
278278
((++ln))
279279
((ln==1)) && {
280-
[[ ${line:0:1} = '#' ]] && newfmt=0 || newfmt=1
280+
[[ ${REPLY:0:1} = '#' ]] && newfmt=0 || newfmt=1
281281
((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))
289291
}
290292
}
291-
case "${line:0:1}" in # first character
293+
case "${REPLY:0:1}" in # first character
292294
'#') ((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"
300302
DNL='' ;;
301303
'-') ((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" ;;
305307
' ') ((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}
309311
# Value: convert {{value}}
310312
val=${val//\{\{/$CX$V}
311313
val=${val//\}\}/$XV$C}
312314
Out "$CNL$CSP$C$val$XC" ;;
313315
'`') ((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}
317319
# Value: convert {{value}}
318320
val=${val//\{\{/$CX$V}
319321
val=${val//\}\}/$XV$C}
320322
Out "$CNL$CSP$C$val$XC" ;;
321323
'') continue ;;
322324
*) ((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" ;;
325327
esac
326328
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
328330
}
329331

330332
# $1: exit code; Uses: platform index
@@ -340,18 +342,21 @@ List_pages(){
340342
# $1: exit code; Uses: dl cachedir zip_url
341343
Cache_fill(){
342344
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"
347347
exit 6
348348
}
349-
unzip="$(type -p unzip) -q" || {
349+
tmp=$(mktemp -d)
350+
$dl "$tmp/pages.zip" "$zip_url" || {
350351
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"
352353
exit 7
353354
}
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+
}
355360
rm -rf -- "${cachedir:?}/"*
356361
mv -- "$tmp/pages/"* "${cachedir:?}/"
357362
rm -rf -- "$tmp"
@@ -361,56 +366,56 @@ Cache_fill(){
361366

362367
# $@: commandline parameters; Uses: version cached; Sets: platform page
363368
Main(){
364-
local markdown err
369+
local markdown err nomore='No more command line arguments allowed'
365370
Config
366371
markdown=0 err=0
367372
case "$1" in
368373
-l|--list) [[ $2 ]] && {
369374
platform=$2
370375
[[ ,common,linux,osx,sunos, = *,$platform,* ]] || {
371376
Err "Unknown platform $I$platform$XI"
372-
Usage 8
377+
Usage 9
373378
}
374-
[[ $3 ]] && Err "No more command line arguments allowed" && err=9
379+
[[ $3 ]] && Err "$nomore" && err=10
375380
}
376381
List_pages "$err" ;;
377-
-c|--cache) [[ $2 ]] && Err "No more command line arguments allowed" && err=10
382+
-c|--cache) [[ $2 ]] && Err "$nomore" && err=11
378383
Cache_fill "$err" ;;
379-
-v|--version) [[ $2 ]] && Err "No more command line arguments allowed" && err=11
384+
-v|--version) [[ $2 ]] && Err "$nomore" && err=12
380385
Inf "$version"
381386
exit "$err" ;;
382-
-u|--update) [[ $2 ]] && Err "No more command line arguments allowed" && err=12
387+
-u|--update) [[ $2 ]] && Err "$nomore" && err=13
383388
Update_index
384389
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
387392
[[ -f "$2" ]] && {
388393
Display_tldr "$2" && exit "$err"
389394
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 ;;
392397
-m|--markdown) shift
393398
page=$*
394-
[[ -z $page ]] && Err "Specify a page to display" && Usage 17
399+
[[ -z $page ]] && Err "Specify a page to display" && Usage 18
395400
[[ -f "$page" && ${page: -3:3} = .md ]] && Out "$(cat "$page")" && exit 0
396401
markdown=1 ;;
397-
''|-h|-\?|--help) [[ $2 ]] && Err "No more command line arguments allowed" && err=18
402+
''|-h|-\?|--help) [[ $2 ]] && Err "$nomore" && err=19
398403
Usage "$err" ;;
399-
-*) Err "Unrecognized option $I$1$XI"; Usage 19 ;;
404+
-*) Err "Unrecognized option $I$1$XI"; Usage 20 ;;
400405
*) page=$* ;;
401406
esac
402407

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
405410
[[ $page = */* ]] && platform=${page%/*} && page=${page##*/}
406411
[[ $platform && ,common,linux,osx,sunos, != *,$platform,* ]] && {
407412
Err "Unknown platform $I$platform$XI"
408-
Usage 22
413+
Usage 23
409414
}
410415

411416
Get_tldr "${page// /-}"
412417
[[ ! -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
414419
((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached"
415420
}
416421

0 commit comments

Comments
 (0)