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(); painter.paint_buffered_minus_and_plus_lines();
state = State::HunkHeader; state = State::HunkHeader;
painter.set_highlighter(); painter.set_highlighter();
if should_handle(&state, config) { painter.emit()?;
painter.emit()?; handle_hunk_header_line(&mut painter, &line, &raw_line, &plus_file, config)?;
handle_hunk_header_line(&mut painter, &line, &raw_line, &plus_file, config)?; continue;
continue;
}
} else if source == Source::DiffUnified && line.starts_with("Only in ") } else if source == Source::DiffUnified && line.starts_with("Only in ")
|| line.starts_with("Submodule ") || line.starts_with("Submodule ")
|| line.starts_with("Binary files ") || line.starts_with("Binary files ")
@ -193,9 +191,6 @@ where
/// Should a handle_* function be called on this element? /// Should a handle_* function be called on this element?
fn should_handle(state: &State, config: &Config) -> bool { fn should_handle(state: &State, config: &Config) -> bool {
if *state == State::HunkHeader && config.line_numbers {
return true;
}
let style = config.get_style(state); let style = config.get_style(state);
!(style.is_raw && style.decoration_style == DecorationStyle::NoDecoration) !(style.is_raw && style.decoration_style == DecorationStyle::NoDecoration)
} }
@ -381,20 +376,25 @@ fn handle_hunk_header_line(
config: &Config, config: &Config,
) -> std::io::Result<()> { ) -> std::io::Result<()> {
let decoration_ansi_term_style; let decoration_ansi_term_style;
let mut pad = false;
let draw_fn = match config.hunk_header_style.decoration_style { let draw_fn = match config.hunk_header_style.decoration_style {
DecorationStyle::Box(style) => { DecorationStyle::Box(style) => {
pad = true;
decoration_ansi_term_style = style; decoration_ansi_term_style = style;
draw::write_boxed draw::write_boxed
} }
DecorationStyle::BoxWithUnderline(style) => { DecorationStyle::BoxWithUnderline(style) => {
pad = true;
decoration_ansi_term_style = style; decoration_ansi_term_style = style;
draw::write_boxed_with_underline draw::write_boxed_with_underline
} }
DecorationStyle::BoxWithOverline(style) => { DecorationStyle::BoxWithOverline(style) => {
pad = true;
decoration_ansi_term_style = style; decoration_ansi_term_style = style;
draw::write_boxed // TODO: not implemented draw::write_boxed // TODO: not implemented
} }
DecorationStyle::BoxWithUnderOverline(style) => { DecorationStyle::BoxWithUnderOverline(style) => {
pad = true;
decoration_ansi_term_style = style; decoration_ansi_term_style = style;
draw::write_boxed // TODO: not implemented draw::write_boxed // TODO: not implemented
} }
@ -423,8 +423,8 @@ fn handle_hunk_header_line(
} }
draw_fn( draw_fn(
painter.writer, painter.writer,
&format!("{} ", line), &format!("{}{}", line, if pad { " " } else { "" }),
&format!("{} ", raw_line), &format!("{}{}", raw_line, if pad { " " } else { "" }),
&config.decorations_width, &config.decorations_width,
config.hunk_header_style, config.hunk_header_style,
decoration_ansi_term_style, decoration_ansi_term_style,

View File

@ -891,9 +891,7 @@ src/align.rs
} else { } else {
output_lines[n] output_lines[n]
}; };
// TODO: this trim() can be removed by simplifing width_boxed of draw_fn. assert_eq!(input_line, output_line);
assert_eq!(input_line.trim(), output_line.trim());
// assert_eq!(input_line, output_line);
} }
} }