mirror of
https://github.com/ratatui-org/ratatui
synced 2024-11-26 14:40:30 +00:00
137 lines
4.1 KiB
Rust
137 lines
4.1 KiB
Rust
use crossterm::{
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode},
|
|
execute,
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
};
|
|
use ratatui::{
|
|
backend::{Backend, CrosstermBackend},
|
|
layout::{Alignment, Constraint, Direction, Layout},
|
|
style::{Color, Modifier, Style},
|
|
text::Span,
|
|
widgets::{Block, BorderType, Borders, Padding, Paragraph},
|
|
Frame, Terminal,
|
|
};
|
|
use std::{error::Error, io};
|
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
|
// setup terminal
|
|
enable_raw_mode()?;
|
|
let mut stdout = io::stdout();
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
let backend = CrosstermBackend::new(stdout);
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
// 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 {
|
|
println!("{err:?}");
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn run_app<B: Backend>(terminal: &mut Terminal<B>) -> io::Result<()> {
|
|
loop {
|
|
terminal.draw(ui)?;
|
|
|
|
if let Event::Key(key) = event::read()? {
|
|
if let KeyCode::Char('q') = key.code {
|
|
return Ok(());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn ui<B: Backend>(f: &mut Frame<B>) {
|
|
// Wrapping block for a group
|
|
// Just draw the block and the group on the same area and build the group
|
|
// with at least a margin of 1
|
|
let size = f.size();
|
|
|
|
// Surrounding block
|
|
let block = Block::default()
|
|
.borders(Borders::ALL)
|
|
.title("Main block with round corners")
|
|
.title_alignment(Alignment::Center)
|
|
.border_type(BorderType::Rounded);
|
|
f.render_widget(block, size);
|
|
|
|
let chunks = Layout::default()
|
|
.direction(Direction::Vertical)
|
|
.margin(4)
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
.split(f.size());
|
|
|
|
// Top two inner blocks
|
|
let top_chunks = Layout::default()
|
|
.direction(Direction::Horizontal)
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
.split(chunks[0]);
|
|
|
|
// Top left inner block with green background
|
|
let block = Block::default()
|
|
.title(vec![
|
|
Span::styled("With", Style::default().fg(Color::Yellow)),
|
|
Span::from(" background"),
|
|
])
|
|
.style(Style::default().bg(Color::Green));
|
|
f.render_widget(block, top_chunks[0]);
|
|
|
|
// Top right inner block with styled title aligned to the right
|
|
let block = Block::default()
|
|
.title(Span::styled(
|
|
"Styled title",
|
|
Style::default()
|
|
.fg(Color::White)
|
|
.bg(Color::Red)
|
|
.add_modifier(Modifier::BOLD),
|
|
))
|
|
.title_alignment(Alignment::Right);
|
|
f.render_widget(block, top_chunks[1]);
|
|
|
|
// Bottom two inner blocks
|
|
let bottom_chunks = Layout::default()
|
|
.direction(Direction::Horizontal)
|
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref())
|
|
.split(chunks[1]);
|
|
|
|
// Bottom left block with all default borders
|
|
let block = Block::default()
|
|
.title("With borders")
|
|
.borders(Borders::ALL)
|
|
.padding(Padding {
|
|
left: 4,
|
|
right: 4,
|
|
top: 2,
|
|
bottom: 2,
|
|
});
|
|
|
|
let text = Paragraph::new("text inside padded block").block(block);
|
|
f.render_widget(text, bottom_chunks[0]);
|
|
|
|
// Bottom right block with styled left and right border
|
|
let block = Block::default()
|
|
.title("With styled borders and doubled borders")
|
|
.border_style(Style::default().fg(Color::Cyan))
|
|
.borders(Borders::LEFT | Borders::RIGHT)
|
|
.border_type(BorderType::Double)
|
|
.padding(Padding::uniform(1));
|
|
|
|
let inner_block = Block::default()
|
|
.title("Block inside padded block")
|
|
.borders(Borders::ALL);
|
|
|
|
let inner_area = block.inner(bottom_chunks[1]);
|
|
f.render_widget(block, bottom_chunks[1]);
|
|
f.render_widget(inner_block, inner_area);
|
|
}
|