fdai7381
2 years ago
committed by
fdai7451
3 changed files with 179 additions and 3 deletions
-
129Cargo.lock
-
2Cargo.toml
-
51src/builtins/fetch.rs
@ -1,11 +1,58 @@ |
|||||
|
use crate::builtins::{Builtin, BuiltinConfig};
|
||||
use crate::error::ShellError;
|
use crate::error::ShellError;
|
||||
|
|
||||
use super::{Builtin, BuiltinConfig};
|
|
||||
|
use colored::Colorize;
|
||||
|
use sysinfo::{CpuExt, System, SystemExt};
|
||||
|
use uname::uname;
|
||||
|
use whoami::{hostname, username};
|
||||
|
|
||||
pub struct Fetch;
|
pub struct Fetch;
|
||||
|
|
||||
impl Builtin for Fetch {
|
impl Builtin for Fetch {
|
||||
fn execute(&mut self, _: &mut BuiltinConfig, _: Vec<String>) -> Result<(), ShellError> {
|
fn execute(&mut self, _: &mut BuiltinConfig, _: Vec<String>) -> Result<(), ShellError> {
|
||||
|
let mut sys = System::new();
|
||||
|
sys.refresh_all();
|
||||
|
|
||||
|
let line = "-".repeat(username().len() + hostname().len() + 1);
|
||||
|
|
||||
|
println!(
|
||||
|
"\n{}{}{}",
|
||||
|
username().bright_green().bold(),
|
||||
|
"@".bold(),
|
||||
|
hostname().bright_green().bold()
|
||||
|
);
|
||||
|
println!("{}", line);
|
||||
|
println!(
|
||||
|
"{} {} {}",
|
||||
|
"OS".bright_blue().bold(),
|
||||
|
sys.name().unwrap_or_default(),
|
||||
|
uname().unwrap().machine
|
||||
|
);
|
||||
|
println!(
|
||||
|
"{} {}",
|
||||
|
"KERNEL".yellow().bold(),
|
||||
|
sys.kernel_version().unwrap_or_default()
|
||||
|
);
|
||||
|
println!("{} {}", "UPTIME".red().bold(), format_uptime(sys.uptime()));
|
||||
|
println!("{} {}", "CPU".magenta().bold(), sys.cpus()[0].brand());
|
||||
|
println!(
|
||||
|
"{} {}MB / {}MB",
|
||||
|
"MEMORY".cyan().bold(),
|
||||
|
bytes_to_mega_bytes(sys.used_memory()),
|
||||
|
bytes_to_mega_bytes(sys.total_memory())
|
||||
|
);
|
||||
|
|
||||
Ok(())
|
Ok(())
|
||||
}
|
}
|
||||
}
|
}
|
||||
|
|
||||
|
fn format_uptime(uptime: u64) -> String {
|
||||
|
let h = uptime / 3600;
|
||||
|
let m = (uptime - h * 3600) / 60;
|
||||
|
let s = uptime - (h * 3600 + m * 60);
|
||||
|
|
||||
|
format!("{:.0}h {:.0}m {:.0}s", h, m, s)
|
||||
|
}
|
||||
|
|
||||
|
fn bytes_to_mega_bytes(bytes: u64) -> u64 {
|
||||
|
bytes / 1024 / 1024
|
||||
|
}
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue