Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2783 setting terminal title #2807

Merged
merged 33 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
2e103ee
able to set terminal title to hardcoded value
Oliver-Looney Dec 10, 2023
4863d42
title is being reset on quit, so no need to restore terminal title
Oliver-Looney Dec 10, 2023
b9b5542
successfully setting the terminal title to bat's input's names
Oliver-Looney Dec 10, 2023
069318b
fixed formatting of terminal title
Oliver-Looney Dec 10, 2023
6ad800e
tidied commits
Oliver-Looney Dec 10, 2023
fd84e4f
fixed all but two failing tests. Last two tests are erroring because …
Oliver-Looney Dec 11, 2023
12b74df
terminal title is only set when pager is being used
Oliver-Looney Dec 18, 2023
907af9e
updated tests since terminal title is set conditionally
Oliver-Looney Dec 18, 2023
1679460
updated CHANGELOG.md
Oliver-Looney Dec 18, 2023
321b3ec
updated CHANGELOG.md
Oliver-Looney Dec 18, 2023
0027055
fixed system_wide_config.rs tests
Oliver-Looney Dec 18, 2023
5b4ce68
ran cargo fmt
Oliver-Looney Dec 18, 2023
d7503bf
Merge branch 'master' into 2783-setting-terminal-title
Oliver-Looney Dec 27, 2023
57016f4
small refactoring of set terminal title function
Oliver-Looney Dec 31, 2023
3b0ade9
slightly changed set terminal command to match docs & broke print lin…
Oliver-Looney Dec 31, 2023
6f1cc80
Merge branch 'master' into 2783-setting-terminal-title
Oliver-Looney Dec 31, 2023
c261b41
trying to fix failing system_wide_config.rs tests
Oliver-Looney Dec 31, 2023
b4fdb5d
Merge branch 'master' into 2783-setting-terminal-title
Oliver-Looney Jan 1, 2024
9239b12
added a flag to config for setting terminal title
Oliver-Looney Jan 27, 2024
b33e33f
terminal title is only set if user opts in with --set_terminal_title …
Oliver-Looney Jan 27, 2024
c911829
fixed failing tests
Oliver-Looney Jan 27, 2024
f6d76e0
added integration test for setting terminal title
Oliver-Looney Jan 27, 2024
2225493
ran cargo fmt
Oliver-Looney Jan 27, 2024
9be2a36
fixed system wide config tests failing
Oliver-Looney Jan 27, 2024
e9a6aaa
cargo fmt
Oliver-Looney Jan 27, 2024
60e32cf
removed set_terminal_title arg from clap_app.rs since other boolean a…
Oliver-Looney Jan 27, 2024
7f12989
added set_terminal_title arg to clap_app.rs to fix ci errors
Oliver-Looney Jan 29, 2024
a8d0733
updated integration_tests.rs
Oliver-Looney Jan 30, 2024
7ce010d
Using hypens instead of underscores for set-terminal-title command
Oliver-Looney Feb 8, 2024
02077db
undid unnecessary api visibility changes
Oliver-Looney Feb 8, 2024
8a51172
simplified basic_set_terminal_title
Oliver-Looney Feb 11, 2024
c3f2ddf
Merge branch 'master' into 2783-setting-terminal-title
Oliver-Looney Feb 11, 2024
1f62820
Merge branch 'master' into 2783-setting-terminal-title
Oliver-Looney Feb 12, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Features

- Set terminal title to file names when Paging is not Paging::Never #2807 (@Oliver-Looney)

## Bugfixes

- Fix `NO_COLOR` support, see #2767 (@acuteenvy)
Expand Down
15 changes: 15 additions & 0 deletions src/bin/bat/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,24 @@ pub fn list_themes(cfg: &Config, config_dir: &Path, cache_dir: &Path) -> Result<
Ok(())
}

