Skip to content

Commit

Permalink
Limited support for languages, missing index.json for other languages.
Browse files Browse the repository at this point in the history
Some bugsquashing, updated screenshots where mandated.
  • Loading branch information
pepa65 committed Mar 14, 2019
1 parent 4562dd7 commit 1869338
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 39 deletions.
16 changes: 9 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# tldr-bash-client

* version 0.43
* version 0.44

### Bash client for tldr: community driven man-by-example
**A fully-functional [bash](https://tiswww.case.edu/php/chet/bash/bashtop.html)
Expand Down Expand Up @@ -28,7 +28,7 @@ If the location is not in $PATH, you need to specify the path to run it.
### Prerequisites
coreutils, grep, unzip, curl / wget, less (optional)

<img alt="tldr find screenshot" src="tldr-find.jpg" title="tldr find" width="600" />
<img alt="tldr search screenshot" src="tldr-search.jpg" title="tldr search" width="600" />

## Output customisation
<img alt="tldr customize screenshot" src="tldr-customize.jpg" title="tldr customize" width="600" />
Expand Down Expand Up @@ -64,13 +64,15 @@ Color/BG (Newline and Space also allowed) for error and info messages
* TLDR_INFO_COLOR (defaults to: Newline Space Green)

How many days before freshly downloading a potentially stale page
* TLDR_EXPIRY (defaults to: 60)

* TLDR_EXPIRY (defaults to 7)
Alternative location of pages cache
* TLDR_CACHE (not set by default)

Instead of `less`, use `cat` for output (automatic if less not available)
* TLDR_LESS (not set by default; if set to *0* `cat` will be used)
Usage of 'less' or 'cat' for output (set to '0' for cat)
* TLDR_LESS (*1* by default; if set to *0* `cat` will be used)
Force current OS
* TLDR_OS (overrides what is read by `uname -s`)
Force preferred language: ISO639 format (2 lowercase letters)
* TLDR_LANG (not set by default, $LANG is used from environment)

<img alt="tldr list screenshot" src="tldr-list.jpg" title="tldr list" width="600" />

Expand Down
71 changes: 39 additions & 32 deletions tldr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env bash
set +vx -o pipefail
[[ $- = *i* ]] && echo "Don't source this script!" && return 1
version='0.43'
# tldr-bash-client version 0.43
version='0.44'
# tldr-bash-client version 0.44
# 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
Expand Down Expand Up @@ -36,16 +36,15 @@ version='0.43'
: ${TLDR_INFO_COLOR:="Newline Space Green"}

# How many days before freshly downloading a potentially stale page
: ${TLDR_EXPIRY:=60}

: ${TLDR_EXPIRY:=7}
# Alternative location of pages cache
: ${TLDR_CACHE_LOCATION:=""}

# Usage of 'less' or 'cat' for output (set to '0' for cat)
: ${TLDR_LESS:=1}

# Force current OS
: ${TLDR_OS:=""}
# Force preferred language: ISO639 format (2 lowercase letters)
: ${TLDR_LANG:=""}

## Function definitions

Expand All @@ -72,7 +71,7 @@ Usage(){
${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.
${HDE}Cached pages will be freshly downloaded after $HUR$TLDR_EXPIRY$XHUR days. Preferred language: $lang.
EOF
)"
exit "${1:-0}"
Expand Down Expand Up @@ -183,10 +182,10 @@ Init_term(){
}

# $1: page
Recent(){ find "$1" -mtime -"$TLDR_EXPIRY" >/dev/null 2>&1;}
Recent(){ [[ $(find "$1" -mtime -"$TLDR_EXPIRY") ]];}

# Initialize globals, check the environment; Uses: config cachedir version
# Sets: stdout os version dl
# Sets: stdout os version dl pages
Config(){
# Don't use less if no stdout or less not available
[[ ! -t 1 ]] && ! type -P less >/dev/null && TLDR_LESS=0
Expand All @@ -201,8 +200,10 @@ Config(){
CYGWIN*|MINGW*) os='windows' ;;
*) os=
esac
lang=${TLDR_LANG:-$LANG} lang=${lang:0:2} lang=${lang,,}
[[ $lang = en ]] && pages=pages || pages=pages.$lang
Init_term
[[ $TLDR_LESS = 0 ]] &&
[[ $TLDR_LESS = 0 ]] &&
trap 'cat <<<"$stdout"' EXIT ||
trap 'less -~RXQFP"Browse up/down, press Q to exit " <<<"$stdout"' EXIT

Expand All @@ -214,7 +215,7 @@ Config(){
Err "tldr requires ${I}curl$XI or ${I}wget$XI installed in your path" &&
exit 3

pages_url='https://raw.githubusercontent.com/tldr-pages/tldr/master/pages'
repo_url='https://raw.githubusercontent.com/tldr-pages/tldr/master'
zip_url='http://tldr.sh/assets/tldr.zip'

cachedir=$TLDR_CACHE_LOCATION
Expand All @@ -227,10 +228,11 @@ Config(){
! mkdir -p "$cachedir" &&
Err "Can't create the pages cache location $cachedir" &&
exit 4
index=$cachedir/index.json
# Indexes for every language should be available, $pages instead of pages
index=$cachedir/pages/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=$(cd "$cachedir/pages"; ls -d -- */ |tr -d /)
platforms=${platforms//$'\n'/,}
}

Expand All @@ -240,7 +242,7 @@ Unlinted(){
exit 5
}

# $1: page; Uses: index cachedir pages_url platform os dl cached md
# $1: page; Uses: index cachedir repo_url platform os dl cached md
# Sets: cached md
Get_tldr(){
local desc err=0 notfound
Expand All @@ -263,8 +265,7 @@ Get_tldr(){
fi
else # no platform specified: check common
if [[ $desc =~ \"common\" ]]
then
md=common/$1.md
then md=common/$1.md
else # not in common either
[[ $notfound ]] && notfound+=" or "
notfound+=${I}common$XI
Expand All @@ -282,12 +283,14 @@ Get_tldr(){
((err)) && Err "tldr page $I$1$XI not found in $notfound, from platform $U${md%/*}$XU instead"

# return local cached copy of tldr-page, or retrieve and cache from github
cached=$cachedir/$md
cached=$cachedir/$pages/$md
[[ $cached ]] || cached=$cachedir/pages/$md
if ! Recent "$cached"
then
mkdir -p "${cached%/*}"
$dl "$cached" "$pages_url/$md" ||
Err "Could not download page $I$cached$XI with $dl"
$dl "$cached" "$repo_url/$pages/$md" &&
Inf "Downloaded page '$pages/$md'" ||
Err "Could not download $repo_url/$pages/$md"
fi
}

Expand Down Expand Up @@ -364,20 +367,20 @@ List_pages(){
pregex=^ ptext="${I}all$XI platforms"
if [[ $platform = current ]]
then [[ $os ]] &&
pregex="-e $os -e common" ptext="$I$os$XI platform and ${I}common$XI" ||
pregex=common ptext="platform not detected, ${I}common$XI"
Inf "Known tldr pages from $ptext:"
pregex="-e $os -e common" ptext="$I$os$XI platform and ${I}common$XI" ||
pregex=common ptext="platform not detected, ${I}common$XI"
fi
Inf "Known tldr pages from $ptext:"
Out "$(tr '{' '\n' <"$index" |grep $pregex |cut -d "$Q" -f4 |column)"
exit "$1"
}

# $1: regex(es); Uses: cachedir
Find_regex(){
local regex=$* list=$cachedir/*/*.md
local regex=$* list=$cachedir/*/*/*.md
while (($#))
do
list=$(grep $1 $list |cut -d: -f1)
list=$(grep "$1" $list |cut -d: -f1)
shift
done
n=$(wc -l <<<"$list")
Expand All @@ -402,16 +405,20 @@ Cache_fill(){
Err "tldr requires ${I}unzip$XI to fill the cache" &&
exit 7
tmp=$(mktemp -d)
! $dl "$tmp/pages.zip" "$zip_url" &&
rm -- "$tmp" &&
Err "Could not download pages archive from $U$zip_url$XU with $dl" &&
if ! $dl "$tmp/pages.zip" "$zip_url"
then
rm -- "$tmp" || Err "Error deleting temporary directory $tmp"
Err "Could not download pages archive from $U$zip_url$XU with $dl"
exit 8
! $unzip "$tmp/pages.zip" -d "$tmp" 'pages/**' &&
rm -- "$tmp" &&
Err "Couldn't unzip the cache archive on $tmp/pages.zip" &&
fi
if ! $unzip "$tmp/pages.zip" -d "$tmp"
then
rm -- "$tmp" || Err "Error deleting temporary directory $tmp"
Err "Couldn't unzip the cache archive on $tmp/pages.zip"
exit 9
fi
rm -rf -- "${cachedir:?}/"*
mv -- "$tmp/pages/"* "${cachedir:?}/"
mv -- "$tmp"/pages* "${cachedir:?}/"
rm -rf -- "$tmp"
Inf "Pages cached in $U$cachedir$XU"
updated=1
Expand All @@ -424,7 +431,7 @@ Main(){
case "$1" in
-s|--search) [[ -z $2 ]] && Err "Search term(s) [regex] needed" && Usage 10
shift
Find_regex $* ;;
Find_regex "$@" ;;
-l|--list)
if [[ $2 ]]
then
Expand Down
Binary file modified tldr-customize.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed tldr-find.jpg
Binary file not shown.
Binary file added tldr-search.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tldr-usage.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 1869338

Please sign in to comment.