retch-cli 0.1.4

A fast, feature-rich system information fetcher written in Rust (similar to fastfetch or neofetch)
Documentation
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use crate::cli::Cli;
use crate::config::Config;
use chrono::TimeZone;
use owo_colors::OwoColorize;
use std::collections::HashSet;
use std::fs;
use sysinfo::{Components, Disks, Networks, System, Users};

/// Comprehensive system information data structure.
///
/// This struct holds all the metrics collected from the system,
/// ranging from OS details to hardware specs and network status.
#[derive(Debug)]
pub struct SystemInfo {
    /// Operating system name and version.
    pub os: String,
    /// Kernel version.
    pub kernel: Option<String>,
    /// System hostname.
    pub hostname: Option<String>,
    /// CPU architecture (e.g., x86_64).
    pub arch: String,
    /// CPU model brand string.
    pub cpu: String,
    /// Total number of logical CPU cores.
    pub cpu_cores: usize,
    /// Formatted memory usage (Used / Total).
    pub memory: String,
    /// Formatted swap usage (Used / Total).
    pub swap: String,
    /// System uptime formatted as a duration.
    pub uptime: String,
    /// Number of currently running processes.
    pub processes: usize,
    /// Load average (1, 5, 15 minutes).
    pub load_avg: Option<String>,
    /// List of mounted disks with usage information.
    pub disks: Vec<String>,
    /// Hardware component temperatures.
    pub temps: Vec<String>,
    /// Network interface statistics and status.
    pub networks: Vec<String>,
    /// System boot time in ISO 8601 format.
    pub boot_time: String,
    /// Battery status (currently placeholder for future feature).
    pub battery: Option<String>,
    /// Path to the current user's shell.
    pub shell: Option<String>,
    /// Name of the terminal emulator in use.
    pub terminal: Option<String>,
    /// Detected desktop environment or window manager.
    pub desktop: Option<String>,
    /// Current CPU frequency (formatted).
    pub cpu_freq: Option<String>,
    /// Number of interactive users (UID >= 1000).
    pub users: usize,
    /// List of detected GPUs with model names.
    pub gpu: Vec<String>,
    /// Total count of installed packages across supported managers.
    pub packages: Option<usize>,
    /// Name of the user running the process.
    pub current_user: Option<String>,
}

/// Helper to lookup PCI device name in standard system pci.ids files.
///
/// Searches `/usr/share/hwdata/pci.ids` or `/usr/share/misc/pci.ids`.
fn lookup_pci_device(vendor_id: &str, device_id: &str) -> Option<String> {
    let vendor_id = vendor_id.trim_start_matches("0x").to_lowercase();
    let device_id = device_id.trim_start_matches("0x").to_lowercase();

    let paths = ["/usr/share/hwdata/pci.ids", "/usr/share/misc/pci.ids"];

    for path in &paths {
        if let Ok(content) = fs::read_to_string(path) {
            let mut in_vendor = false;
            for line in content.lines() {
                if line.starts_with('#') || line.is_empty() {
                    continue;
                }

                if !line.starts_with('\t') {
                    // Vendor line: "vendor_id  Vendor Name"
                    in_vendor = line.starts_with(&vendor_id);
                } else if in_vendor && line.starts_with('\t') && !line.starts_with("\t\t") {
                    // Device line: "\tdevice_id  Device Name"
                    let trimmed = line.trim_start();
                    if trimmed.starts_with(&device_id) {
                        let name = trimmed[device_id.len()..].trim();
                        return Some(name.to_string());
                    }
                }
            }
        }
    }
    None
}

