From 8fdd8176fa2f3a42d17f45ccf4eb7bdc4b84a170 Mon Sep 17 00:00:00 2001 From: Rudis Muiznieks Date: Thu, 13 Jun 2024 17:02:58 -0500 Subject: [PATCH] initial commit --- .editorconfig | 12 ++ .gitignore | 1 + Cargo.lock | 490 ++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 8 + README.md | 42 ++++ src/cli_args.rs | 82 +++++++ src/main.rs | 20 ++ src/stat_computers.rs | 107 +++++++++ 8 files changed, 762 insertions(+) create mode 100644 .editorconfig create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 src/cli_args.rs create mode 100644 src/main.rs create mode 100644 src/stat_computers.rs diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..0316a69 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,12 @@ +root = true + +[*] +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = true +indent_style = space +indent_size = 2 + +[*.rs] +max_line_length = 100 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..78297fa --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,490 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "anstream" +version = "0.6.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b" + +[[package]] +name = "anstyle-parse" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391" +dependencies = [ + "windows-sys 0.52.0", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19" +dependencies = [ + "anstyle", + "windows-sys 0.52.0", +] + +[[package]] +name = "bitflags" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "clap" +version = "4.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5db83dced34638ad474f39f250d7fea9598bdd239eaced1bdf45d597da0f433f" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7e204572485eb3fbf28f871612191521df159bc3e15a9f5064c66dba3a8c05f" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", + "terminal_size", +] + +[[package]] +name = "clap_derive" +version = "4.5.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c780290ccf4fb26629baa7a1081e68ced113f1d3ec302fa5948f1c381ebf06c6" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4b82cf0babdbd58558212896d1a4272303a57bdb245c2bf1147185fb45640e70" + +[[package]] +name = "colorchoice" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" + +[[package]] +name = "core-foundation-sys" +version = "0.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f" + +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + +[[package]] +name = "either" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3dca9240753cf90908d7e4aac30f630662b02aebaa1b58a3cadabdb23385b58b" + +[[package]] +name = "errno" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "534c5cf6194dfab3db3242765c03bbe257cf92f22b38f6bc0c58d59108a820ba" +dependencies = [ + "libc", + "windows-sys 0.52.0", +] + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800" + +[[package]] +name = "libc" +version = "0.2.155" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97b3888a4aecf77e811145cadf6eef5901f4782c53886191b2f693f24761847c" + +[[package]] +name = "linux-raw-sys" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78b3ae25bc7c8c38cec158d1f2757ee79e9b3740fbc7ccf0e59e4b08d793fa89" + +[[package]] +name = "ntapi" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8a3895c6391c39d7fe7ebc444a87eb2991b2a0bc718fdabd071eec617fc68e4" +dependencies = [ + "winapi", +] + +[[package]] +name = "once_cell" +version = "1.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" + +[[package]] +name = "proc-macro2" +version = "1.0.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22244ce15aa966053a896d1accb3a6e68469b97c7f33f284b99f0d576879fc23" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "rayon" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + +[[package]] +name = "rustix" +version = "0.38.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys 0.52.0", +] + +[[package]] +name = "sicli" +version = "1.0.0" +dependencies = [ + "clap", + "sysinfo", +] + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.66" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "sysinfo" +version = "0.30.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "732ffa00f53e6b2af46208fba5718d9662a421049204e156328b66791ffa15ae" +dependencies = [ + "cfg-if", + "core-foundation-sys", + "libc", + "ntapi", + "once_cell", + "rayon", + "windows", +] + +[[package]] +name = "terminal_size" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "21bebf2b7c9e0a515f6e0f8c51dc0f8e4696391e6f1ff30379559f8365fb0df7" +dependencies = [ + "rustix", + "windows-sys 0.48.0", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e48a53791691ab099e5e2ad123536d0fff50652600abaf43bbf952894110d0be" +dependencies = [ + "windows-core", + "windows-targets 0.52.5", +] + +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.5", +] + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets 0.48.5", +] + +[[package]] +name = "windows-sys" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" +dependencies = [ + "windows-targets 0.52.5", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm 0.48.5", + "windows_aarch64_msvc 0.48.5", + "windows_i686_gnu 0.48.5", + "windows_i686_msvc 0.48.5", + "windows_x86_64_gnu 0.48.5", + "windows_x86_64_gnullvm 0.48.5", + "windows_x86_64_msvc 0.48.5", +] + +[[package]] +name = "windows-targets" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6f0713a46559409d202e70e28227288446bf7841d3211583a4b53e3f6d96e7eb" +dependencies = [ + "windows_aarch64_gnullvm 0.52.5", + "windows_aarch64_msvc 0.52.5", + "windows_i686_gnu 0.52.5", + "windows_i686_gnullvm", + "windows_i686_msvc 0.52.5", + "windows_x86_64_gnu 0.52.5", + "windows_x86_64_gnullvm 0.52.5", + "windows_x86_64_msvc 0.52.5", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7088eed71e8b8dda258ecc8bac5fb1153c5cffaf2578fc8ff5d61e23578d3263" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9985fd1504e250c615ca5f281c3f7a6da76213ebd5ccc9561496568a2752afb6" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88ba073cf16d5372720ec942a8ccbf61626074c6d4dd2e745299726ce8b89670" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "87f4261229030a858f36b459e748ae97545d6f1ec60e5e0d6a3d32e0dc232ee9" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_i686_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db3c2bf3d13d5b658be73463284eaf12830ac9a26a90c717b7f771dfe97487bf" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e4246f76bdeff09eb48875a0fd3e2af6aada79d409d33011886d3e1581517d9" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "852298e482cd67c356ddd9570386e2862b5673c85bd5f88df9ab6802b334c596" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.52.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bec47e5bfd1bff0eeaf6d8b485cc1074891a197ab4225d504cb7a1ab88b02bf0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..f1069f2 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "sicli" +version = "1.0.0" +edition = "2021" + +[dependencies] +clap = { version = "4.5.7", features = ["derive","wrap_help"] } +sysinfo = "0.30.12" diff --git a/README.md b/README.md new file mode 100644 index 0000000..00949cf --- /dev/null +++ b/README.md @@ -0,0 +1,42 @@ +## System Information CLI + +Simple CLI to retrieve CPU, memory, and disk usage details as percentages. + +### CPU usage + +Prints the total usage across all cores as a percentage, then the number of cores, then the usage of each core as a percentage. + +``` +> sicli cpu --delay-ms 250 --precision 1 +1.2 16 0.0 3.8 0.0 0.0 0.0 0.0 0.0 4.0 0.0 0.0 0.0 7.4 3.8 0.0 0.0 0.0 +``` + +### Hard drive usage + +Prints the total usage as a percentage, the total disk size, then the total available space. + +``` +> sicli disk --root /home --unit tb --usage-precision 1 --size-precision 3 +63.0 1.968 0.728 +``` + +### Memory usage + +Prints the total usage as a percentage, the total memory, then the available memory. + +``` +> sicli memory --unit gib --usage-precision 1 --size-precision 2 +29.4 30.70 21.68 +``` + +### Unicode icons + +Passing `--include-icons` as the first argument will precede each usage value with a Unicode bar glyph as a visual approximation of the percentage. + +``` +> sicli -i disk -r /home -u tb -p 1 -z 3 +▅ 63.0 1.968 0.728 + +> sicli --include-icons memory --unit gib --usage-precision 1 --size-precision 2 +▂ 29.4 30.70 21.68 +``` diff --git a/src/cli_args.rs b/src/cli_args.rs new file mode 100644 index 0000000..446830b --- /dev/null +++ b/src/cli_args.rs @@ -0,0 +1,82 @@ +use clap::{Parser, Subcommand, ValueEnum}; + +#[derive(Debug, Parser)] +#[clap(name = "sicli", version)] +pub struct Cli { + /// Precede all percentage values with a matching unicode bar glyph + #[arg(short, long, default_value_t = false)] + pub include_icons: bool, + + #[clap(subcommand)] + pub command: Command, +} + +#[derive(Clone, Debug, ValueEnum)] +pub enum Unit { + Byte, + Kb, + Mb, + Gb, + Tb, + Kib, + Mib, + Gib, + Tib, +} + +#[derive(Debug, Subcommand)] +pub enum Command { + /// Print CPU usage as a total percentage, followed by the number of CPUs, followed by + /// each individual CPU's usage as a percentage + Cpu { + /// Number of milliseconds to wait between checks when computing CPU usage + /// (larger values will be more accurate, but also result in a longer delay + /// before the output is printed) + #[arg(short, long, default_value_t = 200)] + delay_ms: u64, + + /// Number of decimal points to round the usage percentages to + #[arg(short, long, default_value_t = 1)] + precision: usize, + }, + /// Print memory usage as a total percentage, followed by the total memory, + /// followed by the available memory + Memory { + /// Print metrics for swap instead of RAM + #[arg(short, long, default_value_t = false)] + swap: bool, + + /// Unit to use for total and available memory values + #[arg(value_enum, short, long, default_value_t = Unit::Gib)] + unit: Unit, + + /// Number of decimal points to round the usage percentage to + #[arg(short = 'p', long, default_value_t = 1)] + usage_precision: usize, + + /// Number of decimal points to round the total and available memory values to + /// (when not displaying bytes) + #[arg(short = 'z', long, default_value_t = 1)] + size_precision: usize, + }, + /// Print disk usage as a total percentage, followed by the total disk size, + /// followed by the available space + Disk { + /// Path to the disk's mount point + #[arg(short, long, default_value = "/")] + root: String, + + /// Unit to use for total size and available space values + #[arg(value_enum, short, long, default_value_t = Unit::Gb)] + unit: Unit, + + /// Number of decimal points to round the usage percentage to + #[arg(short = 'p', long, default_value_t = 1)] + usage_precision: usize, + + /// Number of decimal points to round the total size and available space values to + /// (when not displaying bytes) + #[arg(short = 'z', long, default_value_t = 1)] + size_precision: usize, + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..54fd0cb --- /dev/null +++ b/src/main.rs @@ -0,0 +1,20 @@ +mod cli_args; +mod stat_computers; + +use clap::Parser; +use cli_args::{Cli, Command}; +use stat_computers::{cpu, disk, memory}; + +fn main() { + let args = Cli::parse(); + match args.command { + Command::Cpu { delay_ms, precision } => + cpu(args.include_icons, delay_ms, precision), + + Command::Memory { swap, unit, usage_precision, size_precision } => + memory(args.include_icons, swap, unit, usage_precision, size_precision), + + Command::Disk { root, unit, usage_precision, size_precision } => + disk(args.include_icons, root, unit, usage_precision, size_precision), + }; +} diff --git a/src/stat_computers.rs b/src/stat_computers.rs new file mode 100644 index 0000000..caac924 --- /dev/null +++ b/src/stat_computers.rs @@ -0,0 +1,107 @@ +use sysinfo::{Disks, System}; +use std::{thread, time}; + +use crate::cli_args::Unit; + +fn unit_div(unit: &Unit) -> f64 { + return match unit { + Unit::Byte => 1.0, + Unit::Kb => 1000.0, + Unit::Mb => 1000.0 * 1000.0, + Unit::Gb => 1000.0 * 1000.0 * 1000.0, + Unit::Tb => 1000.0 * 1000.0 * 1000.0 * 1000.0, + Unit::Kib => 1024.0, + Unit::Mib => 1024.0 * 1024.0, + Unit::Gib => 1024.0 * 1024.0* 1024.0, + Unit::Tib => 1024.0 * 1024.0* 1024.0 * 1024.0, + }; +} + +fn get_icon(include_icons: bool, percent: f64) -> String { + return if !include_icons { "" } + else if percent > 87.5 { "█ " } + else if percent > 75.0 { "▆ " } + else if percent > 62.5 { "▅ " } + else if percent > 50.0 { "▄ " } + else if percent > 37.5 { "▃ " } + else if percent > 25.0 { "▂ " } + else if percent > 12.5 { "▁ " } + else { "░ " }.to_string(); +} + +pub fn cpu( + include_icons: bool, + delay_ms: u64, + precision: usize) { + let mut sys = System::new(); + let duration = time::Duration::from_millis(delay_ms); + sys.refresh_cpu(); + let cpu_count = sys.cpus().len(); + sys.refresh_cpu_usage(); + thread::sleep(duration); + sys.refresh_cpu_usage(); + let mut total_usage = 0.0; + let mut core_usage: Vec = vec![]; + for cpu in sys.cpus() { + let usage = cpu.cpu_usage(); + core_usage.push(usage); + total_usage += usage; + } + let total_percent = total_usage as f64 / cpu_count as f64; + print!("{}{:.3$} {}", + get_icon(include_icons, total_percent), total_percent, + cpu_count, + precision); + for usage in core_usage { + let usage64 = usage as f64; + print!(" {}{:.2$}", + get_icon(include_icons, usage64), usage64, + precision); + } + println!(); +} + +pub fn memory( + include_icons: bool, + swap: bool, + unit: Unit, + usage_precision: usize, + size_precision: usize) { + let mut sys = System::new(); + sys.refresh_memory(); + let unit_div = unit_div(&unit); + let total = if swap { sys.total_swap() } else { sys.total_memory() } as f64 / unit_div; + let available = if swap { sys.free_swap() } else { sys.available_memory() } as f64 / unit_div; + let used = total - available; + let usage = used / total * 100.0; + let real_size_precision = if matches!(unit, Unit::Byte) { 0 } else { size_precision }; + println!("{}{:.4$} {:.5$} {:.5$}", + get_icon(include_icons, usage), usage, + total, available, usage_precision, real_size_precision); +} + +pub fn disk( + include_icons: bool, + root: String, + unit: Unit, + usage_precision: usize, + size_precision: usize) { + let disks = Disks::new_with_refreshed_list(); + match disks.list().iter().find(|d| *d.mount_point().as_os_str() == *root) { + Some(disk) => { + let unit_dev = unit_div(&unit); + let total = disk.total_space() as f64 / unit_dev; + let available = disk.available_space() as f64 / unit_dev; + let used = total - available; + let usage = used / total * 100.0; + let real_size_precision = if matches!(unit, Unit::Byte) { 0 } else { size_precision }; + println!("{}{:.4$} {:.5$} {:.5$}", + get_icon(include_icons, usage), usage, + total, available, usage_precision, real_size_precision); + }, + None => { + eprintln!("Error: Could not find disk mounted at `{}`", root); + std::process::exit(1); + }, + }; +}