Skip to content

Commit 18a0218

Browse files
committed
refactor: avoid strange round-trip of draw_fn arguments
1 parent 440cdd3 commit 18a0218

File tree

6 files changed

+120
-57
lines changed

6 files changed

+120
-57
lines changed

src/handlers/commit_meta.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ impl StateMachine<'_> {
3030
if self.config.commit_style.is_omitted {
3131
return Ok(());
3232
}
33-
let (mut draw_fn, pad, decoration_ansi_term_style) =
34-
draw::get_draw_function(self.config.commit_style.decoration_style);
33+
let draw_fn = draw::get_draw_function(self.config.commit_style.decoration_style);
3534
let (formatted_line, formatted_raw_line) = if self.config.hyperlinks {
3635
(
3736
features::hyperlinks::format_commit_line_with_osc8_commit_hyperlink(
@@ -49,13 +48,14 @@ impl StateMachine<'_> {
4948

5049
draw_fn(
5150
self.painter.writer,
52-
&format!("{}{}", formatted_line, if pad { " " } else { "" }),
53-
&format!("{}{}", formatted_raw_line, if pad { " " } else { "" }),
51+
&formatted_line,
52+
&formatted_raw_line,
5453
"",
5554
&self.config.decorations_width,
5655
self.config.commit_style,
57-
decoration_ansi_term_style,
56+
false,
5857
)?;
58+
5959
Ok(())
6060
}
6161
}

src/handlers/diff_header.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -277,21 +277,22 @@ pub fn write_generic_diff_header_header_line(
277277
if config.file_style.is_omitted && !config.color_only {
278278
return Ok(());
279279
}
280-
let (mut draw_fn, pad, decoration_ansi_term_style) =
281-
draw::get_draw_function(config.file_style.decoration_style);
280+
let draw_fn = draw::get_draw_function(config.file_style.decoration_style);
282281
if !config.color_only {
283282
// Maintain 1-1 correspondence between input and output lines.
284283
writeln!(painter.writer)?;
285284
}
285+
286286
draw_fn(
287287
painter.writer,
288-
&format!("{}{}", line, if pad { " " } else { "" }),
289-
&format!("{}{}", raw_line, if pad { " " } else { "" }),
288+
line,
289+
raw_line,
290290
mode_info,
291291
&config.decorations_width,
292292
config.file_style,
293-
decoration_ansi_term_style,
293+
false,
294294
)?;
295+
295296
if !mode_info.is_empty() {
296297
mode_info.truncate(0);
297298
}

src/handlers/draw.rs

Lines changed: 97 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,41 +15,104 @@ fn paint_text(text_style: Style, text: &str, addendum: &str) -> String {
1515
}
1616
}
1717

18-
pub type DrawFunction = dyn FnMut(
19-
&mut dyn Write,
20-
&str,
21-
&str,
22-
&str,
23-
&Width,
24-
Style,
25-
ansi_term::Style,
26-
) -> std::io::Result<()>;
18+
pub type DrawFunction =
19+
dyn Fn(&mut dyn Write, &str, &str, &str, &Width, Style, bool) -> std::io::Result<()>;
2720

28-
pub fn get_draw_function(
29-
decoration_style: DecorationStyle,
30-
) -> (Box<DrawFunction>, bool, ansi_term::Style) {
31-
match decoration_style {
32-
DecorationStyle::Box(style) => (Box::new(write_boxed), true, style),
33-
DecorationStyle::BoxWithUnderline(style) => {
34-
(Box::new(write_boxed_with_underline), true, style)
35-
}
36-
DecorationStyle::BoxWithOverline(style) => {
37-
// TODO: not implemented
38-
(Box::new(write_boxed), true, style)
39-
}
40-
DecorationStyle::BoxWithUnderOverline(style) => {
41-
// TODO: not implemented
42-
(Box::new(write_boxed), true, style)
43-
}
44-
DecorationStyle::Underline(style) => (Box::new(write_underlined), false, style),
45-
DecorationStyle::Overline(style) => (Box::new(write_overlined), false, style),
46-
DecorationStyle::UnderOverline(style) => (Box::new(write_underoverlined), false, style),
47-
DecorationStyle::NoDecoration => (
48-
Box::new(write_no_decoration),
49-
false,
50-
ansi_term::Style::new(),
51-
),
52-
}
21+
pub fn get_draw_function(decoration_style: DecorationStyle) -> Box<DrawFunction> {
22+
Box::new(
23+
move |writer, text, raw_text, addendum, line_width, text_style, never_pad| {
24+
match decoration_style {
25+
DecorationStyle::Box(style) => {
26+
if never_pad {
27+
write_boxed(
28+
writer, text, raw_text, addendum, line_width, text_style, style,
29+
)
30+
} else {
31+
write_boxed(
32+
writer,
33+
&format!("{text} "),
34+
&format!("{raw_text} "),
35+
addendum,
36+
line_width,
37+
text_style,
38+
style,
39+
)
40+
}
41+
}
42+
DecorationStyle::BoxWithUnderline(style) => {
43+
if never_pad {
44+
write_boxed_with_underline(
45+
writer, text, raw_text, addendum, line_width, text_style, style,
46+
)
47+
} else {
48+
write_boxed_with_underline(
49+
writer,
50+
&format!("{text} "),
51+
&format!("{raw_text} "),
52+
addendum,
53+
line_width,
54+
text_style,
55+
style,
56+
)
57+
}
58+
}
59+
// TODO: not implemented
60+
DecorationStyle::BoxWithOverline(style) => {
61+
if never_pad {
62+
write_boxed_with_underline(
63+
writer, text, raw_text, addendum, line_width, text_style, style,
64+
)
65+
} else {
66+
write_boxed_with_underline(
67+
writer,
68+
&format!("{text} "),
69+
&format!("{raw_text} "),
70+
addendum,
71+
line_width,
72+
text_style,
73+
style,
74+
)
75+
}
76+
}
77+
// TODO: not implemented
78+
DecorationStyle::BoxWithUnderOverline(style) => {
79+
if never_pad {
80+
write_boxed_with_underline(
81+
writer, text, raw_text, addendum, line_width, text_style, style,
82+
)
83+
} else {
84+
write_boxed_with_underline(
85+
writer,
86+
&format!("{text} "),
87+
&format!("{raw_text} "),
88+
addendum,
89+
line_width,
90+
text_style,
91+
style,
92+
)
93+
}
94+
}
95+
DecorationStyle::Underline(style) => write_underlined(
96+
writer, text, raw_text, addendum, line_width, text_style, style,
97+
),
98+
DecorationStyle::Overline(style) => write_overlined(
99+
writer, text, raw_text, addendum, line_width, text_style, style,
100+
),
101+
DecorationStyle::UnderOverline(style) => write_underoverlined(
102+
writer, text, raw_text, addendum, line_width, text_style, style,
103+
),
104+
DecorationStyle::NoDecoration => write_no_decoration(
105+
writer,
106+
text,
107+
raw_text,
108+
addendum,
109+
line_width,
110+
text_style,
111+
ansi_term::Style::new(),
112+
),
113+
}
114+
},
115+
)
53116
}
54117

55118
fn write_no_decoration(

src/handlers/hunk_header.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,20 +265,21 @@ fn write_hunk_header_raw(
265265
raw_line: &str,
266266
config: &Config,
267267
) -> std::io::Result<()> {
268-
let (mut draw_fn, pad, decoration_ansi_term_style) =
269-
draw::get_draw_function(config.hunk_header_style.decoration_style);
268+
let draw_fn = draw::get_draw_function(config.hunk_header_style.decoration_style);
270269
if config.hunk_header_style.decoration_style != DecorationStyle::NoDecoration {
271270
writeln!(painter.writer)?;
272271
}
272+
273273
draw_fn(
274274
painter.writer,
275-
&format!("{}{}", line, if pad { " " } else { "" }),
276-
&format!("{}{}", raw_line, if pad { " " } else { "" }),
275+
line,
276+
raw_line,
277277
"",
278278
&config.decorations_width,
279279
config.hunk_header_style,
280-
decoration_ansi_term_style,
280+
false,
281281
)?;
282+
282283
Ok(())
283284
}
284285

@@ -300,7 +301,7 @@ pub fn write_line_of_code_with_optional_path_and_line_number(
300301
file_path_separator: &str,
301302
config: &Config,
302303
) -> std::io::Result<()> {
303-
let (mut draw_fn, _, decoration_ansi_term_style) = draw::get_draw_function(decoration_style);
304+
let draw_fn = draw::get_draw_function(decoration_style);
304305
let line = if config.color_only {
305306
line.to_string()
306307
} else if matches!(include_code_fragment, HunkHeaderIncludeCodeFragment::Yes)
@@ -340,7 +341,7 @@ pub fn write_line_of_code_with_optional_path_and_line_number(
340341
"",
341342
&config.decorations_width,
342343
config.null_style,
343-
decoration_ansi_term_style,
344+
true,
344345
)?;
345346
painter.output_buffer.clear();
346347
}

src/handlers/merge_conflict.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -183,15 +183,13 @@ fn write_diff_header(
183183
painter: &mut paint::Painter,
184184
config: &config::Config,
185185
) -> std::io::Result<()> {
186-
let (mut draw_fn, pad, decoration_ansi_term_style) =
187-
draw::get_draw_function(style.decoration_style);
186+
let draw_fn = draw::get_draw_function(style.decoration_style);
188187
let derived_commit_name = &painter.merge_conflict_commit_names[derived_commit_type];
189188
let text = if let Some(_ancestral_commit) = &painter.merge_conflict_commit_names[Ancestral] {
190189
format!(
191-
"ancestor {} {}{}",
190+
"ancestor {} {}",
192191
config.right_arrow,
193192
derived_commit_name.as_deref().unwrap_or("?"),
194-
if pad { " " } else { "" }
195193
)
196194
} else {
197195
derived_commit_name.as_deref().unwrap_or("?").to_string()
@@ -203,7 +201,7 @@ fn write_diff_header(
203201
"",
204202
&config.decorations_width,
205203
style,
206-
decoration_ansi_term_style,
204+
false,
207205
)?;
208206
Ok(())
209207
}

src/subcommands/external.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ mod test {
216216
}
217217

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

0 commit comments

Comments
 (0)