Refactored handling of Option wrapping primitives
Option wrapping primities implement Copy, so they can be used directly in the closure, and is more efficient to use copies of f64/u16 than with references to them.
This commit is contained in:
parent
d7ac7de275
commit
fb49271a7e
2 changed files with 8 additions and 8 deletions
|
@ -4,6 +4,8 @@
|
||||||
|
|
||||||
Minor refactoring of how the netgraph string is handled.
|
Minor refactoring of how the netgraph string is handled.
|
||||||
|
|
||||||
|
Refactoring of handling of Option types wrapping primitive values.
|
||||||
|
|
||||||
## 0.1.5
|
## 0.1.5
|
||||||
|
|
||||||
Implemented `--netdev_width=<width>` which sets the minimum width of the netdev
|
Implemented `--netdev_width=<width>` which sets the minimum width of the netdev
|
||||||
|
|
14
src/main.rs
14
src/main.rs
|
@ -160,16 +160,14 @@ fn main() {
|
||||||
|
|
||||||
let handle_net = |is_empty: bool,
|
let handle_net = |is_empty: bool,
|
||||||
net: &mut proc::NetInfo,
|
net: &mut proc::NetInfo,
|
||||||
array: &mut SwaybarArray,
|
array: &mut SwaybarArray|
|
||||||
graph_opt: &Option<f64>,
|
|
||||||
width: &Option<u16>|
|
|
||||||
-> Result<(), proc::Error> {
|
-> Result<(), proc::Error> {
|
||||||
net.update()?;
|
net.update()?;
|
||||||
let (netinfo_string, graph_string) = net.get_netstring(*graph_opt)?;
|
let (netinfo_string, graph_string) = net.get_netstring(net_graph_max)?;
|
||||||
let netinfo_parts: Vec<&str> = netinfo_string.split_whitespace().collect();
|
let netinfo_parts: Vec<&str> = netinfo_string.split_whitespace().collect();
|
||||||
|
|
||||||
if is_empty {
|
if is_empty {
|
||||||
if graph_opt.is_some() {
|
if net_graph_max.is_some() {
|
||||||
let mut graph_obj =
|
let mut graph_obj =
|
||||||
SwaybarObject::from_string("net_graph".to_owned(), graph_string);
|
SwaybarObject::from_string("net_graph".to_owned(), graph_string);
|
||||||
graph_obj.color = Some("#ffff88".into());
|
graph_obj.color = Some("#ffff88".into());
|
||||||
|
@ -177,7 +175,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut width_string: Option<String> = None;
|
let mut width_string: Option<String> = None;
|
||||||
if let Some(width) = *width {
|
if let Some(width) = net_width {
|
||||||
let mut string = String::with_capacity(width.into());
|
let mut string = String::with_capacity(width.into());
|
||||||
for _ in 0..width {
|
for _ in 0..width {
|
||||||
string.push('0');
|
string.push('0');
|
||||||
|
@ -207,7 +205,7 @@ fn main() {
|
||||||
array.push_object(up_object);
|
array.push_object(up_object);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if graph_opt.is_some() {
|
if net_graph_max.is_some() {
|
||||||
if let Some(graph_obj) = array.get_by_name_mut("net_graph") {
|
if let Some(graph_obj) = array.get_by_name_mut("net_graph") {
|
||||||
graph_obj.full_text = graph_string;
|
graph_obj.full_text = graph_string;
|
||||||
}
|
}
|
||||||
|
@ -231,7 +229,7 @@ fn main() {
|
||||||
|
|
||||||
// network traffic
|
// network traffic
|
||||||
if let Some(net) = net_obj.as_mut() {
|
if let Some(net) = net_obj.as_mut() {
|
||||||
if let Err(e) = handle_net(is_empty, net, &mut array, &net_graph_max, &net_width) {
|
if let Err(e) = handle_net(is_empty, net, &mut array) {
|
||||||
let mut stderr_handle = io::stderr().lock();
|
let mut stderr_handle = io::stderr().lock();
|
||||||
stderr_handle.write_all(format!("{}\n", e).as_bytes()).ok();
|
stderr_handle.write_all(format!("{}\n", e).as_bytes()).ok();
|
||||||
net_obj = None;
|
net_obj = None;
|
||||||
|
|
Loading…
Reference in a new issue