2018-01-27 09:27:26 +00:00
|
|
|
/// A simple example demonstrating how to handle user input. This is
|
|
|
|
/// a bit out of the scope of the library as it does not provide any
|
|
|
|
/// input handling out of the box. However, it may helps some to get
|
|
|
|
/// started.
|
|
|
|
///
|
|
|
|
/// This is a very simple example:
|
|
|
|
/// * A input box always focused. Every character you type is registered
|
|
|
|
/// here
|
|
|
|
/// * Pressing Backspace erases a character
|
|
|
|
/// * Pressing Enter pushes the current input in the history of previous
|
|
|
|
/// messages
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
#[allow(dead_code)]
|
|
|
|
mod util;
|
2018-01-27 09:27:26 +00:00
|
|
|
|
2018-11-04 17:32:31 +00:00
|
|
|
use std::io::{self, Write};
|
2018-01-27 09:27:26 +00:00
|
|
|
|
2018-11-04 18:04:51 +00:00
|
|
|
use termion::cursor::Goto;
|
2018-09-23 18:59:51 +00:00
|
|
|
use termion::event::Key;
|
|
|
|
use termion::input::MouseTerminal;
|
|
|
|
use termion::raw::IntoRawMode;
|
|
|
|
use termion::screen::AlternateScreen;
|
|
|
|
use tui::backend::TermionBackend;
|
2018-11-26 02:06:34 +00:00
|
|
|
use tui::layout::{Constraint, Direction, Layout};
|
2018-01-27 09:27:26 +00:00
|
|
|
use tui::style::{Color, Style};
|
2018-09-08 06:03:30 +00:00
|
|
|
use tui::widgets::{Block, Borders, List, Paragraph, Text, Widget};
|
2018-06-09 09:02:37 +00:00
|
|
|
use tui::Terminal;
|
2018-11-04 18:04:51 +00:00
|
|
|
use unicode_width::UnicodeWidthStr;
|
2018-01-27 09:27:26 +00:00
|
|
|
|
2019-01-06 11:57:06 +00:00
|
|
|
use crate::util::event::{Event, Events};
|
2018-09-23 18:59:51 +00:00
|
|
|
|
|
|
|
/// App holds the state of the application
|
2018-01-27 09:27:26 +00:00
|
|
|
struct App {
|
2018-09-23 18:59:51 +00:00
|
|
|
/// Current value of the input box
|
2018-01-27 09:27:26 +00:00
|
|
|
input: String,
|
2018-09-23 18:59:51 +00:00
|
|
|
/// History of recorded messages
|
2018-01-27 09:27:26 +00:00
|
|
|
messages: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
impl Default for App {
|
|
|
|
fn default() -> App {
|
2018-01-27 09:27:26 +00:00
|
|
|
App {
|
|
|
|
input: String::new(),
|
|
|
|
messages: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
fn main() -> Result<(), failure::Error> {
|
2018-01-27 09:27:26 +00:00
|
|
|
// Terminal initialization
|
2018-09-23 18:59:51 +00:00
|
|
|
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)?;
|
2018-01-27 09:27:26 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
// Setup event handlers
|
|
|
|
let events = Events::new();
|
2018-01-27 09:27:26 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
// Create default app state
|
|
|
|
let mut app = App::default();
|
2018-01-27 09:27:26 +00:00
|
|
|
|
|
|
|
loop {
|
2018-09-23 18:59:51 +00:00
|
|
|
// Draw UI
|
|
|
|
terminal.draw(|mut f| {
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(2)
|
|
|
|
.constraints([Constraint::Length(3), Constraint::Min(1)].as_ref())
|
2018-12-07 14:43:05 +00:00
|
|
|
.split(f.size());
|
2018-09-23 18:59:51 +00:00
|
|
|
Paragraph::new([Text::raw(&app.input)].iter())
|
|
|
|
.style(Style::default().fg(Color::Yellow))
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("Input"))
|
|
|
|
.render(&mut f, chunks[0]);
|
|
|
|
let messages = app
|
|
|
|
.messages
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, m)| Text::raw(format!("{}: {}", i, m)));
|
|
|
|
List::new(messages)
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("Messages"))
|
|
|
|
.render(&mut f, chunks[1]);
|
|
|
|
})?;
|
|
|
|
|
2018-11-04 17:32:31 +00:00
|
|
|
// Put the cursor back inside the input box
|
2018-11-04 18:04:51 +00:00
|
|
|
write!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
"{}",
|
|
|
|
Goto(4 + app.input.width() as u16, 4)
|
|
|
|
)?;
|
2019-06-02 04:31:15 +00:00
|
|
|
// stdout is buffered, flush it to see the effect immediately when hitting backspace
|
|
|
|
io::stdout().flush().ok();
|
2018-11-04 17:32:31 +00:00
|
|
|
|
2018-09-23 18:59:51 +00:00
|
|
|
// Handle input
|
|
|
|
match events.next()? {
|
2018-01-27 09:27:26 +00:00
|
|
|
Event::Input(input) => match input {
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Char('q') => {
|
2018-01-27 09:27:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Char('\n') => {
|
2018-01-27 09:27:26 +00:00
|
|
|
app.messages.push(app.input.drain(..).collect());
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Char(c) => {
|
2018-01-27 09:27:26 +00:00
|
|
|
app.input.push(c);
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
Key::Backspace => {
|
2018-01-27 09:27:26 +00:00
|
|
|
app.input.pop();
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
2018-09-23 18:59:51 +00:00
|
|
|
_ => {}
|
2018-01-27 09:27:26 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-23 18:59:51 +00:00
|
|
|
Ok(())
|
2018-01-27 09:27:26 +00:00
|
|
|
}
|