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},
|
2020-07-11 17:08:28 +00:00
|
|
|
style::{Color, Modifier, Style},
|
2020-05-10 13:44:30 +00:00
|
|
|
text::{Span, Spans},
|
|
|
|
widgets::{Block, Borders, Paragraph, Wrap},
|
2020-03-13 01:02:51 +00:00
|
|
|
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)?;
|
|
|
|
|
|
|
|
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()
|
2020-07-11 17:08:28 +00:00
|
|
|
.style(Style::default().bg(Color::White).fg(Color::Black));
|
2019-12-15 20:38:18 +00:00
|
|
|
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
|
|
|
|
2020-05-10 13:44:30 +00:00
|
|
|
let text = vec![
|
|
|
|
Spans::from("This is a line "),
|
2020-07-11 17:08:28 +00:00
|
|
|
Spans::from(Span::styled("This is a line ", Style::default().fg(Color::Red))),
|
|
|
|
Spans::from(Span::styled("This is a line", Style::default().bg(Color::Blue))),
|
2020-05-10 13:44:30 +00:00
|
|
|
Spans::from(Span::styled(
|
|
|
|
"This is a longer line",
|
2020-07-11 17:08:28 +00:00
|
|
|
Style::default().add_modifier(Modifier::CROSSED_OUT),
|
2020-05-10 13:44:30 +00:00
|
|
|
)),
|
2020-07-11 17:08:28 +00:00
|
|
|
Spans::from(Span::styled(&long_line, Style::default().bg(Color::Green))),
|
2020-05-10 13:44:30 +00:00
|
|
|
Spans::from(Span::styled(
|
|
|
|
"This is a line",
|
2020-07-11 17:08:28 +00:00
|
|
|
Style::default().fg(Color::Green).add_modifier(Modifier::ITALIC),
|
2020-05-10 13:44:30 +00:00
|
|
|
)),
|
2018-09-23 18:59:51 +00:00
|
|
|
];
|
2018-08-12 20:13:32 +00:00
|
|
|
|
2020-05-10 13:44:30 +00:00
|
|
|
let create_block = |title| {
|
|
|
|
Block::default()
|
|
|
|
.borders(Borders::ALL)
|
2020-07-11 17:08:28 +00:00
|
|
|
.style(Style::default().bg(Color::White).fg(Color::Black))
|
|
|
|
.title(Span::styled(title, Style::default().add_modifier(Modifier::BOLD)))
|
2020-05-10 13:44:30 +00:00
|
|
|
};
|
|
|
|
let paragraph = Paragraph::new(text.clone())
|
2020-07-11 17:08:28 +00:00
|
|
|
.style(Style::default().bg(Color::White).fg(Color::Black))
|
2020-05-10 13:44:30 +00:00
|
|
|
.block(create_block("Left, no wrap"))
|
2019-12-15 20:38:18 +00:00
|
|
|
.alignment(Alignment::Left);
|
|
|
|
f.render_widget(paragraph, chunks[0]);
|
2020-05-10 13:44:30 +00:00
|
|
|
let paragraph = Paragraph::new(text.clone())
|
2020-07-11 17:08:28 +00:00
|
|
|
.style(Style::default().bg(Color::White).fg(Color::Black))
|
2020-05-10 13:44:30 +00:00
|
|
|
.block(create_block("Left, wrap"))
|
2018-12-07 19:30:19 +00:00
|
|
|
.alignment(Alignment::Left)
|
2020-07-06 22:10:24 +00:00
|
|
|
.wrap(Wrap { trim: true });
|
2019-12-15 20:38:18 +00:00
|
|
|
f.render_widget(paragraph, chunks[1]);
|
2020-05-10 13:44:30 +00:00
|
|
|
let paragraph = Paragraph::new(text.clone())
|
2020-07-11 17:08:28 +00:00
|
|
|
.style(Style::default().bg(Color::White).fg(Color::Black))
|
2020-05-10 13:44:30 +00:00
|
|
|
.block(create_block("Center, wrap"))
|
2018-12-07 19:30:19 +00:00
|
|
|
.alignment(Alignment::Center)
|
2020-07-06 22:10:24 +00:00
|
|
|
.wrap(Wrap { trim: 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]);
|
2020-05-10 13:44:30 +00:00
|
|
|
let paragraph = Paragraph::new(text)
|
2020-07-11 17:08:28 +00:00
|
|
|
.style(Style::default().bg(Color::White).fg(Color::Black))
|
2020-05-10 13:44:30 +00:00
|
|
|
.block(create_block("Right, wrap"))
|
2018-12-07 19:30:19 +00:00
|
|
|
.alignment(Alignment::Right)
|
2020-07-06 22:10:24 +00:00
|
|
|
.wrap(Wrap { trim: true });
|
2019-12-15 20:38:18 +00:00
|
|
|
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
|
|
|
}
|