Refactor net error handling

This commit is contained in:
Stephen Seo 2022-07-10 17:58:10 +09:00
parent 35ff17ef0f
commit f9961a149d
2 changed files with 26 additions and 3 deletions

View file

@ -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);
}

View file

@ -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());