feat(system): implement collect_all() with thread spawning
Swadhin Biswas committed 7 months ago
79cf6c1
1 parent 5b8a59e
commit 79cf6c1
1 file changed
+80 -0
Showing 1 changed file+80-0
▼src/system/mod.rs
+80-0
| @@ -1168,3 +1168,83 @@ fn get_memory_with_percent() -> (String, f64) { | ||
1168 | 1168 | } |
1169 | 1169 | let val: u64 = parts[1].parse().unwrap_or(0); |
1170 | 1170 | match parts[0] { |
1171 | + "MemTotal:" => total_kb = val, | |
1172 | + "MemAvailable:" => available_kb = val, | |
1173 | + "Buffers:" => buffers_kb = val, | |
1174 | + "Cached:" => cached_kb = val, | |
1175 | + "Shmem:" => shmem_kb = val, | |
1176 | + "SReclaimable:" => sreclaimable_kb = val, | |
1177 | + _ => {} | |
1178 | + } | |
1179 | + } | |
1180 | + | |
1181 | + if total_kb == 0 { | |
1182 | + return ("Unknown".to_string(), 0.0); | |
1183 | + } | |
1184 | + | |
1185 | + // If MemAvailable is 0, calculate used manually (like neofetch) | |
1186 | + let used_kb = if available_kb > 0 { | |
1187 | + total_kb - available_kb | |
1188 | + } else { | |
1189 | + total_kb - (buffers_kb + cached_kb + sreclaimable_kb - shmem_kb) | |
1190 | + }; | |
1191 | + | |
1192 | + let used_mib = used_kb as f64 / 1024.0; | |
1193 | + let total_mib = total_kb as f64 / 1024.0; | |
1194 | + let percent = if total_kb > 0 { | |
1195 | + (used_kb as f64 / total_kb as f64) * 100.0 | |
1196 | + } else { | |
1197 | + 0.0 | |
1198 | + }; | |
1199 | + | |
1200 | + (format!("{:.0}MiB / {:.0}MiB", used_mib, total_mib), percent) | |
1201 | +} | |
1202 | + | |
1203 | +fn get_disk_with_percent() -> (String, f64) { | |
1204 | + let out = run_cmd("df", &["-h", "--output=used,size,pcent", "/"]); | |
1205 | + if !out.is_empty() { | |
1206 | + if let Some(line) = out.lines().nth(1) { | |
1207 | + let parts: Vec<&str> = line.split_whitespace().collect(); | |
1208 | + if parts.len() >= 3 { | |
1209 | + let pct_str = parts[2].trim_end_matches('%'); | |
1210 | + let pct: f64 = pct_str.parse().unwrap_or(0.0); | |
1211 | + return (format!("{} / {} ({})", parts[0], parts[1], parts[2]), pct); | |
1212 | + } | |
1213 | + } | |
1214 | + } | |
1215 | + | |
1216 | + // Simpler fallback | |
1217 | + let out = run_cmd("df", &["-h", "/"]); | |
1218 | + if !out.is_empty() { | |
1219 | + if let Some(line) = out.lines().nth(1) { | |
1220 | + let parts: Vec<&str> = line.split_whitespace().collect(); | |
1221 | + if parts.len() >= 5 { | |
1222 | + let pct_str = parts[4].trim_end_matches('%'); | |
1223 | + let pct: f64 = pct_str.parse().unwrap_or(0.0); | |
1224 | + return (format!("{} / {} ({})", parts[2], parts[1], parts[4]), pct); | |
1225 | + } | |
1226 | + } | |
1227 | + } | |
1228 | + | |
1229 | + ("Unknown".to_string(), 0.0) | |
1230 | +} | |
1231 | + | |
1232 | +fn get_battery() -> String { | |
1233 | + // Check multiple battery paths | |
1234 | + for bat_name in &["BAT0", "BAT1", "macsmc-battery"] { | |
1235 | + let path = format!("/sys/class/power_supply/{}", bat_name); | |
1236 | + if Path::new(&path).exists() { | |
1237 | + let capacity = read_first_line(&format!("{}/capacity", path)); | |
1238 | + let status = read_first_line(&format!("{}/status", path)); | |
1239 | + | |
1240 | + if !capacity.is_empty() { | |
1241 | + let status_str = match status.as_str() { | |
1242 | + "Charging" => " [Charging]", | |
1243 | + "Discharging" => " [Discharging]", | |
1244 | + "Full" => " [Full]", | |
1245 | + "Not charging" => " [Not Charging]", | |
1246 | + _ => "", | |
1247 | + }; | |
1248 | + return format!("{}%{}", capacity, status_str); | |
1249 | + } | |
1250 | + } | |
1171 | 1251 | |