json output
This commit is contained in:
parent
6c6cb7d566
commit
1a592e1c2e
173
src/main.rs
173
src/main.rs
@ -1,5 +1,7 @@
|
|||||||
mod wayland;
|
mod wayland;
|
||||||
|
|
||||||
|
use std::io::stdout;
|
||||||
|
use std::io::Write;
|
||||||
use crate::wayland::river_status_unstable_v1::{
|
use crate::wayland::river_status_unstable_v1::{
|
||||||
zriver_output_status_v1, zriver_seat_status_v1, zriver_status_manager_v1::ZriverStatusManagerV1,
|
zriver_output_status_v1, zriver_seat_status_v1, zriver_status_manager_v1::ZriverStatusManagerV1,
|
||||||
};
|
};
|
||||||
@ -17,6 +19,36 @@ struct Globals {
|
|||||||
status_manager: Option<Main<ZriverStatusManagerV1>>,
|
status_manager: Option<Main<ZriverStatusManagerV1>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Config {
|
||||||
|
seat_name: String,
|
||||||
|
keypair: Vec<Keypair>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct Keypair {
|
||||||
|
key: String,
|
||||||
|
value: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Keypair {
|
||||||
|
fn to_string(&self) {
|
||||||
|
print!(r#""{}": "{}""#, self.key, self.value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Config {
|
||||||
|
fn json(&self) {
|
||||||
|
let len = self.keypair.len();
|
||||||
|
print!("{{");
|
||||||
|
for (i,keypair) in self.keypair.iter().enumerate() {
|
||||||
|
keypair.to_string();
|
||||||
|
if i+1 < len {
|
||||||
|
print!(", ");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
println!("}}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let display = Display::connect_to_env().unwrap();
|
let display = Display::connect_to_env().unwrap();
|
||||||
|
|
||||||
@ -31,17 +63,20 @@ fn main() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
let mut config = { Config {
|
||||||
|
seat_name: String::new(),
|
||||||
|
keypair: Vec::new()
|
||||||
|
} };
|
||||||
let mut args = std::env::args();
|
let mut args = std::env::args();
|
||||||
let mut monitor = None;
|
let mut monitor = None;
|
||||||
let mut seat_name = String::new();
|
let mut enable_tag = true;
|
||||||
let mut enable_tag = false;
|
|
||||||
let mut enable_title = false;
|
let mut enable_title = false;
|
||||||
let mut enable_views_tag = false;
|
let mut enable_views_tag = true;
|
||||||
args.next();
|
args.next();
|
||||||
loop {
|
loop {
|
||||||
match args.next() {
|
match args.next() {
|
||||||
Some(flag) => match flag.as_str() {
|
Some(flag) => match flag.as_str() {
|
||||||
"--seat" | "-s" => seat_name = args.next().unwrap_or(String::new()),
|
"--seat" | "-s" => config.seat_name = args.next().unwrap_or(String::new()),
|
||||||
"--monitor" | "-m" => {
|
"--monitor" | "-m" => {
|
||||||
monitor = match args.next().unwrap_or(String::new()).parse::<usize>() {
|
monitor = match args.next().unwrap_or(String::new()).parse::<usize>() {
|
||||||
Ok(i) => Some(i),
|
Ok(i) => Some(i),
|
||||||
@ -101,66 +136,78 @@ fn main() {
|
|||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
for seat in globals.seats {
|
for seat in globals.seats {
|
||||||
let seat_status = globals
|
if enable_title {
|
||||||
.status_manager
|
enable_tag = false;
|
||||||
.as_ref()
|
enable_views_tag = false;
|
||||||
.expect("Compositor doesn't implement river_status_unstable_v1")
|
let seat_status = globals
|
||||||
.get_river_seat_status(&seat);
|
.status_manager
|
||||||
seat.quick_assign(move |_, event, mut seat_name| {
|
.as_ref()
|
||||||
match event {
|
.expect("Compositor doesn't implement river_status_unstable_v1")
|
||||||
wl_seat::Event::Name{ name } => if enable_title && seat_name.get::<String>().unwrap().len() == 0 || name.eq(seat_name.get::<String>().unwrap()) {
|
.get_river_seat_status(&seat);
|
||||||
seat_status.quick_assign(move |_, event, _| match event {
|
seat.quick_assign(move |_, event, mut config| {
|
||||||
zriver_seat_status_v1::Event::FocusedView { title } => println!("{}", title),
|
let seat_name = &config.get::<Config>().unwrap().seat_name;
|
||||||
_ => {}
|
match event {
|
||||||
})
|
wl_seat::Event::Name{ name } => if seat_name.len() == 0 || name.eq(seat_name) {
|
||||||
} else { seat_status.quick_assign(move |_, _, _| {}) },
|
seat_status.quick_assign(move |_, event, _| match event {
|
||||||
_ => {}
|
zriver_seat_status_v1::Event::FocusedView { title } => println!("{}", title),
|
||||||
}
|
_ => {}
|
||||||
})
|
})
|
||||||
|
} else { seat_status.quick_assign(move |_, _, _| {}) },
|
||||||
|
_ => {}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else { seat.quick_assign(move |_, _, _| {}) }
|
||||||
}
|
}
|
||||||
for (_, output) in globals
|
if enable_tag || enable_views_tag {
|
||||||
.outputs
|
for (i, output) in globals
|
||||||
.iter()
|
.outputs
|
||||||
.enumerate()
|
.iter()
|
||||||
.filter(|(i, _)| match monitor {
|
.enumerate()
|
||||||
Some(monitor) => {
|
.filter(|(i, _)| if let Some(index) = monitor {
|
||||||
if *i == monitor {
|
if *i == index { true } else { false }
|
||||||
true
|
} else { true })
|
||||||
} else {
|
{
|
||||||
false
|
config.keypair.push({ Keypair {
|
||||||
}
|
key: format!("tag{}",i),
|
||||||
}
|
value: String::new()
|
||||||
None => true,
|
} });
|
||||||
})
|
config.keypair.push({ Keypair {
|
||||||
{
|
key: format!("views_tag{}",i),
|
||||||
let output_status = globals
|
value: String::new()
|
||||||
.status_manager
|
} });
|
||||||
.as_ref()
|
let output_status = globals
|
||||||
.expect("Compositor doesn't implement river_status_unstable_v1")
|
.status_manager
|
||||||
.get_river_output_status(&output);
|
.as_ref()
|
||||||
output_status.quick_assign(move |_, event, _| match event {
|
.expect("Compositor doesn't implement river_status_unstable_v1")
|
||||||
zriver_output_status_v1::Event::FocusedTags { tags } => {
|
.get_river_output_status(&output);
|
||||||
if enable_tag {
|
output_status.quick_assign(move |_, event, mut config| match event {
|
||||||
base10(tags);
|
zriver_output_status_v1::Event::FocusedTags { tags } => {
|
||||||
println!("");
|
if enable_tag {
|
||||||
}
|
for keypair in &mut config.get::<Config>().unwrap().keypair {
|
||||||
}
|
if keypair.key.eq(&format!("tag{}",i)) { keypair.value = base10(tags).trim_end().to_owned(); break }
|
||||||
zriver_output_status_v1::Event::ViewTags { tags } => {
|
}
|
||||||
if enable_views_tag {
|
|
||||||
let len = tags.len();
|
|
||||||
for i in (0..len).into_iter().step_by(4) {
|
|
||||||
let buf: [u8; 4] = [tags[i], tags[i + 1], tags[i + 2], tags[i + 3]];
|
|
||||||
base10(u32::from_le_bytes(buf));
|
|
||||||
}
|
}
|
||||||
println!("");
|
|
||||||
}
|
}
|
||||||
}
|
zriver_output_status_v1::Event::ViewTags { tags } => {
|
||||||
});
|
if enable_views_tag {
|
||||||
|
let len = tags.len();
|
||||||
|
let mut views_tag = String::new();
|
||||||
|
for i in (0..len).into_iter().step_by(4) {
|
||||||
|
let buf: [u8; 4] = [tags[i], tags[i + 1], tags[i + 2], tags[i + 3]];
|
||||||
|
views_tag.push_str(&base10(u32::from_le_bytes(buf)));
|
||||||
|
}
|
||||||
|
for keypair in &mut config.get::<Config>().unwrap().keypair {
|
||||||
|
if keypair.key.eq(&format!("views_tag{}",i)) { keypair.value = views_tag.trim_end().to_owned(); break }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
event_queue
|
event_queue
|
||||||
.dispatch(&mut seat_name, |event, object, _| {
|
.dispatch(&mut config, |event, object, _| {
|
||||||
panic!(
|
panic!(
|
||||||
"[callop] Encountered an orphan event: {}@{}: {}",
|
"[callop] Encountered an orphan event: {}@{}: {}",
|
||||||
event.interface,
|
event.interface,
|
||||||
@ -169,10 +216,14 @@ fn main() {
|
|||||||
);
|
);
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
if enable_views_tag || enable_tag {
|
||||||
|
config.json();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn base10(tagmask: u32) {
|
fn base10(tagmask: u32) -> String {
|
||||||
|
let mut format = String::new();
|
||||||
let mut tag = 0;
|
let mut tag = 0;
|
||||||
let mut current: u32;
|
let mut current: u32;
|
||||||
while {
|
while {
|
||||||
@ -181,9 +232,11 @@ fn base10(tagmask: u32) {
|
|||||||
} {
|
} {
|
||||||
tag += 1;
|
tag += 1;
|
||||||
if current != tagmask && (tagmask / current) % 2 != 0 {
|
if current != tagmask && (tagmask / current) % 2 != 0 {
|
||||||
base10(tagmask - current);
|
format.push_str(&(base10(tagmask - current)));
|
||||||
break;
|
break;
|
||||||
} else if tag == 32 { break; }
|
} else if tag == 32 { break; }
|
||||||
}
|
}
|
||||||
print!("{} ", tag);
|
format.push_str(&tag.to_string());
|
||||||
|
format.push(' ');
|
||||||
|
format
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user