mirror of
https://github.com/NotAShelf/microfetch.git
synced 2024-11-22 15:40:41 +00:00
minify and optimize uptime reader
This commit is contained in:
parent
e3112dba1d
commit
cf0a52150c
1 changed files with 4 additions and 9 deletions
|
@ -1,16 +1,14 @@
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{self, BufRead};
|
use std::io::{self, BufRead, BufReader};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
pub fn get_current() -> Result<String, io::Error> {
|
pub fn get_current() -> Result<String, io::Error> {
|
||||||
let path = Path::new("/proc/uptime");
|
let path = Path::new("/proc/uptime");
|
||||||
let file = File::open(path)?;
|
let file = File::open(path)?;
|
||||||
let reader = io::BufReader::new(file);
|
let mut reader = BufReader::new(file);
|
||||||
|
|
||||||
let line = reader
|
let mut line = String::new();
|
||||||
.lines()
|
reader.read_line(&mut line)?;
|
||||||
.next()
|
|
||||||
.ok_or_else(|| io::Error::new(io::ErrorKind::Other, "Failed to read uptime"))??;
|
|
||||||
|
|
||||||
let uptime_seconds: f64 = line
|
let uptime_seconds: f64 = line
|
||||||
.split_whitespace()
|
.split_whitespace()
|
||||||
|
@ -19,7 +17,6 @@ pub fn get_current() -> Result<String, io::Error> {
|
||||||
.parse()
|
.parse()
|
||||||
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
|
.map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))?;
|
||||||
|
|
||||||
// calculate days, hours, and minutes
|
|
||||||
let total_minutes = (uptime_seconds / 60.0).round() as u64;
|
let total_minutes = (uptime_seconds / 60.0).round() as u64;
|
||||||
let days = total_minutes / (60 * 24);
|
let days = total_minutes / (60 * 24);
|
||||||
let hours = (total_minutes % (60 * 24)) / 60;
|
let hours = (total_minutes % (60 * 24)) / 60;
|
||||||
|
@ -29,11 +26,9 @@ pub fn get_current() -> Result<String, io::Error> {
|
||||||
if days > 0 {
|
if days > 0 {
|
||||||
parts.push(format!("{days} days"));
|
parts.push(format!("{days} days"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if hours > 0 || days > 0 {
|
if hours > 0 || days > 0 {
|
||||||
parts.push(format!("{hours} hours"));
|
parts.push(format!("{hours} hours"));
|
||||||
}
|
}
|
||||||
|
|
||||||
if minutes > 0 || hours > 0 || days > 0 {
|
if minutes > 0 || hours > 0 || days > 0 {
|
||||||
parts.push(format!("{minutes} minutes"));
|
parts.push(format!("{minutes} minutes"));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue