Some muting of shellcheck results

This commit is contained in:
pepa65 2017-02-23 00:28:41 +07:00
parent ec2877da19
commit 77308bd8b1
1 changed files with 27 additions and 27 deletions

54
tldr
View File

@ -13,7 +13,7 @@ VERSION='0.1a'
# Colors: Black, Red, Green, Yellow, Blue, Magenta, Cyan, White # Colors: Black, Red, Green, Yellow, Blue, Magenta, Cyan, White
# BG: BlackBG, RedBG, GreenBG, YellowBG, BlueBG, MagentaBG, CyanBG, WhiteBG # BG: BlackBG, RedBG, GreenBG, YellowBG, BlueBG, MagentaBG, CyanBG, WhiteBG
# Modes: Bold, Underline, Italic, Inverse # Modes: Bold, Underline, Italic, Inverse
# 'Newline' can be added to the Style list to add a newline before the element # 'Newline' can be added to the style list to add a newline before the element
# and 'Space' to add a space at the start of the line (not for Value element) # and 'Space' to add a space at the start of the line (not for Value element)
# (The style items are separated by space, lower or uppercase mixed allowed.) # (The style items are separated by space, lower or uppercase mixed allowed.)
: ${TLDR_TITLE_STYLE:= Newline Space Bold Yellow } : ${TLDR_TITLE_STYLE:= Newline Space Bold Yellow }
@ -22,13 +22,13 @@ VERSION='0.1a'
: ${TLDR_CODE_STYLE:= Space Bold Blue } : ${TLDR_CODE_STYLE:= Space Bold Blue }
: ${TLDR_VALUE_STYLE:= Space Bold Cyan } : ${TLDR_VALUE_STYLE:= Space Bold Cyan }
# Color and/or background (Newline and Space also allowed) for error messages # Color and/or background (Newline and Space also allowed) for error messages
: ${TLDR_ERROR_COLOR= Space Red } : ${TLDR_ERROR_COLOR:= Space Red }
# 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 }
Usage(){ # $1: optional exit code Usage(){ # $1: optional exit code
SELF=$(basename $0) SELF=$(basename "$0")
local exit=${1:-0} local exit=${1:-0}
Out "$(cat <<-EOF Out "$(cat <<-EOF
$version $version
@ -53,7 +53,7 @@ Usage(){ # $1: optional exit code
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
)" )"
exit $exit exit "$exit"
} }
Err(){ STDERR+=$ERRNL$ERRSP$ERR$B$1$XB$XERR$N;} # $1: keep error messages Err(){ STDERR+=$ERRNL$ERRSP$ERR$B$1$XB$XERR$N;} # $1: keep error messages
@ -62,7 +62,7 @@ Out(){ STDOUT+=$1$N;} # $1: keep output
Style(){ # $1: Style specification Style(){ # $1: Style specification
local style local style
STYLES= XSTYLES= COLOR= XCOLOR= NL= SP= STYLES='' XSTYLES='' COLOR='' XCOLOR='' NL='' SP=''
for style in $1 for style in $1
do do
[[ ${style,,} = newline ]] && NL=$N [[ ${style,,} = newline ]] && NL=$N
@ -134,7 +134,7 @@ Init_term(){
ERR=$COLOR XERR=$XCOLOR ERRNL=$NL ERRSP=$SP ERR=$COLOR XERR=$XCOLOR ERRNL=$NL ERRSP=$SP
} }
Recent(){ find "$1" -mtime -${TLDR_EXPIRY// /} &>/dev/null;} # $1: page Recent(){ find "$1" -mtime -"${TLDR_EXPIRY// /}" &>/dev/null;} # $1: page
Update_index(){ # Download index.json Update_index(){ # Download index.json
$DL "$index" "$index_url" && Out "${GRE}Index file $I$index$XI re-downloaded$DEF" || { $DL "$index" "$index_url" && Out "${GRE}Index file $I$index$XI re-downloaded$DEF" || {
@ -144,8 +144,8 @@ Update_index(){ # Download index.json
} }
Config(){ # Initialize globals, check the environment Config(){ # Initialize globals, check the environment
PLATFORM=common STDOUT= STDERR= Q='"' N=$'\n' PLATFORM=common STDOUT='' STDERR='' Q='"' N=$'\n'
case $(uname -s) in case "$(uname -s)" in
Darwin) PLATFORM='osx' ;; Darwin) PLATFORM='osx' ;;
Linux) PLATFORM='linux' ;; Linux) PLATFORM='linux' ;;
SunOS) PLATFORM='sunos' ;; SunOS) PLATFORM='sunos' ;;
@ -198,14 +198,14 @@ Get_tldr(){ # $1: page
} }
fi fi
# if no page found yet, try the system platform # if no page found yet, try the system platform
[[ $PAGE ]] || [[ $platform = $PLATFORM ]] || { [[ $PAGE ]] || [[ $platform = "$PLATFORM" ]] || {
[[ $desc =~ \"$PLATFORM\" ]] && PAGE=$PLATFORM/$1.md [[ $desc =~ \"$PLATFORM\" ]] && PAGE=$PLATFORM/$1.md
} || { } || {
notfound+=" or $I$PLATFORM$XI" notfound+=" or $I$PLATFORM$XI"
err=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
[[ $PAGE ]] || PAGE=$(cut -d $Q -f 8 <<<"$desc")/"$1.md" [[ $PAGE ]] || PAGE=$(cut -d "$Q" -f 8 <<<"$desc")/"$1.md"
((err)) && Err "tldr page $I$1$XI not found in $notfound, page from platform $U${PAGE%/*}$XU instead" ((err)) && Err "tldr page $I$1$XI not found in $notfound, page from platform $U${PAGE%/*}$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
@ -236,7 +236,7 @@ Display_tldr(){
read -r; ((++L)) read -r; ((++L))
} }
} }
case ${REPLY:0:1} in # first character case "${REPLY:0:1}" in # first character
'#') ((newfmt)) && Unlinted "Bad first character" '#') ((newfmt)) && Unlinted "Bad first character"
((${#REPLY} <= 2)) && Unlinted "No title" ((${#REPLY} <= 2)) && Unlinted "No title"
[[ ! ${REPLY:1:1} = ' ' ]] && Unlinted "2nd character no space" [[ ! ${REPLY:1:1} = ' ' ]] && Unlinted "2nd character no space"
@ -245,7 +245,7 @@ Display_tldr(){
[[ ! ${REPLY:1:1} = ' ' ]] && Unlinted "2nd character no space" [[ ! ${REPLY:1:1} = ' ' ]] && Unlinted "2nd character no space"
[[ ! ${REPLY: -1} = '.' ]] && Unlinted "Description doesn't end in full stop" [[ ! ${REPLY: -1} = '.' ]] && Unlinted "Description doesn't end in full stop"
Out "$DNL$DSP$D${REPLY:2}$XD" Out "$DNL$DSP$D${REPLY:2}$XD"
DNL= ;; DNL='' ;;
'-') ((newfmt)) && Unlinted "Bad first character" '-') ((newfmt)) && Unlinted "Bad first character"
((${#REPLY} <= 2)) && Unlinted "No example content" ((${#REPLY} <= 2)) && Unlinted "No example content"
[[ ! ${REPLY:1:1} = ' ' ]] && Unlinted "2nd character no space" [[ ! ${REPLY:1:1} = ' ' ]] && Unlinted "2nd character no space"
@ -279,9 +279,9 @@ List_pages(){ # $1: exit code
local platformtext local platformtext
[[ $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 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"
} }
Cache_fill(){ # $1: exit code Cache_fill(){ # $1: exit code
@ -297,11 +297,11 @@ Cache_fill(){ # $1: exit code
exit 7 exit 7
} }
$unzip "$tmp/pages.zip" -d "$tmp" 'pages/*' $unzip "$tmp/pages.zip" -d "$tmp" 'pages/*'
rm -rf -- "$configdir/"* rm -rf -- "${configdir:?}/"*
mv -- "$tmp/pages/"* "$configdir/" mv -- "$tmp/pages/"* "${configdir:?}/"
rm -rf -- "$tmp" rm -rf -- "$tmp"
Out "${GRE}Pages cached in $U$configdir$XU$DEF" Out "${GRE}Pages cached in $U$configdir$XU$DEF"
exit $1 exit "$1"
} }
Config Config
@ -316,31 +316,31 @@ case "$arg" in
} }
[[ $3 ]] && Err "No more command line arguments allowed" && err=9 [[ $3 ]] && Err "No more command line arguments allowed" && err=9
} }
List_pages $err ;; List_pages "$err" ;;
-c|--cache) [[ $2 ]] && Err "No more command line arguments allowed" && err=10 -c|--cache) [[ $2 ]] && Err "No more command line arguments allowed" && err=10
Cache_fill $err ;; Cache_fill "$err" ;;
-v|--version) [[ $2 ]] && Err "No more command line arguments allowed" && err=11 -v|--version) [[ $2 ]] && Err "No more command line arguments allowed" && err=11
Out "$version" Out "$version"
exit $err ;; exit "$err" ;;
-u|--update) [[ $2 ]] && Err "No more command line arguments allowed" && err=12 -u|--update) [[ $2 ]] && Err "No more command line arguments allowed" && err=12
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 13
[[ $3 ]] && Err "No more command line arguments allowed" && err=14 [[ $3 ]] && Err "No more command line arguments allowed" && err=14
[[ -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 15
} || Err "No file:$I $2$XI" && exit 16 ;; } || Err "No file:$I $2$XI" && exit 16 ;;
-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 17
[[ -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 "No more command line arguments allowed" && err=18
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
@ -351,7 +351,7 @@ esac
Usage 22 Usage 22
} }
Get_tldr ${page// /-} Get_tldr "${page// /-}"
[[ ! -s $CACHED ]] && Err "tldr page for command $I$page$XI not found" && exit 23 [[ ! -s $CACHED ]] && Err "tldr page for command $I$page$XI not found" && exit 23
((markdown)) && Out "$(cat "$CACHED")" || Display_tldr <"$CACHED" ((markdown)) && Out "$(cat "$CACHED")" || Display_tldr <"$CACHED"