]> git.tuebingen.mpg.de Git - gsu.git/blobdiff - subcommand
subcommand: Use pager for man output if possible.
[gsu.git] / subcommand
index fe6ec57d66ae852562c23a20f0bca9b4a2464a75..ab6ae8a239b36609d6f6dbe1ce4b24e1e31087d1 100644 (file)
@@ -209,7 +209,7 @@ gsu_complete_options()
 
 com_prefs_options='e'
 
-export gsu_prefs_txt="
+_gsu_prefs_txt="
 Print the current preferences.
 
 Usage: prefs [-e]
@@ -267,23 +267,35 @@ com_prefs()
        done
 }
 
+_gsu_isatty()
+{(
+       exec 3<&1
+       stty 0<&3 &> /dev/null
+)}
+
 complete_prefs()
 {
        gsu_complete_options "$com_prefs_options" "$@"
 }
 
-export gsu_man_txt="
+_gsu_man_txt='
 Print the manual.
 
-Usage: man"
+Usage: man
 
+If stdout associated with a terminal device, output is piped to
+$PAGER. If $PAGER is unset, less(1) is assumed.
+'
 
 com_man()
 {
        local equal_signs="=================================================="
        local minus_signs="--------------------------------------------------"
-       local com num
+       local com num pager='cat'
 
+       _gsu_isatty && pager="${PAGER:-less}"
+       [[ "$pager" == 'less' ]] && export LESS=${LESS-RI}
+       {
        echo "$gsu_name (_${gsu_banner_txt}_) manual"
        echo "${equal_signs:0:${#gsu_name} + ${#gsu_banner_txt} + 16}"
        echo
@@ -311,10 +323,11 @@ com_man()
                $0 help $com
                echo
        done
+       } | $pager
        ret=$GSU_SUCCESS
 }
 
-export gsu_help_txt="
+_gsu_help_txt="
 Print online help.
 
 Usage: help [command]
@@ -322,7 +335,7 @@ Usage: help [command]
 Without arguments, print the list of available commands. Otherwise,
 print the help text for the given command."
 
-export gsu_complete_txt="
+_gsu_complete_txt="
 Command line completion.
 
 Usage: complete [<cword> <word>...]
@@ -347,10 +360,10 @@ com_help()
                gsu_short_msg "### $gsu_name -- $gsu_banner_txt ###"
                _gsu_usage 2>&1
                {
-                       printf "com_help()\n$gsu_help_txt" | head -n 4; echo "--"
-                       printf "com_man()\n$gsu_man_txt" | head -n 4; echo "--"
-                       printf "com_prefs()\n$gsu_prefs_txt" | head -n 4; echo "--"
-                       printf "com_complete()\n$gsu_complete_txt" | head -n 4; echo "--"
+                       printf "com_help()\n$_gsu_help_txt" | head -n 4; echo "--"
+                       printf "com_man()\n$_gsu_man_txt" | head -n 4; echo "--"
+                       printf "com_prefs()\n$_gsu_prefs_txt" | head -n 4; echo "--"
+                       printf "com_complete()\n$_gsu_complete_txt" | head -n 4; echo "--"
                        grep -EA 2 "$ere" $0
                } | grep -v -- '--' \
                        | sed -En "/$ere/"'!d
@@ -379,22 +392,22 @@ com_help()
                return
        fi
        if test "$1" = "help"; then
-               echo "$gsu_help_txt"
+               echo "$_gsu_help_txt"
                ret=$GSU_SUCCESS
                return
        fi
        if test "$1" = "man"; then
-               echo "$gsu_man_txt"
+               echo "$_gsu_man_txt"
                ret=$GSU_SUCCESS
                return
        fi
        if test "$1" = "prefs"; then
-               echo "$gsu_prefs_txt"
+               echo "$_gsu_prefs_txt"
                ret=$GSU_SUCCESS
                return
        fi
        if test "$1" = "complete"; then
-               echo "$gsu_complete_txt"
+               echo "$_gsu_complete_txt"
                ret=$GSU_SUCCESS
                return
        fi