Skip to content

Commit

Permalink
refactor: avoid strange round-trip of in draw_fn arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Integral-Tech committed Dec 17, 2024
1 parent 440cdd3 commit 50029c7
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 57 deletions.
10 changes: 5 additions & 5 deletions src/handlers/commit_meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ impl StateMachine<'_> {
if self.config.commit_style.is_omitted {
return Ok(());
}
let (mut draw_fn, pad, decoration_ansi_term_style) =
draw::get_draw_function(self.config.commit_style.decoration_style);
let draw_fn = draw::get_draw_function(self.config.commit_style.decoration_style);
let (formatted_line, formatted_raw_line) = if self.config.hyperlinks {
(
features::hyperlinks::format_commit_line_with_osc8_commit_hyperlink(
Expand All @@ -49,13 +48,14 @@ impl StateMachine<'_> {

draw_fn(
self.painter.writer,
&format!("{}{}", formatted_line, if pad { " " } else { "" }),
&format!("{}{}", formatted_raw_line, if pad { " " } else { "" }),
&formatted_line,
&formatted_raw_line,
"",
&self.config.decorations_width,
self.config.commit_style,
decoration_ansi_term_style,
false,
)?;

Ok(())
}
}
11 changes: 6 additions & 5 deletions src/handlers/diff_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,21 +277,22 @@ pub fn write_generic_diff_header_header_line(
if config.file_style.is_omitted && !config.color_only {
return Ok(());
}
let (mut draw_fn, pad, decoration_ansi_term_style) =
draw::get_draw_function(config.file_style.decoration_style);
let draw_fn = draw::get_draw_function(config.file_style.decoration_style);
if !config.color_only {
// Maintain 1-1 correspondence between input and output lines.
writeln!(painter.writer)?;
}

draw_fn(
painter.writer,
&format!("{}{}", line, if pad { " " } else { "" }),
&format!("{}{}", raw_line, if pad { " " } else { "" }),
line,
raw_line,
mode_info,
&config.decorations_width,
config.file_style,
decoration_ansi_term_style,
false,
)?;

if !mode_info.is_empty() {
mode_info.truncate(0);
}
Expand Down
131 changes: 97 additions & 34 deletions src/handlers/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,41 +15,104 @@ fn paint_text(text_style: Style, text: &str, addendum: &str) -> String {
}
}

pub type DrawFunction = dyn FnMut(
&mut dyn Write,
&str,
&str,
&str,
&Width,
Style,
ansi_term::Style,
) -> std::io::Result<()>;
pub type DrawFunction =
dyn Fn(&mut dyn Write, &str, &str, &str, &Width, Style, bool) -> std::io::Result<()>;

pub fn get_draw_function(
decoration_style: DecorationStyle,
) -> (Box<DrawFunction>, bool, ansi_term::Style) {
match decoration_style {
DecorationStyle::Box(style) => (Box::new(write_boxed), true, style),
DecorationStyle::BoxWithUnderline(style) => {
(Box::new(write_boxed_with_underline), true, style)
}
DecorationStyle::BoxWithOverline(style) => {
// TODO: not implemented
(Box::new(write_boxed), true, style)
}
DecorationStyle::BoxWithUnderOverline(style) => {
// TODO: not implemented
(Box::new(write_boxed), true, style)
}
DecorationStyle::Underline(style) => (Box::new(write_underlined), false, style),
DecorationStyle::Overline(style) => (Box::new(write_overlined), false, style),
DecorationStyle::UnderOverline(style) => (Box::new(write_underoverlined), false, style),
DecorationStyle::NoDecoration => (
Box::new(write_no_decoration),
false,
ansi_term::Style::new(),
),
}
pub fn get_draw_function(decoration_style: DecorationStyle) -> Box<DrawFunction> {
Box::new(
move |writer, text, raw_text, addendum, line_width, text_style, never_pad| {
match decoration_style {
DecorationStyle::Box(style) => {
if never_pad {
write_boxed(
writer, text, raw_text, addendum, line_width, text_style, style,
)
} else {
write_boxed(
writer,
&format!("{text} "),
&format!("{raw_text} "),
addendum,
line_width,
text_style,
style,
)
}
}
DecorationStyle::BoxWithUnderline(style) => {
if never_pad {
write_boxed_with_underline(
writer, text, raw_text, addendum, line_width, text_style, style,
)
} else {
write_boxed_with_underline(
writer,
&format!("{text} "),
&format!("{raw_text} "),
addendum,
line_width,
text_style,
style,
)
}
}
// TODO: not implemented
DecorationStyle::BoxWithOverline(style) => {
if never_pad {
write_boxed_with_underline(
writer, text, raw_text, addendum, line_width, text_style, style,
)
} else {
write_boxed_with_underline(
writer,
&format!("{text} "),
&format!("{raw_text} "),
addendum,
line_width,
text_style,
style,
)
}
}
// TODO: not implemented
DecorationStyle::BoxWithUnderOverline(style) => {
if never_pad {
write_boxed_with_underline(
writer, text, raw_text, addendum, line_width, text_style, style,
)
} else {
write_boxed_with_underline(
writer,
&format!("{text} "),
&format!("{raw_text} "),
addendum,
line_width,
text_style,
style,
)
}
}
DecorationStyle::Underline(style) => write_underlined(
writer, text, raw_text, addendum, line_width, text_style, style,
),
DecorationStyle::Overline(style) => write_overlined(
writer, text, raw_text, addendum, line_width, text_style, style,
),
DecorationStyle::UnderOverline(style) => write_underoverlined(
writer, text, raw_text, addendum, line_width, text_style, style,
),
DecorationStyle::NoDecoration => write_no_decoration(
writer,
text,
raw_text,
addendum,
line_width,
text_style,
ansi_term::Style::new(),
),
}
},
)
}

fn write_no_decoration(
Expand Down
15 changes: 8 additions & 7 deletions src/handlers/hunk_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,20 +265,21 @@ fn write_hunk_header_raw(
raw_line: &str,
config: &Config,
) -> std::io::Result<()> {
let (mut draw_fn, pad, decoration_ansi_term_style) =
draw::get_draw_function(config.hunk_header_style.decoration_style);
let draw_fn = draw::get_draw_function(config.hunk_header_style.decoration_style);
if config.hunk_header_style.decoration_style != DecorationStyle::NoDecoration {
writeln!(painter.writer)?;
}

draw_fn(
painter.writer,
&format!("{}{}", line, if pad { " " } else { "" }),
&format!("{}{}", raw_line, if pad { " " } else { "" }),
line,
raw_line,
"",
&config.decorations_width,
config.hunk_header_style,
decoration_ansi_term_style,
false,
)?;

Ok(())
}

Expand All @@ -300,7 +301,7 @@ pub fn write_line_of_code_with_optional_path_and_line_number(
file_path_separator: &str,
config: &Config,
) -> std::io::Result<()> {
let (mut draw_fn, _, decoration_ansi_term_style) = draw::get_draw_function(decoration_style);
let draw_fn = draw::get_draw_function(decoration_style);
let line = if config.color_only {
line.to_string()
} else if matches!(include_code_fragment, HunkHeaderIncludeCodeFragment::Yes)
Expand Down Expand Up @@ -340,7 +341,7 @@ pub fn write_line_of_code_with_optional_path_and_line_number(
"",
&config.decorations_width,
config.null_style,
decoration_ansi_term_style,
true,
)?;
painter.output_buffer.clear();
}
Expand Down
8 changes: 3 additions & 5 deletions src/handlers/merge_conflict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,15 +183,13 @@ fn write_diff_header(
painter: &mut paint::Painter,
config: &config::Config,
) -> std::io::Result<()> {
let (mut draw_fn, pad, decoration_ansi_term_style) =
draw::get_draw_function(style.decoration_style);
let draw_fn = draw::get_draw_function(style.decoration_style);
let derived_commit_name = &painter.merge_conflict_commit_names[derived_commit_type];
let text = if let Some(_ancestral_commit) = &painter.merge_conflict_commit_names[Ancestral] {
format!(
"ancestor {} {}{}",
"ancestor {} {}",
config.right_arrow,
derived_commit_name.as_deref().unwrap_or("?"),
if pad { " " } else { "" }
)
} else {
derived_commit_name.as_deref().unwrap_or("?").to_string()
Expand All @@ -203,7 +201,7 @@ fn write_diff_header(
"",
&config.decorations_width,
style,
decoration_ansi_term_style,
false,
)?;
Ok(())
}
Expand Down
2 changes: 1 addition & 1 deletion src/subcommands/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ mod test {
}

let mut writer = Cursor::new(vec![]);
let needle = format!("{}{}", "Y40ii4RihK6", "lHiK4BDsGS").to_string();
let needle = concat!("Y40ii4RihK6", "lHiK4BDsGS");
// --minus-style has no effect, just for cmdline parsing
let runargs = [
"--minus-style",
Expand Down

0 comments on commit 50029c7

Please sign in to comment.