impl SystemInfo {
    /// Collects system information using sysinfo and environment probes.
    ///
    /// This method aggregates data from the operating system, hardware,
    /// and current user environment into a `SystemInfo` struct.
    pub fn collect(_cli: &Cli, _config: &Config) -> anyhow::Result<Self> {
        let mut sys = System::new_all();
        sys.refresh_all();

        let os = System::long_os_version()
            .or_else(System::name)
            .unwrap_or_else(|| "Unknown".to_string());

        let kernel = System::kernel_version();
        let hostname = System::host_name();

        let cpu = sys
            .cpus()
            .first()
            .map(|c| c.brand().to_string())
            .unwrap_or_else(|| "Unknown CPU".to_string());

        let cpu_cores = sys.cpus().len();

        let total_mem = sys.total_memory() as f64 / 1024.0 / 1024.0;
        let used_mem = sys.used_memory() as f64 / 1024.0 / 1024.0;
        let memory = format!("{:.1} / {:.1} GB", used_mem, total_mem);

        let total_swap = sys.total_swap() as f64 / 1024.0 / 1024.0;
        let used_swap = sys.used_swap() as f64 / 1024.0 / 1024.0;
        let swap = if total_swap > 0.0 {
            format!("{:.1} / {:.1} GB", used_swap, total_swap)
        } else {
            "No swap".to_string()
        };

        let uptime = format!("{}s", System::uptime());

        let disks = Disks::new_with_refreshed_list()
            .iter()
            .map(|d| {
                let total = d.total_space() as f64 / 1024.0 / 1024.0 / 1024.0;
                let avail = d.available_space() as f64 / 1024.0 / 1024.0 / 1024.0;
                let fs = d.file_system().to_string_lossy();
                format!(
                    "{} ({}): {:.1} GB free / {:.1} GB",
                    d.mount_point().display(),
                    fs,
                    avail,
                    total
                )
            })
            .collect();

        let battery: Option<String> = None; // Requires sysinfo "battery" feature

        let arch = System::cpu_arch();

        let processes = sys.processes().len();

        let load_avg = {
            let avg = System::load_average();
            if avg.one > 0.0 || avg.five > 0.0 {
                Some(format!(
                    "{:.2}, {:.2}, {:.2}",
                    avg.one, avg.five, avg.fifteen
                ))
            } else {
                None
            }
        };

        let temps = Components::new_with_refreshed_list()
            .iter()
            .filter_map(|c| {
                c.temperature().and_then(|t| {
                    if t > 0.0 {
                        Some(format!("{}: {:.1}°C", c.label(), t))
                    } else {
                        None
                    }
                })
            })
            .collect();

        let networks = Networks::new_with_refreshed_list()
            .iter()
            .map(|(name, data)| {
                let rx = format_bytes(data.total_received());
                let tx = format_bytes(data.total_transmitted());
                let status = if data.total_received() > 0 || data.total_transmitted() > 0 {
                    "Up".green().to_string()
                } else {
                    "Down".red().to_string()
                };
                format!("{} [{}] RX: {} TX: {}", name, status, rx, tx)
            })
            .collect();

        let boot_timestamp = System::boot_time();
        let boot_dt = chrono::Local
            .timestamp_opt(boot_timestamp as i64, 0)
            .single()
            .map(|dt| dt.format("%Y-%m-%dT%H:%M:%S%:z").to_string())
            .unwrap_or_else(|| boot_timestamp.to_string());
        let boot_time = boot_dt;

        // Environment-based info
        let shell = std::env::var("SHELL").ok();
        let terminal = std::env::var("TERM").ok();
        let desktop = std::env::var("XDG_CURRENT_DESKTOP")
            .or_else(|_| std::env::var("DESKTOP_SESSION"))
            .ok();

        // CPU frequency (first CPU)
        let cpu_freq = sys
            .cpus()
            .first()
            .map(|c| format!("{:.2} GHz", c.frequency() as f64 / 1000.0));

        // Current logged in user
        let current_user = std::env::var("USER").ok();

        // Number of interactive users (UID >= 1000, excluding system accounts)
        let users = Users::new_with_refreshed_list()
            .iter()
            .filter(|user| {
                // UID is exposed via Display
                let uid_str = user.id().to_string();
                if let Ok(uid) = uid_str.parse::<u32>() {
                    uid >= 1000
                } else {
                    false
                }
            })
            .count();

        Ok(Self {
            os,
            kernel,
            hostname,
            arch,
            cpu,
            cpu_cores,
            memory,
            swap,
            uptime,
            processes,
            load_avg,
            disks,
            temps,
            networks,
            boot_time,
            battery,
            shell,
            terminal,
            desktop,
            cpu_freq,
            users,
            gpu: detect_gpu(),
            packages: detect_packages(),
            current_user,
        })
    }
}

