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
// SPDX-FileCopyrightText: 2026 Ken Tobias
// SPDX-License-Identifier: GPL-3.0-or-later
//! System information gathering.
//!
//! Uses the `sysinfo` crate and other heuristics to collect details
//! about the OS, hardware, and environment.
use crate::gpu;
use chrono::TimeZone;
use sysinfo::{Components, Disks, System, Users};
/// Options for controlling what system information is gathered.
///
/// This decouples the collection logic from the CLI argument parser,
/// allowing `retch-sysinfo` to be used as a standalone library.
#[derive(Debug, Default, Clone)]
pub struct CollectOptions {
/// Whether to collect all fields (long mode) or only the primary/default ones.
pub long: bool,
}
/// 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>,
/// Primary local IP address.
pub local_ip: Option<String>,
/// Public IP address (best effort).
pub public_ip: Option<String>,
/// Name of the active/default network interface.
pub active_interface: Option<String>,
/// Detected motherboard name and manufacturer.
pub motherboard: Option<String>,
/// Detected BIOS details.
pub bios: Option<String>,
/// List of connected display resolutions and refresh rates.
pub displays: Vec<String>,
/// Detected active audio driver/server and devices.
pub audio: Option<String>,
/// Connected Wi-Fi SSID and speed.
pub wifi: Option<String>,
/// Bluetooth power status.
pub bluetooth: Option<String>,
/// UI Theme (GTK, Qt, macOS, Windows).
pub ui_theme: Option<String>,
/// Icon theme (GTK/Qt).
pub icons: Option<String>,
/// Cursor theme (GTK/Qt).
pub cursor: Option<String>,
/// System Font.
pub font: Option<String>,
/// Terminal Font (configured in terminal emulator).
pub terminal_font: Option<String>,
/// Connected camera/webcam names.
pub camera: Vec<String>,
/// Connected gamepad/controller names.
pub gamepad: Vec<String>,
}
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(opts: CollectOptions) -> 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 / 1024.0;
let used_mem = sys.used_memory() as f64 / 1024.0 / 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 / 1024.0;
let used_swap = sys.used_swap() as f64 / 1024.0 / 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_list = Disks::new_with_refreshed_list();
let disks: Vec<String> = if !opts.long {
let home = dirs::home_dir().unwrap_or_else(|| std::path::PathBuf::from("/"));
let mut best_match: Option<&sysinfo::Disk> = None;
for disk in disks_list.iter() {
if home.starts_with(disk.mount_point()) {
if let Some(best) = best_match {
if disk.mount_point().components().count()
> best.mount_point().components().count()
{
best_match = Some(disk);
}
} else {
best_match = Some(disk);
}
}
}
let selected_disks = if let Some(best) = best_match {
vec![best]
} else {
disks_list.iter().collect::<Vec<_>>()
};
selected_disks
.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()
} else {
disks_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 = crate::battery::get_battery_info().map(|bat| {
let pct = bat.percentage;
let state = match bat.state {
crate::battery::BatteryState::Charging => "charging",
crate::battery::BatteryState::Discharging => "discharging",
crate::battery::BatteryState::Full => "full",
_ => "not charging",
};
let vendor = bat.vendor;
let model = bat.model;
// Format time remaining as "Xh Ym" or "Xd Yh"
let time_str = match bat.state {
crate::battery::BatteryState::Charging => bat.time_remaining.map(|d| {
let total_mins = d.as_secs() / 60;
let hours = total_mins / 60;
let mins = total_mins % 60;
if hours >= 24 {
let days = hours / 24;
let rem_hours = hours % 24;
format!("{}d {}h until full", days, rem_hours)
} else if hours > 0 {
format!("{}h {}m until full", hours, mins)
} else {
format!("{}m until full", mins)
}
}),
crate::battery::BatteryState::Discharging => bat.time_remaining.map(|d| {
let total_mins = d.as_secs() / 60;
let hours = total_mins / 60;
let mins = total_mins % 60;
if hours >= 24 {
let days = hours / 24;
let rem_hours = hours % 24;
format!("{}d {}h remaining", days, rem_hours)
} else if hours > 0 {
format!("{}h {}m remaining", hours, mins)
} else {
format!("{}m remaining", mins)
}
}),
_ => None,
};
let mut parts = vec![state.to_string()];
if let Some(t) = time_str {
parts.insert(0, t);
}
if let Some(health) = bat.health {
if health < 99.0 {
parts.push(format!("{:.0}% health", health));
}
}
let base = format!("{:.0}% ({})", pct, parts.join(", "));
match (vendor, model) {
(Some(v), Some(m)) => format!("{} [{} {}]", base, v, m),
(Some(v), None) => format!("{} [{}]", base, v),
_ => base,
}
});
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
}
};
// Compute slow system queries concurrently in parallel threads
let (
gpu,
packages,
public_ip,
(local_ip, active_interface),
motherboard,
bios,
displays,
audio,
wifi,
bluetooth,
(ui_theme, icons, cursor, font),
camera,
gamepad,
) = std::thread::scope(|s| {
let gpu_handle = s.spawn(|| {
gpu::detect_gpus()
.into_iter()
.map(|g| g.format())
.collect::<Vec<String>>()
});
let packages_handle = s.spawn(crate::packages::detect_packages);
let public_ip_handle = s.spawn(crate::network::detect_public_ip);
let network_ips_handle = s.spawn(crate::network::detect_active_interface_and_local_ip);
let motherboard_handle = s.spawn(crate::motherboard::detect_motherboard);
let bios_handle = s.spawn(crate::bios::detect_bios);
let displays_handle = s.spawn(crate::display::detect_displays);
let audio_handle = s.spawn(|| crate::audio::detect_audio(&sys));
let wifi_handle = s.spawn(crate::network::detect_wifi);
let bluetooth_handle = s.spawn(crate::bluetooth::detect_bluetooth);
let ui_theme_and_fonts_handle = s.spawn(crate::theme::detect_ui_theme_and_fonts);
let camera_handle = s.spawn(crate::camera::detect_camera);
let gamepad_handle = s.spawn(crate::gamepad::detect_gamepad);
(
gpu_handle.join().unwrap_or_default(),
packages_handle.join().ok().flatten(),
public_ip_handle.join().ok().flatten(),
network_ips_handle.join().unwrap_or((None, None)),
motherboard_handle.join().ok().flatten(),
bios_handle.join().ok().flatten(),
displays_handle.join().unwrap_or_default(),
audio_handle.join().ok().flatten(),
wifi_handle.join().ok().flatten(),
bluetooth_handle.join().ok().flatten(),
ui_theme_and_fonts_handle
.join()
.unwrap_or((None, None, None, None)),
camera_handle.join().unwrap_or_default(),
gamepad_handle.join().unwrap_or_default(),
)
});
let mut temps: Vec<String> = Components::new_with_refreshed_list()
.iter()
.filter_map(|c| {
c.temperature().and_then(|t| {
if t > 0.0 {
Some(format!("{}: {:.0}°C", c.label(), t))
} else {
None
}
})
})
.collect();
// Sort so CPU temperatures appear first
temps.sort_by(|a, b| {
let a_cpu = a.to_lowercase().contains("cpu") || a.to_lowercase().contains("core");
let b_cpu = b.to_lowercase().contains("cpu") || b.to_lowercase().contains("core");
b_cpu.cmp(&a_cpu)
});
let networks =
crate::network::detect_networks(active_interface.as_deref(), local_ip.as_deref());
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 = crate::shell::detect_shell(&sys);
let terminal = crate::terminal::detect_terminal(&sys);
let terminal_font = crate::terminal::detect_terminal_font(terminal.as_deref());
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,
packages,
current_user,
local_ip,
public_ip,
active_interface,
motherboard,
bios,
displays,
audio,
wifi,
bluetooth,
ui_theme,
icons,
cursor,
font,
terminal_font,
camera,
gamepad,
})
}
}