tui-rs/examples/paragraph.rs

111 lines
3.7 KiB
Rust
Raw Normal View History

#[allow(dead_code)]
mod util;
2016-11-07 23:35:46 +00:00
use std::io;
use termion::event::Key;
use termion::input::MouseTerminal;
use termion::raw::IntoRawMode;
use termion::screen::AlternateScreen;
use tui::backend::TermionBackend;
use tui::layout::{Alignment, Constraint, Direction, Layout};
2018-08-12 22:27:56 +00:00
use tui::style::{Color, Modifier, Style};
2018-12-07 19:30:19 +00:00
use tui::widgets::{Block, Borders, Paragraph, Text, Widget};
2018-06-09 09:02:37 +00:00
use tui::Terminal;
2016-11-07 23:35:46 +00:00
2019-01-06 11:57:06 +00:00
use crate::util::event::{Event, Events};
2017-05-08 19:34:41 +00:00
fn main() -> Result<(), failure::Error> {
// Terminal initialization
let stdout = io::stdout().into_raw_mode()?;
let stdout = MouseTerminal::from(stdout);
let stdout = AlternateScreen::from(stdout);
let backend = TermionBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
terminal.hide_cursor()?;
let events = Events::new();
2018-12-08 17:28:25 +00:00
let mut scroll: u16 = 0;
loop {
terminal.draw(|mut f| {
let size = f.size();
2018-12-07 19:30:19 +00:00
// Words made "loooong" to demonstrate line breaking.
2018-12-08 17:28:25 +00:00
let s = "Veeeeeeeeeeeeeeeery loooooooooooooooooong striiiiiiiiiiiiiiiiiiiiiiiiiing. ";
2018-12-07 19:30:19 +00:00
let mut long_line = s.repeat(usize::from(size.width) / s.len() + 4);
long_line.push('\n');
Block::default()
.style(Style::default().bg(Color::White))
.render(&mut f, size);
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(5)
.constraints(
[
2018-12-07 19:30:19 +00:00
Constraint::Percentage(25),
Constraint::Percentage(25),
Constraint::Percentage(25),
Constraint::Percentage(25),
]
.as_ref(),
)
.split(size);
2016-11-07 23:35:46 +00:00
let text = [
2018-12-08 17:28:25 +00:00
Text::raw("This is a line \n"),
Text::styled("This is a line \n", Style::default().fg(Color::Red)),
Text::styled("This is a line\n", Style::default().bg(Color::Blue)),
Text::styled(
2018-12-08 17:28:25 +00:00
"This is a longer line\n",
Style::default().modifier(Modifier::CROSSED_OUT),
),
2018-12-08 17:28:25 +00:00
Text::styled(&long_line, Style::default().bg(Color::Green)),
Text::styled(
2018-12-08 17:28:25 +00:00
"This is a line\n",
Style::default().fg(Color::Green).modifier(Modifier::ITALIC),
),
];
2018-12-07 19:30:19 +00:00
let block = Block::default()
.borders(Borders::ALL)
.title_style(Style::default().modifier(Modifier::BOLD));
Paragraph::new(text.iter())
2018-12-07 19:30:19 +00:00
.block(block.clone().title("Left, no wrap"))
.alignment(Alignment::Left)
.render(&mut f, chunks[0]);
Paragraph::new(text.iter())
2018-12-07 19:30:19 +00:00
.block(block.clone().title("Left, wrap"))
.alignment(Alignment::Left)
.wrap(true)
.render(&mut f, chunks[1]);
Paragraph::new(text.iter())
2018-12-07 19:30:19 +00:00
.block(block.clone().title("Center, wrap"))
.alignment(Alignment::Center)
.wrap(true)
2018-12-08 17:28:25 +00:00
.scroll(scroll)
.render(&mut f, chunks[2]);
2018-12-07 19:30:19 +00:00
Paragraph::new(text.iter())
.block(block.clone().title("Right, wrap"))
.alignment(Alignment::Right)
.wrap(true)
.render(&mut f, chunks[3]);
})?;
2018-12-08 17:28:25 +00:00
scroll += 1;
scroll %= 10;
match events.next()? {
Event::Input(key) => {
if key == Key::Char('q') {
break;
}
}
_ => {}
}
}
Ok(())
2016-11-07 23:35:46 +00:00
}