/// Detects GPUs using sysfs and PCI database lookups.
///
/// Scans `/sys/class/drm` for graphics devices and attempts to extract
/// model names and VRAM info (where supported).
fn detect_gpu() -> Vec<String> {
    let mut gpus = Vec::new();
    let mut seen_devices = HashSet::new();

    // Scan /sys/class/drm for all card* and renderD* entries
    if let Ok(entries) = std::fs::read_dir("/sys/class/drm") {
        for entry in entries.flatten() {
            let name = entry.file_name().into_string().unwrap_or_default();
            if !name.starts_with("card") && !name.starts_with("renderD") {
                continue;
            }

            let device_path = entry.path().join("device");
            if let Ok(real_path) = std::fs::canonicalize(&device_path) {
                if !seen_devices.insert(real_path) {
                    continue;
                }

                // Try to identify vendor and model
                let vendor_id = fs::read_to_string(device_path.join("vendor"))
                    .unwrap_or_default()
                    .trim()
                    .to_string();
                let device_id = fs::read_to_string(device_path.join("device"))
                    .unwrap_or_default()
                    .trim()
                    .to_string();

                if vendor_id.is_empty() || device_id.is_empty() {
                    continue;
                }

                let mut gpu_name = lookup_pci_device(&vendor_id, &device_id).unwrap_or_else(|| {
                    if vendor_id.contains("10de") {
                        "NVIDIA GPU".to_string()
                    } else if vendor_id.contains("1002") {
                        "AMD GPU".to_string()
                    } else if vendor_id.contains("8086") {
                        "Intel GPU".to_string()
                    } else {
                        "Unknown GPU".to_string()
                    }
                });

                // NVIDIA special case: try /proc for even better name
                if vendor_id.contains("10de") {
                    if let Ok(pci_slot_path) = fs::read_link(&device_path) {
                        if let Some(slot_name) = pci_slot_path.file_name() {
                            let proc_info_path = format!(
                                "/proc/driver/nvidia/gpus/{}/information",
                                slot_name.to_string_lossy()
                            );
                            if let Ok(info) = fs::read_to_string(proc_info_path) {
                                for line in info.lines() {
                                    if line.starts_with("Model:") {
                                        gpu_name = line.replace("Model:", "").trim().to_string();
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }

                // Try to get VRAM from common sysfs locations (mainly AMD)
                let vram_path = device_path.join("mem_info_vram_total");
                if let Ok(vram_str) = fs::read_to_string(vram_path) {
                    if let Ok(vram_bytes) = vram_str.trim().parse::<u64>() {
                        let vram_gb = vram_bytes as f64 / 1024.0 / 1024.0 / 1024.0;
                        if vram_gb >= 1.0 {
                            gpu_name = format!("{} ({:.0} GB)", gpu_name, vram_gb);
                        } else {
                            let vram_mb = vram_bytes / 1024 / 1024;
                            gpu_name = format!("{} ({} MB)", gpu_name, vram_mb);
                        }
                    }
                }

                gpus.push(gpu_name);
            }
        }
    }

    if gpus.is_empty() {
        // Fallback for non-standard setups or if /sys/class/drm is empty
        if let Ok(model) = fs::read_to_string("/sys/class/drm/card0/device/model") {
            let model = model.trim();
            if !model.is_empty() {
                gpus.push(model.to_string());
            }
        }
    }

    gpus
}

/// Count installed packages by inspecting package manager databases directly.
///
/// Supports Pacman (Arch), Dpkg (Debian), XBPS (Void), and RPM (Fedora/RHEL).
fn detect_packages() -> Option<usize> {
    // Arch / Manjaro
    if let Ok(entries) = std::fs::read_dir("/var/lib/pacman/local") {
        let count = entries.filter_map(|e| e.ok()).count();
        if count > 0 {
            return Some(count);
        }
    }

    // Debian / Ubuntu
    if let Ok(entries) = std::fs::read_dir("/var/lib/dpkg/info") {
        let count = entries
            .filter_map(|e| e.ok())
            .filter(|e| e.path().extension().is_some_and(|ext| ext == "list"))
            .count();
        if count > 0 {
            return Some(count);
        }
    }

    // Gentoo
    if let Ok(entries) = std::fs::read_dir("/var/db/pkg") {
        let count: usize = entries
            .filter_map(|e| e.ok())
            .map(|e| {
                std::fs::read_dir(e.path())
                    .map(|d| d.filter(|_| true).count())
                    .unwrap_or(0)
            })
            .sum();
        if count > 0 {
            return Some(count);
        }
    }

    // Void Linux
    if let Ok(entries) = std::fs::read_dir("/var/db/xbps") {
        let count = entries
            .filter_map(|e| e.ok())
            .filter(|e| e.path().extension().is_some_and(|ext| ext == "plist"))
            .count();
        if count > 0 {
            return Some(count);
        }
    }

    // Fedora / RHEL / openSUSE - read from RPM SQLite database
    let rpm_db = "/var/lib/rpm/rpmdb.sqlite";
    if std::path::Path::new(rpm_db).exists() {
        if let Ok(conn) = rusqlite::Connection::open(rpm_db) {
            if let Ok(count) = conn.query_row("SELECT COUNT(*) FROM Packages", [], |row| {
                row.get::<_, i64>(0)
            }) {
                if count > 0 {
                    return Some(count as usize);
                }
            }
        }
    }

    None
}

/// Format bytes into human-readable form (KB, MB, GB, etc.)
fn format_bytes(bytes: u64) -> String {
    const KB: u64 = 1024;
    const MB: u64 = KB * 1024;
    const GB: u64 = MB * 1024;

    if bytes >= GB {
        format!("{:.1} GB", bytes as f64 / GB as f64)
    } else if bytes >= MB {
        format!("{:.1} MB", bytes as f64 / MB as f64)
    } else if bytes >= KB {
        format!("{:.1} KB", bytes as f64 / KB as f64)
    } else {
        format!("{} B", bytes)
    }
}