Created
April 21, 2023 06:51
-
-
Save sophacles/7cf4bb8181485e5c418b0f0438881737 to your computer and use it in GitHub Desktop.
New ui func
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fn ui<B: Backend>(f: &mut Frame<B>, app: &App) { | |
let size = f.size(); | |
let chunks = Layout::default() | |
.direction(Direction::Vertical) | |
.margin(5) | |
.constraints( | |
[ | |
Constraint::Percentage(25), | |
Constraint::Percentage(25), | |
Constraint::Percentage(25), | |
Constraint::Percentage(25), | |
] | |
.as_ref(), | |
) | |
.split(size); | |
let text = vec![Spans::from("This is a line ")]; | |
let create_block = |title, dflt| { | |
Block::default() | |
.borders(Borders::ALL) | |
.title(Span::styled( | |
title, | |
Style::default().add_modifier(Modifier::BOLD), | |
)) | |
.border_style(if dflt { | |
Style::default() | |
} else { | |
Style::default().bg(Color::Red).fg(Color::Yellow) | |
}) | |
}; | |
use ratatui::{buffer::Buffer, layout::Rect, text::Text, widgets::Widget}; | |
struct ExampleWidget<'a> { | |
style: Style, | |
block: Block<'a>, | |
paragraph: Paragraph<'a>, | |
} | |
impl<'a> ExampleWidget<'a> { | |
fn new<T: Into<Text<'a>>>(t: T, style: Style, block: Block<'a>) -> Self { | |
Self { | |
style, | |
block, | |
paragraph: Paragraph::new(t), | |
} | |
} | |
} | |
impl<'a> Widget for ExampleWidget<'a> { | |
fn render(mut self, area: Rect, buf: &mut Buffer) { | |
buf.set_style(area, Style::reset()); | |
let text_area = self.block.inner(area); | |
self.block.render(area, buf); | |
self.paragraph | |
.style(self.style) | |
.alignment(Alignment::Left) | |
.render(text_area, buf); | |
} | |
} | |
f.render_widget( | |
ExampleWidget::new( | |
text.clone(), | |
Style::default(), | |
create_block("dflt dflt", true), | |
), | |
chunks[0], | |
); | |
f.render_widget( | |
ExampleWidget::new( | |
text.clone(), | |
Style::default().bg(Color::Blue).fg(Color::Green), | |
create_block("para = Gr/Bl bl = dflt", true), | |
), | |
chunks[1], | |
); | |
f.render_widget( | |
ExampleWidget::new( | |
text.clone(), | |
Style::default(), | |
create_block("para = dflt, block = Y/red", false), | |
), | |
chunks[2], | |
); | |
f.render_widget( | |
ExampleWidget::new( | |
text.clone(), | |
Style::default().bg(Color::Blue).fg(Color::Green), | |
create_block("Para = b/g block = y/red ", false), | |
), | |
chunks[3], | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment