history: improve interactive pager experience

- Colorize history search output when interactive, using
  fish_indent. This is the same way we colorize `type` output.

- Ask less to act like `cat` if the output will fit in the
  terminal window, so it's less jaring with short output.

- history is viewed in a pager when interactive, but pagers
  typically strip escape codes. We accomplish the above by
  doing exactly what `git` does[1] when it has colored output
  for a pager:
        if $LESS is unset, set it to enable -R, -F, and -X options.
	if $LV is unset, set it to -c.

[1]: 398dd4bd03/pager.c (L87)
This commit is contained in:
Aaron Gyes 2018-11-20 04:10:09 -08:00
parent 7367e545f2
commit 007d794b6e

View File

@ -32,7 +32,7 @@ function history --description "display or manipulate interactive command histor
set -l hist_cmd
set -l show_time
set -l skip_indent
set -l max_count
set -q _flag_max
set max_count -n$_flag_max
@ -41,6 +41,10 @@ function history --description "display or manipulate interactive command histor
and set -l _flag_show_time $_flag_with_time
if set -q _flag_show_time[1]
set show_time --show-time=$_flag_show_time
# `history -t` by default shows timestamps on seperate lines beginning with # characters.
# When colorized the timetamps would highlighted as comments by fish_indent. With a custom
# format there is no guarantee fish_indent can do anything useful.
set skip_indent true
else if set -q _flag_show_time
set show_time --show-time
end
@ -87,7 +91,20 @@ function history --description "display or manipulate interactive command histor
set -l pager less
set -q PAGER
and set pager $PAGER
builtin history search $search_mode $show_time $max_count $_flag_case_sensitive $_flag_reverse $_flag_null -- $argv | eval $pager
# If the user hasn't preconfigured less with the $LESS environment variable,
# we do so to prevent the stripping of control characters (for color) and have
# it behave like cat if output fits on one screen. Prevent the screen from clearing
# on quit, so there is something to see if it exits.
# These are the same three options `git` sets through $LESS before starting the pager.
not set -qx LESS
and set -x LESS --RAW-CONTROL-CHARS --quit-if-one-screen --no-init
not set -qx LV # ask the pager lv not to strip colors
and set -x LV -c
set -q skip_indent[1] # --show-time=FORMAT set, arbitrary output may not be valid fish script
and builtin history search $search_mode $show_time $max_count $_flag_case_sensitive $_flag_reverse $_flag_null -- $argv | $pager
or builtin history search $search_mode $show_time $max_count $_flag_case_sensitive $_flag_reverse $_flag_null -- $argv | fish_indent --ansi | $pager
else
builtin history search $search_mode $show_time $max_count $_flag_case_sensitive $_flag_reverse $_flag_null -- $argv
end