1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-09-02 03:12:25 +01:00

require LESSOPEN to have exactly one %s

This commit is contained in:
Anomalocaridid
2023-09-06 15:28:08 -04:00
committed by Martin Nordholts
parent b56021ffa9
commit 433176ab54
2 changed files with 78 additions and 18 deletions

View File

@@ -12,7 +12,10 @@ use os_str_bytes::RawOsString;
use run_script::{IoOptions, ScriptOptions};
use crate::error::Result;
use crate::input::{Input, InputKind, InputReader, OpenedInput, OpenedInputKind};
use crate::{
bat_warning,
input::{Input, InputKind, InputReader, OpenedInput, OpenedInputKind},
};
/// Preprocess files and/or stdin using $LESSOPEN and $LESSCLOSE
pub(crate) struct LessOpenPreprocessor {
@@ -32,10 +35,18 @@ enum LessOpenKind {
impl LessOpenPreprocessor {
/// Create a new instance of LessOpenPreprocessor
/// Will return Ok if and only if $LESSOPEN is set
/// Will return Ok if and only if $LESSOPEN is set and contains exactly one %s
pub(crate) fn new() -> Result<LessOpenPreprocessor> {
let lessopen = env::var("LESSOPEN")?;
// Ignore $LESSOPEN if it does not contains exactly one %s
// Note that $LESSCLOSE has no such requirement
if lessopen.match_indices("%s").count() != 1 {
let error_msg = "LESSOPEN ignored: must contain exactly one %s";
bat_warning!("{}", error_msg);
return Err(error_msg.into());
}
// "||" means pipe directly to bat without making a temporary file
// Also, if preprocessor output is empty and exit code is zero, use the empty output
// Otherwise, if output is empty and exit code is nonzero, use original file contents