2017-07-16 20:24:12 +02:00
|
|
|
#!/usr/bin/env bash
|
2017-03-13 06:11:38 +01:00
|
|
|
set +vx -o pipefail
|
2017-02-07 14:56:42 +01:00
|
|
|
[[ $- = *i* ]] && echo "Don't source this script!" && return 1
|
2017-12-05 14:29:51 +01:00
|
|
|
version='0.39'
|
|
|
|
# tldr-bash-client version 0.39
|
2017-02-22 17:06:21 +01:00
|
|
|
# Bash client for tldr: community driven man-by-example
|
2017-09-22 06:19:52 +02:00
|
|
|
# - forked from Ray Lee, https://github.com/raylee/tldr
|
2018-06-05 16:03:55 +02:00
|
|
|
# - modified and expanded by pepa65: https://gitlab.com/pepa65/tldr-bash-client
|
2017-10-15 18:37:18 +02:00
|
|
|
# - binary download: http://4e4.win/tldr
|
2017-08-09 18:36:45 +02:00
|
|
|
# Requiring: coreutils, grep, unzip, curl/wget, less (optional)
|
2017-02-05 18:00:42 +01:00
|
|
|
|
|
|
|
# The 5 elements in TLDR markup that can be styled with these colors and
|
|
|
|
# backgrounds (last one specified will be used) and modes (more can apply):
|
|
|
|
# Colors: Black, Red, Green, Yellow, Blue, Magenta, Cyan, White
|
|
|
|
# BG: BlackBG, RedBG, GreenBG, YellowBG, BlueBG, MagentaBG, CyanBG, WhiteBG
|
|
|
|
# Modes: Bold, Underline, Italic, Inverse
|
2017-02-22 18:28:41 +01:00
|
|
|
# 'Newline' can be added to the style list to add a newline before the element
|
2017-03-12 20:34:04 +01:00
|
|
|
# and 'Space' to add a space at the start of the line
|
|
|
|
# (style items are separated by space, lower/uppercase mixed allowed)
|
2017-02-23 09:53:37 +01:00
|
|
|
: "${TLDR_TITLE_STYLE:= Newline Space Bold Yellow }"
|
|
|
|
: "${TLDR_DESCRIPTION_STYLE:= Space Yellow }"
|
|
|
|
: "${TLDR_EXAMPLE_STYLE:= Newline Space Bold Green }"
|
|
|
|
: "${TLDR_CODE_STYLE:= Space Bold Blue }"
|
2017-03-12 20:34:04 +01:00
|
|
|
: "${TLDR_VALUE_ISTYLE:= Space Bold Cyan }"
|
|
|
|
# The Value style (above) is an Inline style: doesn't take Newline or Space
|
|
|
|
# Inline styles for help text: default, URL, option, platform, command, header
|
|
|
|
: "${TLDR_DEFAULT_ISTYLE:= White }"
|
|
|
|
: "${TLDR_URL_ISTYLE:= Yellow }"
|
|
|
|
: "${TLDR_HEADER_ISTYLE:= Bold }"
|
|
|
|
: "${TLDR_OPTION_ISTYLE:= Bold Yellow }"
|
|
|
|
: "${TLDR_PLATFORM_ISTYLE:= Bold Blue }"
|
|
|
|
: "${TLDR_COMMAND_ISTYLE:= Bold Cyan }"
|
|
|
|
: "${TLDR_FILE_ISTYLE:= Bold Magenta }"
|
|
|
|
# Color/BG (Newline and Space also allowed) for error and info messages
|
2017-02-23 09:53:37 +01:00
|
|
|
: "${TLDR_ERROR_COLOR:= Newline Space Red }"
|
2017-03-12 20:34:04 +01:00
|
|
|
: "${TLDR_INFO_COLOR:= Newline Space Green }"
|
2017-02-05 18:00:42 +01:00
|
|
|
|
2017-03-12 20:34:04 +01:00
|
|
|
# How many days before freshly downloading a potentially stale page
|
2017-02-23 09:53:37 +01:00
|
|
|
: "${TLDR_EXPIRY:= 60 }"
|
2017-02-05 18:00:42 +01:00
|
|
|
|
2017-03-12 20:34:04 +01:00
|
|
|
# Alternative location of pages cache
|
|
|
|
: "${TLDR_CACHE:= }"
|
|
|
|
|
2017-08-09 18:36:45 +02:00
|
|
|
# Usage of 'less' or 'cat' for output (set to '0' for cat)
|
|
|
|
: "${TLDR_LESS:= }"
|
|
|
|
|
|
|
|
## Function definitions
|
|
|
|
|
2017-02-23 10:17:43 +01:00
|
|
|
# $1: [optional] exit code; Uses: version cachedir
|
2017-02-23 09:53:37 +01:00
|
|
|
Usage(){
|
2017-02-05 18:00:42 +01:00
|
|
|
Out "$(cat <<-EOF
|
2017-11-29 14:50:58 +01:00
|
|
|
$E$version
|
2017-02-05 18:00:42 +01:00
|
|
|
|
2017-11-27 13:31:27 +01:00
|
|
|
${HDE}USAGE: $HHE$(basename "$0")$XHHE [${HOP}option$XHOP] [${HPL}platform$XHPL/]${HCO}command$XHCO
|
2017-02-05 18:00:42 +01:00
|
|
|
|
2017-08-07 07:40:04 +02:00
|
|
|
$HDE[${HPL}platform$XHPL/]${HCO}command$XHCO: Show page for ${HCO}command$XHCO (from ${HPL}platform$XHPL)
|
2017-02-05 18:00:42 +01:00
|
|
|
|
2017-12-05 14:29:51 +01:00
|
|
|
${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)
|
2017-02-05 18:00:42 +01:00
|
|
|
|
2017-08-07 07:40:04 +02:00
|
|
|
${HOP}option$XHOP is optionally one of:
|
2017-11-29 14:50:58 +01:00
|
|
|
$HOP-s$XHOP, $HOP--search$XHOP ${HFI}regex$XHFI: Search for ${HFI}regex$XHFI in all tldr pages
|
2017-08-07 07:40:04 +02:00
|
|
|
$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
|
2017-11-29 14:50:58 +01:00
|
|
|
$HOP-r$XHOP, $HOP--render$XHOP ${HFI}file$XHFI: Render ${HFI}file$XHFI as tldr markdown
|
2017-08-07 07:40:04 +02:00
|
|
|
$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
|
2018-06-05 16:03:55 +02:00
|
|
|
$HOP-v$XHOP, $HOP--version$XHOP: Version number and gitlab repo location
|
2017-08-07 07:40:04 +02:00
|
|
|
$HDE[$HOP-h$XHOP, $HOP-?$XHOP, $HOP--help$XHOP]: This help overview
|
2017-02-05 18:00:42 +01:00
|
|
|
|
2017-08-07 07:40:04 +02:00
|
|
|
${HDE}Element styling:$XHDE ${T}Title$XT ${D}Description$XD ${E}Example$XE ${C}Code$XC ${V}Value$XV
|
|
|
|
${HDE}All pages and the index are cached locally under $HUR$cachedir$XHUR.
|
|
|
|
${HDE}By default, the cached copies will be freshly downloaded after $HUR${TLDR_EXPIRY// /}$XHUR days.
|
2017-02-05 18:00:42 +01:00
|
|
|
EOF
|
|
|
|
)"
|
2017-02-23 09:53:37 +01:00
|
|
|
exit "${1:-0}"
|
2016-01-04 06:33:47 +01:00
|
|
|
}
|
|
|
|
|
2017-02-23 09:53:37 +01:00
|
|
|
# $1: keep output; Uses/Sets: stdout
|
|
|
|
Out(){ stdout+=$1$N;}
|
2017-02-05 18:00:42 +01:00
|
|
|
|
2017-03-12 20:34:04 +01:00
|
|
|
# $1: keep error messages
|
|
|
|
Err(){ Out "$ERRNL$ERRSP$ERR$B$1$XB$XERR";}
|
|
|
|
|
|
|
|
# $1: keep info messages
|
|
|
|
Inf(){ Out "$INFNL$INFSP$INF$B$1$XB$XINF";}
|
|
|
|
|
2017-02-23 09:53:37 +01:00
|
|
|
# $1: Style specification; Uses: color xcolor bg xbg mode xmode
|
|
|
|
Style(){
|
|
|
|
local -l style
|
2017-02-22 18:28:41 +01:00
|
|
|
STYLES='' XSTYLES='' COLOR='' XCOLOR='' NL='' SP=''
|
2017-02-06 03:05:12 +01:00
|
|
|
for style in $1
|
2017-02-05 18:00:42 +01:00
|
|
|
do
|
2017-02-23 09:53:37 +01:00
|
|
|
[[ $style = newline ]] && NL=$N
|
|
|
|
[[ $style = space ]] && SP=' '
|
|
|
|
COLOR+=${color[$style]:-}${bg[$style]:-}
|
|
|
|
XCOLOR=${xbg[$style]:-}${xcolor[$style]:-}$XCOLOR
|
|
|
|
STYLES+=${color[$style]:-}${bg[$style]:-}${mode[$style]:-}
|
|
|
|
XSTYLES=${xmode[$style]:-}${xbg[$style]:-}${xcolor[$style]:-}$XSTYLES
|
2017-02-05 18:00:42 +01:00
|
|
|
done
|
|
|
|
}
|
|
|
|
|
2017-02-23 09:53:37 +01:00
|
|
|
# Sets: color xcolor bg xbg mode xmode
|
2017-02-05 18:00:42 +01:00
|
|
|
Init_term(){
|
|
|
|
[[ -t 2 ]] && { # only if interactive session (stderr open)
|
|
|
|
B=$'\e[1m' # $(tput bold || tput md) # Start bold
|
2017-02-22 11:59:51 +01:00
|
|
|
XB=$'\e[0m' # End bold (no tput code...)
|
2017-02-05 18:00:42 +01:00
|
|
|
U=$'\e[4m' # $(tput smul || tput us) # Start underline
|
|
|
|
XU=$'\e[24m' # $(tput rmul || tput ue) # End underline
|
|
|
|
I=$'\e[3m' # $(tput sitm || tput ZH) # Start italic
|
|
|
|
XI=$'\e[23m' # $(tput ritm || tput ZR) # End italic
|
|
|
|
R=$'\e[7m' # $(tput smso || tput so) # Start reverse
|
|
|
|
XR=$'\e[27m' # $(tput rmso || tput se) # End reverse
|
2017-02-23 09:53:37 +01:00
|
|
|
#X=$'\e[0m' # $(tput sgr0 || tput me) # End all
|
2017-02-05 18:00:42 +01:00
|
|
|
|
|
|
|
[[ $TERM != *-m ]] && {
|
|
|
|
BLA=$'\e[30m' # $(tput setaf 0 || tput AF 0)
|
|
|
|
RED=$'\e[31m' # $(tput setaf 1 || tput AF 1)
|
|
|
|
GRE=$'\e[32m' # $(tput setaf 2 || tput AF 2)
|
|
|
|
YEL=$'\e[33m' # $(tput setaf 3 || tput AF 3)
|
|
|
|
BLU=$'\e[34m' # $(tput setaf 4 || tput AF 4)
|
|
|
|
MAG=$'\e[35m' # $(tput setaf 5 || tput AF 5)
|
|
|
|
CYA=$'\e[36m' # $(tput setaf 6 || tput AF 6)
|
|
|
|
WHI=$'\e[37m' # $(tput setaf 7 || tput AF 7)
|
|
|
|
DEF=$'\e[39m' # $(tput op)
|
|
|
|
BLAB=$'\e[40m' # $(tput setab 0 || tput AB 0)
|
|
|
|
REDB=$'\e[41m' # $(tput setab 1 || tput AB 1)
|
|
|
|
GREB=$'\e[42m' # $(tput setab 2 || tput AB 2)
|
|
|
|
YELB=$'\e[43m' # $(tput setab 3 || tput AB 3)
|
|
|
|
BLUB=$'\e[44m' # $(tput setab 4 || tput AB 4)
|
|
|
|
MAGB=$'\e[45m' # $(tput setab 5 || tput AB 5)
|
|
|
|
CYAB=$'\e[46m' # $(tput setab 6 || tput AB 6)
|
|
|
|
WHIB=$'\e[47m' # $(tput setab 7 || tput AB 7)
|
|
|
|
DEFB=$'\e[49m' # $(tput op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
declare -A color=(['black']=$BLA ['red']=$RED ['green']=$GRE ['yellow']=$YEL \
|
|
|
|
['blue']=$BLU ['magenta']=$MAG ['cyan']=$CYA ['white']=$WHI)
|
|
|
|
declare -A xcolor=(['black']=$DEF ['red']=$DEF ['green']=$DEF ['yellow']=$DEF \
|
|
|
|
['blue']=$DEF ['magenta']=$DEF ['cyan']=$DEF ['white']=$DEF)
|
|
|
|
declare -A bg=(['blackbg']=$BLAB ['redbg']=$REDB ['greenbg']=$GREB ['yellowbg']=$YELB \
|
|
|
|
['bluebg']=$BLUB ['magentabg']=$MAGB ['cyanbg']=$CYAB ['whitebg']=$WHIB)
|
|
|
|
declare -A xbg=(['blackbg']=$DEFB ['redbg']=$DEFB ['greenbg']=$DEFB ['yellowbg']=$DEFB \
|
|
|
|
['bluebg']=$DEFB ['magentabg']=$DEFB ['cyanbg']=$DEFB ['whitebg']=$DEFB)
|
|
|
|
declare -A mode=(['bold']=$B ['underline']=$U ['italic']=$I ['inverse']=$R)
|
|
|
|
declare -A xmode=(['bold']=$XB ['underline']=$XU ['italic']=$XI ['inverse']=$XR)
|
|
|
|
|
2017-02-22 11:59:51 +01:00
|
|
|
# the 5 main tldr page styles and error message colors
|
2017-02-05 18:00:42 +01:00
|
|
|
Style "$TLDR_TITLE_STYLE"
|
|
|
|
T=$STYLES XT=$XSTYLES TNL=$NL TSP=$SP
|
|
|
|
Style "$TLDR_DESCRIPTION_STYLE"
|
|
|
|
D=$STYLES XD=$XSTYLES DNL=$NL DSP=$SP
|
|
|
|
Style "$TLDR_EXAMPLE_STYLE"
|
|
|
|
E=$STYLES XE=$XSTYLES ENL=$NL ESP=$SP
|
|
|
|
Style "$TLDR_CODE_STYLE"
|
|
|
|
C=$STYLES XC=$XSTYLES CNL=$NL CSP=$SP
|
2017-03-12 20:34:04 +01:00
|
|
|
Style "$TLDR_VALUE_ISTYLE"
|
2017-02-05 18:00:42 +01:00
|
|
|
V=$STYLES XV=$XSTYLES
|
2017-03-12 20:34:04 +01:00
|
|
|
Style "$TLDR_DEFAULT_ISTYLE"
|
|
|
|
HDE=$STYLES XHDE=$XSTYLES
|
|
|
|
Style "$TLDR_URL_ISTYLE"
|
|
|
|
URL=$STYLES XURL=$XSTYLES
|
|
|
|
HUR=$XHDE$STYLES XHUR=$XSTYLES$HDE
|
|
|
|
Style "$TLDR_OPTION_ISTYLE"
|
|
|
|
HOP=$XHDE$STYLES XHOP=$XSTYLES$HDE
|
|
|
|
Style "$TLDR_PLATFORM_ISTYLE"
|
|
|
|
HPL=$XHDE$STYLES XHPL=$XSTYLES$HDE
|
|
|
|
Style "$TLDR_COMMAND_ISTYLE"
|
|
|
|
HCO=$XHDE$STYLES XHCO=$XSTYLES$HDE
|
|
|
|
Style "$TLDR_FILE_ISTYLE"
|
|
|
|
HFI=$XHDE$STYLES XHFI=$XSTYLES$HDE
|
|
|
|
Style "$TLDR_HEADER_ISTYLE"
|
|
|
|
HHE=$XHDE$STYLES XHHE=$XSTYLES$HDE
|
2017-02-05 18:00:42 +01:00
|
|
|
Style "$TLDR_ERROR_COLOR"
|
|
|
|
ERR=$COLOR XERR=$XCOLOR ERRNL=$NL ERRSP=$SP
|
2017-03-12 20:34:04 +01:00
|
|
|
Style "$TLDR_INFO_COLOR"
|
|
|
|
INF=$COLOR XINF=$XCOLOR INFNL=$NL INFSP=$SP
|
2016-01-04 06:33:47 +01:00
|
|
|
}
|
|
|
|
|
2017-02-23 09:53:37 +01:00
|
|
|
# $1: page
|
|
|
|
Recent(){ find "$1" -mtime -"${TLDR_EXPIRY// /}" >/dev/null 2>&1;}
|
2016-01-04 06:33:47 +01:00
|
|
|
|
2017-02-23 10:17:43 +01:00
|
|
|
# Initialize globals, check the environment; Uses: config cachedir version
|
2017-03-12 20:34:04 +01:00
|
|
|
# Sets: stdout os version dl
|
2017-02-23 09:53:37 +01:00
|
|
|
Config(){
|
2017-08-09 18:56:23 +02:00
|
|
|
type -p less >/dev/null || TLDR_LESS=0
|
2017-08-09 18:36:45 +02:00
|
|
|
|
2017-03-12 20:34:04 +01:00
|
|
|
os=common stdout='' Q='"' N=$'\n'
|
2017-02-22 18:28:41 +01:00
|
|
|
case "$(uname -s)" in
|
2017-02-23 09:53:37 +01:00
|
|
|
Darwin) os='osx' ;;
|
|
|
|
Linux) os='linux' ;;
|
|
|
|
SunOS) os='sunos' ;;
|
2017-12-05 13:54:51 +01:00
|
|
|
CYGWIN*) os='windows' ;;
|
|
|
|
MINGW*) os='windows' ;;
|
2017-02-05 18:00:42 +01:00
|
|
|
esac
|
|
|
|
Init_term
|
2017-08-09 18:36:45 +02:00
|
|
|
[[ $TLDR_LESS = 0 ]] &&
|
|
|
|
trap 'cat <<<"$stdout"' EXIT ||
|
|
|
|
trap 'less -~RXQFP"Browse up/down, press Q to exit " <<<"$stdout"' EXIT
|
2017-02-05 18:00:42 +01:00
|
|
|
|
2018-06-05 16:03:55 +02:00
|
|
|
version="tldr-bash-client version $version$XB ${URL}http://gitlab.com/pepa65/tldr-bash-client$XURL"
|
2017-02-07 14:56:42 +01:00
|
|
|
|
2017-02-05 18:00:42 +01:00
|
|
|
# Select download method
|
2017-05-29 03:54:24 +02:00
|
|
|
dl="$(type -p curl) -sLfo" || {
|
|
|
|
dl="$(type -p wget) --max-redirect=20 -qNO" || {
|
2017-08-07 07:40:04 +02:00
|
|
|
Err "tldr requires ${I}curl$XI or ${I}wget$XI installed in your path"
|
2017-02-05 18:00:42 +01:00
|
|
|
exit 3
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 06:19:52 +02:00
|
|
|
pages_url='https://raw.githubusercontent.com/tldr-pages/tldr/master/pages'
|
|
|
|
zip_url='http://tldr.sh/assets/tldr.zip'
|
2017-02-05 18:00:42 +01:00
|
|
|
|
2017-11-29 04:33:41 +01:00
|
|
|
cachedir=$(echo $TLDR_CACHE)
|
|
|
|
if [[ -z $cachedir ]]
|
|
|
|
then
|
|
|
|
[[ $XDG_DATA_HOME ]] && cachedir=$XDG_DATA_HOME/tldr ||
|
|
|
|
cachedir=$HOME/.local/share/tldr
|
|
|
|
fi
|
2017-03-12 20:34:04 +01:00
|
|
|
[[ -d "$cachedir" ]] || mkdir -p "$cachedir" || {
|
|
|
|
Err "Can't create the pages cache location $cachedir"
|
|
|
|
exit 4
|
|
|
|
}
|
2017-02-23 10:17:43 +01:00
|
|
|
index=$cachedir/index.json
|
2017-02-06 20:16:54 +01:00
|
|
|
# update if the file doesn't exists, or if it's older than $TLDR_EXPIRY
|
2017-12-08 17:12:31 +01:00
|
|
|
[[ -f $index ]] && Recent "$index" || Cache_fill
|
2016-01-04 06:33:47 +01:00
|
|
|
}
|
|
|
|
|
2017-06-04 17:39:01 +02:00
|
|
|
# $1: error message; Uses: md REPLY ln
|
2017-02-23 09:53:37 +01:00
|
|
|
Unlinted(){
|
2017-06-04 17:39:01 +02:00
|
|
|
Err "Page $I$md$XI not properly linted!$N${ERRSP}${ERR}Line $I$ln$XI [$XERR$U$REPLY$XU$ERR]$N$ERRSP$ERR$1"
|
2017-05-29 03:54:24 +02:00
|
|
|
exit 5
|
2016-01-04 06:33:47 +01:00
|
|
|
}
|
|
|
|
|
2017-09-22 06:19:52 +02:00
|
|
|
# $1: page; Uses: index cachedir pages_url platform os dl cached md
|
2017-02-23 09:53:37 +01:00
|
|
|
# Sets: cached md
|
|
|
|
Get_tldr(){
|
2017-11-27 13:31:27 +01:00
|
|
|
local desc err=0 notfound
|
2017-02-05 18:00:42 +01:00
|
|
|
# convert the local platform name to tldr's version
|
|
|
|
# extract the platform key from index.json, return preferred subpath to page
|
2017-08-07 07:40:04 +02:00
|
|
|
desc=$(tr '{' '\n' <"$index" |grep "\"name\":\"$1\"")
|
2017-02-05 18:00:42 +01:00
|
|
|
# results in, eg, "name":"netstat","platform":["linux","osx"]},
|
|
|
|
|
2017-08-07 07:40:04 +02:00
|
|
|
[[ $desc ]] || return # nothing found
|
2017-02-06 20:16:54 +01:00
|
|
|
|
2017-02-07 03:56:49 +01:00
|
|
|
if [[ $platform ]]
|
|
|
|
then # platform given on commandline
|
2017-11-27 13:31:27 +01:00
|
|
|
[[ ! $desc =~ \"$platform\" ]] && notfound=$I$platform$XI && err=1 || md=$platform/$1.md
|
2017-02-07 03:56:49 +01:00
|
|
|
else # check common
|
2017-02-23 09:53:37 +01:00
|
|
|
[[ $desc =~ \"common\" ]] && md=common/$1.md || { # not in common either
|
2017-02-07 03:56:49 +01:00
|
|
|
[[ $notfound ]] && notfound+=" or "
|
|
|
|
notfound+=${I}common$XI
|
2017-02-06 20:16:54 +01:00
|
|
|
}
|
2017-02-07 03:56:49 +01:00
|
|
|
fi
|
2017-02-06 20:16:54 +01:00
|
|
|
# if no page found yet, try the system platform
|
2017-08-07 07:40:04 +02:00
|
|
|
[[ $md ]] || [[ $platform = $os ]] || {
|
2017-02-23 09:53:37 +01:00
|
|
|
[[ $desc =~ \"$os\" ]] && md=$os/$1.md
|
2017-02-07 03:01:32 +01:00
|
|
|
} || {
|
2017-02-23 09:53:37 +01:00
|
|
|
notfound+=" or $I$os$XI"
|
2017-11-27 13:31:27 +01:00
|
|
|
err=1
|
2017-02-06 20:16:54 +01:00
|
|
|
}
|
|
|
|
# if still no page found, get the first entry in index
|
2017-02-23 09:53:37 +01:00
|
|
|
[[ $md ]] || md=$(cut -d "$Q" -f 8 <<<"$desc")/"$1.md"
|
2017-11-27 13:31:27 +01:00
|
|
|
((err)) && Err "tldr page $I$1$XI not found in $notfound, from platform $U${md%/*}$XU instead"
|
2017-02-05 18:00:42 +01:00
|
|
|
|
|
|
|
# return the local cached copy of the tldrpage, or retrieve and cache from github
|
2017-02-23 10:17:43 +01:00
|
|
|
cached=$cachedir/$md
|
2017-02-23 09:53:37 +01:00
|
|
|
Recent "$cached" || {
|
|
|
|
mkdir -p "${cached%/*}"
|
2017-09-22 06:19:52 +02:00
|
|
|
$dl "$cached" "$pages_url/$md" || Err "Could not download page $I$cached$XI with $dl"
|
2017-02-05 18:00:42 +01:00
|
|
|
}
|
2016-01-04 06:33:47 +01:00
|
|
|
}
|
|
|
|
|
2017-11-27 13:31:27 +01:00
|
|
|
# $1: file (optional); Uses: page stdout; Sets: ln REPLY
|
2017-02-05 18:00:42 +01:00
|
|
|
Display_tldr(){
|
2017-02-23 09:53:37 +01:00
|
|
|
local newfmt len val
|
2017-06-04 17:39:01 +02:00
|
|
|
ln=0 REPLY=''
|
2017-03-12 20:34:04 +01:00
|
|
|
[[ $md ]] || md=$1
|
2017-02-23 09:53:37 +01:00
|
|
|
# Read full lines, and process even when no newline at the end
|
2017-06-04 17:39:01 +02:00
|
|
|
while read -r || [[ $REPLY ]]
|
2017-02-05 18:00:42 +01:00
|
|
|
do
|
2017-02-23 09:53:37 +01:00
|
|
|
((++ln))
|
|
|
|
((ln==1)) && {
|
2017-06-04 17:39:01 +02:00
|
|
|
[[ ${REPLY:0:1} = '#' ]] && newfmt=0 || newfmt=1
|
2017-02-06 20:16:54 +01:00
|
|
|
((newfmt)) && {
|
2017-06-04 17:39:01 +02:00
|
|
|
[[ $REPLY ]] || Unlinted "Empty title"
|
|
|
|
Out "$TNL$TSP$T$REPLY$XT"
|
|
|
|
len=${#REPLY} # title length
|
|
|
|
read -r
|
|
|
|
((++ln))
|
|
|
|
[[ $REPLY =~ [^=] ]] && Unlinted "Title underline must be equal signs"
|
|
|
|
((len!=${#REPLY})) && Unlinted "Underline length not equal to title's"
|
|
|
|
read -r
|
|
|
|
((++ln))
|
2017-02-06 20:16:54 +01:00
|
|
|
}
|
|
|
|
}
|
2017-06-04 17:39:01 +02:00
|
|
|
case "${REPLY:0:1}" in # first character
|
2017-02-06 20:16:54 +01:00
|
|
|
'#') ((newfmt)) && Unlinted "Bad first character"
|
2017-06-04 17:39:01 +02:00
|
|
|
((${#REPLY} <= 2)) && Unlinted "No title"
|
|
|
|
[[ ! ${REPLY:1:1} = ' ' ]] && Unlinted "2nd character no space"
|
|
|
|
Out "$TNL$TSP$T${REPLY:2}$XT" ;;
|
|
|
|
'>') ((${#REPLY} <= 3)) && Unlinted "No valid desciption"
|
|
|
|
[[ ! ${REPLY:1:1} = ' ' ]] && Unlinted "2nd character no space"
|
|
|
|
[[ ! ${REPLY: -1} = '.' ]] && Unlinted "Description doesn't end in full stop"
|
|
|
|
Out "$DNL$DSP$D${REPLY:2}$XD"
|
2017-02-22 18:28:41 +01:00
|
|
|
DNL='' ;;
|
2017-02-06 20:16:54 +01:00
|
|
|
'-') ((newfmt)) && Unlinted "Bad first character"
|
2017-06-04 17:39:01 +02:00
|
|
|
((${#REPLY} <= 2)) && Unlinted "No example content"
|
|
|
|
[[ ! ${REPLY:1:1} = ' ' ]] && Unlinted "2nd character no space"
|
|
|
|
Out "$ENL$ESP$E${REPLY:2}$XE" ;;
|
2017-02-06 20:16:54 +01:00
|
|
|
' ') ((newfmt)) || Unlinted "Bad first character"
|
2017-06-04 17:39:01 +02:00
|
|
|
((${#REPLY} <= 4)) && Unlinted "No valid code content"
|
|
|
|
[[ ${REPLY:0:4} = ' ' ]] || Unlinted "No four spaces before code"
|
|
|
|
val=${REPLY:4}
|
2017-02-06 20:16:54 +01:00
|
|
|
# Value: convert {{value}}
|
2017-02-23 09:53:37 +01:00
|
|
|
val=${val//\{\{/$CX$V}
|
|
|
|
val=${val//\}\}/$XV$C}
|
|
|
|
Out "$CNL$CSP$C$val$XC" ;;
|
2017-02-06 20:16:54 +01:00
|
|
|
'`') ((newfmt)) && Unlinted "Bad first character"
|
2017-06-04 17:39:01 +02:00
|
|
|
((${#REPLY} <= 2)) && Unlinted "No valid code content"
|
|
|
|
[[ ! ${REPLY: -1} = '`' ]] && Unlinted "Code doesn't end in backtick"
|
2017-10-04 10:29:38 +02:00
|
|
|
val=${REPLY:1:${#REPLY}-2}
|
2017-02-06 20:16:54 +01:00
|
|
|
# Value: convert {{value}}
|
2017-02-23 09:53:37 +01:00
|
|
|
val=${val//\{\{/$CX$V}
|
|
|
|
val=${val//\}\}/$XV$C}
|
|
|
|
Out "$CNL$CSP$C$val$XC" ;;
|
2017-02-05 18:00:42 +01:00
|
|
|
'') continue ;;
|
2017-02-06 20:16:54 +01:00
|
|
|
*) ((newfmt)) || Unlinted "Bad first character"
|
2017-06-04 17:39:01 +02:00
|
|
|
[[ -z $REPLY ]] && Unlinted "No example content"
|
|
|
|
Out "$ENL$ESP$E$REPLY$XE" ;;
|
2017-02-05 18:00:42 +01:00
|
|
|
esac
|
2017-03-12 20:34:04 +01:00
|
|
|
done <"$1"
|
2017-08-09 18:36:45 +02:00
|
|
|
[[ $TLDR_LESS = 0 ]] &&
|
|
|
|
trap 'cat <<<"$stdout"' EXIT ||
|
2018-01-17 16:20:44 +01:00
|
|
|
trap 'less +Gg -~RXQFP"%pB\% tldr $I$page$XI - browse up/down, press Q to exit" <<<"$stdout"' EXIT
|
2016-01-04 06:33:47 +01:00
|
|
|
}
|
|
|
|
|
2017-02-23 09:53:37 +01:00
|
|
|
# $1: exit code; Uses: platform index
|
|
|
|
List_pages(){
|
|
|
|
local platformtext c1 c2 c3
|
2017-08-07 07:40:04 +02:00
|
|
|
[[ $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:"
|
2017-02-23 10:17:43 +01:00
|
|
|
Out "$(while read -r c1 c2 c3; do printf "%-19s %-19s %-19s %-19s$N" $c1 $c2 $c3; done \
|
2017-08-07 07:40:04 +02:00
|
|
|
<<<$(tr '{' '\n' <"$index" |grep $platform |cut -d "$Q" -f4))"
|
2017-02-22 18:28:41 +01:00
|
|
|
exit "$1"
|
2016-01-04 06:33:47 +01:00
|
|
|
}
|
|
|
|
|
2017-11-29 04:33:41 +01:00
|
|
|
# $1: regex, $2: exit code; Uses: cachedir
|
2017-11-27 13:31:27 +01:00
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2017-02-23 10:17:43 +01:00
|
|
|
# $1: exit code; Uses: dl cachedir zip_url
|
2017-02-23 09:53:37 +01:00
|
|
|
Cache_fill(){
|
|
|
|
local tmp unzip
|
2017-05-29 03:54:24 +02:00
|
|
|
unzip="$(type -p unzip) -q" || {
|
2017-12-08 17:12:31 +01:00
|
|
|
Err "tldr requires ${I}unzip$XI to fill the cache"
|
2017-11-27 13:31:27 +01:00
|
|
|
exit 7
|
2017-02-05 18:00:42 +01:00
|
|
|
}
|
2017-05-29 03:54:24 +02:00
|
|
|
tmp=$(mktemp -d)
|
|
|
|
$dl "$tmp/pages.zip" "$zip_url" || {
|
2017-02-07 16:57:38 +01:00
|
|
|
rm -- "$tmp"
|
2017-05-29 03:54:24 +02:00
|
|
|
Err "Could not download pages archive from $U$zip_url$XU with $dl"
|
2017-11-27 13:31:27 +01:00
|
|
|
exit 8
|
2017-02-05 18:00:42 +01:00
|
|
|
}
|
2017-05-29 03:54:24 +02:00
|
|
|
$unzip "$tmp/pages.zip" -d "$tmp" 'pages/*' || {
|
|
|
|
rm -- "$tmp"
|
|
|
|
Err "Couldn't unzip the cache archive on $tmp/pages.zip"
|
2017-11-27 13:31:27 +01:00
|
|
|
exit 9
|
|
|
|
}
|
2017-02-23 10:17:43 +01:00
|
|
|
rm -rf -- "${cachedir:?}/"*
|
|
|
|
mv -- "$tmp/pages/"* "${cachedir:?}/"
|
2017-02-05 18:00:42 +01:00
|
|
|
rm -rf -- "$tmp"
|
2017-03-12 20:34:04 +01:00
|
|
|
Inf "Pages cached in $U$cachedir$XU"
|
2016-01-04 06:33:47 +01:00
|
|
|
}
|
|
|
|
|
2017-02-23 10:17:43 +01:00
|
|
|
# $@: commandline parameters; Uses: version cached; Sets: platform page
|
|
|
|
Main(){
|
2017-11-27 13:31:27 +01:00
|
|
|
local markdown=0 err=0 nomore='No more command line arguments allowed'
|
2017-02-23 10:17:43 +01:00
|
|
|
Config
|
|
|
|
case "$1" in
|
2017-11-29 02:23:36 +01:00
|
|
|
-s|--search) [[ -z $2 ]] && Err "Search term (regex) needed" && Usage 10
|
2017-11-27 13:31:27 +01:00
|
|
|
[[ $3 ]] && Err "$nomore" && err=11
|
|
|
|
Find_regex "$2" "$err" ;;
|
2017-02-06 04:11:13 +01:00
|
|
|
-l|--list) [[ $2 ]] && {
|
|
|
|
platform=$2
|
2017-12-05 13:54:51 +01:00
|
|
|
[[ ,common,linux,osx,sunos,windows,current, != *,$platform,* ]] &&
|
2017-11-27 13:31:27 +01:00
|
|
|
Err "Unknown platform $I$platform$XI" && Usage 12
|
|
|
|
[[ $3 ]] && Err "$nomore" && err=13
|
2017-02-05 18:00:42 +01:00
|
|
|
}
|
2017-02-22 18:28:41 +01:00
|
|
|
List_pages "$err" ;;
|
2017-11-27 13:31:27 +01:00
|
|
|
-a|--list-all) [[ $2 ]] && Err "$nomore" && err=14
|
2017-08-07 07:40:04 +02:00
|
|
|
platform=current
|
|
|
|
List_pages $err ;;
|
2017-11-27 13:31:27 +01:00
|
|
|
-u|--update) [[ $2 ]] && Err "$nomore" && err=15
|
2017-12-08 17:12:31 +01:00
|
|
|
Cache_fill
|
|
|
|
exit "$err" ;;
|
2017-11-27 13:31:27 +01:00
|
|
|
-v|--version) [[ $2 ]] && Err "$nomore" && err=16
|
2017-03-12 20:34:04 +01:00
|
|
|
Inf "$version"
|
2017-02-22 18:28:41 +01:00
|
|
|
exit "$err" ;;
|
2017-11-27 13:31:27 +01:00
|
|
|
-r|--render) [[ -z $2 ]] && Err "Specify a file to render" && Usage 17
|
|
|
|
[[ $3 ]] && Err "$nomore" && err=18
|
2017-02-06 20:16:54 +01:00
|
|
|
[[ -f "$2" ]] && {
|
2017-03-12 20:34:04 +01:00
|
|
|
Display_tldr "$2" && exit "$err"
|
2017-02-06 20:16:54 +01:00
|
|
|
Err "A file error occured"
|
2017-11-27 13:31:27 +01:00
|
|
|
exit 19
|
|
|
|
} || Err "No file: ${I}$2$XI" && exit 20 ;;
|
2017-02-06 20:16:54 +01:00
|
|
|
-m|--markdown) shift
|
2017-02-22 18:28:41 +01:00
|
|
|
page=$*
|
2017-11-27 13:31:27 +01:00
|
|
|
[[ -z $page ]] && Err "Specify a page to display" && Usage 21
|
2017-02-06 20:16:54 +01:00
|
|
|
[[ -f "$page" && ${page: -3:3} = .md ]] && Out "$(cat "$page")" && exit 0
|
|
|
|
markdown=1 ;;
|
2017-11-27 13:31:27 +01:00
|
|
|
''|-h|-\?|--help) [[ $2 ]] && Err "$nomore" && err=22
|
2017-02-22 18:28:41 +01:00
|
|
|
Usage "$err" ;;
|
2017-11-27 13:31:27 +01:00
|
|
|
-*) Err "Unrecognized option $I$1$XI"; Usage 23 ;;
|
2017-02-22 18:28:41 +01:00
|
|
|
*) page=$* ;;
|
2017-02-23 10:17:43 +01:00
|
|
|
esac
|
|
|
|
|
2017-11-27 13:31:27 +01:00
|
|
|
[[ -z $page ]] && Err "No command specified" && Usage 24
|
|
|
|
[[ ${page:0:1} = '-' || $page = *' '-* ]] && Err "Only one option allowed" && Usage 25
|
2017-02-23 10:17:43 +01:00
|
|
|
[[ $page = */* ]] && platform=${page%/*} && page=${page##*/}
|
2017-12-05 13:54:51 +01:00
|
|
|
[[ $platform && ,common,linux,osx,sunos,windows, != *,$platform,* ]] && {
|
2017-02-23 10:17:43 +01:00
|
|
|
Err "Unknown platform $I$platform$XI"
|
2017-11-27 13:31:27 +01:00
|
|
|
Usage 26
|
2017-02-23 10:17:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Get_tldr "${page// /-}"
|
2017-03-12 20:34:04 +01:00
|
|
|
[[ ! -s $cached ]] && Err "tldr page for command $I$page$XI not found" \
|
2017-11-27 13:31:27 +01:00
|
|
|
&& Inf "Contribute new pages at:$XB ${URL}https://github.com/tldr-pages/tldr$XURL" && exit 27
|
2017-03-12 20:34:04 +01:00
|
|
|
((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached"
|
2017-02-23 10:17:43 +01:00
|
|
|
}
|
2016-01-04 06:33:47 +01:00
|
|
|
|
2017-02-23 10:17:43 +01:00
|
|
|
Main "$@"
|
2017-03-12 20:34:04 +01:00
|
|
|
# The error trap will output the accumulated stdout
|
2016-01-04 06:33:47 +01:00
|
|
|
exit 0
|