Refactor handle_hunk_header_line and should_handle (#333)

* Delete pad and move whitespace handle into draw

* Revert deleting pad

* Refactor handle_hunk_header_line and should_handle
This commit is contained in:
ryuta69 2020-10-06 11:38:53 +09:00 committed by GitHub
parent 31e2661cde
commit 1dbfd35b54
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 13 deletions

View File

@ -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 ")
@ -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)
}
@ -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
}
@ -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,

View File

@ -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);
}
}