use ratatui::style::Color;
use super::{UsageDisplayState, UsageLayout, UsageMetric};
impl UsageDisplayState {
pub fn new() -> Self {
Self::default()
}
pub fn with_metrics(metrics: Vec<UsageMetric>) -> Self {
Self {
metrics,
..Self::default()
}
}
pub fn with_layout(mut self, layout: UsageLayout) -> Self {
self.layout = layout;
self
}
pub fn with_title(mut self, title: impl Into<String>) -> Self {
self.title = Some(title.into());
self
}
pub fn with_separator(mut self, separator: impl Into<String>) -> Self {
self.separator = separator.into();
self
}
pub fn with_disabled(mut self, disabled: bool) -> Self {
self.disabled = disabled;
self
}
pub fn metric(mut self, metric: UsageMetric) -> Self {
self.metrics.push(metric);
self
}
pub fn metrics(&self) -> &[UsageMetric] {
&self.metrics
}
pub fn layout(&self) -> UsageLayout {
self.layout
}
pub fn title(&self) -> Option<&str> {
self.title.as_deref()
}
pub fn separator(&self) -> &str {
&self.separator
}
pub fn len(&self) -> usize {
self.metrics.len()
}
pub fn is_empty(&self) -> bool {
self.metrics.is_empty()
}
pub fn is_disabled(&self) -> bool {
self.disabled
}
pub fn set_metrics(&mut self, metrics: Vec<UsageMetric>) {
self.metrics = metrics;
}
pub fn add_metric(&mut self, metric: UsageMetric) {
self.metrics.push(metric);
}
pub fn remove_metric(&mut self, label: &str) {
self.metrics.retain(|m| m.label != label);
}
pub fn update_value(&mut self, label: &str, value: impl Into<String>) -> bool {
if let Some(metric) = self.metrics.iter_mut().find(|m| m.label == label) {
metric.value = value.into();
true
} else {
false
}
}
pub fn update_color(&mut self, label: &str, color: Option<Color>) -> bool {
if let Some(metric) = self.metrics.iter_mut().find(|m| m.label == label) {
metric.color = color;
true
} else {
false
}
}
pub fn set_layout(&mut self, layout: UsageLayout) {
self.layout = layout;
}
pub fn set_title(&mut self, title: Option<String>) {
self.title = title;
}
pub fn set_separator(&mut self, separator: impl Into<String>) {
self.separator = separator.into();
}
pub fn set_disabled(&mut self, disabled: bool) {
self.disabled = disabled;
}
pub fn clear(&mut self) {
self.metrics.clear();
}
pub fn find(&self, label: &str) -> Option<&UsageMetric> {
self.metrics.iter().find(|m| m.label == label)
}
pub fn find_mut(&mut self, label: &str) -> Option<&mut UsageMetric> {
self.metrics.iter_mut().find(|m| m.label == label)
}
}