make LESSOPEN support opt-in

This commit is contained in:
Anomalocaridid 2023-09-05 22:40:29 -04:00 committed by Martin Nordholts
parent ac2953c070
commit b56021ffa9
8 changed files with 62 additions and 12 deletions

View File

@ -59,7 +59,8 @@ Register-ArgumentCompleter -Native -CommandName '{{PROJECT_EXECUTABLE}}' -Script
[CompletionResult]::new('--unbuffered', 'unbuffered', [CompletionResultType]::ParameterName, 'unbuffered') [CompletionResult]::new('--unbuffered', 'unbuffered', [CompletionResultType]::ParameterName, 'unbuffered')
[CompletionResult]::new('--no-config', 'no-config', [CompletionResultType]::ParameterName, 'Do not use the configuration file') [CompletionResult]::new('--no-config', 'no-config', [CompletionResultType]::ParameterName, 'Do not use the configuration file')
[CompletionResult]::new('--no-custom-assets', 'no-custom-assets', [CompletionResultType]::ParameterName, 'Do not load custom assets') [CompletionResult]::new('--no-custom-assets', 'no-custom-assets', [CompletionResultType]::ParameterName, 'Do not load custom assets')
[CompletionResult]::new('--no-lessopen', 'no-lessopen', [CompletionResultType]::ParameterName, 'Do not use the $LESSOPEN preprocessor') [CompletionResult]::new('--lessopen', 'lessopen', [CompletionResultType]::ParameterName, 'Enable the $LESSOPEN preprocessor')
[CompletionResult]::new('--no-lessopen', 'no-lessopen', [CompletionResultType]::ParameterName, 'Disable the $LESSOPEN preprocessor if enabled (overrides --lessopen)')
[CompletionResult]::new('--config-file', 'config-file', [CompletionResultType]::ParameterName, 'Show path to the configuration file.') [CompletionResult]::new('--config-file', 'config-file', [CompletionResultType]::ParameterName, 'Show path to the configuration file.')
[CompletionResult]::new('--generate-config-file', 'generate-config-file', [CompletionResultType]::ParameterName, 'Generates a default configuration file.') [CompletionResult]::new('--generate-config-file', 'generate-config-file', [CompletionResultType]::ParameterName, 'Generates a default configuration file.')
[CompletionResult]::new('--config-dir', 'config-dir', [CompletionResultType]::ParameterName, 'Show bat''s configuration directory.') [CompletionResult]::new('--config-dir', 'config-dir', [CompletionResultType]::ParameterName, 'Show bat''s configuration directory.')

View File

