From e3cdf05554de11cb8f59032357d4b22f1ce2577a Mon Sep 17 00:00:00 2001 From: Fabio Petrillo Date: Sun, 5 Apr 2020 17:12:06 -0400 Subject: [PATCH 1/2] Adding Network speed in bits per second - issue #57 --- src/app.rs | 2 +- src/args.rs | 6 +++++- src/widgets/net.rs | 34 ++++++++++++++++++++++++---------- 3 files changed, 30 insertions(+), 12 deletions(-) diff --git a/src/app.rs b/src/app.rs index 3cef51f..9f088e8 100644 --- a/src/app.rs +++ b/src/app.rs @@ -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)), ) }; diff --git a/src/args.rs b/src/args.rs index 7b79f6f..c24d8c1 100644 --- a/src/args.rs +++ b/src/args.rs @@ -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, +} \ No newline at end of file diff --git a/src/widgets/net.rs b/src/widgets/net.rs index 1a05ffc..ed54204 100644 --- a/src/widgets/net.rs +++ b/src/widgets/net.rs @@ -18,6 +18,7 @@ pub struct NetWidget<'a, 'b> { colorscheme: &'a Colorscheme, interface: &'b str, + show_bits: bool, bytes_recv: Vec, bytes_sent: Vec, @@ -28,8 +29,8 @@ pub struct NetWidget<'a, 'b> { collector: network::NetIoCountersCollector, } -impl NetWidget<'_, '_> { - pub fn new<'a, 'b>(colorscheme: &'a Colorscheme, interface: &'b str) -> NetWidget<'a, 'b> { +impl NetWidget<'_, '_,> { + 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() @@ -48,6 +49,7 @@ impl NetWidget<'_, '_> { total_bytes_sent: 0, collector: network::NetIoCountersCollector::default(), + show_bits: show_bits, } } } @@ -139,10 +141,16 @@ 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", + 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), ); @@ -176,10 +184,16 @@ 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), ); From 7d25de545b478ae5d58ccbd9ddbcd842f31873b8 Mon Sep 17 00:00:00 2001 From: Fabio Petrillo Date: Sun, 5 Apr 2020 17:24:54 -0400 Subject: [PATCH 2/2] Adding Network speed in bits per second - issue #57 --- src/args.rs | 2 +- src/widgets/net.rs | 30 ++++++++++++++++++------------ 2 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/args.rs b/src/args.rs index c24d8c1..f342859 100644 --- a/src/args.rs +++ b/src/args.rs @@ -55,4 +55,4 @@ pub struct Args { /// Show Network speed in bits per second #[structopt(short = "B", long = "bits")] pub bits: bool, -} \ No newline at end of file +} diff --git a/src/widgets/net.rs b/src/widgets/net.rs index ed54204..d16ca3a 100644 --- a/src/widgets/net.rs +++ b/src/widgets/net.rs @@ -29,8 +29,12 @@ pub struct NetWidget<'a, 'b> { collector: network::NetIoCountersCollector, } -impl NetWidget<'_, '_,> { - pub fn new<'a, 'b>(colorscheme: &'a Colorscheme, interface: &'b str, show_bits: bool) -> NetWidget<'a, 'b> { +impl NetWidget<'_, '_> { + 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() @@ -49,7 +53,7 @@ impl NetWidget<'_, '_,> { total_bytes_sent: 0, collector: network::NetIoCountersCollector::default(), - show_bits: show_bits, + show_bits, } } } @@ -144,12 +148,13 @@ impl Widget for NetWidget<'_, '_> { if self.show_bits { format!( "Rx/s: {} bits/s", - self.bytes_recv.last().unwrap().to_owned()*8) - } - else { + self.bytes_recv.last().unwrap().to_owned() * 8 + ) + } else { format!( "Rx/s: {}/s", - Size::Bytes(self.bytes_recv.last().unwrap().to_owned())) + Size::Bytes(self.bytes_recv.last().unwrap().to_owned()) + ) }, self.colorscheme.text.modifier(Modifier::BOLD), ); @@ -187,13 +192,14 @@ impl Widget for NetWidget<'_, '_> { if self.show_bits { format!( "Tx/s: {} bits/s", - self.bytes_sent.last().unwrap().to_owned()*8) - } - else { + self.bytes_sent.last().unwrap().to_owned() * 8 + ) + } else { format!( "Rx/s: {}/s", - Size::Bytes(self.bytes_sent.last().unwrap().to_owned())) - }, + Size::Bytes(self.bytes_sent.last().unwrap().to_owned()) + ) + }, self.colorscheme.text.modifier(Modifier::BOLD), );