Skip to content

Commit

Permalink
Refactor handle_hunk_header_line and should_handle (dandavison#333)
Browse files Browse the repository at this point in the history
* Delete pad and move whitespace handle into draw

* Revert deleting pad

* Refactor handle_hunk_header_line and should_handle
  • Loading branch information
ryuta69 authored Oct 6, 2020
1 parent 31e2661 commit 1dbfd35
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
20 changes: 10 additions & 10 deletions src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,9 @@ where
painter.paint_buffered_minus_and_plus_lines();
state = State::HunkHeader;
painter.set_highlighter();
if should_handle(&state, config) {
painter.emit()?;
handle_hunk_header_line(&mut painter, &line, &raw_line, &plus_file, config)?;
continue;
}
painter.emit()?;
handle_hunk_header_line(&mut painter, &line, &raw_line, &plus_file, config)?;
continue;
} else if source == Source::DiffUnified && line.starts_with("Only in ")
|| line.starts_with("Submodule ")
|| line.starts_with("Binary files ")
Expand Down Expand Up @@ -193,9 +191,6 @@ where

/// Should a handle_* function be called on this element?
fn should_handle(state: &State, config: &Config) -> bool {
if *state == State::HunkHeader && config.line_numbers {
return true;
}
let style = config.get_style(state);
!(style.is_raw && style.decoration_style == DecorationStyle::NoDecoration)
}
Expand Down Expand Up @@ -381,20 +376,25 @@ fn handle_hunk_header_line(
config: &Config,
) -> std::io::Result<()> {
let decoration_ansi_term_style;
let mut pad = false;
let draw_fn = match config.hunk_header_style.decoration_style {
DecorationStyle::Box(style) => {
pad = true;
decoration_ansi_term_style = style;
draw::write_boxed
}
DecorationStyle::BoxWithUnderline(style) => {
pad = true;
decoration_ansi_term_style = style;
draw::write_boxed_with_underline
}
DecorationStyle::BoxWithOverline(style) => {
pad = true;
decoration_ansi_term_style = style;
draw::write_boxed // TODO: not implemented
}
DecorationStyle::BoxWithUnderOverline(style) => {
pad = true;
decoration_ansi_term_style = style;
draw::write_boxed // TODO: not implemented
}
Expand Down Expand Up @@ -423,8 +423,8 @@ fn handle_hunk_header_line(
}
draw_fn(
painter.writer,
&format!("{} ", line),
&format!("{} ", raw_line),
&format!("{}{}", line, if pad { " " } else { "" }),
&format!("{}{}", raw_line, if pad { " " } else { "" }),
&config.decorations_width,
config.hunk_header_style,
decoration_ansi_term_style,
Expand Down
4 changes: 1 addition & 3 deletions src/tests/test_example_diffs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -891,9 +891,7 @@ src/align.rs
} else {
output_lines[n]
};
// TODO: this trim() can be removed by simplifing width_boxed of draw_fn.
assert_eq!(input_line.trim(), output_line.trim());
// assert_eq!(input_line, output_line);
assert_eq!(input_line, output_line);
}
}

Expand Down

0 comments on commit 1dbfd35

Please sign in to comment.