feat: add add/remove-class subcommand to wc command

This commit is contained in:
Byson94
2025-11-26 19:15:28 +05:30
parent 166c440978
commit 8ec080a290
3 changed files with 73 additions and 0 deletions

View File

@@ -820,6 +820,36 @@ impl<B: DisplayBackend> App<B> {
log::error!("Failed to acquire lock on widget registry");
}
}
crate::opts::WidgetControlAction::AddClass { class, widget_name } => {
if let Ok(mut maybe_registry) = self.widget_reg_store.lock() {
if let Some(widget_registry) = maybe_registry.as_mut() {
widget_registry.update_class_of_widget_by_name(
&widget_name,
&class,
false,
);
} else {
log::error!("Widget registry is empty");
}
} else {
log::error!("Failed to acquire lock on widget registry");
}
}
crate::opts::WidgetControlAction::RemoveClass { class, widget_name } => {
if let Ok(mut maybe_registry) = self.widget_reg_store.lock() {
if let Some(widget_registry) = maybe_registry.as_mut() {
widget_registry.update_class_of_widget_by_name(
&widget_name,
&class,
true,
);
} else {
log::error!("Widget registry is empty");
}
} else {
log::error!("Failed to acquire lock on widget registry");
}
}
}
Ok(())

View File

@@ -272,6 +272,26 @@ pub enum WidgetControlAction {
#[arg(long = "widget", short = 'w')]
widget_name: String,
},
/// Add a class to a widget with given name
AddClass {
/// The class to add to the widget
class: String,
/// Name of the widget to add class to
#[arg(long = "widget", short = 'w')]
widget_name: String,
},
/// Remove a class to a widget with given name
RemoveClass {
/// The class to remove from the widget
class: String,
/// Name of the widget to remove class from
#[arg(long = "widget", short = 'w')]
widget_name: String,
},
}
impl Opt {

View File

@@ -257,6 +257,29 @@ impl WidgetRegistry {
false
}
pub fn update_class_of_widget_by_name(
&mut self,
widget_name: &str,
class: &str,
remove: bool,
) -> bool {
if let Some((&id, _)) = self
.widgets
.iter()
.find(|(_, entry)| entry.widget.widget_name().as_str() == widget_name)
{
if let Some(entry) = self.widgets.get(&id) {
if !remove {
entry.widget.style_context().add_class(class);
} else {
entry.widget.style_context().remove_class(class);
}
}
}
false
}
}
pub(super) fn build_gtk_box(