summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2021-12-06 21:00:48 -0500
committerDan Davison <dandavison7@gmail.com>2021-12-09 20:46:28 -0500
commit9554f47ce9e79051ad7a3a9dfbdab7579003469d (patch)
tree3092eb8ff632ce62c2ec5a12e9ac3af73e722d9c
parent354731b95a09b624ab17ab5729422125f95e24d0 (diff)
Refactor
Ref #829
-rw-r--r--src/handlers/hunk.rs51
-rw-r--r--src/paint.rs10
-rw-r--r--src/style.rs21
3 files changed, 38 insertions, 44 deletions
diff --git a/src/handlers/hunk.rs b/src/handlers/hunk.rs
index e580dfa6..9f9097da 100644
--- a/src/handlers/hunk.rs
+++ b/src/handlers/hunk.rs
@@ -71,36 +71,22 @@ impl<'a> StateMachine<'a> {
}
let n_parents = diff_type.n_parents();
let line = self.painter.prepare(&self.line, n_parents);
- let state = match self.config.inspect_raw_lines {
- cli::InspectRawLines::True
- if style::line_has_style_other_than(
- &self.raw_line,
- [*style::GIT_DEFAULT_MINUS_STYLE, self.config.git_minus_style].iter(),
- ) =>
- {
- let raw_line = self.painter.prepare_raw_line(&self.raw_line, n_parents);
- HunkMinus(diff_type, Some(raw_line))
- }
- _ => HunkMinus(diff_type, None),
- };
+ let raw_line = self.maybe_raw_line(
+ n_parents,
+ &[*style::GIT_DEFAULT_MINUS_STYLE, self.config.git_minus_style],
+ );
+ let state = HunkMinus(diff_type, raw_line);
self.painter.minus_lines.push((line, state.clone()));
state
}
Some(HunkPlus(diff_type, _)) => {
let n_parents = diff_type.n_parents();
let line = self.painter.prepare(&self.line, n_parents);
- let state = match self.config.inspect_raw_lines {
- cli::InspectRawLines::True
- if style::line_has_style_other_than(
- &self.raw_line,
- [*style::GIT_DEFAULT_PLUS_STYLE, self.config.git_plus_style].iter(),
- ) =>
- {
- let raw_line = self.painter.prepare_raw_line(&self.raw_line, n_parents);
- HunkPlus(diff_type, Some(raw_line))
- }
- _ => HunkPlus(diff_type, None),
- };
+ let raw_line = self.maybe_raw_line(
+ n_parents,
+ &[*style::GIT_DEFAULT_PLUS_STYLE, self.config.git_plus_style],
+ );
+ let state = HunkPlus(diff_type, raw_line);
self.painter.plus_lines.push((line, state.clone()));
state
}
@@ -109,8 +95,11 @@ impl<'a> StateMachine<'a> {
// sequence of consecutive minus (removed) and/or plus (added) lines). Process that
// subhunk and flush the line buffers.
self.painter.paint_buffered_minus_and_plus_lines();
- self.painter.paint_zero_line(&self.line, diff_type.clone());
- HunkZero(diff_type)
+ let n_parents = diff_type.n_parents();
+ let line = self.painter.prepare(&self.line, n_parents);
+ let state = State::HunkZero(diff_type);
+ self.painter.paint_zero_line(&line, state.clone());
+ state
}
_ => {
// The first character here could be e.g. '\' from '\ No newline at end of file'. This
@@ -127,6 +116,16 @@ impl<'a> StateMachine<'a> {
self.painter.emit()?;
Ok(true)
}
+
+ fn maybe_raw_line(&self, n_parents: usize, non_raw_styles: &[style::Style]) -> Option<String> {
+ let emit_raw_line = self.config.inspect_raw_lines == cli::InspectRawLines::True
+ && style::line_has_style_other_than(&self.raw_line, non_raw_styles);
+ if emit_raw_line {
+ Some(self.painter.prepare_raw_line(&self.raw_line, n_parents))
+ } else {
+ None
+ }
+ }
}
// Return the new state corresponding to `new_line`, given the previous state. A return value of
diff --git a/src/paint.rs b/src/paint.rs
index 1ce7bf03..3a904206 100644
--- a/src/paint.rs
+++ b/src/paint.rs
@@ -176,12 +176,10 @@ impl<'p> Painter<'p> {
self.plus_lines.clear();
}
- pub fn paint_zero_line(&mut self, line: &str, diff_type: DiffType) {
- let line = self.prepare(line, diff_type.n_parents());
- let state = State::HunkZero(diff_type);
- let lines = vec![(line, state.clone())];
+ pub fn paint_zero_line(&mut self, line: &str, state: State) {
+ let lines = &[(line.to_string(), state.clone())];
let syntax_style_sections =
- get_syntax_style_sections_for_lines(&lines, self.highlighter.as_mut(), self.config);
+ get_syntax_style_sections_for_lines(lines, self.highlighter.as_mut(), self.config);
let diff_style_sections = vec![(self.config.zero_style, lines[0].0.as_str())]; // TODO: compute style from state
if self.config.side_by_side {
@@ -198,7 +196,7 @@ impl<'p> Painter<'p> {
);
} else {
Painter::paint_lines(
- &lines,
+ lines,
&syntax_style_sections,
&[diff_style_sections],
&[false],
diff --git a/src/style.rs b/src/style.rs
index 6ffb1f01..fe0da888 100644
--- a/src/style.rs
+++ b/src/style.rs
@@ -355,7 +355,7 @@ lazy_static! {
};
}
-pub fn line_has_style_other_than<'a>(line: &str, styles: impl Iterator<Item = &'a Style>) -> bool {
+pub fn line_has_style_other_than(line: &str, styles: &[Style]) -> bool {
if !ansi::string_starts_with_ansi_style_sequence(line) {
return false;
}
@@ -443,38 +443,35 @@ pub mod tests {
let plus_line_from_unconfigured_git = "\x1b[32m+\x1b[m\x1b[32m____\x1b[m\n";
// Unstyled lines should test negative, regardless of supplied styles.
- assert!(!line_has_style_other_than("", [].iter()));
- assert!(!line_has_style_other_than(
- "",
- [*GIT_DEFAULT_MINUS_STYLE].iter()
- ));
+ assert!(!line_has_style_other_than("", &[]));
+ assert!(!line_has_style_other_than("", &[*GIT_DEFAULT_MINUS_STYLE]));
// Lines from git should test negative when corresponding default is supplied
assert!(!line_has_style_other_than(
minus_line_from_unconfigured_git,
- [*GIT_DEFAULT_MINUS_STYLE].iter()
+ &[*GIT_DEFAULT_MINUS_STYLE]
));
assert!(!line_has_style_other_than(
plus_line_from_unconfigured_git,
- [*GIT_DEFAULT_PLUS_STYLE].iter()
+ &[*GIT_DEFAULT_PLUS_STYLE]
));
// Styled lines should test positive when unless their style is supplied.
assert!(line_has_style_other_than(
minus_line_from_unconfigured_git,
- [*GIT_DEFAULT_PLUS_STYLE].iter()
+ &[*GIT_DEFAULT_PLUS_STYLE]
));
assert!(line_has_style_other_than(
minus_line_from_unconfigured_git,
- [].iter()
+ &[]
));
assert!(line_has_style_other_than(
plus_line_from_unconfigured_git,
- [*GIT_DEFAULT_MINUS_STYLE].iter()
+ &[*GIT_DEFAULT_MINUS_STYLE]
));
assert!(line_has_style_other_than(
plus_line_from_unconfigured_git,
- [].iter()
+ &[]
));
}