Skip to content

Commit

Permalink
Make file style to be colored with same structure while color_only #405
Browse files Browse the repository at this point in the history
… (#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
  • Loading branch information
ulwlu authored Dec 8, 2020
1 parent f83f53f commit 1dfb1f7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 6 deletions.
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 15 additions & 3 deletions src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 ")
Expand All @@ -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
{
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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 { "" }),
Expand Down
6 changes: 3 additions & 3 deletions src/options/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions src/tests/test_example_diffs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,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(&[
Expand Down

0 comments on commit 1dfb1f7

Please sign in to comment.