improved json output

This commit is contained in:
Bryan Ndjeutcha 2021-09-01 19:46:50 -04:00
parent ff8c50a416
commit aa6a531616

View File

@ -10,7 +10,6 @@ use wayland_client::{Display, GlobalManager, Main};
#[derive(Debug)] #[derive(Debug)]
enum Value { enum Value {
Tags(u32), Tags(u32),
Title(String),
ViewsTag(Vec<u32>), ViewsTag(Vec<u32>),
} }
@ -39,57 +38,80 @@ impl Flags {
struct Env { struct Env {
flags: Flags, flags: Flags,
hashmap: HashMap<String, Value>, title: Option<String>,
tags: HashMap<String, u32>,
viewstag: HashMap<String, Vec<u32>>,
status_manager: Option<Main<ZriverStatusManagerV1>>, status_manager: Option<Main<ZriverStatusManagerV1>>,
} }
impl Env { impl Env {
fn new() -> Env { fn new() -> Env {
Env { Env {
status_manager: None, title: None,
flags: configuration(), flags: configuration(),
hashmap: HashMap::new(), viewstag: HashMap::new(),
tags: HashMap::new(),
status_manager: None,
} }
} }
fn set_value(&mut self, key: &str, value: Value) { fn set_value(&mut self, key: &str, value: Value) {
if let Some(inner_value) = self.hashmap.get_mut(key) { match value {
(*inner_value) = value; Value::Tags( tags ) => {
if let Some(inner_value) = self.tags.get_mut(key) {
(*inner_value) = tags;
} else { } else {
self.hashmap.insert(key.to_string(), value); self.tags.insert(key.to_string(), tags);
}
}
Value::ViewsTag( tags ) => {
if let Some(inner_value) = self.viewstag.get_mut(key) {
(*inner_value) = tags;
} else {
self.viewstag.insert(key.to_string(), tags);
}
}
} }
} }
fn fmt(&self) { fn fmt(&self) {
let mut i = 0; if !self.tags.is_empty()
let len = self.hashmap.len(); || !self.viewstag.is_empty()
if len > 0 { || self.title.is_some() {
print!("{{"); print!("{{");
for (key, val) in self.hashmap.iter() { let mut comma = false;
print!("{:?} :", key); if !self.tags.is_empty() {
match val { print!("\"tags\" : [");
Value::Tags(tags) => { let len = self.tags.len();
for (i, (key, tags)) in self.tags.iter().enumerate() {
print!("{{{:?} : ", key);
print!("["); print!("[");
fmt_tags(*tags); fmt_tags(*tags);
print!("]}}");
if i < len - 1 { print!(", "); }
}
print!("]"); print!("]");
comma = true;
} }
Value::Title(title) => { if !self.viewstag.is_empty() {
print!("{:?}", title); if comma { print!(", "); }
} print!("\"viewstag\" : [");
Value::ViewsTag(tags) => { let vlen = self.viewstag.len();
for (i, (key, tags)) in self.viewstag.iter().enumerate() {
print!("{{{:?} : ", key);
print!("["); print!("[");
let len = tags.len(); let len = tags.len();
for (i, tag) in tags.iter().enumerate() { for (i, tag) in tags.iter().enumerate() {
print!("\"{}\"", tag); print!("\"{}\"", tag);
if i < len - 1 { if i < len - 1 { print!(", "); }
print!(", ");
} }
print!("]}}");
if i < vlen - 1 { print!(", "); }
} }
print!("]"); print!("]");
comma = true;
} }
} if let Some(title) = self.title.as_ref() {
i += 1; if comma { print!(", "); }
if i < len { print!("\"title\" : {:?}", title);
print!(", ");
}
} }
println!("}}"); println!("}}");
} }
@ -129,7 +151,7 @@ fn main() {
move |_, event, mut env| match event { move |_, event, mut env| match event {
zriver_seat_status_v1::Event::FocusedView { title } => { zriver_seat_status_v1::Event::FocusedView { title } => {
if let Some(env) = env.get::<Env>() { if let Some(env) = env.get::<Env>() {
env.set_value("Title", Value::Title(title)); env.title = Some(title);
} }
} }
_ => {} _ => {}
@ -160,9 +182,6 @@ fn main() {
{ {
if let Some(status_manager) = &env.status_manager { if let Some(status_manager) = &env.status_manager {
make = make.replace(' ', "").to_string(); make = make.replace(' ', "").to_string();
let tags_key = format!("Tags-{}", make);
let urgent_key = format!("UrgentTags-{}", make);
let views_key = format!("ViewsTag-{}", make);
let output_status = let output_status =
status_manager.get_river_output_status(&output); status_manager.get_river_output_status(&output);
output_status.quick_assign(move |_, event, mut env| { output_status.quick_assign(move |_, event, mut env| {
@ -172,7 +191,7 @@ fn main() {
tags, tags,
} => { } => {
if env.flags.tags { if env.flags.tags {
env.set_value(&tags_key, Value::Tags(tags)); env.set_value(&make, Value::Tags(tags));
} }
} }
zriver_output_status_v1::Event::ViewTags { zriver_output_status_v1::Event::ViewTags {
@ -194,7 +213,7 @@ fn main() {
}) })
.collect(); .collect();
env.set_value( env.set_value(
&views_key, &make,
Value::ViewsTag(tags), Value::ViewsTag(tags),
); );
} }
@ -204,7 +223,7 @@ fn main() {
} => { } => {
if env.flags.urgency { if env.flags.urgency {
env.set_value( env.set_value(
&urgent_key, &make,
Value::Tags(tags), Value::Tags(tags),
); );
} }