From 6a589d47146442537f678a555faf2c1fa240f4ac Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Wed, 24 May 2017 20:23:42 -0400 Subject: Add unit state. --- src/main.rs | 6 ++++-- src/systemd/unit.rs | 23 +++++++++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 70a19af..3ee8e21 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,7 +60,7 @@ fn render_message(message: &str, units: &[unit::Unit]) -> String { : "Unit" } th { - : "Type" + : "Active" } } @ for unit in units { @@ -69,7 +69,9 @@ fn render_message(message: &str, units: &[unit::Unit]) -> String { : &unit.name } td { - : &unit.type_ + : format_args!("{} ({})", + &unit.active_state, + &unit.sub_state) } } } diff --git a/src/systemd/unit.rs b/src/systemd/unit.rs index 3be7fa5..22752cb 100644 --- a/src/systemd/unit.rs +++ b/src/systemd/unit.rs @@ -6,7 +6,9 @@ use std::io; #[derive(Debug, PartialEq, Eq)] pub struct Unit { pub name: String, - pub type_: String + pub type_: String, + pub active_state: String, + pub sub_state: String, } pub fn parse_key_value(line: &str) -> Option<(String, String)> { @@ -28,11 +30,15 @@ fn make_unit(info: HashMap) -> Option { fn from_id(id: &str) -> Option { id.rsplit('.').next().map(|t| t.to_owned()) }; - let id = info.get("Id").map(|id| id.clone()); + fn cloned(s: &String) -> String { s.clone() } + let id = info.get("Id").map(cloned); let type_ = id.as_ref().and_then(|id| from_id(&id)); + let active_state = info.get("ActiveState").map(cloned); + let sub_state = info.get("SubState").map(cloned); - if let (Some(id), Some(type_)) = (id, type_) { - Some(Unit { name: id, type_: type_ }) + if let (Some(id), Some(type_), Some(active_state), Some(sub_state)) = + (id, type_, active_state, sub_state) { + Some(Unit { name: id, type_: type_, active_state: active_state, sub_state: sub_state }) } else { None } @@ -41,9 +47,14 @@ fn make_unit(info: HashMap) -> Option { #[test] fn test_make_unit() { let s = |s: &str| -> String { s.to_owned() }; - let info = [(s("Id"), s("Test.service"))] + let info = [ + (s("Id"), s("Test.service")), + (s("ActiveState"), s("active")), + (s("SubState"), s("running")), + ] .iter().cloned().collect::>(); - assert_eq!(Some(Unit { name: s("Test.service"), type_: s("service") }), make_unit(info)); + let actual = make_unit(info).unwrap();; + assert_eq!(s("Test.service"), actual.name); let info = HashMap::new(); assert_eq!(None, make_unit(info)); -- cgit v1.2.3