diff options
author | David Peter <sharkdp@users.noreply.github.com> | 2024-02-23 21:37:28 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-02-23 21:37:28 +0100 |
commit | 7604fe5567f2dafaa0174e8dde661d18bfbe17e8 (patch) | |
tree | 5962ac375ba0995e3bc56f4367bf455a28bacc56 | |
parent | 6a6b02117b577df90c483aac406952a3814462da (diff) | |
parent | 1f628203e5091da967d920ddc2f201d068f31be1 (diff) |
Merge pull request #2807 from Oliver-Looney/2783-setting-terminal-title
2783 setting terminal title
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | doc/long-help.txt | 3 | ||||
-rw-r--r-- | src/bin/bat/app.rs | 1 | ||||
-rw-r--r-- | src/bin/bat/clap_app.rs | 7 | ||||
-rw-r--r-- | src/bin/bat/main.rs | 24 | ||||
-rw-r--r-- | src/config.rs | 3 | ||||
-rw-r--r-- | tests/integration_tests.rs | 12 |
7 files changed, 52 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index a672edcb..3d4f3f6c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Features +- Set terminal title to file names when Paging is not Paging::Never #2807 (@Oliver-Looney) + ## Bugfixes - Fix long file name wrapping in header, see #2835 (@FilipRazek) diff --git a/doc/long-help.txt b/doc/long-help.txt index 247120fb..3ac4a40f 100644 --- a/doc/long-help.txt +++ b/doc/long-help.txt @@ -160,6 +160,9 @@ Options: --acknowledgements Show acknowledgements. + --set-terminal-title + Sets terminal title to filenames when using a pager. + -h, --help Print help (see a summary with '-h') diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index a2c09770..8843d53b 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -289,6 +289,7 @@ impl App { use_custom_assets: !self.matches.get_flag("no-custom-assets"), #[cfg(feature = "lessopen")] use_lessopen: self.matches.get_flag("lessopen"), + set_terminal_title: self.matches.get_flag("set-terminal-title"), }) } diff --git a/src/bin/bat/clap_app.rs b/src/bin/bat/clap_app.rs index e8222a1d..6ceed784 100644 --- a/src/bin/bat/clap_app.rs +++ b/src/bin/bat/clap_app.rs @@ -567,6 +567,13 @@ pub fn build_app(interactive_output: bool) -> Command { .action(ArgAction::SetTrue) .hide_short_help(true) .help("Show acknowledgements."), + ) + .arg( + Arg::new("set-terminal-title") + .long("set-terminal-title") + .action(ArgAction::SetTrue) + .hide_short_help(true) + .help("Sets terminal title to filenames when using a pager."), ); // Check if the current directory contains a file name cache. Otherwise, diff --git a/src/bin/bat/main.rs b/src/bin/bat/main.rs index f48abdc1..d877bb9b 100644 --- a/src/bin/bat/main.rs +++ b/src/bin/bat/main.rs @@ -229,9 +229,33 @@ pub fn list_themes(cfg: &Config, config_dir: &Path, cache_dir: &Path) -> Result< Ok(()) } +fn set_terminal_title_to(new_terminal_title: String) { + let osc_command_for_setting_terminal_title = "\x1b]0;"; + let osc_end_command = "\x07"; + print!( + "{}{}{}", + osc_command_for_setting_terminal_title, new_terminal_title, osc_end_command + ); + io::stdout().flush().unwrap(); +} + +fn get_new_terminal_title(inputs: &Vec<Input>) -> String { + let mut new_terminal_title = "bat: ".to_string(); + for (index, input) in inputs.iter().enumerate() { + new_terminal_title += input.description().title(); + if index < inputs.len() - 1 { + new_terminal_title += ", "; + } + } + new_terminal_title +} + fn run_controller(inputs: Vec<Input>, config: &Config, cache_dir: &Path) -> Result<bool> { let assets = assets_from_cache_or_binary(config.use_custom_assets, cache_dir)?; let controller = Controller::new(config, &assets); + if config.paging_mode != PagingMode::Never && config.set_terminal_title { + set_terminal_title_to(get_new_terminal_title(&inputs)); + } controller.run(inputs, None) } diff --git a/src/config.rs b/src/config.rs index 83acc7df..c5cc2abd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -94,6 +94,9 @@ pub struct Config<'a> { // Whether or not to use $LESSOPEN if set #[cfg(feature = "lessopen")] pub use_lessopen: bool, + + // Weather or not to set terminal title when using a pager + pub set_terminal_title: bool, } #[cfg(all(feature = "minimal-application", feature = "paging"))] diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index ecc37ed7..3612654b 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -937,6 +937,18 @@ fn env_var_bat_paging() { } #[test] +fn basic_set_terminal_title() { + bat() + .arg("--paging=always") + .arg("--set-terminal-title") + .arg("test.txt") + .assert() + .success() + .stdout("\u{1b}]0;bat: test.txt\x07hello world\n") + .stderr(""); +} + +#[test] fn diagnostic_sanity_check() { bat() .arg("--diagnostic") |