Skip to content

Commit

Permalink
Merge pull request #308 from feefs/main
Browse files Browse the repository at this point in the history
Implement palette.spacing feature
  • Loading branch information
grtcdr authored May 12, 2024
2 parents 3f46c47 + c3b94b6 commit 0023f77
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 14 deletions.
3 changes: 2 additions & 1 deletion contrib/themes/Beryllium.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ border = "plain"
visible = true

[palette]
glyph = ""
glyph = ""
spacing = 2
visible = true

[bar]
Expand Down
3 changes: 2 additions & 1 deletion contrib/themes/Lithium.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ separator_color = "Yellow"

[palette]
type = "Light"
glyph = ""
glyph = ""
spacing = 2
visible = true

[bar]
Expand Down
14 changes: 12 additions & 2 deletions src/theme/components.rs
Original file line number Diff line number Diff line change
@@ -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<PaletteType>,
glyph: Option<String>,
spacing: Option<usize>,
visible: Option<bool>,
}

Expand All @@ -18,6 +19,7 @@ impl Default for Palette {
Palette {
r#type: Some(PaletteType::Dark),
glyph: Some(String::from(" ")),
spacing: Some(0),
visible: None,
}
}
Expand All @@ -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;
Expand Down
46 changes: 36 additions & 10 deletions src/widgets/readout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u16>() + constraints.len() as u16 - 1;
let total_line_width = constraints.iter().sum::<u16>();
if total_line_width > max_line_width {
max_line_width = total_line_width;
}
Expand All @@ -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,
Expand All @@ -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() {
Expand Down Expand Up @@ -174,17 +181,24 @@ impl<'a> ReadoutList<'a> {
];

let span_vector = |colors: &[Color]| -> Vec<_> {
let mut spans: Vec<Span<'_>> = 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() {
Expand All @@ -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(
Expand Down

0 comments on commit 0023f77

Please sign in to comment.