From 1ea5467b3cee48a72c86382f687be0f68b5ba7f6 Mon Sep 17 00:00:00 2001 From: feefs Date: Sun, 31 Mar 2024 11:06:33 -0700 Subject: [PATCH 1/2] Implement palette.spacing feature --- src/theme/components.rs | 14 +++++++++++-- src/widgets/readout.rs | 46 ++++++++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 12 deletions(-) diff --git a/src/theme/components.rs b/src/theme/components.rs index a8abca7..d95a43a 100644 --- a/src/theme/components.rs +++ b/src/theme/components.rs @@ -1,15 +1,16 @@ use crate::theme::borders::Border; use crate::theme::color::*; use rand::Rng; -use serde::{Deserialize, Serialize}; -use std::path::PathBuf; use ratatui::style::Color; use ratatui::widgets::BorderType; +use serde::{Deserialize, Serialize}; +use std::path::PathBuf; #[derive(Debug, Clone, Serialize, Deserialize)] pub struct Palette { r#type: Option, glyph: Option, + spacing: Option, visible: Option, } @@ -18,6 +19,7 @@ impl Default for Palette { Palette { r#type: Some(PaletteType::Dark), glyph: Some(String::from(" ")), + spacing: Some(0), visible: None, } } @@ -36,6 +38,14 @@ impl Palette { self.glyph.as_ref() } + pub fn get_spacing(&self) -> usize { + if let Some(v) = self.spacing { + return v; + } + + 0 + } + pub fn is_visible(&self) -> bool { if let Some(v) = self.visible { return v; diff --git a/src/widgets/readout.rs b/src/widgets/readout.rs index 74424f1..56c898d 100644 --- a/src/widgets/readout.rs +++ b/src/widgets/readout.rs @@ -104,7 +104,7 @@ impl<'a> Widget for ReadoutList<'a> { self.create_item_constraints(max_key_width, &themed_separator, readout_data); let layout = Self::create_layout(&list_item_area, &constraints); - let total_line_width = constraints.iter().sum::() + constraints.len() as u16 - 1; + let total_line_width = constraints.iter().sum::(); if total_line_width > max_line_width { max_line_width = total_line_width; } @@ -125,7 +125,13 @@ impl<'a> Widget for ReadoutList<'a> { height += readout_data.height() as u16; } - self.print_palette(buf, &list_area, &mut height, self.theme.get_palette()); + self.print_palette( + buf, + &list_area, + &mut height, + &mut max_line_width, + self.theme.get_palette(), + ); Self::render_block( self.block, @@ -145,6 +151,7 @@ impl<'a> ReadoutList<'a> { buf: &mut Buffer, list_area: &Rect, height: &mut u16, + max_line_width: &mut u16, palette: &Palette, ) { if !palette.is_visible() { @@ -174,17 +181,24 @@ impl<'a> ReadoutList<'a> { ]; let span_vector = |colors: &[Color]| -> Vec<_> { + let mut spans: Vec> = Vec::with_capacity(colors.len() * 2); + let spacing_span = Span::raw(" ".repeat(palette.get_spacing())); if let Some(glyph) = palette.get_glyph() { - colors - .iter() - .map(|c| Span::styled(glyph.to_owned(), Style::default().fg(c.to_owned()))) - .collect() + for c in colors { + spans.push(Span::styled( + glyph.to_owned(), + Style::default().fg(c.to_owned()), + )); + spans.push(spacing_span.clone()); + } } else { - colors - .iter() - .map(|c| Span::styled(" ", Style::default().bg(c.to_owned()))) - .collect() + for c in colors { + spans.push(Span::styled(" ", Style::default().bg(c.to_owned()))); + spans.push(spacing_span.clone()); + } } + spans.pop(); + spans }; let spans = match palette.get_type() { @@ -196,6 +210,18 @@ impl<'a> ReadoutList<'a> { ], }; + let mut width_values = vec![]; + if self.theme.get_padding() > 0 { + width_values.push(self.theme.get_padding()) + } + if let Some(span_max_width) = spans.iter().map(|s| s.width()).max() { + width_values.push(span_max_width); + } + let palette_max_line_width: usize = width_values.iter().sum(); + if palette_max_line_width as u16 > *max_line_width { + *max_line_width = palette_max_line_width as u16; + } + let padding = self.theme.get_padding() as u16; let area = Rect::new( From c3b94b6e2461a4ad90bbe5a24b028ade0b456c82 Mon Sep 17 00:00:00 2001 From: feefs Date: Sun, 31 Mar 2024 11:22:22 -0700 Subject: [PATCH 2/2] Update Beryllium and Lithium themes with palette.spacing feature --- contrib/themes/Beryllium.toml | 3 ++- contrib/themes/Lithium.toml | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/contrib/themes/Beryllium.toml b/contrib/themes/Beryllium.toml index df0ba58..f2abcdc 100644 --- a/contrib/themes/Beryllium.toml +++ b/contrib/themes/Beryllium.toml @@ -10,7 +10,8 @@ border = "plain" visible = true [palette] -glyph = "○ " +glyph = "○" +spacing = 2 visible = true [bar] diff --git a/contrib/themes/Lithium.toml b/contrib/themes/Lithium.toml index 9d62533..aaaca49 100644 --- a/contrib/themes/Lithium.toml +++ b/contrib/themes/Lithium.toml @@ -9,7 +9,8 @@ separator_color = "Yellow" [palette] type = "Light" -glyph = " ● " +glyph = "●" +spacing = 2 visible = true [bar]