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:
pepa65 2018-12-19 00:18:49 -08:00
parent eeabbc5487
commit 4150394765
3 changed files with 76 additions and 58 deletions

View File

@ -1,6 +1,6 @@
# tldr-bash-client
* version 0.41
* version 0.42
### Bash client for tldr: community driven man-by-example
**A fully-functional [bash](https://tiswww.case.edu/php/chet/bash/bashtop.html)

132
tldr
View File

@ -1,8 +1,8 @@
#!/usr/bin/env bash
set +vx -o pipefail
[[ $- = *i* ]] && echo "Don't source this script!" && return 1
version='0.41'
# tldr-bash-client version 0.41
version='0.42'
# tldr-bash-client version 0.42
# Bash client for tldr: community driven man-by-example
# - forked from Ray Lee, https://github.com/raylee/tldr
# - modified and expanded by pepa65: https://gitlab.com/pepa65/tldr-bash-client
@ -46,25 +46,24 @@ version='0.41'
## Function definitions
# $1: [optional] exit code; Uses: version cachedir
# $1: [optional] exit code; Uses: ver cachedir
Usage(){
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)
${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,
${HPL}current$XHPL (includes ${HPL}common$XHPL)
${HOP}option$XHOP is optionally one of:
${HOP}option$XHOP (optional) 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-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-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
$HDE[$HOP-h$XHOP, $HOP-?$XHOP, $HOP--help$XHOP]: This help overview
@ -202,7 +201,7 @@ Config(){
trap 'cat <<<"$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
! dl="$(type -P curl) -sLfo" &&
@ -226,6 +225,8 @@ Config(){
index=$cachedir/index.json
# update if the file doesn't exists, or if it's older than $TLDR_EXPIRY
[[ -f $index ]] && Recent "$index" || Cache_fill
platforms=$(cd "$cachedir"; ls -d -- */ |tr -d /)
platforms=${platforms//$'\n'/,}
}
# $1: error message; Uses: md REPLY ln
@ -247,8 +248,15 @@ Get_tldr(){
if [[ $platform ]]
then # platform given on commandline
[[ ! $desc =~ \"$platform\" ]] && notfound=$I$platform$XI && err=1 || md=$platform/$1.md
else # check common
if [[ $platform = current ]]
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\" ]]
then
md=common/$1.md
@ -259,19 +267,16 @@ Get_tldr(){
fi
# if no page found yet, try the system platform
[[ $md ]] ||
if [[ $platform = $os ]]
then
[[ $desc =~ \"$os\" ]] && md=$os/$1.md
else
notfound+=" or $I$os$XI"
err=1
if [[ $desc =~ \"$os\" ]]
then md=$os/$1.md
else [[ $platform = $os ]] || notfound+=" or $I$os$XI" err=1
fi
# if still no page found, get the first entry in index
[[ $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"
# 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
if ! Recent "$cached"
then
@ -349,33 +354,36 @@ Display_tldr(){
# $1: exit code; Uses: platform index
List_pages(){
local platformtext c1 c2 c3
[[ $platform ]] && platformtext="platform $I$platform$XI" ||
platform=^ platformtext="${I}all$XI platforms"
[[ $platform = current ]] && platform="-e $os -e common" &&
platformtext="$I$os$XI platform and ${I}common$XI"
Inf "Known tldr pages from $platformtext:"
Out "$(while read -r c1 c2 c3; do printf "%-19s %-19s %-19s %-19s$N" $c1 $c2 $c3; done \
<<<$(tr '{' '\n' <"$index" |grep $platform |cut -d "$Q" -f4))"
local ptext pregex=$platform
[[ $platform ]] && ptext="platform $I$platform$XI" ||
pregex=^ ptext="${I}all$XI platforms"
[[ $platform = current ]] &&
pregex="-e $os -e common" ptext="$I$os$XI platform and ${I}common$XI"
Inf "Known tldr pages from $ptext:"
Out "$(tr '{' '\n' <"$index" |grep $pregex |cut -d "$Q" -f4 |column)"
exit "$1"
}
# $1: regex, $2: exit code; Uses: cachedir
# $1: regex(es); Uses: cachedir
Find_regex(){
local list=$(grep "$1" "$cachedir"/*/*.md |cut -d: -f1) regex="$U$1$XU"
local n=$(wc -l <<<"$list")
local regex=$* list=$cachedir/*/*.md
while (($#))
do
list=$(grep $1 $list |cut -d: -f1)
shift
done
n=$(wc -l <<<"$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")
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"))"
Inf "Regex $U$regex$XU $I$n$XI times found in these $I$t$XI tldr pages:"
Out "$(sed -e 's@.*/@@' -e 's@...$@@' <<<"$list" |column)"
fi
exit "$2"
exit 0
}
# $1: exit code; Uses: dl cachedir zip_url
@ -403,35 +411,44 @@ Cache_fill(){
# $@: commandline parameters; Uses: version cached; Sets: platform page
Main(){
local markdown=0 err=0 nomore='Extraneous commandline arguments ignored'
local markdown=0 err=0 nomore='Arguments ignored: '
Config
case "$1" in
-s|--search) [[ -z $2 ]] && Err "Search term (regex) needed" && Usage 10
[[ $3 ]] && Err "$nomore" && err=11
Find_regex "$2" "$err" ;;
-s|--search) [[ -z $2 ]] && Err "Search term(s) [regex] needed" && Usage 10
shift
Find_regex $* ;;
-l|--list)
if [[ $2 ]]
then
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
[[ $3 ]] && Err "$nomore" && err=13
[[ $platform = all ]] && platform=
shift 2
[[ $1 ]] && Err "$nomore$*" && err=13
fi
List_pages "$err" ;;
-a|--list-all) [[ $2 ]] && Err "$nomore" && err=14
-a|--list-all) shift
[[ $1 ]] && Err "$nomore$*" && err=14
platform=current
List_pages $err ;;
-u|--update) [[ $2 ]] && Err "$nomore" && err=15
-u|--update) shift
[[ $1 ]] && Err "$nomore$*" && err=15
((updated)) || Cache_fill
exit "$err" ;;
-v|--version) [[ $2 ]] && Err "$nomore" && err=16
Inf "$version"
-v|--version) shift
[[ $1 ]] && Err "$nomore$*" && err=16
Inf "$ver"
exit "$err" ;;
-r|--render) [[ -z $2 ]] && Err "Specify a file to render" && Usage 17
[[ $3 ]] && Err "$nomore" && err=18
if [[ -f "$2" ]]
-r|--render)[[ -z $2 ]] && Err "Specify a file to render" && Usage 17
file=$2
shift 2
[[ $1 ]] && Err "$nomore$*" && err=18
if [[ -f "$file" ]]
then
Display_tldr "$2" && exit "$err"
Display_tldr "$file" && exit "$err"
Err "A file error occured"
exit 19
else
@ -450,15 +467,16 @@ Main(){
esac
[[ -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##*/}
[[ $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" &&
Usage 26
Usage 27
[[ $platform = all ]] && platform=
Get_tldr "${page// /-}"
[[ ! -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
[[ ! -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 28
((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached"
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 44 KiB

After

Width:  |  Height:  |  Size: 64 KiB