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
8c3e1c9a3f
commit
739c4e043a
1 changed files with 69 additions and 44 deletions
113
tldr
113
tldr
|
@ -1,8 +1,8 @@
|
||||||
#!/usr/bin/env bash
|
#!/usr/bin/env 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.36'
|
version='0.39'
|
||||||
# tldr-bash-client version 0.36
|
# tldr-bash-client version 0.39
|
||||||
# Bash client for tldr: community driven man-by-example
|
# Bash client for tldr: community driven man-by-example
|
||||||
# - forked from Ray Lee, https://github.com/raylee/tldr
|
# - forked from Ray Lee, https://github.com/raylee/tldr
|
||||||
# - modified and expanded by pepa65: https://github.com/pepa65/tldr-bash-client
|
# - modified and expanded by pepa65: https://github.com/pepa65/tldr-bash-client
|
||||||
|
@ -49,21 +49,23 @@ version='0.36'
|
||||||
# $1: [optional] exit code; Uses: version cachedir
|
# $1: [optional] exit code; Uses: version cachedir
|
||||||
Usage(){
|
Usage(){
|
||||||
Out "$(cat <<-EOF
|
Out "$(cat <<-EOF
|
||||||
$HHE$version
|
$E$version
|
||||||
|
|
||||||
$HDEUSAGE: $HHE$(basename "$0")$XHHE [${HOP}option$XHOP] [${HPL}platform$XHPL/]${HCO}command$XHCO
|
${HDE}USAGE: $HHE$(basename "$0")$XHHE [${HOP}option$XHOP] [${HPL}platform$XHPL/]${HCO}command$XHCO
|
||||||
|
|
||||||
$HDE[${HPL}platform$XHPL/]${HCO}command$XHCO: Show page for ${HCO}command$XHCO (from ${HPL}platform$XHPL)
|
$HDE[${HPL}platform$XHPL/]${HCO}command$XHCO: Show page for ${HCO}command$XHCO (from ${HPL}platform$XHPL)
|
||||||
|
|
||||||
${HPL}platform$XHPL (optional) one of: ${HPL}common$XHPL, ${HPL}linux$XHPL, ${HPL}osx$XHPL, ${HPL}sunos$XHPL, ${HPL}current$XHPL (includes common)
|
${HPL}platform$XHPL (optional) one of: ${HPL}common$XHPL, ${HPL}linux$XHPL, ${HPL}osx$XHPL, ${HPL}sunos$XHPL, ${HPL}windows$XHPL,
|
||||||
|
${HPL}current$XHPL (includes ${HPL}common$XHPL)
|
||||||
|
|
||||||
${HOP}option$XHOP is optionally one of:
|
${HOP}option$XHOP is optionally one of:
|
||||||
|
$HOP-s$XHOP, $HOP--search$XHOP ${HFI}regex$XHFI: Search for ${HFI}regex$XHFI in all tldr pages
|
||||||
$HOP-l$XHOP, $HOP--list$XHOP [${HPL}platform$XHPL]: List all pages (from ${HPL}platform$XHPL)
|
$HOP-l$XHOP, $HOP--list$XHOP [${HPL}platform$XHPL]: List all pages (from ${HPL}platform$XHPL)
|
||||||
$HOP-a$XHOP, $HOP--list-all$XHOP: List all pages from current platform + common
|
$HOP-a$XHOP, $HOP--list-all$XHOP: List all pages from current platform + common
|
||||||
$HOP-r$XHOP, $HOP--render$XHOP ${HFI}file$XHFI: Render a local ${HFI}file$XHFI as tldr markdown
|
$HOP-r$XHOP, $HOP--render$XHOP ${HFI}file$XHFI: Render ${HFI}file$XHFI as tldr markdown
|
||||||
$HOP-m$XHOP, $HOP--markdown$XHOP ${HCO}command$XHCO: Show the markdown source for ${HCO}command$XHCO
|
$HOP-m$XHOP, $HOP--markdown$XHOP ${HCO}command$XHCO: Show the markdown source for ${HCO}command$XHCO
|
||||||
$HOP-u$XHOP, $HOP--update$XHOP: Update the pages cache by downloading repo archive
|
$HOP-u$XHOP, $HOP--update$XHOP: Update the pages cache by downloading repo archive
|
||||||
$HOP-v$XHOP, $HOP--version$XHOP: Version number and local repo location
|
$HOP-v$XHOP, $HOP--version$XHOP: Version number and github repo location
|
||||||
$HDE[$HOP-h$XHOP, $HOP-?$XHOP, $HOP--help$XHOP]: This help overview
|
$HDE[$HOP-h$XHOP, $HOP-?$XHOP, $HOP--help$XHOP]: This help overview
|
||||||
|
|
||||||
${HDE}Element styling:$XHDE ${T}Title$XT ${D}Description$XD ${E}Example$XE ${C}Code$XC ${V}Value$XV
|
${HDE}Element styling:$XHDE ${T}Title$XT ${D}Description$XD ${E}Example$XE ${C}Code$XC ${V}Value$XV
|
||||||
|
@ -189,6 +191,8 @@ Config(){
|
||||||
Darwin) os='osx' ;;
|
Darwin) os='osx' ;;
|
||||||
Linux) os='linux' ;;
|
Linux) os='linux' ;;
|
||||||
SunOS) os='sunos' ;;
|
SunOS) os='sunos' ;;
|
||||||
|
CYGWIN*) os='windows' ;;
|
||||||
|
MINGW*) os='windows' ;;
|
||||||
esac
|
esac
|
||||||
Init_term
|
Init_term
|
||||||
[[ $TLDR_LESS = 0 ]] &&
|
[[ $TLDR_LESS = 0 ]] &&
|
||||||
|
@ -208,11 +212,12 @@ Config(){
|
||||||
pages_url='https://raw.githubusercontent.com/tldr-pages/tldr/master/pages'
|
pages_url='https://raw.githubusercontent.com/tldr-pages/tldr/master/pages'
|
||||||
zip_url='http://tldr.sh/assets/tldr.zip'
|
zip_url='http://tldr.sh/assets/tldr.zip'
|
||||||
|
|
||||||
read cachedir <<<$TLDR_CACHE
|
cachedir=$(echo $TLDR_CACHE)
|
||||||
[[ $cachedir ]] || {
|
if [[ -z $cachedir ]]
|
||||||
[[ $XDG_DATA_HOME ]] && cachedir=$XDG_DATA_HOME/tldr \
|
then
|
||||||
|| cachedir=$HOME/.local/share/tldr
|
[[ $XDG_DATA_HOME ]] && cachedir=$XDG_DATA_HOME/tldr ||
|
||||||
}
|
cachedir=$HOME/.local/share/tldr
|
||||||
|
fi
|
||||||
[[ -d "$cachedir" ]] || mkdir -p "$cachedir" || {
|
[[ -d "$cachedir" ]] || mkdir -p "$cachedir" || {
|
||||||
Err "Can't create the pages cache location $cachedir"
|
Err "Can't create the pages cache location $cachedir"
|
||||||
exit 4
|
exit 4
|
||||||
|
@ -220,6 +225,9 @@ Config(){
|
||||||
index=$cachedir/index.json
|
index=$cachedir/index.json
|
||||||
# update if the file doesn't exists, or if it's older than $TLDR_EXPIRY
|
# update if the file doesn't exists, or if it's older than $TLDR_EXPIRY
|
||||||
[[ -f $index ]] && Recent "$index" || Cache_fill 0
|
[[ -f $index ]] && Recent "$index" || Cache_fill 0
|
||||||
|
|
||||||
|
# initiate autocomplete for current session
|
||||||
|
complete -W "$(q=($cachedir/*/*); sed 's@\.md @ @g' <<<${q[@]##*/})" tldr
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: error message; Uses: md REPLY ln
|
# $1: error message; Uses: md REPLY ln
|
||||||
|
@ -231,7 +239,7 @@ Unlinted(){
|
||||||
# $1: page; Uses: index cachedir pages_url platform os dl cached md
|
# $1: page; Uses: index cachedir pages_url platform os dl cached md
|
||||||
# Sets: cached md
|
# Sets: cached md
|
||||||
Get_tldr(){
|
Get_tldr(){
|
||||||
local desc err notfound
|
local desc err=0 notfound
|
||||||
# convert the local platform name to tldr's version
|
# convert the local platform name to tldr's version
|
||||||
# extract the platform key from index.json, return preferred subpath to page
|
# extract the platform key from index.json, return preferred subpath to page
|
||||||
desc=$(tr '{' '\n' <"$index" |grep "\"name\":\"$1\"")
|
desc=$(tr '{' '\n' <"$index" |grep "\"name\":\"$1\"")
|
||||||
|
@ -239,10 +247,9 @@ Get_tldr(){
|
||||||
|
|
||||||
[[ $desc ]] || return # nothing found
|
[[ $desc ]] || return # nothing found
|
||||||
|
|
||||||
error=0
|
|
||||||
if [[ $platform ]]
|
if [[ $platform ]]
|
||||||
then # platform given on commandline
|
then # platform given on commandline
|
||||||
[[ ! $desc =~ \"$platform\" ]] && notfound=$I$platform$XI && error=1 || md=$platform/$1.md
|
[[ ! $desc =~ \"$platform\" ]] && notfound=$I$platform$XI && err=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 "
|
||||||
|
@ -254,11 +261,11 @@ Get_tldr(){
|
||||||
[[ $desc =~ \"$os\" ]] && md=$os/$1.md
|
[[ $desc =~ \"$os\" ]] && md=$os/$1.md
|
||||||
} || {
|
} || {
|
||||||
notfound+=" or $I$os$XI"
|
notfound+=" or $I$os$XI"
|
||||||
error=1
|
err=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"
|
||||||
((error)) && Err "tldr page $I$1$XI not found in $notfound, from platform $U${md%/*}$XU instead"
|
((err)) && 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
|
||||||
|
@ -268,7 +275,7 @@ Get_tldr(){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: text; Uses: page stdout; Sets: ln REPLY
|
# $1: file (optional); Uses: page stdout; Sets: ln REPLY
|
||||||
Display_tldr(){
|
Display_tldr(){
|
||||||
local newfmt len val
|
local newfmt len val
|
||||||
ln=0 REPLY=''
|
ln=0 REPLY=''
|
||||||
|
@ -345,24 +352,42 @@ List_pages(){
|
||||||
exit "$1"
|
exit "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# $1: regex, $2: exit code; Uses: cachedir
|
||||||
|
Find_regex(){
|
||||||
|
local list=$(grep "$1" "$cachedir"/*/*.md |cut -d: -f1) regex="$U$1$XU"
|
||||||
|
local n=$(wc -l <<<"$list")
|
||||||
|
list=$(sort -u <<<"$list")
|
||||||
|
[[ -z $list ]] && Err "Regex $regex not found" && exit 6
|
||||||
|
local t=$(wc -l <<<"$list")
|
||||||
|
if ((t==1))
|
||||||
|
then
|
||||||
|
Display_tldr "$list"
|
||||||
|
else
|
||||||
|
Inf "Regex $regex $I$n$XI times found in these $I$t$XI tldr pages:"
|
||||||
|
Out "$(while read -r c1 c2 c3; do printf "%-19s %-19s %-19s %-19s$N" $c1 $c2 $c3; done \
|
||||||
|
<<<$(sed -e 's@.*/@@' -e 's@...$@@' <<<"$list"))"
|
||||||
|
fi
|
||||||
|
exit "$2"
|
||||||
|
}
|
||||||
|
|
||||||
# $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" || {
|
unzip="$(type -p unzip) -q" || {
|
||||||
Err "Unzip is necessary to fill the cache"
|
Err "Unzip is necessary to fill the cache"
|
||||||
exit 6
|
exit 7
|
||||||
}
|
}
|
||||||
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 with $dl"
|
Err "Could not download pages archive from $U$zip_url$XU with $dl"
|
||||||
exit 7
|
exit 8
|
||||||
}
|
}
|
||||||
$unzip "$tmp/pages.zip" -d "$tmp" 'pages/*' || {
|
$unzip "$tmp/pages.zip" -d "$tmp" 'pages/*' || {
|
||||||
rm -- "$tmp"
|
rm -- "$tmp"
|
||||||
Err "Couldn't unzip the cache archive on $tmp/pages.zip"
|
Err "Couldn't unzip the cache archive on $tmp/pages.zip"
|
||||||
exit 8
|
exit 9
|
||||||
}
|
}
|
||||||
rm -rf -- "${cachedir:?}/"*
|
rm -rf -- "${cachedir:?}/"*
|
||||||
mv -- "$tmp/pages/"* "${cachedir:?}/"
|
mv -- "$tmp/pages/"* "${cachedir:?}/"
|
||||||
rm -rf -- "$tmp"
|
rm -rf -- "$tmp"
|
||||||
|
@ -372,56 +397,56 @@ Cache_fill(){
|
||||||
|
|
||||||
# $@: commandline parameters; Uses: version cached; Sets: platform page
|
# $@: commandline parameters; Uses: version cached; Sets: platform page
|
||||||
Main(){
|
Main(){
|
||||||
local markdown err nomore='No more command line arguments allowed'
|
local markdown=0 err=0 nomore='No more command line arguments allowed'
|
||||||
Config
|
Config
|
||||||
markdown=0 err=0
|
|
||||||
case "$1" in
|
case "$1" in
|
||||||
|
-s|--search) [[ -z $2 ]] && Err "Search term (regex) needed" && Usage 10
|
||||||
|
[[ $3 ]] && Err "$nomore" && err=11
|
||||||
|
Find_regex "$2" "$err" ;;
|
||||||
-l|--list) [[ $2 ]] && {
|
-l|--list) [[ $2 ]] && {
|
||||||
platform=$2
|
platform=$2
|
||||||
[[ ,common,linux,osx,sunos,current, = *,$platform,* ]] || {
|
[[ ,common,linux,osx,sunos,windows,current, != *,$platform,* ]] &&
|
||||||
Err "Unknown platform $I$platform$XI"
|
Err "Unknown platform $I$platform$XI" && Usage 12
|
||||||
Usage 9
|
[[ $3 ]] && Err "$nomore" && err=13
|
||||||
}
|
|
||||||
[[ $3 ]] && Err "$nomore" && err=10
|
|
||||||
}
|
}
|
||||||
List_pages "$err" ;;
|
List_pages "$err" ;;
|
||||||
-a|--list-all) [[ $2 ]] && Err "$nomore" && err=11
|
-a|--list-all) [[ $2 ]] && Err "$nomore" && err=14
|
||||||
platform=current
|
platform=current
|
||||||
List_pages $err ;;
|
List_pages $err ;;
|
||||||
-u|--update) [[ $2 ]] && Err "$nomore" && err=12
|
-u|--update) [[ $2 ]] && Err "$nomore" && err=15
|
||||||
Cache_fill "$err" ;;
|
Cache_fill "$err" ;;
|
||||||
-v|--version) [[ $2 ]] && Err "$nomore" && err=13
|
-v|--version) [[ $2 ]] && Err "$nomore" && err=16
|
||||||
Inf "$version"
|
Inf "$version"
|
||||||
exit "$err" ;;
|
exit "$err" ;;
|
||||||
-r|--render) [[ -z $2 ]] && Err "Specify a file to render" && Usage 14
|
-r|--render) [[ -z $2 ]] && Err "Specify a file to render" && Usage 17
|
||||||
[[ $3 ]] && Err "$nomore" && err=15
|
[[ $3 ]] && Err "$nomore" && err=18
|
||||||
[[ -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 16
|
exit 19
|
||||||
} || Err "No file: ${I}$2$XI" && exit 17 ;;
|
} || Err "No file: ${I}$2$XI" && exit 20 ;;
|
||||||
-m|--markdown) shift
|
-m|--markdown) shift
|
||||||
page=$*
|
page=$*
|
||||||
[[ -z $page ]] && Err "Specify a page to display" && Usage 18
|
[[ -z $page ]] && Err "Specify a page to display" && Usage 21
|
||||||
[[ -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 "$nomore" && err=19
|
''|-h|-\?|--help) [[ $2 ]] && Err "$nomore" && err=22
|
||||||
Usage "$err" ;;
|
Usage "$err" ;;
|
||||||
-*) Err "Unrecognized option $I$1$XI"; Usage 20 ;;
|
-*) Err "Unrecognized option $I$1$XI"; Usage 23 ;;
|
||||||
*) page=$* ;;
|
*) page=$* ;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
[[ -z $page ]] && Err "No command specified" && Usage 21
|
[[ -z $page ]] && Err "No command specified" && Usage 24
|
||||||
[[ ${page:0:1} = '-' || $page = *' '-* ]] && Err "Only one option allowed" && Usage 22
|
[[ ${page:0:1} = '-' || $page = *' '-* ]] && Err "Only one option allowed" && Usage 25
|
||||||
[[ $page = */* ]] && platform=${page%/*} && page=${page##*/}
|
[[ $page = */* ]] && platform=${page%/*} && page=${page##*/}
|
||||||
[[ $platform && ,common,linux,osx,sunos, != *,$platform,* ]] && {
|
[[ $platform && ,common,linux,osx,sunos,windows, != *,$platform,* ]] && {
|
||||||
Err "Unknown platform $I$platform$XI"
|
Err "Unknown platform $I$platform$XI"
|
||||||
Usage 23
|
Usage 26
|
||||||
}
|
}
|
||||||
|
|
||||||
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 24
|
&& Inf "Contribute new pages at:$XB ${URL}https://github.com/tldr-pages/tldr$XURL" && exit 27
|
||||||
((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached"
|
((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue