Compare commits

...

15 commits

9 changed files with 458 additions and 128 deletions

322
Cargo.lock generated
View file

@ -4,13 +4,28 @@ version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
dependencies = [
"memchr",
]
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]]
name = "autocfg"
version = "1.1.0"
@ -18,78 +33,152 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "chrono"
version = "0.4.19"
name = "bumpalo"
version = "3.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "670ad68c9088c2a963aaa298cb369688cf3f9465ce5e2d4ca10e6e0098a1ce73"
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
[[package]]
name = "cc"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
dependencies = [
"libc",
"num-integer",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"time",
"winapi",
"wasm-bindgen",
"windows-targets",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "iana-time-zone"
version = "0.1.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8326b86b6cff230b97d0d312a6c40a60726df3332e721f72a1b035f451663b20"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]]
name = "itoa"
version = "1.0.2"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "112c678d4050afce233f4f2852bb2eb519230b3cf12f33585275537d7e41578d"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "js-sys"
version = "0.3.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "libc"
version = "0.2.126"
version = "0.2.150"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "349d5a591cd28b49e1d1037471617a32ddcda5731b99419008085f72d5a53836"
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "memchr"
version = "2.5.0"
version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "num-integer"
version = "0.1.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
dependencies = [
"autocfg",
"num-traits",
]
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
[[package]]
name = "num-traits"
version = "0.2.15"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
dependencies = [
"autocfg",
]
[[package]]
name = "proc-macro2"
version = "1.0.40"
name = "once_cell"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd96a1e8ed2596c337f8eae5f24924ec83f5ad5ab21ea8e455d3566c69fbcaf7"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
[[package]]
name = "proc-macro2"
version = "1.0.70"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39278fbbf5fb4f646ce651690877f89d1c5811a3d4acb27700c1cb3cdb78fd3b"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.20"
version = "1.0.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3bcdf212e9776fbcb2d23ab029360416bb1706b1aea2d1a5ba002727cbcab804"
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
dependencies = [
"proc-macro2",
]
[[package]]
name = "regex"
version = "1.6.0"
version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c4eb3267174b8c6c2f654116623910a0fef09c4753f8dd83db29c48a0df988b"
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
dependencies = [
"aho-corasick",
"memchr",
@ -98,30 +187,30 @@ dependencies = [
[[package]]
name = "regex-syntax"
version = "0.6.27"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a3f87b73ce11b1619a3c6332f45341e0047173771e8b8b73f87bfeefb7b56244"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "ryu"
version = "1.0.10"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3f6f92acf49d1b98f7a81226834412ada05458b7364277387724a237f062695"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
[[package]]
name = "serde"
version = "1.0.138"
version = "1.0.193"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1578c6245786b9d168c5447eeacfb96856573ca56c9d68fdcf394be134882a47"
checksum = "25dd9975e68d0cb5aa1120c288333fc98731bd1dd12f561e468ea4728c042b89"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.138"
version = "1.0.193"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "023e9b1467aef8a10fb88f25611870ada9800ef7e22afce356bb0d2387b6f27c"
checksum = "43576ca501357b9b071ac53cdc7da8ef0cbd9493d8df094cd821777ea6e894d3"
dependencies = [
"proc-macro2",
"quote",
@ -130,9 +219,9 @@ dependencies = [
[[package]]
name = "serde_json"
version = "1.0.82"
version = "1.0.108"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "82c2c1fdcd807d1098552c5b9a36e425e42e9fbd7c6a37a8425f390f781f7fa7"
checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b"
dependencies = [
"itoa",
"ryu",
@ -141,7 +230,7 @@ dependencies = [
[[package]]
name = "swaybar_info"
version = "0.1.9"
version = "0.1.15"
dependencies = [
"chrono",
"regex",
@ -151,56 +240,137 @@ dependencies = [
[[package]]
name = "syn"
version = "1.0.98"
version = "2.0.39"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c50aef8a904de4c23c788f104b7dddc7d6f79c647c7c8ce4cc8f73eb0ca773dd"
checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "time"
version = "0.1.44"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255"
dependencies = [
"libc",
"wasi",
"winapi",
]
[[package]]
name = "unicode-ident"
version = "1.0.1"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5bd2fe26506023ed7b5e1e315add59d6f584c621d037f9368fea9cfb988f368c"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "wasi"
version = "0.10.0+wasi-snapshot-preview1"
name = "wasm-bindgen"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a143597ca7c7793eff794def352d41792a93c481eb1042423ff7ff72ba2c31f"
[[package]]
name = "winapi"
version = "0.3.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e"
dependencies = [
"winapi-i686-pc-windows-gnu",
"winapi-x86_64-pc-windows-gnu",
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "winapi-i686-pc-windows-gnu"
version = "0.4.0"
name = "wasm-bindgen-backend"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
name = "wasm-bindgen-macro"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f"
[[package]]
name = "windows-core"
version = "0.51.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[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_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[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_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[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_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"

View file

@ -1,6 +1,6 @@
[package]
name = "swaybar_info"
version = "0.1.9"
version = "0.1.15"
edition = "2021"
description = "Provides swaybar with info to be displayed"
license = "MIT"
@ -12,4 +12,4 @@ repository = "https://github.com/Stephen-Seo/swaybar_info"
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
chrono = "0.4"
regex = "1.6"
regex = "1.10"

View file

@ -2,6 +2,32 @@
## Upcoming Changes
## 0.1.15
Update dependencies (`cargo update` and bump `regex` version).
## 0.1.14
Bumped dependency `regex` to version `1.9`.
## 0.1.13
Fix to workaround when `acpi` output contains a `0%` line with "unavailable".
When such a line is encountered, it is ignored.
## 0.1.12
Some refactoring of the code related to colorizing the netgraph.
## 0.1.11
Use pango markup to colorize the netgraph, making it look cleaner.
## 0.1.10
Colorize the netgraph based on if download or upload is greater.
Download is red, upload is green, and same amount is yellow.
## 0.1.9
Impl. changing the size of the net-graph (default 10).

View file

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2022 Stephen Seo
Copyright (c) 2022-2023 Stephen Seo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -96,7 +96,7 @@ for the output text, and the color will be left unspecified (usually defaulting
to white).
For a reference of what kind of regex is supported,
[see this page](https://docs.rs/regex/1.6.0/regex/index.html#syntax).
[see this page](https://docs.rs/regex/1.9.1/regex/index.html#syntax).
## Net graph
@ -119,6 +119,9 @@ the netgraph. Use the `--netgraph_dyn_display` option to enable this. (This
only works when dynamic netgraph is enabled with
`--netgraph_max_bytes=dynamic`.)
The size of the netgraph is 10 characters by default, but this can be changed by
using `--netgraph_size=<size>`.
## Dependencies
Uses [`serde_json`](https://crates.io/crates/serde_json),

Binary file not shown.

Before

Width:  |  Height:  |  Size: 24 KiB

After

Width:  |  Height:  |  Size: 27 KiB

View file

@ -127,7 +127,14 @@ impl BattInfo {
cmd_builder.arg("-b");
let output = cmd_builder.output()?;
let string = String::from_utf8(output.stdout)?;
let regex_captures_result = self.regex.captures(&string);
let mut last_line = "unknown".to_owned();
for line in string.lines() {
if !line.contains("unavailable") {
last_line = line.to_owned();
break;
}
}
let regex_captures_result = self.regex.captures(&last_line);
if regex_captures_result.is_none() {
self.acpi_error = true;
return Err(Error::Generic("battinfo: regex captured nothing".into()));

View file

@ -4,6 +4,7 @@ mod external;
mod proc;
mod swaybar_object;
use std::fmt::Write as FMTWrite;
use std::io::{self, Write};
use std::time::Duration;
use swaybar_object::*;
@ -49,8 +50,8 @@ fn main() {
let mut net_graph_is_dynamic: bool = false;
let mut net_graph_show_dynamic_max: bool = false;
let mut interval: Duration = Duration::from_secs(5);
let mut net_graph_size: Option<usize> = None;
if args_result.map.contains_key("netdev") {
let mut net_graph_size: Option<usize> = None;
if let Some(size_str) = args_result.map.get("netgraph-size") {
if let Ok(size) = size_str.parse::<usize>() {
if size > 0 {
@ -79,6 +80,10 @@ fn main() {
net_graph_size,
));
}
if net_graph_size.is_none() {
net_graph_size = Some(10);
}
if args_result.map.contains_key("netdevwidth") {
let width_result: Result<u16, _> = args_result.map.get("netdevwidth").unwrap().parse();
if let Ok(width) = width_result {
@ -207,7 +212,8 @@ fn main() {
array: &mut SwaybarArray|
-> Result<(), proc::Error> {
net.update()?;
let (netinfo_string, graph_string, history_max) = net.get_netstring(net_graph_max)?;
let (netinfo_string, graph_items, max_idx, history_max) =
net.get_netstring(net_graph_max)?;
let netinfo_parts: Vec<&str> = netinfo_string.split_whitespace().collect();
if is_empty {
@ -219,9 +225,11 @@ fn main() {
}
if net_graph_max.is_some() || net_graph_is_dynamic {
let mut graph_obj =
SwaybarObject::from_string("net_graph".to_owned(), graph_string);
graph_obj.color = Some("#ffff88".into());
let mut graph_obj = SwaybarObject::from_string(
"net_graph".to_owned(),
" ".to_owned().repeat(net_graph_size.unwrap()),
);
graph_obj.markup = Some("pango".to_owned());
array.push_object(graph_obj);
}
@ -259,12 +267,50 @@ fn main() {
if net_graph_is_dynamic && net_graph_show_dynamic_max {
if let Some(graph_obj) = array.get_by_name_mut("net_graph_dyn_max") {
graph_obj.full_text = history_max;
if (net_graph_max.is_some() || net_graph_is_dynamic) && !graph_items.is_empty()
{
match graph_items[max_idx].get_value_type() {
proc::GraphItemType::Download => {
graph_obj.color = Some("#ff8888ff".into())
}
proc::GraphItemType::Upload => {
graph_obj.color = Some("#88ff88ff".into())
}
proc::GraphItemType::Both => graph_obj.color = Some("#ffff88ff".into()),
}
}
}
}
if net_graph_max.is_some() || net_graph_is_dynamic {
if let Some(graph_obj) = array.get_by_name_mut("net_graph") {
graph_obj.full_text = graph_string;
let mut text = String::new();
for item in graph_items.iter() {
match item.get_value_type() {
proc::GraphItemType::Download => {
write!(
&mut text,
"<span color=\"#ff8888ff\">{}</span>",
item.get_value()
)?;
}
proc::GraphItemType::Upload => {
write!(
&mut text,
"<span color=\"#88ff88ff\">{}</span>",
item.get_value()
)?;
}
proc::GraphItemType::Both => {
write!(
&mut text,
"<span color=\"#ffff88ff\">{}</span>",
item.get_value()
)?;
}
}
}
graph_obj.full_text = text;
}
}

View file

@ -57,10 +57,41 @@ impl std::error::Error for Error {
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum GraphItemType {
Download,
Upload,
Both,
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub struct GraphItem {
value: char,
num_value: f64,
value_type: GraphItemType,
}
impl GraphItem {
pub fn get_value(&self) -> char {
self.value
}
pub fn set_value(&mut self, c: char) {
self.value = c;
}
pub fn get_num_value(&self) -> f64 {
self.num_value
}
pub fn get_value_type(&self) -> GraphItemType {
self.value_type
}
}
pub struct NetInfo {
dev_name: String,
graph: String,
graph_history: Vec<f64>,
graph: Vec<GraphItem>,
down: u64,
prev_down: u64,
up: u64,
@ -72,8 +103,11 @@ impl NetInfo {
pub fn new(dev_name: String, graph_size_opt: Option<usize>) -> Self {
let mut s = Self {
dev_name,
graph: String::from(" "),
graph_history: Vec::new(),
graph: vec![GraphItem {
value: ' ',
num_value: 0.0,
value_type: GraphItemType::Both,
}],
down: 0,
prev_down: 0,
up: 0,
@ -83,7 +117,6 @@ impl NetInfo {
if let Some(graph_size) = graph_size_opt {
if graph_size > 0 {
s.graph_history.resize(graph_size, 0.0);
s.graph = s.graph.repeat(graph_size);
} else {
let mut stderr_handle = std::io::stderr().lock();
@ -93,11 +126,9 @@ impl NetInfo {
.as_bytes(),
)
.ok();
s.graph_history.resize(10, 0.0);
s.graph = s.graph.repeat(10);
}
} else {
s.graph_history.resize(10, 0.0);
s.graph = s.graph.repeat(10);
}
@ -145,11 +176,11 @@ impl NetInfo {
Ok(())
}
// Returns netinfo down/up, graph, and history_max (if dynamic is enabled)
// Returns netinfo down/up, graph, max idx, and history_max (if dynamic is enabled)
pub fn get_netstring(
&mut self,
graph_max_opt: Option<f64>,
) -> Result<(String, String, String), Error> {
) -> Result<(String, &Vec<GraphItem>, usize, String), Error> {
let down_diff: f64 = if self.down > self.prev_down {
let value = (self.down - self.prev_down) as f64;
self.prev_down = self.down;
@ -182,13 +213,19 @@ impl NetInfo {
write!(&mut output, "{:.0} B", up_diff)?;
}
let mut graph_type = GraphItemType::Both;
let diff_max = if down_diff > up_diff {
graph_type = GraphItemType::Download;
down_diff
} else {
if down_diff < up_diff {
graph_type = GraphItemType::Upload;
}
up_diff
};
let mut diff_max_string = String::new();
let mut history_max_idx = 0;
if let Some(graph_max) = graph_max_opt {
let graph_value: u8 = if diff_max > graph_max {
@ -199,27 +236,73 @@ impl NetInfo {
self.graph.remove(0);
match graph_value {
0 => self.graph.push(' '),
1 => self.graph.push('▁'),
2 => self.graph.push('▂'),
3 => self.graph.push('▃'),
4 => self.graph.push('▄'),
5 => self.graph.push('▅'),
6 => self.graph.push('▆'),
7 => self.graph.push('▇'),
_ => self.graph.push('█'),
0 => self.graph.push(GraphItem {
value: ' ',
num_value: 0.0,
value_type: graph_type,
}),
1 => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
2 => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
3 => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
4 => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
5 => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
6 => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
7 => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
_ => self.graph.push(GraphItem {
value: '',
num_value: 0.0,
value_type: graph_type,
}),
}
} else {
self.graph_history.rotate_left(1);
self.graph.rotate_left(1);
{
let end_idx = self.graph_history.len() - 1;
self.graph_history[end_idx] = diff_max;
let end_idx = self.graph.len() - 1;
self.graph[end_idx] = GraphItem {
value: ' ',
num_value: diff_max,
value_type: graph_type,
};
}
let mut history_max: f64 = 0.0;
for value in &self.graph_history {
if history_max < *value {
history_max = *value;
for (idx, value) in self
.graph
.iter()
.map(|item| item.get_num_value())
.enumerate()
{
if history_max < value {
history_max = value;
history_max_idx = idx;
}
}
@ -235,27 +318,22 @@ impl NetInfo {
write!(&mut diff_max_string, "{:.0} B", history_max)?;
}
self.graph.clear();
if history_max == 0.0 {
self.graph = String::from(" ").repeat(self.graph_history.len());
} else {
for value in &self.graph_history {
match (8.0 * value / history_max).round() as u8 {
0 => self.graph.push(' '),
1 => self.graph.push('▁'),
2 => self.graph.push('▂'),
3 => self.graph.push('▃'),
4 => self.graph.push('▄'),
5 => self.graph.push('▅'),
6 => self.graph.push('▆'),
7 => self.graph.push('▇'),
_ => self.graph.push('█'),
}
for item in self.graph.iter_mut() {
match (8.0 * item.get_num_value() / history_max).round() as u8 {
0 => item.set_value(' '),
1 => item.set_value('▁'),
2 => item.set_value('▂'),
3 => item.set_value('▃'),
4 => item.set_value('▄'),
5 => item.set_value('▅'),
6 => item.set_value('▆'),
7 => item.set_value('▇'),
_ => item.set_value('█'),
}
}
}
Ok((output, self.graph.clone(), diff_max_string))
Ok((output, &self.graph, history_max_idx, diff_max_string))
}
}