This commit is contained in:
WanderingPenwing 2024-07-31 01:53:59 +02:00
parent deb8af44ee
commit 7363255263

View file

@ -5,11 +5,20 @@ use chrono::Local;
use sysinfo::System; use sysinfo::System;
use chrono::Timelike; use chrono::Timelike;
use sysinfo::Cpu; use sysinfo::Cpu;
//use reqwest::Client; use std::net::TcpStream;
use std::fs;
use std::io::{self, BufRead};
use std::path::Path;
const BATTERY_STATE : [&str; 5] = ["", "", "", "", ""]; const BATTERY_STATE : [&str; 5] = ["", "", "", "", ""];
const CPU_STATE : [&str; 5] = ["", "", "", "", ""]; const CPU_STATE : [&str; 5] = ["", "", "", "", ""];
//enum Connection {
// Wired,
// Wifi,
// None,
//}
fn main() { fn main() {
let manager = battery::Manager::new().expect("could not create battery manager"); let manager = battery::Manager::new().expect("could not create battery manager");
let mut sys = System::new(); let mut sys = System::new();
@ -28,7 +37,9 @@ fn main() {
sys.refresh_memory(); sys.refresh_memory();
let mem_str = mem_display(sys.used_memory()); let mem_str = mem_display(sys.used_memory());
display(format!("| {} | {} | {} | {} ", mem_str, cpu_str, battery_str, time_str)); let internet_str = internet_display();
display(format!("| {} | {} | {} | {} | {} ", internet_str, mem_str, cpu_str, battery_str, time_str));
let mut event = false; let mut event = false;
@ -120,40 +131,70 @@ fn cpu_display(cpus : &[Cpu]) -> String {
fn mem_display(mem_usage: u64) -> String { fn mem_display(mem_usage: u64) -> String {
let used_memory_gb = mem_usage as f64 / 1024.0 / 1024.0 / 1024.0; let used_memory_gb = mem_usage as f64 / 1024.0 / 1024.0 / 1024.0;
return format!(" {:.1} ", used_memory_gb); return format!(" {:.1}", used_memory_gb);
} }
fn is_connected_to_internet() -> bool {
match TcpStream::connect_timeout(&"8.8.8.8:53".parse().unwrap(), Duration::from_secs(2)) {
Ok(_) => true,
Err(_) => false,
}
}
//fn measure_bandwidth(url: &str, duration_secs: u64) -> reqwest::Result<f64> { // Check if connected via Ethernet
// let client = Client::new(); fn is_ethernet() -> bool {
// let mut response = client.get(url).send().await?; let output = Command::new("nmcli")
// .arg("device")
// let start = Instant::now(); .arg("status")
// let mut total_bytes = 0; .output()
// let mut buffer = vec![0; 64 * 1024]; // 64 KB buffer .expect("Failed to execute command");
//
// while start.elapsed().as_secs() < duration_secs { let status = String::from_utf8_lossy(&output.stdout);
// match response.copy_to(&mut buffer).await { status.contains("ethernet")
// Ok(n) => total_bytes += n, }
// Err(e) => return Err(e.into()),
// } // Get Wi-Fi signal strength
// } fn get_wifi_strength() -> Option<f32> {
// let file_path = "/proc/net/wireless";
// let elapsed = start.elapsed().as_secs_f64();
// let bandwidth = total_bytes as f64 / elapsed; // Bytes per second // Check if the file exists
// println!("Downloaded {:.2} Mb/s measured in {:.2} seconds", bandwidth / (1024.0 * 1024.0), elapsed); if !Path::new(file_path).exists() {
// return None;
// Ok(bandwidth / (1024.0 * 1024.0)) }
//}
// // Read the file line by line
//fn internet_display() -> String { let file = fs::File::open(file_path).expect("Failed to open file");
// match measure_bandwidth("https://example.com", 5).await { let reader = io::BufReader::new(file);
// Ok(bandwidth) => {
// return format!("{} Mb/s", bandwidth) // Parse the file content
// } for (index, line) in reader.lines().enumerate() {
// Err(e) => { let line = line.expect("Failed to read line");
// eprintln!("Error measuring bandwidth: {}", e); if index == 2 { // The third line in the file
// return " ".to_string() let fields: Vec<&str> = line.split_whitespace().collect();
// } if fields.len() > 2 {
// } if let Ok(signal_dbm) = fields[2].parse::<f32>() {
//} // Convert dBm to percentage using the same formula as awk
let signal_strength_percentage = (signal_dbm * 10.0 / 7.0).clamp(0.0, 100.0);
return Some(signal_strength_percentage);
}
}
}
}
None
}
fn internet_display() -> String {
if is_connected_to_internet() {
if is_ethernet() {
return "".to_string();
} else {
match get_wifi_strength() {
Some(strength) => return format!("{:.0}%", strength),
None => return " ? ".to_string(),
}
}
} else {
return "".to_string();
}
}