From ed489d0d5272f0343f25cecc9c0e4fbb4fe2a0d1 Mon Sep 17 00:00:00 2001 From: Fabian Boehm Date: Sat, 28 Oct 2023 10:31:24 +0200 Subject: [PATCH] Add __fish_anypager helper This makes it easier to get *any pager* in the number of places we do. Unfortunately: 1. It can't just execute the pager because that might block 2. We can't really set the necessary options for less here so they still need to be set outside. This Fixes #10074 by falling back to `cat` in that case. We could also decide to abort instead of using a non-pager, but for history that's probably fine. --- share/functions/__fish_anypager.fish | 31 +++++++++++++++++++ share/functions/__fish_paginate.fish | 6 ++-- .../__fish_preview_current_file.fish | 4 +-- share/functions/__fish_print_help.fish | 11 ++----- share/functions/fish_delta.fish | 6 ++-- share/functions/history.fish | 6 ++-- 6 files changed, 44 insertions(+), 20 deletions(-) create mode 100644 share/functions/__fish_anypager.fish diff --git a/share/functions/__fish_anypager.fish b/share/functions/__fish_anypager.fish new file mode 100644 index 000000000..3546ca984 --- /dev/null +++ b/share/functions/__fish_anypager.fish @@ -0,0 +1,31 @@ +function __fish_anypager --description "Print a pager to use" + set -l pager + # We prefer $PAGER if we have it + set -q PAGER + and echo $PAGER | read -at pager + + # or even $MANPAGER if we're allowed to + if test "$argv[1]" = "--with-manpager" + set -q MANPAGER + and echo $MANPAGER | read -at pager + end + + # We use them if they *exist* + if command -q $pager[1] + printf %s\n $pager + return 0 + end + + # Cheesy hardcoded list of pagers. + for cmd in bat lv most less more + if command -q $cmd + echo -- $cmd + return 0 + end + end + + # We have no pager. + # We could fall back to "cat", + # but in some cases that's probably not helpful. + return 1 +end diff --git a/share/functions/__fish_paginate.fish b/share/functions/__fish_paginate.fish index 8fe789a61..3b6e4b4dd 100644 --- a/share/functions/__fish_paginate.fish +++ b/share/functions/__fish_paginate.fish @@ -1,8 +1,6 @@ function __fish_paginate -d "Paginate the current command using the users default pager" - set -l cmd less - if set -q PAGER - echo $PAGER | read -at cmd - end + set -l cmd (__fish_anypager) + or return 1 fish_commandline_append " &| $cmd" end diff --git a/share/functions/__fish_preview_current_file.fish b/share/functions/__fish_preview_current_file.fish index cb384673d..ae8db1091 100644 --- a/share/functions/__fish_preview_current_file.fish +++ b/share/functions/__fish_preview_current_file.fish @@ -1,6 +1,6 @@ function __fish_preview_current_file --description "Open the file at the cursor in a pager" - set -l pager less -- - set -q PAGER && echo $PAGER | read -at pager + set -l pager (__fish_anypager) + or set pager cat # commandline -t will never return an empty list. However, the token # could comprise multiple lines, so join them into a single string. diff --git a/share/functions/__fish_print_help.fish b/share/functions/__fish_print_help.fish index d65d3c946..79b77cd15 100644 --- a/share/functions/__fish_print_help.fish +++ b/share/functions/__fish_print_help.fish @@ -125,15 +125,10 @@ function __fish_print_help --description "Print help message for the specified f end end | string replace -ra '^ ' '' | begin - set -l pager less - # Try both PAGER and MANPAGER, last one wins - set -q PAGER - and echo -- $PAGER | read -at pager - set -q MANPAGER - and echo -- $MANPAGER | read -at pager + set -l pager (__fish_anypager --with-manpager) + and isatty stdout + or set pager cat # cannot use a builtin here - not isatty stdout - and set pager cat # cannot use a builtin here # similar to man, but add -F to quit paging when the help output is brief (#6227) # Also set -X for less < v530, see #8157. set -l lessopts isRF diff --git a/share/functions/fish_delta.fish b/share/functions/fish_delta.fish index 75220f4f5..4d17347f2 100644 --- a/share/functions/fish_delta.fish +++ b/share/functions/fish_delta.fish @@ -70,9 +70,9 @@ function fish_delta if isatty stdout set -f colors "$(set_color normal)" "$(set_color brblue)" "$(set_color bryellow)" "$(set_color green)" "$(set_color red)" - set -f pager less - set -q PAGER - and echo $PAGER | read -at pager + set -f pager (__fish_anypager) + or set pager cat + if type -q less set -l lessopts isRF if test (less --version | string match -r 'less (\d+)')[2] -lt 530 2>/dev/null diff --git a/share/functions/history.fish b/share/functions/history.fish index 806fcec17..09b078fff 100644 --- a/share/functions/history.fish +++ b/share/functions/history.fish @@ -86,9 +86,9 @@ function history --description "display or manipulate interactive command histor and set search_mode --contains if isatty stdout - set -l pager less - set -q PAGER - and echo $PAGER | read -at pager + set -l pager (__fish_anypager) + and isatty stdout + or set pager cat # If the user hasn't preconfigured less with the $LESS environment variable, # we do so to have it behave like cat if output fits on one screen.