From 46e07b02166228f4d3f5a649bce0b6d68a8c9551 Mon Sep 17 00:00:00 2001 From: LiuZengqiang <12021032@zju.edu.cn> Date: Sun, 4 Jun 2023 11:00:13 +0800 Subject: [PATCH] feat: if not found cmd in other language, tried to locate in English & add timeout limit for downloading. --- tldr | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/tldr b/tldr index c79cdac..d21d6a5 100755 --- a/tldr +++ b/tldr @@ -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 @@ -492,9 +492,19 @@ 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 + ((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached" + + elif [[ $lang != en ]] ; then + pages=pages + Get_tldr "${page// /-}" + if [[ -s $cached ]] ; then + Err "tldr page for command $I$page$XI not found in preferred language $lang, but found in language en." && ((markdown)) && Out "$(cat "$cached")" || Display_tldr "$cached" + fi + else + 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