Skip to content

Commit

Permalink
fix: bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
tyroruyk committed Mar 5, 2024
1 parent 1126e75 commit 62c6378
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 29 deletions.
6 changes: 6 additions & 0 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ mod sh;
mod up;
#[path = "modules/user.rs"]
mod user;
#[path = "modules/disk.rs"]
mod disk;

pub fn info() {
match (user::get_user(), host::get_host()) {
Expand Down Expand Up @@ -69,4 +71,8 @@ pub fn info() {
if let Ok(memory) = mem::get_mem() {
println!("Memory : {}", memory);
}

if let Ok(disk) = disk::get_disk() {
println!("Disk : {}", disk);
}
}
44 changes: 44 additions & 0 deletions src/modules/disk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use std::process::{Command, exit};
use std::io;

pub fn get_disk() -> io::Result<String> {
// Run the df command and capture its output
let output = Command::new("df").output();

// Check if the command was successful
match output {
Ok(output) => {
// Convert the output to a string
let output_str = String::from_utf8_lossy(&output.stdout);

// Extract the filesystem and usage information
if let Some(line) = output_str.lines().nth(1) {
// Split the line into columns
let columns: Vec<&str> = line.split_whitespace().collect();

// Check if the columns have the expected format
if columns.len() >= 5 {
let size = columns[1];
let used = columns[2];
let usage_percentage = columns[4];

// Return disk usage information as a formatted string
return Ok(format!(
"{} of {} ({})",
size, used, usage_percentage
));
}
}

Err(io::Error::new(
io::ErrorKind::NotFound,
"Disk usage information not found",
))
}
Err(error) => {
// Print the error and exit the program
eprintln!("Failed to execute df command: {}", error);
exit(1);
}
}
}
24 changes: 12 additions & 12 deletions src/modules/res.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::process::{Command, exit};
use std::io;
use std::process::Command;

pub fn get_res() -> io::Result<String> {
// Run the xrandr command and capture its output
Expand All @@ -14,20 +14,20 @@ pub fn get_res() -> io::Result<String> {
// Find the line containing the primary display resolution
if let Some(line) = output_str.lines().find(|line| line.contains("*+")) {
// Extract the resolution from the line
let resolution = line.split_whitespace().next_back();

return Ok(resolution.unwrap_or("N/A").to_string());
} else {
return Err(io::Error::new(
io::ErrorKind::NotFound,
"Primary display resolution not found",
));
if let Some(resolution) = line.split_whitespace().find(|&word| word.contains('x')) {
return Ok(resolution.to_string());
}
}

Err(io::Error::new(
io::ErrorKind::NotFound,
"Primary display resolution not found",
))
}
Err(error) => {
// Print the error and return N/A
eprintln!("{}", error);
Ok("N/A".to_string())
// Print the error and exit the program
eprintln!("Failed to execute xrandr command: {}", error);
exit(1);
}
}
}
41 changes: 24 additions & 17 deletions src/modules/uptime.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
use std::fs::read_to_string;
use std::process::{Command, exit};
use std::io;

pub fn get_uptime() -> io::Result<String> {
// Read the content of /proc/uptime into a string
let uptime_str = read_to_string("/proc/uptime")?;
// Run the uptime -p command and capture its output
let output = Command::new("uptime").arg("-p").output();

// Parse the uptime value from the string
let uptime_secs: f64 = uptime_str.trim().parse().map_err(|e| {
io::Error::new(
io::ErrorKind::InvalidData,
format!("Failed to parse uptime: {}", e),
)
})?;
// Check if the command was successful
match output {
Ok(output) => {
// Convert the output to a string
let output_str = String::from_utf8_lossy(&output.stdout);

// Calculate days, hours, and minutes
let days = (uptime_secs / (24.0 * 3600.0)).floor() as u64;
let hours = ((uptime_secs % (24.0 * 3600.0)) / 3600.0).floor() as u64;
let minutes = ((uptime_secs % 3600.0) / 60.0).floor() as u64;
// Extract the remaining text after 'up'
if let Some(remaining_text) = output_str.strip_prefix("up") {
return Ok(remaining_text.trim().to_string());
}

// Format the result as "days, hours, minutes"
Ok(format!("{}, {}, {}", days, hours, minutes))
}
Err(io::Error::new(
io::ErrorKind::NotFound,
"Invalid 'uptime -p' output format",
))
}
Err(error) => {
// Print the error and exit the program
eprintln!("Failed to execute uptime command: {}", error);
exit(1);
}
}
}

0 comments on commit 62c6378

Please sign in to comment.