From f9961a149df487ca43f50355a1e224dc32101469 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Sun, 10 Jul 2022 17:58:10 +0900 Subject: [PATCH] Refactor net error handling --- src/main.rs | 7 ++++--- src/swaybar_object.rs | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index ea7b0c8..c27d40f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -55,10 +55,11 @@ fn main() { let mut array = SwaybarArray::new(); let set_net_error = |is_empty: bool, array: &mut SwaybarArray| { if is_empty { - let down_obj = SwaybarObject::from_string("net_down".to_owned(), "Net ERROR".into()); + let down_obj = + SwaybarObject::from_error_string("net_down".to_owned(), "Net ERROR".into()); array.push_object(down_obj); - let up_obj = SwaybarObject::from_string("net_up".to_owned(), "Net ERROR".into()); + let up_obj = SwaybarObject::from_error_string("net_up".to_owned(), "Net ERROR".into()); array.push_object(up_obj); } else { let down_ref_opt = array.get_by_name_mut("net_down"); @@ -120,7 +121,7 @@ fn main() { if let Some(net) = net_obj.as_mut() { if let Err(e) = handle_net(is_empty, net, &mut array) { let mut stderr_handle = io::stderr().lock(); - stderr_handle.write_all(e.to_string().as_bytes()).ok(); + stderr_handle.write_all(format!("{}\n", e).as_bytes()).ok(); net_obj = None; set_net_error(is_empty, &mut array); } diff --git a/src/swaybar_object.rs b/src/swaybar_object.rs index 131aae1..f07ef6e 100644 --- a/src/swaybar_object.rs +++ b/src/swaybar_object.rs @@ -117,6 +117,28 @@ impl SwaybarObject { } } + pub fn from_error_string(name: String, msg: String) -> Self { + Self { + full_text: msg, + short_text: None, + color: Some("#ff2222ff".into()), + background: None, + border: Some("#ffffffff".into()), + border_top: None, + border_bottom: None, + border_left: None, + border_right: None, + min_width: None, + align: None, + name: Some(name), + instance: None, + urgent: None, + separator: None, + separator_block_width: None, + markup: None, + } + } + pub fn update_as_net_down(&mut self, metric: String) { self.full_text = metric; self.color = Some("#ff8888ff".to_owned());