fn set_terminal_title_to_inputs_names(inputs: &Vec<Input>) {
let mut input_names = "bat: ".to_string();
for (index, input) in inputs.iter().enumerate() {
input_names += &input.description.name.to_string();
if index < inputs.len() - 1 {
input_names += ", ";
}
}
print!("\x1b]2;{}\x07", input_names);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this behave well in all major terminal emulators on all major OSes?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe ANSI escape characters are standard across terminal emulators on different OSes, https://en.wikipedia.org/wiki/ANSI_escape_code#OSC_(Operating_System_Command)_sequences

I have updated the 2 in this line to 0 to match those docs

io::stdout().flush().unwrap();
}

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 {
set_terminal_title_to_inputs_names(&inputs);
}
controller.run(inputs, None)
}

Expand Down
4 changes: 2 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::error::*;
/// This tells bat how to refer to the input.
#[derive(Clone)]
pub struct InputDescription {
pub(crate) name: String,
pub name: String,
eth-p marked this conversation as resolved.
Show resolved Hide resolved

/// The input title.
/// This replaces the name if provided.
Expand Down Expand Up @@ -94,7 +94,7 @@ pub(crate) struct InputMetadata {
pub struct Input<'a> {
pub(crate) kind: InputKind<'a>,
pub(crate) metadata: InputMetadata,
pub(crate) description: InputDescription,
pub description: InputDescription,
eth-p marked this conversation as resolved.
Show resolved Hide resolved
}

pub(crate) enum OpenedInputKind {
Expand Down
14 changes: 7 additions & 7 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -653,7 +653,7 @@ fn pager_disable() {
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("hello world\n").normalize());
.stdout(predicate::eq("\u{1b}]2;bat: test.txt\x07hello world\n").normalize());
}

#[test]
Expand Down Expand Up @@ -734,7 +734,7 @@ fn env_var_pager_value_bat() {
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("hello world\n").normalize());
.stdout(predicate::eq("\u{1b}]2;bat: test.txt\x07hello world\n").normalize());
}

#[test]
Expand Down Expand Up @@ -772,7 +772,7 @@ fn pager_most_from_pager_env_var() {
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("hello world\n").normalize());
.stdout(predicate::eq("\u{1b}]2;bat: test.txt\x07hello world\n").normalize());
});
}

Expand Down Expand Up @@ -818,7 +818,7 @@ fn pager_most_with_arg() {
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("hello world\n").normalize());
.stdout(predicate::eq("\u{1b}]2;bat: test.txt\x07hello world\n").normalize());
});
}

Expand All @@ -833,7 +833,7 @@ fn pager_more() {
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("hello world\n").normalize());
.stdout(predicate::eq("\u{1b}]2;bat: test.txt\x07hello world\n").normalize());
});
}

Expand Down Expand Up @@ -896,7 +896,7 @@ fn enable_pager_if_disable_paging_flag_comes_before_paging() {
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("pager-output\n").normalize());
.stdout(predicate::eq("\u{1b}]2;bat: test.txt\x07pager-output\n").normalize());
}

#[test]
Expand All @@ -908,7 +908,7 @@ fn enable_pager_if_pp_flag_comes_before_paging() {
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("pager-output\n").normalize());
.stdout(predicate::eq("\u{1b}]2;bat: test.txt\x07pager-output\n").normalize());
}

#[test]
Expand Down
10 changes: 4 additions & 6 deletions tests/system_wide_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ use utils::command::bat_with_config;
#[test]
#[ignore]
fn use_systemwide_config() {
bat_with_config()
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("dummy-pager-from-system-config\n").normalize());
bat_with_config().arg("test.txt").assert().success().stdout(
predicate::eq("\u{1b}]2;bat: test.txt\x07dummy-pager-from-system-config\n").normalize(),
);
}

// This test is ignored, as it needs a special system wide config put into place
Expand All @@ -25,5 +23,5 @@ fn config_overrides_system_config() {
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("dummy-pager-from-config\n").normalize());
.stdout(predicate::eq("\u{1b}]2;bat: test.txt\x07dummy-pager-from-config\n").normalize());
}