Skip to content
This repository has been archived by the owner on Aug 29, 2020. It is now read-only.

Adding Network speed in bits per second - issue #57 #61

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn setup_app<'a, 'b>(
None
},
Some(DiskWidget::new(colorscheme)),
Some(NetWidget::new(colorscheme, &args.interface)),
Some(NetWidget::new(colorscheme, &args.interface, args.bits)),
Some(TempWidget::new(colorscheme, args.fahrenheit)),
)
};
Expand Down
4 changes: 4 additions & 0 deletions src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ pub struct Args {
/// Show a statusbar with the time.
#[structopt(short = "s", long = "statusbar")]
pub statusbar: bool,

/// Show Network speed in bits per second
#[structopt(short = "B", long = "bits")]
pub bits: bool,
}
38 changes: 29 additions & 9 deletions src/widgets/net.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ pub struct NetWidget<'a, 'b> {
colorscheme: &'a Colorscheme,

interface: &'b str,
show_bits: bool,

bytes_recv: Vec<u64>,
bytes_sent: Vec<u64>,
Expand All @@ -29,7 +30,11 @@ pub struct NetWidget<'a, 'b> {
}

impl NetWidget<'_, '_> {
pub fn new<'a, 'b>(colorscheme: &'a Colorscheme, interface: &'b str) -> NetWidget<'a, 'b> {
pub fn new<'a, 'b>(
colorscheme: &'a Colorscheme,
interface: &'b str,
show_bits: bool,
) -> NetWidget<'a, 'b> {
NetWidget {
title: if interface == "all" {
" Network Usage ".to_string()
Expand All @@ -48,6 +53,7 @@ impl NetWidget<'_, '_> {
total_bytes_sent: 0,

collector: network::NetIoCountersCollector::default(),
show_bits,
}
}
}
Expand Down Expand Up @@ -139,10 +145,17 @@ impl Widget for NetWidget<'_, '_> {
buf.set_string(
top_half.x + 1,
top_half.y + 2,
format!(
"Rx/s: {}/s",
Size::Bytes(self.bytes_recv.last().unwrap().to_owned())
),
if self.show_bits {
format!(
"Rx/s: {} bits/s",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything looks good except we need the 'bits' label to change to 'Kb', 'Mb', etc as the amount grows. Maybe there's a rust library that can do this for us or we can write our own function to do this.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are currently using https://github.com/neosmart/prettysize-rs to do this for the bytes. I don't know if it works for bits, if there's another library that does, or if we should make a pull request.

self.bytes_recv.last().unwrap().to_owned() * 8
)
} else {
format!(
"Rx/s: {}/s",
Size::Bytes(self.bytes_recv.last().unwrap().to_owned())
)
},
self.colorscheme.text.modifier(Modifier::BOLD),
);

Expand Down Expand Up @@ -176,10 +189,17 @@ impl Widget for NetWidget<'_, '_> {
buf.set_string(
bottom_half.x + 1,
bottom_half.y + 2,
format!(
"Tx/s: {}/s",
Size::Bytes(self.bytes_sent.last().unwrap().to_owned())
),
if self.show_bits {
format!(
"Tx/s: {} bits/s",
self.bytes_sent.last().unwrap().to_owned() * 8
)
} else {
format!(
"Rx/s: {}/s",
Size::Bytes(self.bytes_sent.last().unwrap().to_owned())
)
},
self.colorscheme.text.modifier(Modifier::BOLD),
);

Expand Down