feat(examples): allow overlap spacing in explorer (#2316)

Store spacing as i16 so negative values map to Spacing::Overlap, and
show overlap in the axis label.

Co-authored-by: Orhun Parmaksız <orhun@archlinux.org>
This commit is contained in:
Josh McKinney
2026-01-06 17:23:44 -08:00
committed by GitHub
parent 3df8c20dfb
commit ae975c7200

View File

@@ -1,3 +1,5 @@
use std::cmp::Ordering;
/// A Ratatui example that demonstrates how different layout constraints work. /// A Ratatui example that demonstrates how different layout constraints work.
/// ///
/// It also supports swapping constraints, adding and removing blocks, and changing the spacing /// It also supports swapping constraints, adding and removing blocks, and changing the spacing
@@ -30,7 +32,7 @@ fn main() -> Result<()> {
#[derive(Default)] #[derive(Default)]
struct App { struct App {
mode: AppMode, mode: AppMode,
spacing: u16, spacing: i16,
constraints: Vec<Constraint>, constraints: Vec<Constraint>,
selected_index: usize, selected_index: usize,
value: u16, value: u16,
@@ -269,7 +271,7 @@ impl App {
} }
fn instructions() -> impl Widget { fn instructions() -> impl Widget {
let text = "◄ ►: select, ▲ ▼: edit, 1-6: swap, a: add, x: delete, q: quit, + -: spacing"; let text = "◄ ►: select, ▲ ▼: edit, 1-6: swap, a: add, x: delete, q: quit, +/-: spacing";
Paragraph::new(text) Paragraph::new(text)
.fg(Self::TEXT_COLOR) .fg(Self::TEXT_COLOR)
.centered() .centered()
@@ -307,10 +309,12 @@ impl App {
/// ///
/// Only shows the gap when spacing is not zero /// Only shows the gap when spacing is not zero
fn axis(&self, width: u16) -> impl Widget { fn axis(&self, width: u16) -> impl Widget {
let label = if self.spacing != 0 { let label = match self.spacing.cmp(&0) {
format!("{} px (gap: {} px)", width, self.spacing) Ordering::Greater => format!("{width} px (gap: {} px)", self.spacing),
} else { Ordering::Less => {
format!("{width} px") format!("{width} px (overlap: {} px)", self.spacing.unsigned_abs())
}
Ordering::Equal => format!("{width} px"),
}; };
let bar_width = width.saturating_sub(2) as usize; // we want to `<` and `>` at the ends let bar_width = width.saturating_sub(2) as usize; // we want to `<` and `>` at the ends
let width_bar = format!("<{label:-^bar_width$}>"); let width_bar = format!("<{label:-^bar_width$}>");