1use crate::cli::Cli;
9use crate::config::Config;
10use crate::fetch::SystemInfo;
11use crate::logo;
12use crate::theme::Theme;
13use owo_colors::OwoColorize;
14
15impl SystemInfo {
16 pub fn display(&self, cli: &Cli, _config: &Config) -> anyhow::Result<()> {
22 println!();
23 let theme_name = _config.theme.as_deref().or(cli.theme.as_deref());
24 let mut theme = match theme_name {
25 Some(name) => Theme::from_name(name),
26 None => Theme::detect_system_theme(), };
28
29 if let Some(custom) = &_config.custom_theme {
31 theme = Theme::with_custom_overrides(theme, custom);
32 }
33
34 let show_logo = _config.show_logo.unwrap_or(true) && !cli.no_logo;
35 if show_logo {
36 let distro_hint = _config.logo.clone().or_else(logo::detect_distro);
37 #[cfg(feature = "graphics")]
38 let mut printed_logo = false;
39 #[cfg(not(feature = "graphics"))]
40 let printed_logo = false;
41
42 if !cli.ascii_only {
43 let user_logo = if let Some(config_dir) = dirs::config_dir() {
45 let p = config_dir.join("retch").join("logo.png");
46 if p.exists() {
47 Some(p)
48 } else {
49 None
50 }
51 } else {
52 None
53 };
54
55 #[cfg(feature = "graphics")]
57 if !printed_logo && logo::supports_kitty() {
58 if let Some(path) = &user_logo {
59 logo::print_graphical_logo_from_path(path);
60 printed_logo = true;
61 } else if let Some(distro) = &distro_hint {
62 if let Some(bytes) = logo::get_embedded_logo(Some(distro)) {
63 logo::print_graphical_logo(bytes);
64 printed_logo = true;
65 }
66 }
67 }
68
69 #[cfg(feature = "graphics")]
71 if !printed_logo && logo::supports_iterm2() {
72 if let Some(path) = &user_logo {
73 logo::print_iterm2_logo_from_path(path);
74 printed_logo = true;
75 } else if let Some(distro) = &distro_hint {
76 if let Some(bytes) = logo::get_embedded_logo(Some(distro)) {
77 logo::print_iterm2_logo(bytes);
78 printed_logo = true;
79 }
80 }
81 }
82
83 #[cfg(feature = "graphics")]
85 if !printed_logo && logo::supports_sixel() {
86 if let Some(path) = &user_logo {
87 logo::print_sixel_logo_from_path(path);
88 printed_logo = true;
89 } else if let Some(distro) = &distro_hint {
90 if let Some(bytes) = logo::get_embedded_logo(Some(distro)) {
91 logo::print_sixel_logo(bytes);
92 printed_logo = true;
93 }
94 }
95 }
96
97 if !printed_logo && logo::chafa_available() {
99 if let Some(path) = &user_logo {
100 if logo::print_with_chafa(path) {
101 printed_logo = true;
102 }
103 } else if distro_hint.is_some() {
104 }
107 }
108 }
109
110 if !printed_logo {
112 logo::print_distro_logo_with_ascii(distro_hint.as_deref(), cli.ascii_only);
114 }
115 println!(); }
117
118 let allowed_fields: Option<Vec<String>> = if cli.long {
120 None } else if cli.short {
122 Some(vec![
123 "os".to_string(),
124 "kernel".to_string(),
125 "host".to_string(),
126 "cpu".to_string(),
127 "gpu".to_string(),
128 "memory".to_string(),
129 "disk".to_string(),
130 "net".to_string(),
131 ])
132 } else if let Some(fields) = &_config.fields {
133 Some(fields.iter().map(|s| s.to_lowercase()).collect())
134 } else {
135 Some(vec![
137 "os".to_string(),
138 "kernel".to_string(),
139 "host".to_string(),
140 "cpu".to_string(),
141 "motherboard".to_string(),
142 "bios".to_string(),
143 "gpu".to_string(),
144 "display".to_string(),
145 "memory".to_string(),
146 "swap".to_string(),
147 "load".to_string(),
148 "disk".to_string(),
149 "net".to_string(),
150 "uptime".to_string(),
151 ])
152 };
153
154 let should_show = |label: &str| -> bool {
155 match &allowed_fields {
156 Some(fields) => fields.contains(&label.to_lowercase()),
157 None => true,
158 }
159 };
160
161 let label_width = 10;
163 let print_line = |label: &str, value: &str| {
164 if should_show(label) {
165 println!(
166 "{:>width$}{} {}",
167 theme.color_label(label),
168 theme.color_separator(":"),
169 theme.color_value(value),
170 width = label_width
171 );
172 }
173 };
174
175 print_line("OS", &self.os);
176 if let Some(kernel) = &self.kernel {
177 print_line("Kernel", kernel);
178 }
179 if let Some(host) = &self.hostname {
180 print_line("Host", host);
181 }
182 if let Some(user) = &self.current_user {
183 print_line("User", user);
184 }
185 print_line("Arch", &self.arch);
186 print_line("CPU", &format!("{} ({} cores)", self.cpu, self.cpu_cores));
187 if let Some(freq) = &self.cpu_freq {
188 print_line("CPU Freq", freq);
189 }
190 if let Some(motherboard) = &self.motherboard {
191 print_line("Motherboard", motherboard);
192 }
193 if let Some(bios) = &self.bios {
194 print_line("BIOS", bios);
195 }
196 if should_show("GPU") {
197 for gpu in &self.gpu {
198 print_line("GPU", gpu);
199 }
200 }
201 if should_show("Display") {
202 for display in &self.displays {
203 print_line("Display", display);
204 }
205 }
206 print_line("Memory", &self.memory);
207 print_line("Swap", &self.swap);
208 print_line("Procs", &self.processes.to_string());
209 if let Some(load) = &self.load_avg {
210 print_line("Load", load);
211 }
212
213 if should_show("Disk") {
214 for disk in &self.disks {
215 print_line("Disk", disk);
216 }
217 }
218
219 if should_show("Temp") {
220 for temp in &self.temps {
221 print_line("Temp", temp);
222 }
223 }
224
225 if should_show("Net") {
226 if cli.long {
227 for net in &self.networks {
228 if let Some(ref active) = self.active_interface {
229 if net.contains(active) {
230 print_line("Net", &net.bright_blue().to_string());
231 }
232 }
233 }
234 for net in &self.networks {
235 if let Some(ref active) = self.active_interface {
236 if net.contains(active) {
237 continue;
238 }
239 }
240 print_line("Net", net);
241 }
242 } else {
243 let mut printed = false;
244 if let Some(ref active) = self.active_interface {
245 for net in &self.networks {
246 if net.contains(active) {
247 print_line("Net", net);
248 printed = true;
249 break;
250 }
251 }
252 }
253 if !printed {
254 for net in &self.networks {
255 if net.contains("[Up]") {
256 print_line("Net", net);
257 break;
258 }
259 }
260 }
261 }
262 }
263
264 if let Some(ip) = &self.public_ip {
265 print_line("Public IP", ip);
266 }
267
268 let uptime_str = format_uptime(&self.uptime);
270 let boot_display = format!("{} since {}", uptime_str, self.boot_time);
271 print_line("Uptime", &boot_display);
272
273 if let Some(bat) = &self.battery {
274 print_line("Battery", bat);
275 }
276
277 if let Some(shell) = &self.shell {
278 print_line("Shell", shell);
279 }
280 if let Some(term) = &self.terminal {
281 print_line("Terminal", term);
282 }
283 if let Some(de) = &self.desktop {
284 print_line("Desktop", de);
285 }
286 print_line("Users", &self.users.to_string());
287 if let Some(pkgs) = self.packages {
288 if pkgs > 0 {
289 print_line("Packages", &pkgs.to_string());
290 }
291 }
292
293 Ok(())
294 }
295}
296
297fn format_uptime(uptime: &str) -> String {
301 let seconds: u64 = uptime.trim_end_matches('s').parse().unwrap_or(0);
303
304 let years = seconds / (365 * 24 * 3600);
305 let days = (seconds % (365 * 24 * 3600)) / (24 * 3600);
306 let hours = (seconds % (24 * 3600)) / 3600;
307 let minutes = (seconds % 3600) / 60;
308 let secs = seconds % 60;
309
310 let mut parts = Vec::new();
311 if years > 0 {
312 parts.push(format!("{}y", years));
313 }
314 if days > 0 {
315 parts.push(format!("{}d", days));
316 }
317 if hours > 0 {
318 parts.push(format!("{}h", hours));
319 }
320 if minutes > 0 {
321 parts.push(format!("{}m", minutes));
322 }
323 if secs > 0 || parts.is_empty() {
324 parts.push(format!("{}s", secs));
325 }
326
327 parts.join(" ")
328}
329
330#[cfg(test)]
331mod tests {
332 use super::*;
333
334 #[test]
335 fn test_format_uptime() {
336 assert_eq!(format_uptime("60s"), "1m");
337 assert_eq!(format_uptime("3600s"), "1h");
338 assert_eq!(format_uptime("3661s"), "1h 1m 1s");
339 assert_eq!(format_uptime("86400s"), "1d");
340 assert_eq!(format_uptime("90061s"), "1d 1h 1m 1s");
341 assert_eq!(format_uptime("31536000s"), "1y");
342 assert_eq!(format_uptime("31626061s"), "1y 1d 1h 1m 1s");
343 assert_eq!(format_uptime("0s"), "0s");
344 }
345}