From e138c5dd0a019547dbaf1a9ff882fd8f8ce08e41 Mon Sep 17 00:00:00 2001 From: pepa65 Date: Wed, 29 Nov 2017 10:33:41 +0700 Subject: [PATCH] Add autocomplete option to readme --- README.md | 11 ++++++++++- tldr | 16 ++++++++++------ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index d22ffa5..1bff44a 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ coreutils, grep, unzip, curl / wget, less (optional) tldr find screenshot -## Customisation +## Output customisation tldr customize screenshot The 5 elements in TLDR markup that can be styled with these colors and @@ -74,6 +74,15 @@ Instead of `less`, use `cat` for output (automatic if less not available) tldr list screenshot +## Autocompletion +When the following lines are added to `~/.bashrc` or run in the current session, +then *tldr* will autocomplete, which means possible tldr page names will be +suggested when tapping the **Tab** key twice on an incomplete tldr page name: +``` +cachedir=~/.local/share/tldr # Or whatever else the location of the tldr cache is +complete -W "$(q=($cachedir/*/*); sed 's@\.md @ @g' <<<${q[@]##*/})" tldr +``` + # tldr-lint * version 0.11 diff --git a/tldr b/tldr index 82c56b5..af46af2 100755 --- a/tldr +++ b/tldr @@ -209,11 +209,12 @@ Config(){ pages_url='https://raw.githubusercontent.com/tldr-pages/tldr/master/pages' zip_url='http://tldr.sh/assets/tldr.zip' - read cachedir <<<$TLDR_CACHE - [[ $cachedir ]] || { - [[ $XDG_DATA_HOME ]] && cachedir=$XDG_DATA_HOME/tldr \ - || cachedir=$HOME/.local/share/tldr - } + cachedir=$(echo $TLDR_CACHE) + if [[ -z $cachedir ]] + then + [[ $XDG_DATA_HOME ]] && cachedir=$XDG_DATA_HOME/tldr || + cachedir=$HOME/.local/share/tldr + fi [[ -d "$cachedir" ]] || mkdir -p "$cachedir" || { Err "Can't create the pages cache location $cachedir" exit 4 @@ -221,6 +222,9 @@ Config(){ index=$cachedir/index.json # update if the file doesn't exists, or if it's older than $TLDR_EXPIRY [[ -f $index ]] && Recent "$index" || Cache_fill 0 + + # initiate autocomplete for current session + complete -W "$(q=($cachedir/*/*); sed 's@\.md @ @g' <<<${q[@]##*/})" tldr } # $1: error message; Uses: md REPLY ln @@ -345,7 +349,7 @@ List_pages(){ exit "$1" } -# $1: regex, $1: exit code; Uses: cachedir +# $1: regex, $2: exit code; Uses: cachedir Find_regex(){ local list=$(grep "$1" "$cachedir"/*/*.md |cut -d: -f1) regex="$U$1$XU" local n=$(wc -l <<<"$list")