mirror of
https://github.com/sbrl/bin.git
synced 2018-01-10 21:33:46 +00:00
Update tldr
This commit is contained in:
parent
60d2eafba9
commit
1efe6f913e
1 changed files with 39 additions and 36 deletions
75
tldr
75
tldr
|
@ -1,8 +1,8 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
set +vx -o pipefail
|
set +vx -o pipefail
|
||||||
[[ $- = *i* ]] && echo "Don't source this script!" && return 1
|
[[ $- = *i* ]] && echo "Don't source this script!" && return 1
|
||||||
version='0.3'
|
version='0.32'
|
||||||
# tldr-bash-client version 0.3
|
# tldr-bash-client version 0.32
|
||||||
# Bash client for tldr: community driven man-by-example
|
# Bash client for tldr: community driven man-by-example
|
||||||
# - forked from Ray Lee, http://github.com/raylee/tldr
|
# - forked from Ray Lee, http://github.com/raylee/tldr
|
||||||
# - modified and expanded by pepa65: http://github.com/pepa65/tldr-bash-client
|
# - 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;}
|
||||||
# Download index.json; Uses: index index_url base_url zip_url dl
|
# Download index.json; Uses: index index_url base_url zip_url dl
|
||||||
Update_index(){
|
Update_index(){
|
||||||
$dl "$index" "$index_url" && Inf "Index file $I$index$XI freshly downloaded" || {
|
$dl "$index" "$index_url" && Inf "Index file $I$index$XI freshly downloaded" || {
|
||||||
Err "Could not download index from $I$index_url$XI"
|
Err "Could not download index from $I$index_url$XI with $dl"
|
||||||
exit 2
|
exit 2
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -196,8 +196,8 @@ Config(){
|
||||||
version="tldr-bash-client version $version $XB$URL http://github.com/pepa65/tldr-bash-client$XURL"
|
version="tldr-bash-client version $version $XB$URL http://github.com/pepa65/tldr-bash-client$XURL"
|
||||||
|
|
||||||
# Select download method
|
# Select download method
|
||||||
dl="$(type -p curl) -sfo" || {
|
dl="$(type -p curl) -sLfo" || {
|
||||||
dl="$(type -p wget) -qNO" || {
|
dl="$(type -p wget) --max-redirect=20 -qNO" || {
|
||||||
Err "tldr requires$I curl$XI or$I wget $XI installed in your path"
|
Err "tldr requires$I curl$XI or$I wget $XI installed in your path"
|
||||||
exit 3
|
exit 3
|
||||||
}
|
}
|
||||||
|
@ -224,7 +224,7 @@ Config(){
|
||||||
# $1: error message; Uses: md line ln
|
# $1: error message; Uses: md line ln
|
||||||
Unlinted(){
|
Unlinted(){
|
||||||
Err "Page $I$md$XI not properly linted!$N${ERRSP}${ERR}Line $I$ln$XI [$XERR$U$line$XU$ERR]$N$ERRSP$ERR$1"
|
Err "Page $I$md$XI not properly linted!$N${ERRSP}${ERR}Line $I$ln$XI [$XERR$U$line$XU$ERR]$N$ERRSP$ERR$1"
|
||||||
exit 4
|
exit 5
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: page; Uses: index index_url cachedir base_url platform os dl cached md
|
# $1: page; Uses: index index_url cachedir base_url platform os dl cached md
|
||||||
|
@ -238,10 +238,10 @@ Get_tldr(){
|
||||||
|
|
||||||
[[ $desc ]] || return # just not found
|
[[ $desc ]] || return # just not found
|
||||||
|
|
||||||
err=0
|
error=0
|
||||||
if [[ $platform ]]
|
if [[ $platform ]]
|
||||||
then # platform given on commandline
|
then # platform given on commandline
|
||||||
[[ ! $desc =~ \"$platform\" ]] && notfound=$I$platform$XI && err=1 || md=$platform/$1.md
|
[[ ! $desc =~ \"$platform\" ]] && notfound=$I$platform$XI && error=1 || md=$platform/$1.md
|
||||||
else # check common
|
else # check common
|
||||||
[[ $desc =~ \"common\" ]] && md=common/$1.md || { # not in common either
|
[[ $desc =~ \"common\" ]] && md=common/$1.md || { # not in common either
|
||||||
[[ $notfound ]] && notfound+=" or "
|
[[ $notfound ]] && notfound+=" or "
|
||||||
|
@ -253,17 +253,17 @@ Get_tldr(){
|
||||||
[[ $desc =~ \"$os\" ]] && md=$os/$1.md
|
[[ $desc =~ \"$os\" ]] && md=$os/$1.md
|
||||||
} || {
|
} || {
|
||||||
notfound+=" or $I$os$XI"
|
notfound+=" or $I$os$XI"
|
||||||
err=1
|
error=1
|
||||||
}
|
}
|
||||||
# if still no page found, get the first entry in index
|
# if still no page found, get the first entry in index
|
||||||
[[ $md ]] || md=$(cut -d "$Q" -f 8 <<<"$desc")/"$1.md"
|
[[ $md ]] || md=$(cut -d "$Q" -f 8 <<<"$desc")/"$1.md"
|
||||||
((err)) && Err "tldr page $I$1$XI not found in $notfound, from platform $U${md%/*}$XU instead"
|
((error)) && Err "tldr page $I$1$XI not found in $notfound, from platform $U${md%/*}$XU instead"
|
||||||
|
|
||||||
# return the local cached copy of the tldrpage, or retrieve and cache from github
|
# return the local cached copy of the tldrpage, or retrieve and cache from github
|
||||||
cached=$cachedir/$md
|
cached=$cachedir/$md
|
||||||
Recent "$cached" || {
|
Recent "$cached" || {
|
||||||
mkdir -p "${cached%/*}"
|
mkdir -p "${cached%/*}"
|
||||||
$dl "$cached" "$base_url/$md" || Err "Could not download page $I$cached$XI from index $U$index_url$XU"
|
$dl "$cached" "$base_url/$md" || Err "Could not download page $I$cached$XI from index $U$index_url$XU with $dl"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -324,7 +324,7 @@ Display_tldr(){
|
||||||
Out "$ENL$EPS$E$line$XE" ;;
|
Out "$ENL$EPS$E$line$XE" ;;
|
||||||
esac
|
esac
|
||||||
done <"$1"
|
done <"$1"
|
||||||
trap 'less +Gg -~RXQFP"%pB\% tldr $I$page$XI - browse up/down, press Q to exit" <<<"$stdout"' EXIT
|
trap 'less -Gg -~RXQFP"%pB\% tldr $I$page$XI - browse up/down, press Q to exit" <<<"$stdout"' EXIT
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: exit code; Uses: platform index
|
# $1: exit code; Uses: platform index
|
||||||
|
@ -340,18 +340,21 @@ List_pages(){
|
||||||
# $1: exit code; Uses: dl cachedir zip_url
|
# $1: exit code; Uses: dl cachedir zip_url
|
||||||
Cache_fill(){
|
Cache_fill(){
|
||||||
local tmp unzip
|
local tmp unzip
|
||||||
|
unzip="$(type -p unzip) -q" || {
|
||||||
|
Err "Unzip is necessary to fill the cache"
|
||||||
|
exit 6
|
||||||
|
}
|
||||||
tmp=$(mktemp -d)
|
tmp=$(mktemp -d)
|
||||||
$dl "$tmp/pages.zip" "$zip_url" || {
|
$dl "$tmp/pages.zip" "$zip_url" || {
|
||||||
rm -- "$tmp"
|
rm -- "$tmp"
|
||||||
Err "Could not download pages archive from $U$zip_url$XU"
|
Err "Could not download pages archive from $U$zip_url$XU with $dl"
|
||||||
exit 6
|
|
||||||
}
|
|
||||||
unzip="$(type -p unzip) -q" || {
|
|
||||||
rm -- "$tmp"
|
|
||||||
Err "Unzip is necessary to fill the cache"
|
|
||||||
exit 7
|
exit 7
|
||||||
}
|
}
|
||||||
$unzip "$tmp/pages.zip" -d "$tmp" 'pages/*'
|
$unzip "$tmp/pages.zip" -d "$tmp" 'pages/*' || {
|
||||||
|
rm -- "$tmp"
|
||||||
|
Err "Couldn't unzip the cache archive on $tmp/pages.zip"
|
||||||
|
exit 8
|
||||||
|
}
|
||||||
rm -rf -- "${cachedir:?}/"*
|
rm -rf -- "${cachedir:?}/"*
|
||||||
mv -- "$tmp/pages/"* "${cachedir:?}/"
|
mv -- "$tmp/pages/"* "${cachedir:?}/"
|
||||||
rm -rf -- "$tmp"
|
rm -rf -- "$tmp"
|
||||||
|
@ -361,7 +364,7 @@ Cache_fill(){
|
||||||
|
|
||||||
# $@: commandline parameters; Uses: version cached; Sets: platform page
|
# $@: commandline parameters; Uses: version cached; Sets: platform page
|
||||||
Main(){
|
Main(){
|
||||||
local markdown err
|
local markdown err nomore='No more command line arguments allowed'
|
||||||
Config
|
Config
|
||||||
markdown=0 err=0
|
markdown=0 err=0
|
||||||
case "$1" in
|
case "$1" in
|
||||||
|
@ -369,48 +372,48 @@ Main(){
|
||||||
platform=$2
|
platform=$2
|
||||||
[[ ,common,linux,osx,sunos, = *,$platform,* ]] || {
|
[[ ,common,linux,osx,sunos, = *,$platform,* ]] || {
|
||||||
Err "Unknown platform $I$platform$XI"
|
Err "Unknown platform $I$platform$XI"
|
||||||
Usage 8
|
Usage 9
|
||||||
}
|
}
|
||||||
[[ $3 ]] && Err "No more command line arguments allowed" && err=9
|
[[ $3 ]] && Err "$nomore" && err=10
|
||||||
}
|
}
|
||||||
List_pages "$err" ;;
|
List_pages "$err" ;;
|
||||||
-c|--cache) [[ $2 ]] && Err "No more command line arguments allowed" && err=10
|
-c|--cache) [[ $2 ]] && Err "$nomore" && err=11
|
||||||
Cache_fill "$err" ;;
|
Cache_fill "$err" ;;
|
||||||
-v|--version) [[ $2 ]] && Err "No more command line arguments allowed" && err=11
|
-v|--version) [[ $2 ]] && Err "$nomore" && err=12
|
||||||
Inf "$version"
|
Inf "$version"
|
||||||
exit "$err" ;;
|
exit "$err" ;;
|
||||||
-u|--update) [[ $2 ]] && Err "No more command line arguments allowed" && err=12
|
-u|--update) [[ $2 ]] && Err "$nomore" && err=13
|
||||||
Update_index
|
Update_index
|
||||||
exit "$err" ;;
|
exit "$err" ;;
|
||||||
-r|--render) [[ -z $2 ]] && Err "Specify a file to render" && Usage 13
|
-r|--render) [[ -z $2 ]] && Err "Specify a file to render" && Usage 14
|
||||||
[[ $3 ]] && Err "No more command line arguments allowed" && err=14
|
[[ $3 ]] && Err "$nomore" && err=15
|
||||||
[[ -f "$2" ]] && {
|
[[ -f "$2" ]] && {
|
||||||
Display_tldr "$2" && exit "$err"
|
Display_tldr "$2" && exit "$err"
|
||||||
Err "A file error occured"
|
Err "A file error occured"
|
||||||
exit 15
|
exit 16
|
||||||
} || Err "No file:$I $2$XI" && exit 16 ;;
|
} || Err "No file:$I $2$XI" && exit 17 ;;
|
||||||
-m|--markdown) shift
|
-m|--markdown) shift
|
||||||
page=$*
|
page=$*
|
||||||
[[ -z $page ]] && Err "Specify a page to display" && Usage 17
|
[[ -z $page ]] && Err "Specify a page to display" && Usage 18
|
||||||
[[ -f "$page" && ${page: -3:3} = .md ]] && Out "$(cat "$page")" && exit 0
|
[[ -f "$page" && ${page: -3:3} = .md ]] && Out "$(cat "$page")" && exit 0
|
||||||
markdown=1 ;;
|
markdown=1 ;;
|
||||||
''|-h|-\?|--help) [[ $2 ]] && Err "No more command line arguments allowed" && err=18
|
''|-h|-\?|--help) [[ $2 ]] && Err "$nomore" && err=19
|
||||||
Usage "$err" ;;
|
Usage "$err" ;;
|
||||||
-*) Err "Unrecognized option $I$1$XI"; Usage 19 ;;
|
-*) Err "Unrecognized option $I$1$XI"; Usage 20 ;;
|
||||||
*) page=$* ;;
|
*) page=$* ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
[[ -z $page ]] && Err "No command specified" && Usage 20
|
[[ -z $page ]] && Err "No command specified" && Usage 21
|
||||||
[[ $page =~ ' -' || ${page:0:1} = '-' ]] && Err "Only one option allowed" && Usage 21
|
[[ $page =~ ' -' || ${page:0:1} = '-' ]] && Err "Only one option allowed" && Usage 22
|
||||||
[[ $page = */* ]] && platform=${page%/*} && page=${page##*/}
|
[[ $page = */* ]] && platform=${page%/*} && page=${page##*/}
|
||||||
[[ $platform && ,common,linux,osx,sunos, != *,$platform,* ]] && {
|
[[ $platform && ,common,linux,osx,sunos, != *,$platform,* ]] && {
|
||||||
Err "Unknown platform $I$platform$XI"
|
Err "Unknown platform $I$platform$XI"
|
||||||
Usage 22
|
Usage 23
|
||||||
}
|
}
|
||||||
|
|
||||||
Get_tldr "${page// /-}"
|
Get_tldr "${page// /-}"
|
||||||
[[ ! -s $cached ]] && Err "tldr page for command $I$page$XI not found" \
|
[[ ! -s $cached ]] && Err "tldr page for command $I$page$XI not found" \
|
||||||
&& Inf "Contribute new pages at:$XB$URL https://github.com/tldr-pages/tldr$XURL" && exit 23
|
&& Inf "Contribute new pages at:$XB$URL https://github.com/tldr-pages/tldr$XURL" && exit 24
|
||||||
((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached"
|
((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue