tui-rs/examples/paragraph.rs

68 lines
2.0 KiB
Rust
Raw Normal View History

2016-11-07 23:35:46 +00:00
extern crate tui;
extern crate termion;
use std::io;
use termion::event;
use termion::input::TermRead;
use tui::Terminal;
2017-09-03 13:34:05 +00:00
use tui::backend::MouseBackend;
2016-11-07 23:35:46 +00:00
use tui::widgets::{Widget, Block, Paragraph};
2017-05-08 19:34:41 +00:00
use tui::layout::{Group, Direction, Size, Rect};
2016-11-07 23:35:46 +00:00
use tui::style::{Style, Color};
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) {
2016-11-07 23:35:46 +00:00
Block::default()
.style(Style::default().bg(Color::White))
2017-05-21 09:13:24 +00:00
.render(t, size);
2016-11-07 23:35:46 +00:00
Group::default()
.direction(Direction::Vertical)
.margin(5)
.sizes(&[Size::Percent(100)])
2017-05-21 09:13:24 +00:00
.render(t, size, |t, chunks| {
2016-11-07 23:35:46 +00:00
Group::default()
.direction(Direction::Horizontal)
.sizes(&[Size::Percent(100)])
.render(t, &chunks[0], |t, chunks| {
Paragraph::default()
2017-09-11 05:58:37 +00:00
.text(
"This is a line\n{fg=red This is a line}\n{bg=red This is a \
2016-11-07 23:35:46 +00:00
line}\n{mod=italic This is a line}\n{mod=bold This is a \
line}\n{mod=crossed_out This is a line}\n{mod=invert This is a \
line}\n{mod=underline This is a \
2017-09-11 05:58:37 +00:00
line}\n{bg=green;fg=yellow;mod=italic This is a line}\n",
)
2016-11-07 23:35:46 +00:00
.render(t, &chunks[0]);
});
});
t.draw().unwrap();
}