@ -78,6 +78,7 @@ _bat() {
--list-themes | \ --list-themes | \
--line-range | \ --line-range | \
-L | --list-languages | \ -L | --list-languages | \
--lessopen | \
--diagnostic | \ --diagnostic | \
--acknowledgements | \ --acknowledgements | \
-h | --help | \ -h | --help | \
@ -171,6 +172,7 @@ _bat() {
--style --style
--line-range --line-range
--list-languages --list-languages
--lessopen
--diagnostic --diagnostic
--acknowledgements --acknowledgements
--help --help

View File

@ -163,6 +163,8 @@ complete -c $bat -l italic-text -x -a "$italic_text_opts" -d "When to use italic
complete -c $bat -s l -l language -x -k -a "(__bat_complete_list_languages)" -d "Set the syntax highlighting language" -n __bat_no_excl_args complete -c $bat -s l -l language -x -k -a "(__bat_complete_list_languages)" -d "Set the syntax highlighting language" -n __bat_no_excl_args
complete -c $bat -l lessopen -d "Enable the $LESSOPEN preprocessor" -n __fish_is_first_arg
complete -c $bat -s r -l line-range -x -d "Only print lines [M]:[N] (either optional)" -n __bat_no_excl_args complete -c $bat -s r -l line-range -x -d "Only print lines [M]:[N] (either optional)" -n __bat_no_excl_args
complete -c $bat -l list-languages -f -d "List syntax highlighting languages" -n __fish_is_first_arg complete -c $bat -l list-languages -f -d "List syntax highlighting languages" -n __fish_is_first_arg

View File

@ -46,7 +46,8 @@ _{{PROJECT_EXECUTABLE}}_main() {
'(: --list-themes --list-languages -L)'{-L,--list-languages}'[Display all supported languages]' '(: --list-themes --list-languages -L)'{-L,--list-languages}'[Display all supported languages]'
'(: --no-config)'--no-config'[Do not use the configuration file]' '(: --no-config)'--no-config'[Do not use the configuration file]'
'(: --no-custom-assets)'--no-custom-assets'[Do not load custom assets]' '(: --no-custom-assets)'--no-custom-assets'[Do not load custom assets]'
'(: --no-lessopen)'--no-lessopen'[Do not use the $LESSOPEN preprocessor]' '(: --lessopen)'--lessopen'[Enable the $LESSOPEN preprocessor]'
'(: --no-lessopen)'--no-lessopen'[Disable the $LESSOPEN preprocessor if enabled (overrides --lessopen)]'
'(: --config-dir)'--config-dir'[Show bat'"'"'s configuration directory]' '(: --config-dir)'--config-dir'[Show bat'"'"'s configuration directory]'
'(: --config-file)'--config-file'[Show path to the configuration file]' '(: --config-file)'--config-file'[Show path to the configuration file]'
'(: --generate-config-file)'--generate-config-file'[Generates a default configuration file]' '(: --generate-config-file)'--generate-config-file'[Generates a default configuration file]'

View File

@ -248,7 +248,13 @@ These can be installed to `\fB$({{PROJECT_EXECUTABLE}} --config-dir)/themes\fR`,
Much like less(1) does, {{PROJECT_EXECUTABLE}} supports input preprocessors via the LESSOPEN and LESSCLOSE environment variables. Much like less(1) does, {{PROJECT_EXECUTABLE}} supports input preprocessors via the LESSOPEN and LESSCLOSE environment variables.
In addition, {{PROJECT_EXECUTABLE}} attempts to be as compatible with less's preprocessor implementation as possible. In addition, {{PROJECT_EXECUTABLE}} attempts to be as compatible with less's preprocessor implementation as possible.
To run {{PROJECT_EXECUTABLE}} without using the preprocessor, call: To use the preprocessor, call:
\fB{{PROJECT_EXECUTABLE}} --lessopen\fR
Alternatively, the preprocessor may be enabled by default by adding the '\-\-lessopen' option to the configuration file.
To temporarily disable the preprocessor if it is enabled by default, call:
\fB{{PROJECT_EXECUTABLE}} --no-lessopen\fR \fB{{PROJECT_EXECUTABLE}} --no-lessopen\fR

View File

@ -282,7 +282,7 @@ impl App {
.unwrap_or_default(), .unwrap_or_default(),
use_custom_assets: !self.matches.get_flag("no-custom-assets"), use_custom_assets: !self.matches.get_flag("no-custom-assets"),
#[cfg(feature = "lessopen")] #[cfg(feature = "lessopen")]
use_lessopen: !self.matches.get_flag("no-lessopen"), use_lessopen: self.matches.get_flag("lessopen"),
}) })
} }

View File

