2023-06-12 05:07:15 +00:00
|
|
|
use std::{error::Error, io};
|
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
use crossterm::{
|
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2023-08-13 07:38:43 +00:00
|
|
|
use itertools::Itertools;
|
|
|
|
use ratatui::{layout::Constraint::*, prelude::*, widgets::*};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2021-11-01 21:27:46 +00:00
|
|
|
// setup terminal
|
|
|
|
enable_raw_mode()?;
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
2018-09-23 18:59:51 +00:00
|
|
|
let mut terminal = Terminal::new(backend)?;
|
2018-04-01 15:57:26 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// create app and run it
|
|
|
|
let res = run_app(&mut terminal);
|
|
|
|
|
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
|
|
|
|
|
|
|
if let Err(err) = res {
|
2023-05-22 03:46:02 +00:00
|
|
|
println!("{err:?}");
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2018-04-01 15:57:26 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {
|
2018-04-01 15:57:26 +00:00
|
|
|
loop {
|
2023-09-26 05:30:36 +00:00
|
|
|
terminal.draw(ui)?;
|
2021-11-01 21:27:46 +00:00
|
|
|
|
|
|
|
if let Event::Key(key) = event::read()? {
|
2021-12-23 17:46:25 +00:00
|
|
|
if let KeyCode::Char('q') = key.code {
|
|
|
|
return Ok(());
|
2018-12-07 15:17:33 +00:00
|
|
|
}
|
2018-04-01 15:57:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2018-04-01 15:57:26 +00:00
|
|
|
|
2023-09-26 05:30:36 +00:00
|
|
|
fn ui(frame: &mut Frame) {
|
2024-01-05 15:45:14 +00:00
|
|
|
let vertical = Layout::vertical([
|
|
|
|
Length(4), // text
|
|
|
|
Length(50), // examples
|
|
|
|
Min(0), // fills remaining space
|
|
|
|
]);
|
|
|
|
let [text_area, examples_area, _] = frame.size().split(&vertical);
|
2021-11-01 21:27:46 +00:00
|
|
|
|
2023-08-13 07:38:43 +00:00
|
|
|
// title
|
2023-07-24 19:05:37 +00:00
|
|
|
frame.render_widget(
|
2023-08-13 07:38:43 +00:00
|
|
|
Paragraph::new(vec![
|
|
|
|
Line::from("Horizontal Layout Example. Press q to quit".dark_gray())
|
|
|
|
.alignment(Alignment::Center),
|
|
|
|
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"),
|
|
|
|
]),
|
2024-01-05 15:45:14 +00:00
|
|
|
text_area,
|
2023-07-24 19:05:37 +00:00
|
|
|
);
|
|
|
|
|
2024-01-05 15:45:14 +00:00
|
|
|
let example_rows = Layout::vertical([
|
|
|
|
Length(9),
|
|
|
|
Length(9),
|
|
|
|
Length(9),
|
|
|
|
Length(9),
|
|
|
|
Length(9),
|
|
|
|
Min(0), // fills remaining space
|
|
|
|
])
|
|
|
|
.split(examples_area);
|
2023-08-13 07:38:43 +00:00
|
|
|
let example_areas = example_rows
|
|
|
|
.iter()
|
|
|
|
.flat_map(|area| {
|
2024-01-05 15:45:14 +00:00
|
|
|
Layout::horizontal([
|
|
|
|
Constraint::Length(14),
|
|
|
|
Constraint::Length(14),
|
|
|
|
Constraint::Length(14),
|
|
|
|
Constraint::Length(14),
|
|
|
|
Constraint::Length(14),
|
|
|
|
Constraint::Min(0), // fills remaining space
|
|
|
|
])
|
|
|
|
.split(*area)
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.take(5) // ignore Min(0)
|
|
|
|
.collect_vec()
|
2023-08-13 07:38:43 +00:00
|
|
|
})
|
|
|
|
.collect_vec();
|
|
|
|
|
|
|
|
// the examples are a cartesian product of the following constraints
|
|
|
|
// e.g. Len/Len, Len/Min, Len/Max, Len/Perc, Len/Ratio, Min/Len, Min/Min, ...
|
|
|
|
let examples = [
|
|
|
|
(
|
|
|
|
"Len",
|
|
|
|
vec![
|
|
|
|
Length(0),
|
|
|
|
Length(2),
|
|
|
|
Length(3),
|
|
|
|
Length(6),
|
|
|
|
Length(10),
|
|
|
|
Length(15),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Min",
|
|
|
|
vec![Min(0), Min(2), Min(3), Min(6), Min(10), Min(15)],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Max",
|
|
|
|
vec![Max(0), Max(2), Max(3), Max(6), Max(10), Max(15)],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Perc",
|
|
|
|
vec![
|
|
|
|
Percentage(0),
|
|
|
|
Percentage(25),
|
|
|
|
Percentage(50),
|
|
|
|
Percentage(75),
|
|
|
|
Percentage(100),
|
|
|
|
Percentage(150),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"Ratio",
|
|
|
|
vec![
|
|
|
|
Ratio(0, 4),
|
|
|
|
Ratio(1, 4),
|
|
|
|
Ratio(2, 4),
|
|
|
|
Ratio(3, 4),
|
|
|
|
Ratio(4, 4),
|
|
|
|
Ratio(6, 4),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
];
|
|
|
|
|
|
|
|
for (i, (a, b)) in examples
|
|
|
|
.iter()
|
|
|
|
.cartesian_product(examples.iter())
|
|
|
|
.enumerate()
|
|
|
|
{
|
|
|
|
let (name_a, examples_a) = a;
|
|
|
|
let (name_b, examples_b) = b;
|
|
|
|
let constraints = examples_a
|
|
|
|
.iter()
|
|
|
|
.copied()
|
|
|
|
.zip(examples_b.iter().copied())
|
|
|
|
.collect_vec();
|
|
|
|
render_example_combination(
|
|
|
|
frame,
|
|
|
|
example_areas[i],
|
|
|
|
&format!("{name_a}/{name_b}"),
|
|
|
|
constraints,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Renders a single example box
|
2023-09-26 05:30:36 +00:00
|
|
|
fn render_example_combination(
|
|
|
|
frame: &mut Frame,
|
2023-08-13 07:38:43 +00:00
|
|
|
area: Rect,
|
|
|
|
title: &str,
|
|
|
|
constraints: Vec<(Constraint, Constraint)>,
|
|
|
|
) {
|
|
|
|
let block = Block::default()
|
|
|
|
.title(title.gray())
|
|
|
|
.style(Style::reset())
|
|
|
|
.borders(Borders::ALL)
|
|
|
|
.border_style(Style::default().fg(Color::DarkGray));
|
|
|
|
let inner = block.inner(area);
|
|
|
|
frame.render_widget(block, area);
|
2024-01-05 15:45:14 +00:00
|
|
|
let layout = Layout::vertical(vec![Length(1); constraints.len() + 1]).split(inner);
|
2023-08-13 07:38:43 +00:00
|
|
|
for (i, (a, b)) in constraints.iter().enumerate() {
|
|
|
|
render_single_example(frame, layout[i], vec![*a, *b, Min(0)]);
|
|
|
|
}
|
|
|
|
// This is to make it easy to visually see the alignment of the examples
|
|
|
|
// with the constraints.
|
|
|
|
frame.render_widget(Paragraph::new("123456789012"), layout[6]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Renders a single example line
|
2023-09-26 05:30:36 +00:00
|
|
|
fn render_single_example(frame: &mut Frame, area: Rect, constraints: Vec<Constraint>) {
|
2023-08-13 07:38:43 +00:00
|
|
|
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();
|
2024-01-05 15:45:14 +00:00
|
|
|
let horizontal = Layout::horizontal(constraints);
|
|
|
|
let [r, b, g] = area.split(&horizontal);
|
|
|
|
frame.render_widget(red, r);
|
|
|
|
frame.render_widget(blue, b);
|
|
|
|
frame.render_widget(green, g);
|
2023-08-13 07:38:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn constraint_label(constraint: Constraint) -> String {
|
|
|
|
match constraint {
|
|
|
|
Length(n) => format!("{n}"),
|
|
|
|
Min(n) => format!("{n}"),
|
|
|
|
Max(n) => format!("{n}"),
|
|
|
|
Percentage(n) => format!("{n}"),
|
|
|
|
Ratio(a, b) => format!("{a}:{b}"),
|
|
|
|
}
|
2018-04-01 15:57:26 +00:00
|
|
|
}
|