2018-09-23 18:59:51 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
mod util;
|
|
|
|
|
2019-01-06 11:57:06 +00:00
|
|
|
use crate::util::event::{Event, Events};
|
2020-03-13 01:02:51 +00:00
|
|
|
use std::{error::Error, io};
|
|
|
|
use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen};
|
|
|
|
use tui::{
|
|
|
|
backend::TermionBackend,
|
|
|
|
layout::{Alignment, Constraint, Direction, Layout},
|
|
|
|
style::{Color, Modifier, Style},
|
|
|
|
widgets::{Block, Borders, Paragraph, Text},
|
|
|
|
Terminal,
|
|
|
|
};
|
2017-05-08 19:34:41 +00:00
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn Error>> {
|
2018-09-23 18:59:51 +00:00
|
|
|
// 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;
|
2018-09-23 18:59:51 +00:00
|
|
|
loop {
|
2020-06-15 20:57:23 +00:00
|
|
|
terminal.draw(|f| {
|
2018-12-07 14:43:05 +00:00
|
|
|
let size = f.size();
|
2018-09-23 18:59:51 +00:00
|
|
|
|
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);
|
2018-12-07 14:43:05 +00:00
|
|
|
long_line.push('\n');
|
2018-09-29 21:12:34 +00:00
|
|
|
|
2019-12-15 20:38:18 +00:00
|
|
|
let block = Block::default()
|
|
|
|
.style(Style::default().bg(Color::White));
|
|
|
|
f.render_widget(block, size);
|
2018-09-23 18:59:51 +00:00
|
|
|
|
|
|
|
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),
|
2018-09-23 18:59:51 +00:00
|
|
|
]
|
2018-12-07 15:17:33 +00:00
|
|
|
.as_ref(),
|
|
|
|
)
|
|
|
|
.split(size);
|
2016-11-07 23:35:46 +00:00
|
|
|
|
2018-09-23 18:59:51 +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)),
|
2018-09-23 18:59:51 +00:00
|
|
|
Text::styled(
|
2018-12-08 17:28:25 +00:00
|
|
|
"This is a longer line\n",
|
2019-02-25 14:15:00 +00:00
|
|
|
Style::default().modifier(Modifier::CROSSED_OUT),
|
2018-09-23 18:59:51 +00:00
|
|
|
),
|
2018-12-08 17:28:25 +00:00
|
|
|
Text::styled(&long_line, Style::default().bg(Color::Green)),
|
2018-09-23 18:59:51 +00:00
|
|
|
Text::styled(
|
2018-12-08 17:28:25 +00:00
|
|
|
"This is a line\n",
|
2019-02-25 14:15:00 +00:00
|
|
|
Style::default().fg(Color::Green).modifier(Modifier::ITALIC),
|
2018-09-23 18:59:51 +00:00
|
|
|
),
|
|
|
|
];
|
2018-08-12 20:13:32 +00:00
|
|
|
|
2018-12-07 19:30:19 +00:00
|
|
|
let block = Block::default()
|
|
|
|
.borders(Borders::ALL)
|
2019-02-25 14:15:00 +00:00
|
|
|
.title_style(Style::default().modifier(Modifier::BOLD));
|
2019-12-15 20:38:18 +00:00
|
|
|
let paragraph = Paragraph::new(text.iter())
|
2018-12-07 19:30:19 +00:00
|
|
|
.block(block.clone().title("Left, no wrap"))
|
2019-12-15 20:38:18 +00:00
|
|
|
.alignment(Alignment::Left);
|
|
|
|
f.render_widget(paragraph, chunks[0]);
|
|
|
|
let paragraph = Paragraph::new(text.iter())
|
2018-12-07 19:30:19 +00:00
|
|
|
.block(block.clone().title("Left, wrap"))
|
|
|
|
.alignment(Alignment::Left)
|
2019-12-15 20:38:18 +00:00
|
|
|
.wrap(true);
|
|
|
|
f.render_widget(paragraph, chunks[1]);
|
|
|
|
let paragraph = Paragraph::new(text.iter())
|
2018-12-07 19:30:19 +00:00
|
|
|
.block(block.clone().title("Center, wrap"))
|
|
|
|
.alignment(Alignment::Center)
|
2018-09-23 18:59:51 +00:00
|
|
|
.wrap(true)
|
2020-07-06 21:47:52 +00:00
|
|
|
.scroll((scroll, 0));
|
2019-12-15 20:38:18 +00:00
|
|
|
f.render_widget(paragraph, chunks[2]);
|
|
|
|
let paragraph = Paragraph::new(text.iter())
|
2018-12-07 19:30:19 +00:00
|
|
|
.block(block.clone().title("Right, wrap"))
|
|
|
|
.alignment(Alignment::Right)
|
2019-12-15 20:38:18 +00:00
|
|
|
.wrap(true);
|
|
|
|
f.render_widget(paragraph, chunks[3]);
|
2018-09-23 18:59:51 +00:00
|
|
|
})?;
|
|
|
|
|
2018-12-08 17:28:25 +00:00
|
|
|
scroll += 1;
|
|
|
|
scroll %= 10;
|
|
|
|
|
2020-05-16 22:51:57 +00:00
|
|
|
if let Event::Input(key) = events.next()? {
|
|
|
|
if key == Key::Char('q') {
|
|
|
|
break;
|
2018-12-07 15:17:33 +00:00
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
2016-11-07 23:35:46 +00:00
|
|
|
}
|