Browse Source

feat: add simple fetch builtin

main
fdai7381 2 years ago
parent
commit
831f4aa08f
  1. 129
      Cargo.lock
  2. 2
      Cargo.toml
  3. 51
      src/builtins/fetch.rs

129
Cargo.lock

@ -17,11 +17,17 @@ version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
dependencies = [ dependencies = [
"hermit-abi",
"hermit-abi 0.1.19",
"libc", "libc",
"winapi", "winapi",
] ]
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.3.2" version = "1.3.2"
@ -68,6 +74,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "core-foundation-sys"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5827cebf4670468b8772dd191856768aedcb1b0278a04f989f7766351917b9dc"
[[package]] [[package]]
name = "crossbeam-channel" name = "crossbeam-channel"
version = "0.5.6" version = "0.5.6"
@ -78,6 +90,30 @@ dependencies = [
"crossbeam-utils", "crossbeam-utils",
] ]
[[package]]
name = "crossbeam-deque"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "715e8152b692bba2d374b53d4875445368fdf21a94751410af607a5ac677d1fc"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "01a9af1f4c2ef74bb8aa1f7e19706bc72d03598c8a570bb5de72243c7a9d9d5a"
dependencies = [
"autocfg",
"cfg-if",
"crossbeam-utils",
"memoffset",
"scopeguard",
]
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.8.14" version = "0.8.14"
@ -138,6 +174,12 @@ dependencies = [
"winapi", "winapi",
] ]
[[package]]
name = "either"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
[[package]] [[package]]
name = "endian-type" name = "endian-type"
version = "0.1.2" version = "0.1.2"
@ -206,6 +248,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "hermit-abi"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee512640fe35acbfb4bb779db6f0d80704c2cacfa2e39b601ef3e3f47d1ae4c7"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "io-lifetimes" name = "io-lifetimes"
version = "1.0.4" version = "1.0.4"
@ -258,6 +309,15 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "memoffset"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5de893c32cde5f383baa4c04c5d6dbdd735cfd4a794b0debdb2bb1b421da5ff4"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "nibble_vec" name = "nibble_vec"
version = "0.1.0" version = "0.1.0"
@ -279,6 +339,25 @@ dependencies = [
"static_assertions", "static_assertions",
] ]
[[package]]
name = "ntapi"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc51db7b362b205941f71232e56c625156eb9a929f8cf74a428fd5bc094a4afc"
dependencies = [
"winapi",
]
[[package]]
name = "num_cpus"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0fac9e2da13b5eb447a6ce3d392f23a29d8694bff781bf03a16cd9ac8697593b"
dependencies = [
"hermit-abi 0.2.6",
"libc",
]
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.17.0" version = "1.17.0"
@ -297,6 +376,8 @@ dependencies = [
"regex", "regex",
"rustyline", "rustyline",
"shlex", "shlex",
"sysinfo",
"uname",
"whoami", "whoami",
] ]
@ -328,6 +409,28 @@ dependencies = [
"nibble_vec", "nibble_vec",
] ]
[[package]]
name = "rayon"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db3a213adf02b3bcfd2d3846bb41cb22857d131789e01df434fb7e7bc0759b7"
dependencies = [
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "356a0625f1954f730c0201cdab48611198dc6ce21f4acff55089b5a78e6e835b"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"num_cpus",
]
[[package]] [[package]]
name = "redox_syscall" name = "redox_syscall"
version = "0.2.16" version = "0.2.16"
@ -443,6 +546,21 @@ dependencies = [
"unicode-ident", "unicode-ident",
] ]
[[package]]
name = "sysinfo"
version = "0.27.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "975fe381e0ecba475d4acff52466906d95b153a40324956552e027b2a9eaa89e"
dependencies = [
"cfg-if",
"core-foundation-sys",
"libc",
"ntapi",
"once_cell",
"rayon",
"winapi",
]
[[package]] [[package]]
name = "thiserror" name = "thiserror"
version = "1.0.38" version = "1.0.38"
@ -463,6 +581,15 @@ dependencies = [
"syn", "syn",
] ]
[[package]]
name = "uname"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b72f89f0ca32e4db1c04e2a72f5345d59796d4866a1ee0609084569f73683dc8"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "unicode-ident" name = "unicode-ident"
version = "1.0.6" version = "1.0.6"

2
Cargo.toml

@ -12,4 +12,6 @@ once_cell = "1.17"
regex = "1.7" regex = "1.7"
rustyline = "10.1" rustyline = "10.1"
shlex = "1.1" shlex = "1.1"
sysinfo = "0.27"
uname = "0.1"
whoami = "1.3" whoami = "1.3"

51
src/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
}
Loading…
Cancel
Save