mirror of
https://github.com/martinvonz/jj.git
synced 2025-01-18 02:04:19 +00:00
formatter: don't write escape codes until we write text
We often end up writing escape codes for one style and then immediately after, we write escape codes for another style. That seems harmless, but it's a little ugly. More importantly, it prepares for not emitting any escapes for turning off attributes at the end of formatted contents across multiple lines (see next commit).
This commit is contained in:
parent
041b42ced6
commit
6ae671960c
5 changed files with 16 additions and 12 deletions
|
@ -346,6 +346,7 @@ fn color_for_name(color_name: &str) -> Option<Color> {
|
|||
|
||||
impl<W: Write> Write for ColorFormatter<W> {
|
||||
fn write(&mut self, data: &[u8]) -> Result<usize, Error> {
|
||||
self.write_new_style()?;
|
||||
self.output.write(data)
|
||||
}
|
||||
|
||||
|
@ -357,12 +358,15 @@ impl<W: Write> Write for ColorFormatter<W> {
|
|||
impl<W: Write> Formatter for ColorFormatter<W> {
|
||||
fn push_label(&mut self, label: &str) -> io::Result<()> {
|
||||
self.labels.push(label.to_owned());
|
||||
self.write_new_style()
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn pop_label(&mut self) -> io::Result<()> {
|
||||
self.labels.pop();
|
||||
self.write_new_style()
|
||||
if self.labels.is_empty() {
|
||||
self.write_new_style()?
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -511,7 +515,7 @@ mod tests {
|
|||
[1m bold only [0m
|
||||
[4m underlined only [24m
|
||||
[1m[4m[38;5;2m[48;5;3m single rule [0m
|
||||
[38;5;1m[48;5;4m two rules [49m[39m
|
||||
[38;5;1m[48;5;4m two rules [39m[49m
|
||||
"###);
|
||||
}
|
||||
|
||||
|
|
|
@ -80,7 +80,7 @@ fn test_log_default() {
|
|||
| [0m
|
||||
o [38;5;5m9a45c67d3e96[39m [38;5;3mtest.user@example.com[39m [38;5;6m2001-02-03 04:05:08.000 +07:00[39m [38;5;4m4291e264ae97[39m
|
||||
| add a file
|
||||
o [38;5;5m000000000000[39m [38;5;3m[39m [38;5;6m1970-01-01 00:00:00.000 +00:00[39m [38;5;4m000000000000[39m
|
||||
o [38;5;5m000000000000[39m [38;5;6m1970-01-01 00:00:00.000 +00:00[39m [38;5;4m000000000000[39m
|
||||
(no description set)
|
||||
"###);
|
||||
|
||||
|
@ -91,7 +91,7 @@ fn test_log_default() {
|
|||
[38;5;15mdescription 1[39m
|
||||
[0m[38;5;5m9a45c67d3e96[39m [38;5;3mtest.user@example.com[39m [38;5;6m2001-02-03 04:05:08.000 +07:00[39m [38;5;4m4291e264ae97[39m
|
||||
add a file
|
||||
[38;5;5m000000000000[39m [38;5;3m[39m [38;5;6m1970-01-01 00:00:00.000 +00:00[39m [38;5;4m000000000000[39m
|
||||
[38;5;5m000000000000[39m [38;5;6m1970-01-01 00:00:00.000 +00:00[39m [38;5;4m000000000000[39m
|
||||
(no description set)
|
||||
"###);
|
||||
}
|
||||
|
@ -138,7 +138,7 @@ fn test_log_default_divergence() {
|
|||
| @ [1m[38;5;9m9a45c67d3e96??[39m [38;5;11mtest.user@example.com[39m [38;5;14m2001-02-03 04:05:08.000 +07:00[39m [38;5;12m7a17d52e633c[39m
|
||||
|/ [38;5;15mdescription 1[39m
|
||||
| [0m
|
||||
o [38;5;5m000000000000[39m [38;5;3m[39m [38;5;6m1970-01-01 00:00:00.000 +00:00[39m [38;5;4m000000000000[39m
|
||||
o [38;5;5m000000000000[39m [38;5;6m1970-01-01 00:00:00.000 +00:00[39m [38;5;4m000000000000[39m
|
||||
(no description set)
|
||||
"###);
|
||||
}
|
||||
|
|
|
@ -182,7 +182,7 @@ fn test_color_config() {
|
|||
// Test that --color=always is respected.
|
||||
let stdout = test_env.jj_cmd_success(&repo_path, &["--color=always", "log", "-T", "commit_id"]);
|
||||
insta::assert_snapshot!(stdout, @r###"
|
||||
@ [1m[38;5;12m230dd059e1b059aefc0da06a2e5a7dbf22362f22[39m[0m
|
||||
@ [1m[38;5;12m230dd059e1b059aefc0da06a2e5a7dbf22362f22[0m
|
||||
o [38;5;4m0000000000000000000000000000000000000000[39m
|
||||
"###);
|
||||
|
||||
|
@ -193,7 +193,7 @@ color="always""#,
|
|||
);
|
||||
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
|
||||
insta::assert_snapshot!(stdout, @r###"
|
||||
@ [1m[38;5;12m230dd059e1b059aefc0da06a2e5a7dbf22362f22[39m[0m
|
||||
@ [1m[38;5;12m230dd059e1b059aefc0da06a2e5a7dbf22362f22[0m
|
||||
o [38;5;4m0000000000000000000000000000000000000000[39m
|
||||
"###);
|
||||
|
||||
|
@ -249,7 +249,7 @@ color="always""#,
|
|||
test_env.add_env_var("NO_COLOR", "");
|
||||
let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "commit_id"]);
|
||||
insta::assert_snapshot!(stdout, @r###"
|
||||
@ [1m[38;5;12m230dd059e1b059aefc0da06a2e5a7dbf22362f22[39m[0m
|
||||
@ [1m[38;5;12m230dd059e1b059aefc0da06a2e5a7dbf22362f22[0m
|
||||
o [38;5;4m0000000000000000000000000000000000000000[39m
|
||||
"###);
|
||||
|
||||
|
|
|
@ -574,7 +574,7 @@ fn test_graph_template_color() {
|
|||
// extra line at the end
|
||||
insta::assert_snapshot!(stdout, @r###"
|
||||
@ [1m[38;5;2msingle line
|
||||
| [39m[0m
|
||||
| [0m
|
||||
o [38;5;1mfirst line
|
||||
| second line
|
||||
| third line
|
||||
|
|
|
@ -386,7 +386,7 @@ fn test_too_many_parents() {
|
|||
// Test warning color
|
||||
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["resolve", "--list", "--color=always"]),
|
||||
@r###"
|
||||
file [38;5;3m[38;5;1m3-sided[38;5;3m conflict[39m
|
||||
file [38;5;1m3-sided[38;5;3m conflict[39m
|
||||
"###);
|
||||
|
||||
let error = test_env.jj_cmd_failure(&repo_path, &["resolve"]);
|
||||
|
@ -518,7 +518,7 @@ fn test_description_with_dir_and_deletion() {
|
|||
// Test warning color. The deletion is fine, so it's not highlighted
|
||||
insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["resolve", "--list", "--color=always"]),
|
||||
@r###"
|
||||
file [38;5;3m[38;5;1m3-sided[38;5;3m conflict including 1 deletion and [38;5;1ma directory[38;5;3m[39m
|
||||
file [38;5;1m3-sided[38;5;3m conflict including 1 deletion and [38;5;1ma directory[39m
|
||||
"###);
|
||||
let error = test_env.jj_cmd_failure(&repo_path, &["resolve"]);
|
||||
insta::assert_snapshot!(error, @r###"
|
||||
|
|
Loading…
Reference in a new issue