From 47e93a831cac0eaca73e0d6d31128ff661c30379 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Thu, 17 Jun 2021 21:05:19 +0200 Subject: [PATCH 1/2] Also use pager for normal queries I don't use --shell very much, rather ad-hoc "cht.sh " in the shell. The paging that is used in shell mode would be helpful there as well, to avoid scrolling back in the terminal when there's long output. The existing do_query() function that is used in shell mode looks like it can be a drop-in replacement for the duplicated curl calls, and with that we get paging for normal queries as well. (And this reuses any set session id as well.) Some initializations need to be moved further up; they still don't run for --help, --mode, and --standalone-install. --- share/cht.sh.txt | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/share/cht.sh.txt b/share/cht.sh.txt index 2f662aca..f08a994e 100755 --- a/share/cht.sh.txt +++ b/share/cht.sh.txt @@ -478,6 +478,21 @@ elif [ x"$1" = x--mode ]; then exit "$?" fi +mkdir -p "$CHTSH_HOME/" +lines=$(tput lines) + +if command -v less >/dev/null; then + defpager="less -R" +elif command -v more >/dev/null; then + defpager="more" +else + defpager="cat" +fi + +TMP1=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX) +trap 'rm -f $TMP1 $TMP2' EXIT +trap 'true' INT + prompt="cht.sh" opts="" input="" @@ -491,7 +506,7 @@ done query=$(echo "$input" | sed 's@ *$@@; s@^ *@@; s@ @/@; s@ @+@g') if [ "$shell_mode" != yes ]; then - curl -s "${CHTSH_URL}"/"$(get_query_options "$query")" + do_query "$query" exit 0 else new_section="$1" @@ -509,7 +524,7 @@ else fi if [ -n "$this_query" ] && [ -z "$CHEATSH_RESTART" ]; then printf "$this_prompt$this_query\n" - curl -s "${CHTSH_URL}"/"$(get_query_options "$query")" + do_query "$query" fi fi @@ -522,17 +537,6 @@ if [ "$is_macos" != yes ]; then fi command -v rlwrap >/dev/null || { echo 'DEPENDENCY: install "rlwrap" to use cht.sh in the shell mode' >&2; exit 1; } -mkdir -p "$CHTSH_HOME/" -lines=$(tput lines) - -if command -v less >/dev/null; then - defpager="less -R" -elif command -v more >/dev/null; then - defpager="more" -else - defpager="cat" -fi - cmd_cd() { if [ $# -eq 0 ]; then section="" @@ -757,10 +761,6 @@ cmd_version() { rm -f "$TMP2" > /dev/null 2>&1 } -TMP1=$(mktemp /tmp/cht.sh.XXXXXXXXXXXXX) -trap 'rm -f $TMP1 $TMP2' EXIT -trap 'true' INT - if ! [ -e "$CHTSH_HOME/.hushlogin" ] && [ -z "$this_query" ]; then echo "type 'help' for the cht.sh shell help" fi From cee3efd0921c1867af6a9a24ecb1d022e0967ac7 Mon Sep 17 00:00:00 2001 From: Ingo Karkat Date: Thu, 17 Jun 2021 21:16:54 +0200 Subject: [PATCH 2/2] Do not do paging if output is not to a terminal We don't want the pager to interfere if the output is redirected to a file, or maybe consumed by another process. For the original do_query() application in shell mode this wasn't so important (nobody would combine interactive shell mode with output redirection), but the pager is now used for normal queries as well. The existing lines variable is reused for disabling the paging (as paging already is skipped if the variable is empty). The overloading with such a control flag has a bit of a smell; on the other hand, we really don't have a number of lines when output is not to a terminal, so it kind-of makes sense. --- share/cht.sh.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/share/cht.sh.txt b/share/cht.sh.txt index f08a994e..b15ebe0a 100755 --- a/share/cht.sh.txt +++ b/share/cht.sh.txt @@ -479,7 +479,7 @@ elif [ x"$1" = x--mode ]; then fi mkdir -p "$CHTSH_HOME/" -lines=$(tput lines) +lines=''; [ -t 1 ] && lines=$(tput lines) if command -v less >/dev/null; then defpager="less -R"