2
0
mirror of https://github.com/pepa65/tldr-bash-client.git synced 2024-11-01 00:51:01 +01:00

Version 0.2

Fixed columns in listing
Added Main function
This commit is contained in:
pepa65 2017-02-23 16:17:43 +07:00
parent 84bc7dcb55
commit 6d023a0747
3 changed files with 39 additions and 33 deletions

View File

@ -1,6 +1,6 @@
# tldr-bash-client # tldr-bash-client
* version 0.1a * version 0.2
* https://github.com/pepa65/tldr-bash-client * https://github.com/pepa65/tldr-bash-client
### Bash client for tldr: community driven man-by-example ### Bash client for tldr: community driven man-by-example

View File

@ -1,6 +1,6 @@
{ {
"name": "tldr-bash-client", "name": "tldr-bash-client",
"version": "0.1a", "version": "0.2",
"description": "Bash client for tldr: community driven man-by-example", "description": "Bash client for tldr: community driven man-by-example",
"global": "true", "global": "true",
"install": "make install", "install": "make install",

68
tldr
View File

@ -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.1a' version='0.2'
# tldr-bash-client version 0.1a # tldr-bash-client version 0.2
# 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
@ -27,7 +27,7 @@ version='0.1a'
# How long before an attempt will be made to re-download a page # How long before an attempt will be made to re-download a page
: "${TLDR_EXPIRY:= 60 }" : "${TLDR_EXPIRY:= 60 }"
# $1: [optional] exit code; Uses: version configdir # $1: [optional] exit code; Uses: version cachedir
Usage(){ Usage(){
Out "$(cat <<-EOF Out "$(cat <<-EOF
$version $version
@ -48,7 +48,7 @@ Usage(){
[$B$YEL-h$DEF,$YEL -?$DEF,$YEL --help$DEF$XB]: This help overview [$B$YEL-h$DEF,$YEL -?$DEF,$YEL --help$DEF$XB]: This help overview
Element styling:$T Title$XT$D Description$XD$E Example$XD$C Code$XC$V Value$XV Element styling:$T Title$XT$D Description$XD$E Example$XD$C Code$XC$V Value$XV
All pages and the index are cached locally under $YEL$configdir$DEF. All pages and the index are cached locally under $YEL$cachedir$DEF.
By default, the cached copies will be re-downloaded after $YEL${TLDR_EXPIRY// /}$DEF days. By default, the cached copies will be re-downloaded after $YEL${TLDR_EXPIRY// /}$DEF days.
EOF EOF
)" )"
@ -148,7 +148,7 @@ Update_index(){
} }
} }
# Initialize globals, check the environment; Uses: config configdir version # Initialize globals, check the environment; Uses: config cachedir version
# Sets: stdout stderr os version dl # Sets: stdout stderr os version dl
Config(){ Config(){
os=common stdout='' stderr='' Q='"' N=$'\n' os=common stdout='' stderr='' Q='"' N=$'\n'
@ -158,7 +158,7 @@ Config(){
SunOS) os='sunos' ;; SunOS) os='sunos' ;;
esac esac
Init_term Init_term
trap 'less -RXQFP"Press Q to exit " <<<"$stdout$stderr"' EXIT trap 'less -~RXQFP"Press Q to exit " <<<"$stdout$stderr"' EXIT
version="$GRE$B tldr-bash-client version $version$XB $YEL http://github.com/pepa65/tldr-bash-client$DEF" version="$GRE$B tldr-bash-client version $version$XB $YEL http://github.com/pepa65/tldr-bash-client$DEF"
@ -174,9 +174,9 @@ Config(){
zip_url='http://tldr-pages.github.io/assets/tldr.zip' zip_url='http://tldr-pages.github.io/assets/tldr.zip'
index_url='http://tldr-pages.github.io/assets/index.json' index_url='http://tldr-pages.github.io/assets/index.json'
[[ -d ~/.config ]] && configdir=~/.config/tldr || configdir=~/.tldr [[ -d ~/.config ]] && cachedir=~/.config/tldr || cachedir=~/.tldr
[[ -d "$configdir" ]] || mkdir -p "$configdir" [[ -d "$cachedir" ]] || mkdir -p "$cachedir"
index=$configdir/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" || Update_index [[ -f $index ]] && Recent "$index" || Update_index
} }
@ -187,7 +187,7 @@ Unlinted(){
exit 4 exit 4
} }
# $1: page; Uses: index index_url configdir base_url platform os dl cached md # $1: page; Uses: index index_url cachedir base_url platform os dl cached md
# Sets: cached md # Sets: cached md
Get_tldr(){ Get_tldr(){
local desc err notfound local desc err notfound
@ -220,7 +220,7 @@ Get_tldr(){
((err)) && Err "tldr page $I$1$XI not found in $notfound, page from platform $U${md%/*}$XU instead" ((err)) && Err "tldr page $I$1$XI not found in $notfound, page 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=$configdir/$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"
@ -283,7 +283,7 @@ Display_tldr(){
Out "$ENL$EPS$E$line$XE" ;; Out "$ENL$EPS$E$line$XE" ;;
esac esac
done done
trap 'less +Gg -RXQFP"%pB\% tldr $I$page$XI - press Q to exit" <<<"$stdout$stderr"' EXIT trap 'less +Gg -~RXQFP"%pB\% tldr $I$page$XI - press Q to exit" <<<"$stdout$stderr"' EXIT
} }
# $1: exit code; Uses: platform index # $1: exit code; Uses: platform index
@ -291,12 +291,12 @@ List_pages(){
local platformtext c1 c2 c3 local platformtext c1 c2 c3
[[ $platform ]] && platformtext=" from platform $I$platform$XI" [[ $platform ]] && platformtext=" from platform $I$platform$XI"
Out "${GRE}Known tldr pages$platformtext:" Out "${GRE}Known tldr pages$platformtext:"
Out "$(while read -r c1 c2 c3; do printf "%-19s %-19s %-19s %-19s$N" "$c1" "$c2" "$c3"; done \ 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))" <<<$(tr '{' '\n' <$index |grep "$platform" |cut -d "$Q" -f4))"
exit "$1" exit "$1"
} }
# $1: exit code; Uses: dl configdir zip_url # $1: exit code; Uses: dl cachedir zip_url
Cache_fill(){ Cache_fill(){
local tmp unzip local tmp unzip
tmp=$(mktemp -d) tmp=$(mktemp -d)
@ -311,16 +311,19 @@ Cache_fill(){
exit 7 exit 7
} }
$unzip "$tmp/pages.zip" -d "$tmp" 'pages/*' $unzip "$tmp/pages.zip" -d "$tmp" 'pages/*'
rm -rf -- "${configdir:?}/"* rm -rf -- "${cachedir:?}/"*
mv -- "$tmp/pages/"* "${configdir:?}/" mv -- "$tmp/pages/"* "${cachedir:?}/"
rm -rf -- "$tmp" rm -rf -- "$tmp"
Out "${GRE}Pages cached in $U$configdir$XU$DEF" Out "${GRE}Pages cached in $U$cachedir$XU$DEF"
exit "$1" exit "$1"
} }
Config # $@: commandline parameters; Uses: version cached; Sets: platform page
markdown=0 err=0 arg=$1 Main(){
case "$arg" in local markdown err
Config
markdown=0 err=0
case "$1" in
-l|--list) [[ $2 ]] && { -l|--list) [[ $2 ]] && {
platform=$2 platform=$2
[[ ,common,linux,osx,sunos, = *,$platform,* ]] || { [[ ,common,linux,osx,sunos, = *,$platform,* ]] || {
@ -354,19 +357,22 @@ case "$arg" in
Usage "$err" ;; Usage "$err" ;;
-*) Err "Unrecognized option $I$1$XI"; Usage 19 ;; -*) Err "Unrecognized option $I$1$XI"; Usage 19 ;;
*) page=$* ;; *) page=$* ;;
esac esac
[[ -z $page ]] && Err "No command specified" && Usage 20 [[ -z $page ]] && Err "No command specified" && Usage 20
[[ $page =~ ' -' || ${page:0:1} = '-' ]] && Err "Only one option allowed" && Usage 21 [[ $page =~ ' -' || ${page:0:1} = '-' ]] && Err "Only one option allowed" && Usage 21
[[ $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 22
}
Get_tldr "${page// /-}"
[[ ! -s $cached ]] && Err "tldr page for command $I$page$XI not found" && exit 23
((markdown)) && Out "$(cat "$cached")" || Display_tldr <"$cached"
} }
Get_tldr "${page// /-}" Main "$@"
[[ ! -s $cached ]] && Err "tldr page for command $I$page$XI not found" && exit 23
((markdown)) && Out "$(cat "$cached")" || Display_tldr <"$cached"
# The error trap will output the accumulated stdout and stderr # The error trap will output the accumulated stdout and stderr
exit 0 exit 0