ratatui/examples/paragraph.rs

80 lines
2.4 KiB
Rust
Raw Normal View History

2016-11-07 23:35:46 +00:00
extern crate termion;
2017-10-30 21:28:37 +00:00
extern crate tui;
2016-11-07 23:35:46 +00:00
use std::io;
use termion::event;
use termion::input::TermRead;
2017-09-03 13:34:05 +00:00
use tui::backend::MouseBackend;
use tui::layout::{Alignment, Constraint, Direction, Layout, Rect};
use tui::style::{Color, Style, Modifier};
use tui::widgets::{Block, Paragraph, Widget, Text};
2018-06-09 09:02:37 +00:00
use tui::Terminal;
2016-11-07 23:35:46 +00:00
fn main() {
2017-09-03 13:34:05 +00:00
let mut terminal = Terminal::new(MouseBackend::new().unwrap()).unwrap();
2016-11-07 23:35:46 +00:00
let stdin = io::stdin();
terminal.clear().unwrap();
terminal.hide_cursor().unwrap();
2017-05-08 19:34:41 +00:00
let mut term_size = terminal.size().unwrap();
draw(&mut terminal, &term_size);
2016-11-07 23:35:46 +00:00
for c in stdin.keys() {
2017-05-08 19:34:41 +00:00
let size = terminal.size().unwrap();
if size != term_size {
terminal.resize(size).unwrap();
term_size = size;
}
draw(&mut terminal, &term_size);
2016-11-07 23:35:46 +00:00
let evt = c.unwrap();
if evt == event::Key::Char('q') {
break;
}
}
terminal.show_cursor().unwrap();
}
2017-09-03 13:34:05 +00:00
fn draw(t: &mut Terminal<MouseBackend>, size: &Rect) {
{
let mut f = t.get_frame();
Block::default()
.style(Style::default().bg(Color::White))
.render(&mut f, size);
2016-11-07 23:35:46 +00:00
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(5)
.constraints(
[
Constraint::Percentage(30),
Constraint::Percentage(30),
Constraint::Percentage(30),
].as_ref(),
)
.split(size);
2016-11-07 23:35:46 +00:00
let text = [
Text::Data("This a line\n"),
Text::StyledData("This a line\n", Style::default().fg(Color::Red)),
Text::StyledData("This a line\n", Style::default().bg(Color::Blue)),
Text::StyledData("This a longer line\n", Style::default().modifier(Modifier::CrossedOut)),
Text::StyledData("This a line\n", Style::default().fg(Color::Green).modifier(Modifier::Italic)),
];
Paragraph::new(text.iter())
.alignment(Alignment::Left)
.render(&mut f, &chunks[0]);
Paragraph::new(text.iter())
.alignment(Alignment::Center)
.wrap(true)
.render(&mut f, &chunks[1]);
Paragraph::new(text.iter())
.alignment(Alignment::Right)
.wrap(true)
.render(&mut f, &chunks[2]);
}
2016-11-07 23:35:46 +00:00
t.draw().unwrap();
}