summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorulwlu <ooulwluoo@gmail.com>2020-12-08 23:01:47 +0900
committerGitHub <noreply@github.com>2020-12-08 14:01:47 +0000
commit1dfb1f7fb18e4cc9817d11570c1d6f4ecb2e8081 (patch)
tree0c4f453c645ba523957bfa8af9e031738a30a658
parentf83f53f9d3188b657d0659c264eef5cd327c05bc (diff)
Make file style to be colored with same structure while color_only #405 (#436)
* Add config property of color_only * Delete force assign raw to file_style when color_only * Print filemeta in color with rawline when color_only mode * Cargo fmt * Add test if file_style with color_only has style * Add comment about color_only
-rw-r--r--src/config.rs2
-rw-r--r--src/delta.rs18
-rw-r--r--src/options/set.rs6
-rw-r--r--src/tests/test_example_diffs.rs18
4 files changed, 38 insertions, 6 deletions
diff --git a/src/config.rs b/src/config.rs
index a8718c51..b2cd2eab 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -21,6 +21,7 @@ pub struct Config {
pub available_terminal_width: usize,
pub background_color_extends_to_terminal_width: bool,
pub commit_style: Style,
+ pub color_only: bool,
pub decorations_width: cli::Width,
pub file_added_label: String,
pub file_copied_label: String,
@@ -150,6 +151,7 @@ impl From<cli::Opt> for Config {
.computed
.background_color_extends_to_terminal_width,
commit_style,
+ color_only: opt.color_only,
decorations_width: opt.computed.decorations_width,
file_added_label: opt.file_added_label,
file_copied_label: opt.file_copied_label,
diff --git a/src/delta.rs b/src/delta.rs
index 94b72650..d7724802 100644
--- a/src/delta.rs
+++ b/src/delta.rs
@@ -115,6 +115,10 @@ where
&minus_file,
));
}
+ if config.color_only {
+ handle_generic_file_meta_header_line(&mut painter, &line, &raw_line, config)?;
+ continue;
+ }
} else if (state == State::FileMeta || source == Source::DiffUnified)
&& (line.starts_with("+++ ")
|| line.starts_with("rename to ")
@@ -127,6 +131,10 @@ where
&plus_file,
));
current_file_pair = Some((minus_file.clone(), plus_file.clone()));
+ if config.color_only {
+ handle_generic_file_meta_header_line(&mut painter, &line, &raw_line, config)?;
+ continue;
+ }
if should_handle(&State::FileMeta, config)
&& handled_file_meta_header_line_file_pair != current_file_pair
{
@@ -181,7 +189,8 @@ where
continue;
}
- if state == State::FileMeta && should_handle(&State::FileMeta, config) {
+ if state == State::FileMeta && should_handle(&State::FileMeta, config) && !config.color_only
+ {
// The file metadata section is 4 lines. Skip them under non-plain file-styles.
continue;
} else {
@@ -318,7 +327,8 @@ fn handle_generic_file_meta_header_line(
raw_line: &str,
config: &Config,
) -> std::io::Result<()> {
- if config.file_style.is_omitted {
+ // FIXME: this may be able to be refactored by moving to should_handle.
+ if config.file_style.is_omitted && !config.color_only {
return Ok(());
}
let decoration_ansi_term_style;
@@ -361,7 +371,9 @@ fn handle_generic_file_meta_header_line(
draw::write_no_decoration
}
};
- writeln!(painter.writer)?;
+ if !config.color_only {
+ writeln!(painter.writer)?;
+ }
draw_fn(
painter.writer,
&format!("{}{}", line, if pad { " " } else { "" }),
diff --git a/src/options/set.rs b/src/options/set.rs
index 1fc8b4d2..eda89add 100644
--- a/src/options/set.rs
+++ b/src/options/set.rs
@@ -190,13 +190,13 @@ pub fn set_options(
compute_line_numbers_mode(opt, &builtin_features, git_config, &option_names);
opt.computed.paging_mode = parse_paging_mode(&opt.paging_mode);
- // --color-only is used for interactive.diffFilter (git add -p) and side-by-side cannot be used
- // there (does not emit lines in 1-1 correspondence with raw git output). See #274.
+ // --color-only is used for interactive.diffFilter (git add -p). side-by-side, and
+ // **-decoration-style cannot be used there (does not emit lines in 1-1 correspondence with raw git output).
+ // See #274.
if opt.color_only {
opt.side_by_side = false;
opt.file_decoration_style = "none".to_string();
opt.commit_decoration_style = "none".to_string();
- opt.file_style = "raw".to_string();
opt.commit_style = "raw".to_string();
opt.hunk_header_style = "raw".to_string();
opt.hunk_header_decoration_style = "none".to_string();
diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs
index 6668acce..e9a265de 100644
--- a/src/tests/test_example_diffs.rs
+++ b/src/tests/test_example_diffs.rs
@@ -972,6 +972,24 @@ src/align.rs
}
#[test]
+ // TODO: commit-style and hunk-header-style are required to add.
+ fn test_file_style_with_color_only_has_style() {
+ let config =
+ integration_test_utils::make_config_from_args(&["--color-only", "--file-style", "red"]);
+ let output = integration_test_utils::run_delta(GIT_DIFF_SINGLE_HUNK, &config);
+
+ ansi_test_utils::assert_line_has_style(&output, 8, "--- a/src/align.rs", "red", &config);
+ ansi_test_utils::assert_line_has_style(&output, 9, "+++ b/src/align.rs", "red", &config);
+ let output = strip_ansi_codes(&output);
+ assert!(output.contains(
+ "\
+--- a/src/align.rs
++++ b/src/align.rs
+"
+ ));
+ }
+
+ #[test]
fn test_hunk_header_style_colored_input_color_is_stripped_under_normal() {
let config = integration_test_utils::make_config_from_args(&[
"--hunk-header-style",