diff --git a/.github/workflows/CICD.yml b/.github/workflows/CICD.yml index fc807718..138bc575 100644 --- a/.github/workflows/CICD.yml +++ b/.github/workflows/CICD.yml @@ -458,7 +458,7 @@ jobs: needs: build if: startsWith(github.ref, 'refs/tags/v') steps: - - uses: vedantmgoyal9/winget-releaser@a8fff44b123f115ce4c21543edf8b677376fc7e0 + - uses: vedantmgoyal9/winget-releaser@b7370d4d152efdce803a26cc6ca9eee58cb2ad54 with: identifier: sharkdp.bat installers-regex: '-pc-windows-msvc\.zip$' diff --git a/CHANGELOG.md b/CHANGELOG.md index ab23dedc..2016bc37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -30,11 +30,13 @@ ## Bugfixes - Offer the valid `--wrap` value `auto` instead of the rejected `always` in the PowerShell completion, see #4070 (@ump45nose) - Report `BAT_CONFIG_DIR` in `bat --diagnostic`, so bug reports show a config directory that overrides `$XDG_CONFIG_HOME`. See #4068 (@ump45nose) +- Ignore a UTF-8 byte order mark at the start of the config file instead of treating it as part of the first argument, see #4103 (@jaeminbird) - Fix `bat` hanging forever on JavaScript (and Svelte) code where an expression is followed by a block comment, e.g. `a /* x */`. Closes #4080, see #4091 (@NavpreetSSidhu) -- Fix the first line of UTF-16BE input swallowing the second line, which broke line numbers, `--line-range` and highlighting, see #XXXX (@cindykrafft) +- Fix the first line of UTF-16BE input swallowing the second line, which broke line numbers, `--line-range` and highlighting, see #4102 (@cindykrafft) - Exit quietly with status 0 instead of panicking when stdout is closed early (broken pipe) on auxiliary output paths such as `--completion`, `--config-file`, `--diagnostic`, `--generate-config-file`, `--set-terminal-title` and `cache --build`/`--clear`, matching the main output path since #232, closes #4034, see #4035 (@Mathjk) - Track strikethrough (SGR 9 / 29) in the ANSI style tracker so it is re-emitted like bold, dim, italic, and underline, and is no longer dropped after the first highlighted token when displaying ANSI input with syntax highlighting, see #4015 (@mikehasa) - Allow boolean flags to be given more than once, so that a flag set in the config file can also be passed on the command line without erroring, see #3912 (@logarithmone1128) +- Allow options that take a value (`--binary`, `--italic-text`, `--nonprintable-notation`, `--fallback-syntax`, `--squeeze-limit`) to be given more than once, so that a value from the config file can be overridden on the command line, see #4102 (@Huang-404-Q) - Use parsed CLI arguments to detect number flags, see #3908 (@cuishuang) - Detect binary content beyond the first line, preventing encrypted files with early line breaks from being treated as text. Closes #3554, see #3877 (@Matei02355) - Use the last occurrence of numbering and plain flags in combined short arguments, see #3897 (@cuishuang) diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs index 9d57fc92..3d626d53 100644 --- a/src/bin/bat/clap_app.rs +++ b/src/bin/bat/clap_app.rs @@ -65,6 +65,7 @@ pub fn build_app(interactive_output: bool) -> Command { .arg( Arg::new("nonprintable-notation") .long("nonprintable-notation") + .overrides_with("nonprintable-notation") .action(ArgAction::Set) .default_value("unicode") .value_parser(["unicode", "caret"]) @@ -81,6 +82,7 @@ pub fn build_app(interactive_output: bool) -> Command { .arg( Arg::new("binary") .long("binary") + .overrides_with("binary") .action(ArgAction::Set) .default_value("no-printing") .value_parser(["no-printing", "as-text"]) @@ -125,6 +127,7 @@ pub fn build_app(interactive_output: bool) -> Command { .arg( Arg::new("fallback-syntax") .long("fallback-syntax") + .overrides_with("fallback-syntax") .visible_alias("fallback-language") .help("Set a fallback language for undetected syntaxes.") .long_help( @@ -317,6 +320,7 @@ pub fn build_app(interactive_output: bool) -> Command { .arg( Arg::new("italic-text") .long("italic-text") + .overrides_with("italic-text") .value_name("when") .value_parser(["always", "never"]) .default_value("never") @@ -484,6 +488,7 @@ pub fn build_app(interactive_output: bool) -> Command { .arg( Arg::new("squeeze-limit") .long("squeeze-limit") + .overrides_with("squeeze-limit") .value_parser(|s: &str| s.parse::().map_err(|_| "Requires a non-negative number".to_owned())) .long_help("Set the maximum number of consecutive empty lines to be printed.") .hide_short_help(true) diff --git a/src/bin/bat/config.rs b/src/bin/bat/config.rs index 863aa3d7..3a7e2b83 100644 --- a/src/bin/bat/config.rs +++ b/src/bin/bat/config.rs @@ -110,7 +110,7 @@ pub fn get_args_from_config_file() -> Result, shell_words::ParseEr let user_config = config_file(); if let Ok(c) = fs::read_to_string(&system_config) { - config.push_str(&c); + config.push_str(strip_bom(&c)); config.push('\n'); } @@ -118,7 +118,7 @@ pub fn get_args_from_config_file() -> Result, shell_words::ParseEr // which can happen when BAT_CONFIG_DIR is set to e.g. "/etc/bat". See #3589. if !same_file(&system_config, &user_config) { if let Ok(c) = fs::read_to_string(&user_config) { - config.push_str(&c); + config.push_str(strip_bom(&c)); } } @@ -136,6 +136,12 @@ pub fn get_args_from_env_opts_var() -> Option, shell_words: env::var("BAT_OPTS").ok().map(|s| get_args_from_str(&s)) } +/// Editors on Windows (e.g. `Set-Content -Encoding UTF8` in Windows PowerShell) +/// may prepend a UTF-8 byte order mark, which would otherwise end up in the first argument. +fn strip_bom(content: &str) -> &str { + content.strip_prefix('\u{feff}').unwrap_or(content) +} + fn get_args_from_str(content: &str) -> Result, shell_words::ParseError> { let args_per_line = content .split('\n') diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 1d252015..822a5921 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -729,6 +729,51 @@ fn boolean_flag_in_config_and_on_command_line() { .success(); } +#[test] +fn repeated_valued_option_is_accepted() { + // Options that take a value must be repeatable (last value wins) just like + // boolean flags, otherwise a value from the config file cannot be + // overridden on the command line. + for options in [ + ["--binary=as-text", "--binary=no-printing"], + ["--italic-text=always", "--italic-text=never"], + [ + "--nonprintable-notation=unicode", + "--nonprintable-notation=caret", + ], + ["--fallback-syntax=bash", "--fallback-syntax=json"], + ["--squeeze-limit=5", "--squeeze-limit=2"], + ] { + bat() + .arg("test.txt") + .args(options) + .assert() + .success() + .stdout("hello world\n"); + } +} + +#[test] +fn valued_option_in_config_can_be_overridden_on_command_line() { + // The config file is prepended to the command line, so a value given in + // both places used to be rejected as "cannot be used multiple times". + let tmp_dir = tempdir().expect("can create temporary directory"); + let tmp_config_path = tmp_dir.path().join("config"); + std::fs::write(&tmp_config_path, "--binary=as-text").expect("can write config file"); + + bat_with_config() + .env("BAT_CONFIG_PATH", tmp_config_path.to_str().unwrap()) + .arg("test.binary") + .arg("--binary=no-printing") + .arg("--color=always") + .arg("--decorations=never") + .arg("--paging=never") + .assert() + .success() + .stdout(predicate::str::contains("Binary content from")) + .stdout(predicate::str::contains("\0").not()); +} + #[test] fn squeeze_blank() { bat() @@ -1786,6 +1831,28 @@ fn help_works_with_invalid_config() { .stdout(predicate::str::contains("A cat(1) clone with wings")); } +#[test] +fn config_file_with_utf8_bom() { + let tmp_dir = tempdir().expect("can create temporary directory"); + let tmp_config_path = tmp_dir.path().join("config"); + + for config in [ + "\u{feff}--style=numbers\r\n", + "\u{feff}# comment\r\n--style=numbers\r\n", + ] { + std::fs::write(&tmp_config_path, config).expect("can write config file"); + + bat_with_config() + .env("BAT_CONFIG_PATH", tmp_config_path.to_str().unwrap()) + .arg("test.txt") + .arg("--decorations=always") + .assert() + .success() + .stdout(" 1 hello world\n") + .stderr(""); + } +} + #[test] fn help_uses_valid_config() { let tmp_dir = tempdir().expect("can create temporary directory");