Version 0.42
Use column for lists Modified help and messages Platforms not hardcoded but read from archive Improve platform processing
This commit is contained in:
parent
eeabbc5487
commit
4150394765
|
@ -1,6 +1,6 @@
|
||||||
# tldr-bash-client
|
# tldr-bash-client
|
||||||
|
|
||||||
* version 0.41
|
* version 0.42
|
||||||
|
|
||||||
### Bash client for tldr: community driven man-by-example
|
### Bash client for tldr: community driven man-by-example
|
||||||
**A fully-functional [bash](https://tiswww.case.edu/php/chet/bash/bashtop.html)
|
**A fully-functional [bash](https://tiswww.case.edu/php/chet/bash/bashtop.html)
|
||||||
|
|
128
tldr
128
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.41'
|
version='0.42'
|
||||||
# tldr-bash-client version 0.41
|
# tldr-bash-client version 0.42
|
||||||
# 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://gitlab.com/pepa65/tldr-bash-client
|
# - modified and expanded by pepa65: https://gitlab.com/pepa65/tldr-bash-client
|
||||||
|
@ -46,25 +46,24 @@ version='0.41'
|
||||||
|
|
||||||
## Function definitions
|
## Function definitions
|
||||||
|
|
||||||
# $1: [optional] exit code; Uses: version cachedir
|
# $1: [optional] exit code; Uses: ver cachedir
|
||||||
Usage(){
|
Usage(){
|
||||||
Out "$(cat <<-EOF
|
Out "$(cat <<-EOF
|
||||||
$E$version
|
$E$ver
|
||||||
|
|
||||||
${HDE}USAGE: $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}${platforms//,/$XHPL, $HPL}$XHPL,
|
||||||
|
${HPL}current$XHPL (includes ${HPL}common$XHPL), ${HPL}all$XHPL (default)
|
||||||
|
|
||||||
${HPL}platform$XHPL (optional) one of: ${HPL}common$XHPL, ${HPL}linux$XHPL, ${HPL}osx$XHPL, ${HPL}sunos$XHPL, ${HPL}windows$XHPL,
|
${HOP}option$XHOP (optional) one of:
|
||||||
${HPL}current$XHPL (includes ${HPL}common$XHPL)
|
|
||||||
|
|
||||||
${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-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 ${HPL}current$XHPL platform
|
||||||
$HOP-r$XHOP, $HOP--render$XHOP ${HFI}file$XHFI: Render ${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 archive
|
||||||
$HOP-v$XHOP, $HOP--version$XHOP: Version number and gitlab repo location
|
$HOP-v$XHOP, $HOP--version$XHOP: Version number and gitlab 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
|
||||||
|
|
||||||
|
@ -202,7 +201,7 @@ Config(){
|
||||||
trap 'cat <<<"$stdout"' EXIT ||
|
trap 'cat <<<"$stdout"' EXIT ||
|
||||||
trap 'less -~RXQFP"Browse up/down, press Q to exit " <<<"$stdout"' EXIT
|
trap 'less -~RXQFP"Browse up/down, press Q to exit " <<<"$stdout"' EXIT
|
||||||
|
|
||||||
version="tldr-bash-client version $version$XB ${URL}http://gitlab.com/pepa65/tldr-bash-client$XURL"
|
ver="tldr-bash-client version $version$XB ${URL}http://gitlab.com/pepa65/tldr-bash-client$XURL"
|
||||||
|
|
||||||
# Select download method
|
# Select download method
|
||||||
! dl="$(type -P curl) -sLfo" &&
|
! dl="$(type -P curl) -sLfo" &&
|
||||||
|
@ -226,6 +225,8 @@ 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
|
[[ -f $index ]] && Recent "$index" || Cache_fill
|
||||||
|
platforms=$(cd "$cachedir"; ls -d -- */ |tr -d /)
|
||||||
|
platforms=${platforms//$'\n'/,}
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: error message; Uses: md REPLY ln
|
# $1: error message; Uses: md REPLY ln
|
||||||
|
@ -247,8 +248,15 @@ Get_tldr(){
|
||||||
|
|
||||||
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
|
if [[ $platform = current ]]
|
||||||
else # check common
|
then
|
||||||
|
[[ $desc =~ \"common\" ]] && md=common/$1.md
|
||||||
|
[[ $desc =~ \"$os\" ]] && md=$os/$1.md
|
||||||
|
elif [[ $desc =~ \"$platform\" ]]
|
||||||
|
then md=$platform/$1.md
|
||||||
|
else notfound=$I$platform$XI err=1
|
||||||
|
fi
|
||||||
|
else # no platform specified: check common
|
||||||
if [[ $desc =~ \"common\" ]]
|
if [[ $desc =~ \"common\" ]]
|
||||||
then
|
then
|
||||||
md=common/$1.md
|
md=common/$1.md
|
||||||
|
@ -259,19 +267,16 @@ Get_tldr(){
|
||||||
fi
|
fi
|
||||||
# if no page found yet, try the system platform
|
# if no page found yet, try the system platform
|
||||||
[[ $md ]] ||
|
[[ $md ]] ||
|
||||||
if [[ $platform = $os ]]
|
if [[ $desc =~ \"$os\" ]]
|
||||||
then
|
then md=$os/$1.md
|
||||||
[[ $desc =~ \"$os\" ]] && md=$os/$1.md
|
else [[ $platform = $os ]] || notfound+=" or $I$os$XI" err=1
|
||||||
else
|
|
||||||
notfound+=" or $I$os$XI"
|
|
||||||
err=1
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 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"
|
((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 local cached copy of tldr-page, or retrieve and cache from github
|
||||||
cached=$cachedir/$md
|
cached=$cachedir/$md
|
||||||
if ! Recent "$cached"
|
if ! Recent "$cached"
|
||||||
then
|
then
|
||||||
|
@ -349,33 +354,36 @@ Display_tldr(){
|
||||||
|
|
||||||
# $1: exit code; Uses: platform index
|
# $1: exit code; Uses: platform index
|
||||||
List_pages(){
|
List_pages(){
|
||||||
local platformtext c1 c2 c3
|
local ptext pregex=$platform
|
||||||
[[ $platform ]] && platformtext="platform $I$platform$XI" ||
|
[[ $platform ]] && ptext="platform $I$platform$XI" ||
|
||||||
platform=^ platformtext="${I}all$XI platforms"
|
pregex=^ ptext="${I}all$XI platforms"
|
||||||
[[ $platform = current ]] && platform="-e $os -e common" &&
|
[[ $platform = current ]] &&
|
||||||
platformtext="$I$os$XI platform and ${I}common$XI"
|
pregex="-e $os -e common" ptext="$I$os$XI platform and ${I}common$XI"
|
||||||
Inf "Known tldr pages from $platformtext:"
|
Inf "Known tldr pages from $ptext:"
|
||||||
Out "$(while read -r c1 c2 c3; do printf "%-19s %-19s %-19s %-19s$N" $c1 $c2 $c3; done \
|
Out "$(tr '{' '\n' <"$index" |grep $pregex |cut -d "$Q" -f4 |column)"
|
||||||
<<<$(tr '{' '\n' <"$index" |grep $platform |cut -d "$Q" -f4))"
|
|
||||||
exit "$1"
|
exit "$1"
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: regex, $2: exit code; Uses: cachedir
|
# $1: regex(es); Uses: cachedir
|
||||||
Find_regex(){
|
Find_regex(){
|
||||||
local list=$(grep "$1" "$cachedir"/*/*.md |cut -d: -f1) regex="$U$1$XU"
|
local regex=$* list=$cachedir/*/*.md
|
||||||
local n=$(wc -l <<<"$list")
|
while (($#))
|
||||||
|
do
|
||||||
|
list=$(grep $1 $list |cut -d: -f1)
|
||||||
|
shift
|
||||||
|
done
|
||||||
|
n=$(wc -l <<<"$list")
|
||||||
list=$(sort -u <<<"$list")
|
list=$(sort -u <<<"$list")
|
||||||
[[ -z $list ]] && Err "Regex $regex not found" && exit 6
|
[[ -z $list ]] && Err "Regex $U$regex$XU not found" && exit 6
|
||||||
local t=$(wc -l <<<"$list")
|
local t=$(wc -l <<<"$list")
|
||||||
if ((t==1))
|
if ((t==1))
|
||||||
then
|
then
|
||||||
Display_tldr "$list"
|
Display_tldr "$list"
|
||||||
else
|
else
|
||||||
Inf "Regex $regex $I$n$XI times found in these $I$t$XI tldr pages:"
|
Inf "Regex $U$regex$XU $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 \
|
Out "$(sed -e 's@.*/@@' -e 's@...$@@' <<<"$list" |column)"
|
||||||
<<<$(sed -e 's@.*/@@' -e 's@...$@@' <<<"$list"))"
|
|
||||||
fi
|
fi
|
||||||
exit "$2"
|
exit 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# $1: exit code; Uses: dl cachedir zip_url
|
# $1: exit code; Uses: dl cachedir zip_url
|
||||||
|
@ -403,35 +411,44 @@ Cache_fill(){
|
||||||
|
|
||||||
# $@: commandline parameters; Uses: version cached; Sets: platform page
|
# $@: commandline parameters; Uses: version cached; Sets: platform page
|
||||||
Main(){
|
Main(){
|
||||||
local markdown=0 err=0 nomore='Extraneous commandline arguments ignored'
|
local markdown=0 err=0 nomore='Arguments ignored: '
|
||||||
Config
|
Config
|
||||||
case "$1" in
|
case "$1" in
|
||||||
-s|--search) [[ -z $2 ]] && Err "Search term (regex) needed" && Usage 10
|
-s|--search) [[ -z $2 ]] && Err "Search term(s) [regex] needed" && Usage 10
|
||||||
[[ $3 ]] && Err "$nomore" && err=11
|
shift
|
||||||
Find_regex "$2" "$err" ;;
|
Find_regex $* ;;
|
||||||
-l|--list)
|
-l|--list)
|
||||||
if [[ $2 ]]
|
if [[ $2 ]]
|
||||||
then
|
then
|
||||||
platform=$2
|
platform=$2
|
||||||
[[ ,common,linux,osx,sunos,windows,current, != *,$platform,* ]] &&
|
[[ $platform == *,* ]] && Err "No commma allowed, just one platform" &&
|
||||||
|
Usage 11
|
||||||
|
[[ ,$platforms,all,current, != *,$platform,* ]] &&
|
||||||
Err "Unknown platform $I$platform$XI" && Usage 12
|
Err "Unknown platform $I$platform$XI" && Usage 12
|
||||||
[[ $3 ]] && Err "$nomore" && err=13
|
[[ $platform = all ]] && platform=
|
||||||
|
shift 2
|
||||||
|
[[ $1 ]] && Err "$nomore$*" && err=13
|
||||||
fi
|
fi
|
||||||
List_pages "$err" ;;
|
List_pages "$err" ;;
|
||||||
-a|--list-all) [[ $2 ]] && Err "$nomore" && err=14
|
-a|--list-all) shift
|
||||||
|
[[ $1 ]] && Err "$nomore$*" && err=14
|
||||||
platform=current
|
platform=current
|
||||||
List_pages $err ;;
|
List_pages $err ;;
|
||||||
-u|--update) [[ $2 ]] && Err "$nomore" && err=15
|
-u|--update) shift
|
||||||
|
[[ $1 ]] && Err "$nomore$*" && err=15
|
||||||
((updated)) || Cache_fill
|
((updated)) || Cache_fill
|
||||||
exit "$err" ;;
|
exit "$err" ;;
|
||||||
-v|--version) [[ $2 ]] && Err "$nomore" && err=16
|
-v|--version) shift
|
||||||
Inf "$version"
|
[[ $1 ]] && Err "$nomore$*" && err=16
|
||||||
|
Inf "$ver"
|
||||||
exit "$err" ;;
|
exit "$err" ;;
|
||||||
-r|--render)[[ -z $2 ]] && Err "Specify a file to render" && Usage 17
|
-r|--render)[[ -z $2 ]] && Err "Specify a file to render" && Usage 17
|
||||||
[[ $3 ]] && Err "$nomore" && err=18
|
file=$2
|
||||||
if [[ -f "$2" ]]
|
shift 2
|
||||||
|
[[ $1 ]] && Err "$nomore$*" && err=18
|
||||||
|
if [[ -f "$file" ]]
|
||||||
then
|
then
|
||||||
Display_tldr "$2" && exit "$err"
|
Display_tldr "$file" && exit "$err"
|
||||||
Err "A file error occured"
|
Err "A file error occured"
|
||||||
exit 19
|
exit 19
|
||||||
else
|
else
|
||||||
|
@ -450,15 +467,16 @@ Main(){
|
||||||
esac
|
esac
|
||||||
|
|
||||||
[[ -z $page ]] && Err "No command specified" && Usage 24
|
[[ -z $page ]] && Err "No command specified" && Usage 24
|
||||||
[[ ${page:0:1} = '-' || $page = *' '-* ]] && Err "Only one option allowed" && Usage 25
|
[[ $page = *' '-* ]] && Err "No options after page allowed" && Usage 25
|
||||||
[[ $page = */* ]] && platform=${page%/*} && page=${page##*/}
|
[[ $page = */* ]] && platform=${page%/*} && page=${page##*/}
|
||||||
[[ $platform && ,common,linux,osx,sunos,windows, != *,$platform,* ]] &&
|
[[ $platform == *,* ]] && Err "No comma allowed in platform" && Usage 26
|
||||||
|
[[ $platform && ,$platforms,all,current, != *,$platform,* ]] &&
|
||||||
Err "Unknown platform $I$platform$XI" &&
|
Err "Unknown platform $I$platform$XI" &&
|
||||||
Usage 26
|
Usage 27
|
||||||
|
[[ $platform = all ]] && platform=
|
||||||
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 27
|
&& Inf "Contribute new pages at:$XB ${URL}https://github.com/tldr-pages/tldr$XURL" && exit 28
|
||||||
((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached"
|
((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
BIN
tldr-usage.jpg
BIN
tldr-usage.jpg
Binary file not shown.
Before Width: | Height: | Size: 44 KiB After Width: | Height: | Size: 64 KiB |
Loading…
Reference in New Issue