use itertools::Itertools;
use ratatui::{
crossterm::event::{self, Event, KeyCode, KeyEventKind},
layout::{
Constraint::{self, Length, Max, Min, Percentage, Ratio},
Layout, Rect,
},
style::{Color, Style, Stylize},
text::Line,
widgets::{Block, Paragraph},
DefaultTerminal, Frame,
};
fn main() -> color_eyre::Result<()> {
color_eyre::install()?;
let terminal = ratatui::init();
let app_result = run(terminal);
ratatui::restore();
app_result
}
fn run(mut terminal: DefaultTerminal) -> color_eyre::Result<()> {
loop {
terminal.draw(draw)?;
if let Event::Key(key) = event::read()? {
if key.kind == KeyEventKind::Press && key.code == KeyCode::Char('q') {
break Ok(());
}
}
}
}
#[allow(clippy::too_many_lines)]
fn draw(frame: &mut Frame) {
let vertical = Layout::vertical([
Length(4), Length(50), Min(0), ]);
let [text_area, examples_area, _] = vertical.areas(frame.area());
frame.render_widget(
Paragraph::new(vec![
Line::from("Horizontal Layout Example. Press q to quit".dark_gray()).centered(),
Line::from("Each line has 2 constraints, plus Min(0) to fill the remaining space."),
Line::from("E.g. the second line of the Len/Min box is [Length(2), Min(2), Min(0)]"),
Line::from("Note: constraint labels that don't fit are truncated"),
]),
text_area,
);
let example_rows = Layout::vertical([
Length(9),
Length(9),
Length(9),
Length(9),
Length(9),
Min(0), ])
.split(examples_area);
let example_areas = example_rows.iter().flat_map(|area| {
Layout::horizontal([
Length(14),
Length(14),
Length(14),
Length(14),
Length(14),
Min(0), ])
.split(*area)
.iter()
.copied()
.take(5) .collect_vec()
});
let examples = [
(
"Len",
[
Length(0),
Length(2),
Length(3),
Length(6),
Length(10),
Length(15),
],
),
("Min", [Min(0), Min(2), Min(3), Min(6), Min(10), Min(15)]),
("Max", [Max(0), Max(2), Max(3), Max(6), Max(10), Max(15)]),
(
"Perc",
[
Percentage(0),
Percentage(25),
Percentage(50),
Percentage(75),
Percentage(100),
Percentage(150),
],
),
(
"Ratio",
[
Ratio(0, 4),
Ratio(1, 4),
Ratio(2, 4),
Ratio(3, 4),
Ratio(4, 4),
Ratio(6, 4),
],
),
];
for ((a, b), area) in examples
.iter()
.cartesian_product(examples.iter())
.zip(example_areas)
{
let (name_a, examples_a) = a;
let (name_b, examples_b) = b;
let constraints = examples_a.iter().copied().zip(examples_b.iter().copied());
render_example_combination(frame, area, &format!("{name_a}/{name_b}"), constraints);
}
}
fn render_example_combination(
frame: &mut Frame,
area: Rect,
title: &str,
constraints: impl ExactSizeIterator<Item = (Constraint, Constraint)>,
) {
let block = Block::bordered()
.title(title.gray())
.style(Style::reset())
.border_style(Style::default().fg(Color::DarkGray));
let inner = block.inner(area);
frame.render_widget(block, area);
let layout = Layout::vertical(vec![Length(1); constraints.len() + 1]).split(inner);
for ((a, b), &area) in constraints.into_iter().zip(layout.iter()) {
render_single_example(frame, area, vec![a, b, Min(0)]);
}
frame.render_widget(Paragraph::new("123456789012"), layout[6]);
}
fn render_single_example(frame: &mut Frame, area: Rect, constraints: Vec<Constraint>) {
let red = Paragraph::new(constraint_label(constraints[0])).on_red();
let blue = Paragraph::new(constraint_label(constraints[1])).on_blue();
let green = Paragraph::new("ยท".repeat(12)).on_green();
let horizontal = Layout::horizontal(constraints);
let [r, b, g] = horizontal.areas(area);
frame.render_widget(red, r);
frame.render_widget(blue, b);
frame.render_widget(green, g);
}
fn constraint_label(constraint: Constraint) -> String {
match constraint {
Constraint::Ratio(a, b) => format!("{a}:{b}"),
Constraint::Length(n)
| Constraint::Min(n)
| Constraint::Max(n)
| Constraint::Percentage(n)
| Constraint::Fill(n) => format!("{n}"),
}
}