Skip to content

Commit 886d942

Browse files
committed
remove duplication
1 parent c38a6b5 commit 886d942

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

src/ui/printer/mod.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ impl Printer {
3939
pub fn print(&self, writer: &mut dyn std::io::Write) -> Result<()> {
4040
match &self.r#type {
4141
PrinterType::Json => {
42-
writeln!(writer, "{}", serde_json::to_string_pretty(&self.info)?)?;
42+
write!(writer, "{}", serde_json::to_string_pretty(&self.info)?)?;
4343
Ok(())
4444
}
4545
PrinterType::Yaml => {
46-
writeln!(writer, "{}", serde_yaml::to_string(&self.info)?)?;
46+
write!(writer, "{}", serde_yaml::to_string(&self.info)?)?;
4747
Ok(())
4848
}
4949
PrinterType::Plain => {
50-
write!(writer, "\x1B[?7l{}\x1B[?7h", self.info)?;
50+
write_with_line_wrapping(writer, &self.info.to_string())?;
5151
Ok(())
5252
}
5353
PrinterType::Image {
@@ -66,7 +66,7 @@ impl Printer {
6666
.add_image(info_lines, image, *resolution)
6767
.context("Failed to render image")?;
6868

69-
write!(writer, "\x1B[?7l{rendered}\x1B[?7h")?;
69+
write_with_line_wrapping(writer, &rendered)?;
7070
Ok(())
7171
}
7272
PrinterType::Ascii { art, no_bold } => {
@@ -86,21 +86,23 @@ impl Printer {
8686
"",
8787
width = logo_lines.width()
8888
)?,
89-
(None, None) => {
90-
buf.push('\n');
91-
break;
92-
}
89+
(None, None) => break,
9390
}
9491
}
9592

96-
// \x1B[?7l turns off line wrapping and \x1B[?7h turns it on
97-
write!(writer, "\x1B[?7l{buf}\x1B[?7h")?;
93+
write_with_line_wrapping(writer, &buf)?;
9894
Ok(())
9995
}
10096
}
10197
}
10298
}
10399

100+
fn write_with_line_wrapping(writer: &mut dyn std::io::Write, content: &str) -> Result<()> {
101+
// \x1B[?7l turns off line wrapping and \x1B[?7h turns it on
102+
write!(writer, "\x1B[?7l{content}\x1B[?7h")?;
103+
Ok(())
104+
}
105+
104106
impl PartialEq for PrinterType {
105107
fn eq(&self, other: &Self) -> bool {
106108
matches!(

src/ui/text_colors.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@ pub struct TextColors {
1212
}
1313

1414
impl TextColors {
15-
pub fn new(colors: &[u8], logo_primary_color: DynColors) -> Self {
15+
pub fn new(colors: &[u8], primary_color: DynColors) -> Self {
1616
let mut text_colors = Self {
17-
title: logo_primary_color,
17+
title: primary_color,
1818
tilde: DynColors::Ansi(AnsiColors::Default),
1919
underline: DynColors::Ansi(AnsiColors::Default),
20-
subtitle: logo_primary_color,
20+
subtitle: primary_color,
2121
colon: DynColors::Ansi(AnsiColors::Default),
2222
info: DynColors::Ansi(AnsiColors::Default),
2323
};
2424

2525
if !colors.is_empty() {
2626
let custom_color = colors.iter().map(num_to_color).collect::<Vec<DynColors>>();
2727

28-
text_colors.title = *custom_color.first().unwrap_or(&logo_primary_color);
28+
text_colors.title = *custom_color.first().unwrap_or(&primary_color);
2929
text_colors.tilde = *custom_color
3030
.get(1)
3131
.unwrap_or(&DynColors::Ansi(AnsiColors::Default));
3232
text_colors.underline = *custom_color
3333
.get(2)
3434
.unwrap_or(&DynColors::Ansi(AnsiColors::Default));
35-
text_colors.subtitle = *custom_color.get(3).unwrap_or(&logo_primary_color);
35+
text_colors.subtitle = *custom_color.get(3).unwrap_or(&primary_color);
3636
text_colors.colon = *custom_color
3737
.get(4)
3838
.unwrap_or(&DynColors::Ansi(AnsiColors::Default));

0 commit comments

Comments
 (0)