-
Notifications
You must be signed in to change notification settings - Fork 286
/
Copy pathprinter.rs
154 lines (141 loc) · 5.14 KB
/
printer.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
use crate::cli::{Config, When};
use crate::info::Info;
use crate::ui::Language;
use anyhow::{Context, Result};
use image::DynamicImage;
use onefetch_ascii::AsciiArt;
use onefetch_image::ImageBackend;
use std::fmt::Write as _;
use std::io::Write;
use terminal_size::{terminal_size, Width};
const CENTER_PAD_LENGTH: usize = 3;
const MAX_TERM_WIDTH: u16 = 95;
#[derive(Clone, clap::ValueEnum, PartialEq, Eq, Debug)]
pub enum SerializationFormat {
Json,
Yaml,
}
pub struct Printer<W> {
writer: W,
info: Info,
output: Option<SerializationFormat>,
art_off: bool,
image: Option<DynamicImage>,
image_backend: Option<Box<dyn ImageBackend>>,
color_resolution: usize,
no_bold: bool,
ascii_input: Option<String>,
ascii_language: Option<Language>,
}
impl<W: Write> Printer<W> {
pub fn new(writer: W, info: Info, config: Config) -> Result<Self> {
let art_off = match config.show_logo {
When::Always => false,
When::Never => true,
When::Auto => {
if let Some((Width(width), _)) = terminal_size() {
width < MAX_TERM_WIDTH
} else {
false
}
}
};
let image = match config.image {
Some(p) => Some(image::open(p).context("Could not load the specified image")?),
None => None,
};
let image_backend = if image.is_some() {
config
.image_protocol
.map_or_else(onefetch_image::get_best_backend, |s| {
onefetch_image::get_image_backend(s)
})
} else {
None
};
Ok(Self {
writer,
info,
output: config.output,
art_off,
image,
image_backend,
color_resolution: config.color_resolution,
no_bold: config.no_bold,
ascii_input: config.ascii_input,
ascii_language: config.ascii_language,
})
}
pub fn print(&mut self) -> Result<()> {
match &self.output {
Some(format) => match format {
SerializationFormat::Json => {
writeln!(self.writer, "{}", serde_json::to_string_pretty(&self.info)?)?
}
SerializationFormat::Yaml => {
writeln!(self.writer, "{}", serde_yaml::to_string(&self.info)?)?
}
},
None => {
let center_pad = " ".repeat(CENTER_PAD_LENGTH);
let info_str = self.info.to_string();
let mut info_lines = info_str.lines();
let mut buf = String::new();
if self.art_off {
buf.push_str(&info_str);
} else if let Some(custom_image) = &self.image {
let image_backend = self
.image_backend
.as_ref()
.context("Could not detect a supported image backend")?;
buf.push_str(
&image_backend
.add_image(
info_lines.map(|s| format!("{center_pad}{s}")).collect(),
custom_image,
self.color_resolution,
)
.context("Error while drawing image")?,
);
} else {
let mut logo_lines = if let Some(custom_ascii) = &self.ascii_input {
AsciiArt::new(custom_ascii, &self.info.ascii_colors, !self.no_bold)
} else {
AsciiArt::new(self.get_ascii(), &self.info.ascii_colors, !self.no_bold)
};
loop {
match (logo_lines.next(), info_lines.next()) {
(Some(logo_line), Some(info_line)) => {
writeln!(buf, "{logo_line}{center_pad}{info_line:^}")?
}
(Some(logo_line), None) => writeln!(buf, "{logo_line}")?,
(None, Some(info_line)) => writeln!(
buf,
"{:<width$}{}{:^}",
"",
center_pad,
info_line,
width = logo_lines.width()
)?,
(None, None) => {
buf.push('\n');
break;
}
}
}
}
// \x1B[?7l turns off line wrapping and \x1B[?7h turns it on
write!(self.writer, "\x1B[?7l{buf}\x1B[?7h")?;
}
}
Ok(())
}
fn get_ascii(&self) -> &str {
let language = if let Some(ascii_language) = &self.ascii_language {
ascii_language
} else {
&self.info.dominant_language
};
language.get_ascii_art()
}
}