Compare commits

...

4 Commits

Author SHA1 Message Date
gitlab.com/pepa65 e97179376b After Pullreq.#19: Show English tldr if not found in other language 2023-06-04 11:24:34 +07:00
gitlab.com/pepa65 2156390101 Different fix for bash's bug in reading UTF-8 2023-05-31 14:47:16 +07:00
gitlab.com/pepa65 15854a1465 Fix Chinese display in less and debug linting 2023-05-31 06:10:54 +07:00
gitlab.com/pepa65 c65bb3fb1d Debug full stop 2023-05-31 05:39:19 +07:00
2 changed files with 26 additions and 12 deletions

View File

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

36
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.5.0'
# tldr-bash-client version 0.48
version=0.6.2
# tldr-bash-client v0.6.2
# 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
@ -182,7 +182,7 @@ Init_term(){
}
# $1: page
Recent(){ [[ $(find "$1" -mtime -"$TLDR_EXPIRY") ]];}
Recent(){ [[ $(find "$1" -mtime -"$TLDR_EXPIRY" 2>/dev/null) ]];}
# Initialize globals, check the environment; Uses: config cachedir version
# Sets: stdout os version dl pages
@ -210,8 +210,8 @@ Config(){
ver="tldr-bash-client version $version$XB ${URL}https://gitlab.com/pepa65/tldr-bash-client$XURL"
# Select download method
! dl="$(type -P curl) -sLfo" &&
! dl="$(type -P wget) --max-redirect=20 -qNO" &&
! dl="$(type -P curl) --connect-timeout 10 -sLfo" &&
! dl="$(type -P wget) --timeout=10 --max-redirect=20 -qNO" &&
Err "tldr requires ${I}curl$XI or ${I}wget$XI installed in your path" &&
exit 3
@ -296,7 +296,7 @@ Get_tldr(){
# $1: file (optional); Uses: page stdout; Sets: ln REPLY
Display_tldr(){
local newfmt len val
local newfmt len val reply
ln=0 REPLY=
[[ $md ]] || md=$1
# Read full lines, and process even when no newline at the end
@ -326,7 +326,8 @@ Display_tldr(){
Out "$TNL$TSP$T${REPLY:2}$XT" ;;
'>') ((${#REPLY} <= 3)) && Unlinted "No valid desciption"
[[ ! ${REPLY:1:1} = ' ' ]] && Unlinted "2nd character no space"
[[ ! ${REPLY: -1} = '.' || ! ${REPLY: -1} = '。' ]] && Unlinted "Description doesn't end in full stop"
reply=${REPLY//$'\n'}
[[ ! ${reply: -1} = '.' && ! ${reply: -1} = '。' ]] && Unlinted "Description doesn't end in full stop"
Out "$DNL$DSP$D${REPLY:2}$XD"
DNL= ;;
'-') ((newfmt)) && Unlinted "Bad first character"
@ -355,7 +356,7 @@ Display_tldr(){
Out "$ENL$ESP$E$REPLY$XE" ;;
esac
done <"$1"
[[ $TLDR_LESS = 0 ]] &&
[[ $TLDR_LESS = 0 ]] &&
trap 'cat <<<"$stdout"' EXIT ||
trap 'less -Gg -~RXQFP"%pB\% tldr $I$page$XI - browse up/down, press Q to exit" <<<"$stdout"' EXIT
}
@ -491,11 +492,24 @@ Main(){
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 28
((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached"
if [[ -s $cached ]]
then # File present
((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached"
elif [[ ! $lang = en ]]
then # Not English and no file
pages=pages
Get_tldr "${page// /-}"
[[ -s $cached ]] &&
Err "tldr page for command $I$page$XI not found in '$lang' but found in English" &&
((markdown)) && Out "$(cat "$cached")" ||
Display_tldr "$cached"
else # Just not found
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
fi
}
export LC_ALL=en_US.UTF-8
Main "$@"
# The exit trap will output the accumulated stdout
exit 0