Print the title of each output

This commit is contained in:
Alexander Rosenberg 2024-04-27 03:18:14 -07:00
parent 770568e68f
commit 380c72f0c3
Signed by: Zander671
GPG Key ID: 5FD0394ADBD72730

View File

@ -16,7 +16,7 @@ use wayland_client::{Attached, DispatchData, Display, GlobalEvent, GlobalManager
#[derive(Debug)]
struct Flags {
tags: bool,
title: bool,
titles: bool,
urgency: bool,
viewtags: bool,
layout: bool,
@ -29,7 +29,7 @@ impl Flags {
fn default() -> Flags {
Flags {
tags: false,
title: false,
titles: false,
urgency: false,
viewtags: false,
layout: false,
@ -48,7 +48,7 @@ struct Env {
output_names: HashMap<u32, String>,
focused: Option<u32>,
layout: Option<String>,
title: Option<String>,
titles: Option<HashMap<u32, String>>,
mode: Option<String>,
tags: Option<HashMap<u32, Tags>>,
urgency: Option<HashMap<u32, Tags>>,
@ -95,10 +95,10 @@ impl Serialize for Env {
} else {
state.skip_field("layout")?;
}
if let Some(title) = self.title.as_ref() {
state.serialize_field("title", title)?;
if let Some(titles) = self.titles.as_ref() {
state.serialize_field("titles", &self.translate_output_map(titles))?;
} else {
state.skip_field("title")?;
state.skip_field("titles")?;
}
if let Some(mode) = self.mode.as_ref() {
state.serialize_field("mode", mode)?;
@ -106,7 +106,7 @@ impl Serialize for Env {
state.skip_field("mode")?;
}
// kind of inelegant
if self.focused.is_some() &&
if self.flags.focused && self.focused.is_some() &&
self.output_names.contains_key(&self.focused.unwrap()) {
state.serialize_field(
"focused", &self.output_names[&self.focused.unwrap()])?;
@ -136,10 +136,10 @@ impl Env {
fn new() -> Env {
let flags = configuration();
Env {
title: None,
layout: None,
mode: None,
focused: None,
titles: flags.titles.then(Default::default),
tags: flags.tags.then(Default::default),
urgency: flags.urgency.then(Default::default),
viewtags: flags.viewtags.then(Default::default),
@ -150,7 +150,7 @@ impl Env {
}
fn fmt(&self) {
if self.title.is_some()
if self.titles.is_some()
|| self.tags.is_some()
|| self.mode.is_some()
|| self.urgency.is_some()
@ -178,21 +178,23 @@ fn handle_river_seat_status(
if let Some(env) = env.get::<Env>() {
match event {
zriver_seat_status_v1::Event::FocusedOutput { output } => {
if env.flags.focused {
if env.flags.focused || env.flags.titles {
env.focused = Some(output.as_ref().id());
}
}
},
zriver_seat_status_v1::Event::FocusedView { title } => {
if env.flags.title {
env.title = Some(title);
if env.flags.titles && env.focused.is_some() {
if let Some(titles) = &mut env.titles {
titles.insert(env.focused.unwrap(), title);
}
}
},
zriver_seat_status_v1::Event::Mode { name } => {
if env.flags.mode {
env.mode = Some(name);
}
}
_ => {}
},
_ => {},
}
}
}
@ -203,7 +205,7 @@ fn handle_seat_event(
match event {
wl_seat::Event::Name { name } => {
if let Some(env) = env.get::<Env>() {
if (env.flags.title || env.flags.mode || env.flags.focused)
if (env.flags.titles || env.flags.mode || env.flags.focused)
&& (env.flags.seat.is_none()
|| name.eq(env.flags.seat.as_ref().unwrap()))
{
@ -230,13 +232,9 @@ fn handle_river_output_status(
tags: focused_tags,
} => {
if let Some(tags) = &mut env.tags {
if let Some(inner_value) = tags.get_mut(&output_id) {
(*inner_value) = Tags(focused_tags);
} else {
tags.insert(output_id, Tags(focused_tags));
}
}
}
zriver_output_status_v1::Event::ViewTags {
tags,
} => {
@ -245,8 +243,7 @@ fn handle_river_output_status(
.chunks(4)
.map(|s| {
let buf = [s[0], s[1], s[2], s[3]];
let tagmask =
u32::from_le_bytes(buf);
let tagmask = u32::from_le_bytes(buf);
for i in 0..32 {
if 1 << i == tagmask {
return 1 + i;
@ -255,22 +252,14 @@ fn handle_river_output_status(
0
})
.collect();
if let Some(inner_value) = viewtags.get_mut(&output_id) {
(*inner_value) = tags;
} else {
viewtags.insert(output_id, tags);
}
}
}
zriver_output_status_v1::Event::UrgentTags {
tags,
} => {
if let Some(urgency) = &mut env.urgency {
if let Some(inner_value) = urgency.get_mut(&output_id) {
(*inner_value) = Tags(tags);
} else {
urgency .insert(output_id, Tags(tags));
}
urgency.insert(output_id, Tags(tags));
}
}
zriver_output_status_v1::Event::LayoutName {
@ -339,6 +328,18 @@ fn global_manager_callback(
GlobalEvent::Removed { id, interface: _ } => {
if let Some(env) = env.get::<Env>() {
env.output_names.remove(&id);
if let Some(titles) = &mut env.titles {
titles.remove(&id);
}
if let Some(tags) = &mut env.tags {
tags.remove(&id);
}
if let Some(urgency) = &mut env.urgency {
urgency.remove(&id);
}
if let Some(viewtags) = &mut env.viewtags {
viewtags.remove(&id);
}
}
}
}
@ -353,7 +354,7 @@ fn configuration() -> Flags {
Some(flag) => match flag.as_str() {
"--seat" | "-s" => default.seat = args.next(),
"--urgency" | "-u" => default.urgency = true,
"--title" | "-w" => default.title = true,
"--title" | "-w" => default.titles = true,
"--mode" | "-m" => default.mode = true,
"--focused" | "-f" => default.focused = true,
"--tags" | "-t" => default.tags = true,
@ -362,7 +363,7 @@ fn configuration() -> Flags {
"--help" | "-h" => {
print!("Usage: ristate [option]\n\n");
print!(" --tag | -t the focused tag of each output\n");
print!(" --title | -w the title of the focused view\n");
print!(" --titles | -w the titles of focused views\n");
print!(" --mode | -m the current input mode\n");
print!(" --layout | -l display the name of the layout\n");
print!(" --focused | -f the name of the focused output\n");
@ -376,7 +377,7 @@ fn configuration() -> Flags {
None => break,
}
}
if !default.urgency && !default.title && !default.mode && !default.focused
if !default.urgency && !default.titles && !default.mode && !default.focused
&& !default.tags && !default.layout && !default.viewtags {
println!("error: You must specify at least one thing to listen for!");
std::process::exit(1);