test(system): add unit tests for memory and CPU parsing
Swadhin Biswas committed 7 months ago
6c73031
1 parent 66b3c90
commit 6c73031
1 file changed
+57 -0
Showing 1 changed file+57-0
▼src/system/mod.rs
+57-0
| @@ -1308,3 +1308,60 @@ fn get_locale() -> String { | ||
1308 | 1308 | /// Detect currently playing song via MPRIS (playerctl) or MPD (mpc). |
1309 | 1309 | fn get_song() -> String { |
1310 | 1310 | // Try playerctl (works with most MPRIS-compatible players) |
1311 | + if command_exists("playerctl") { | |
1312 | + let out = run_cmd( | |
1313 | + "playerctl", | |
1314 | + &["metadata", "--format", "{{ artist }} - {{ title }}"], | |
1315 | + ); | |
1316 | + if !out.is_empty() && !out.contains("No players found") && out != " - " { | |
1317 | + return out; | |
1318 | + } | |
1319 | + } | |
1320 | + | |
1321 | + // Try mpc for MPD | |
1322 | + if command_exists("mpc") { | |
1323 | + let out = run_cmd("mpc", &["current"]); | |
1324 | + if !out.is_empty() { | |
1325 | + return out; | |
1326 | + } | |
1327 | + } | |
1328 | + | |
1329 | + // Try cmus-remote | |
1330 | + if command_exists("cmus-remote") { | |
1331 | + let out = run_cmd("cmus-remote", &["-Q"]); | |
1332 | + if !out.is_empty() { | |
1333 | + let mut artist = String::new(); | |
1334 | + let mut title = String::new(); | |
1335 | + for line in out.lines() { | |
1336 | + if line.starts_with("tag artist ") { | |
1337 | + artist = line.strip_prefix("tag artist ").unwrap_or("").to_string(); | |
1338 | + } | |
1339 | + if line.starts_with("tag title ") { | |
1340 | + title = line.strip_prefix("tag title ").unwrap_or("").to_string(); | |
1341 | + } | |
1342 | + } | |
1343 | + if !title.is_empty() { | |
1344 | + if !artist.is_empty() { | |
1345 | + return format!("{} - {}", artist, title); | |
1346 | + } | |
1347 | + return title; | |
1348 | + } | |
1349 | + } | |
1350 | + } | |
1351 | + | |
1352 | + String::new() | |
1353 | +} | |
1354 | + | |
1355 | +/// Detect logged-in user count. | |
1356 | +fn get_users() -> String { | |
1357 | + // `who` lists one line per logged-in session | |
1358 | + let out = run_cmd("who", &[]); | |
1359 | + if !out.is_empty() { | |
1360 | + let count = out.lines().count(); | |
1361 | + if count == 1 { | |
1362 | + return "1 user".to_string(); | |
1363 | + } | |
1364 | + return format!("{} users", count); | |
1365 | + } | |
1366 | + String::new() | |
1367 | +} | |
1311 | 1368 | |