@ -501,13 +501,21 @@ pub fn build_app(interactive_output: bool) -> Command {
#[cfg(feature = "lessopen")] #[cfg(feature = "lessopen")]
{ {
app = app.arg( app = app
Arg::new("no-lessopen") .arg(
.long("no-lessopen") Arg::new("lessopen")
.action(ArgAction::SetTrue) .long("lessopen")
.hide(true) .action(ArgAction::SetTrue)
.help("Do not use the $LESSOPEN preprocessor"), .help("Enable the $LESSOPEN preprocessor"),
) )
.arg(
Arg::new("no-lessopen")
.long("no-lessopen")
.action(ArgAction::SetTrue)
.overrides_with("lessopen")
.hide(true)
.help("Disable the $LESSOPEN preprocessor if enabled (overrides --lessopen)"),
)
} }
app = app app = app

View File

@ -2079,6 +2079,7 @@ fn acknowledgements() {
fn lessopen_file_piped() { fn lessopen_file_piped() {
bat() bat()
.env("LESSOPEN", "|echo File is %s") .env("LESSOPEN", "|echo File is %s")
.arg("--lessopen")
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
@ -2091,6 +2092,7 @@ fn lessopen_file_piped() {
fn lessopen_stdin_piped() { fn lessopen_stdin_piped() {
bat() bat()
.env("LESSOPEN", "|cat") .env("LESSOPEN", "|cat")
.arg("--lessopen")
.write_stdin("hello world\n") .write_stdin("hello world\n")
.assert() .assert()
.success() .success()
@ -2107,6 +2109,7 @@ fn lessopen_and_lessclose_file_temp() {
bat() bat()
.env("LESSOPEN", "echo empty.txt") .env("LESSOPEN", "echo empty.txt")
.env("LESSCLOSE", "echo lessclose: %s %s") .env("LESSCLOSE", "echo lessclose: %s %s")
.arg("--lessopen")
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
@ -2124,6 +2127,7 @@ fn lessopen_and_lessclose_file_piped() {
// This test will not work properly if $LESSOPEN does not output anything // This test will not work properly if $LESSOPEN does not output anything
.env("LESSOPEN", "|cat test.txt ") .env("LESSOPEN", "|cat test.txt ")
.env("LESSCLOSE", "echo lessclose: %s %s") .env("LESSCLOSE", "echo lessclose: %s %s")
.arg("--lessopen")
.arg("empty.txt") .arg("empty.txt")
.assert() .assert()
.success() .success()
@ -2132,6 +2136,7 @@ fn lessopen_and_lessclose_file_piped() {
bat() bat()
.env("LESSOPEN", "||cat empty.txt") .env("LESSOPEN", "||cat empty.txt")
.env("LESSCLOSE", "echo lessclose: %s %s") .env("LESSCLOSE", "echo lessclose: %s %s")
.arg("--lessopen")
.arg("empty.txt") .arg("empty.txt")
.assert() .assert()
.success() .success()
@ -2148,6 +2153,7 @@ fn lessopen_and_lessclose_stdin_temp() {
bat() bat()
.env("LESSOPEN", "-echo empty.txt") .env("LESSOPEN", "-echo empty.txt")
.env("LESSCLOSE", "echo lessclose: %s %s") .env("LESSCLOSE", "echo lessclose: %s %s")
.arg("--lessopen")
.write_stdin("test.txt") .write_stdin("test.txt")
.assert() .assert()
.success() .success()
@ -2165,6 +2171,7 @@ fn lessopen_and_lessclose_stdin_piped() {
// This test will not work properly if $LESSOPEN does not output anything // This test will not work properly if $LESSOPEN does not output anything
.env("LESSOPEN", "|-cat test.txt") .env("LESSOPEN", "|-cat test.txt")
.env("LESSCLOSE", "echo lessclose: %s %s") .env("LESSCLOSE", "echo lessclose: %s %s")
.arg("--lessopen")
.write_stdin("empty.txt") .write_stdin("empty.txt")
.assert() .assert()
.success() .success()
@ -2173,6 +2180,7 @@ fn lessopen_and_lessclose_stdin_piped() {
bat() bat()
.env("LESSOPEN", "||-cat empty.txt") .env("LESSOPEN", "||-cat empty.txt")
.env("LESSCLOSE", "echo lessclose: %s %s") .env("LESSCLOSE", "echo lessclose: %s %s")
.arg("--lessopen")
.write_stdin("empty.txt") .write_stdin("empty.txt")
.assert() .assert()
.success() .success()
@ -2185,6 +2193,7 @@ fn lessopen_and_lessclose_stdin_piped() {
fn lessopen_handling_empty_output_file() { fn lessopen_handling_empty_output_file() {
bat() bat()
.env("LESSOPEN", "|cat empty.txt") .env("LESSOPEN", "|cat empty.txt")
.arg("--lessopen")
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
@ -2192,6 +2201,7 @@ fn lessopen_handling_empty_output_file() {
bat() bat()
.env("LESSOPEN", "|cat nonexistent.txt") .env("LESSOPEN", "|cat nonexistent.txt")
.arg("--lessopen")
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
@ -2199,6 +2209,7 @@ fn lessopen_handling_empty_output_file() {
bat() bat()
.env("LESSOPEN", "||cat empty.txt") .env("LESSOPEN", "||cat empty.txt")
.arg("--lessopen")
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
@ -2206,6 +2217,7 @@ fn lessopen_handling_empty_output_file() {
bat() bat()
.env("LESSOPEN", "||cat nonexistent.txt") .env("LESSOPEN", "||cat nonexistent.txt")
.arg("--lessopen")
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
@ -2218,6 +2230,7 @@ fn lessopen_handling_empty_output_file() {
fn lessopen_handling_empty_output_stdin() { fn lessopen_handling_empty_output_stdin() {
bat() bat()
.env("LESSOPEN", "|-cat empty.txt") .env("LESSOPEN", "|-cat empty.txt")
.arg("--lessopen")
.write_stdin("hello world\n") .write_stdin("hello world\n")
.assert() .assert()
.success() .success()
@ -2225,6 +2238,7 @@ fn lessopen_handling_empty_output_stdin() {
bat() bat()
.env("LESSOPEN", "|-cat nonexistent.txt") .env("LESSOPEN", "|-cat nonexistent.txt")
.arg("--lessopen")
.write_stdin("hello world\n") .write_stdin("hello world\n")
.assert() .assert()
.success() .success()
@ -2232,6 +2246,7 @@ fn lessopen_handling_empty_output_stdin() {
bat() bat()
.env("LESSOPEN", "||-cat empty.txt") .env("LESSOPEN", "||-cat empty.txt")
.arg("--lessopen")
.write_stdin("hello world\n") .write_stdin("hello world\n")
.assert() .assert()
.success() .success()
@ -2239,6 +2254,7 @@ fn lessopen_handling_empty_output_stdin() {
bat() bat()
.env("LESSOPEN", "||-cat nonexistent.txt") .env("LESSOPEN", "||-cat nonexistent.txt")
.arg("--lessopen")
.write_stdin("hello world\n") .write_stdin("hello world\n")
.assert() .assert()
.success() .success()
@ -2251,6 +2267,7 @@ fn lessopen_handling_empty_output_stdin() {
fn lessopen_uses_shell() { fn lessopen_uses_shell() {
bat() bat()
.env("LESSOPEN", "|cat < %s") .env("LESSOPEN", "|cat < %s")
.arg("--lessopen")
.arg("test.txt") .arg("test.txt")
.assert() .assert()
.success() .success()
@ -2260,9 +2277,22 @@ fn lessopen_uses_shell() {
#[cfg(unix)] #[cfg(unix)]
#[cfg(feature = "lessopen")] #[cfg(feature = "lessopen")]
#[test] #[test]
fn do_not_use_lessopen() { fn do_not_use_lessopen_by_default() {
bat() bat()
.env("LESSOPEN", "|echo File is %s") .env("LESSOPEN", "|echo File is %s")
.arg("test.txt")
.assert()
.success()
.stdout("hello world\n");
}
#[cfg(unix)]
#[cfg(feature = "lessopen")]
#[test]
fn do_not_use_lessopen_if_overridden() {
bat()
.env("LESSOPEN", "|echo File is %s")
.arg("--lessopen")
.arg("--no-lessopen") .arg("--no-lessopen")
.arg("test.txt") .arg("test.txt")
.assert() .assert()