tui-rs/examples/user_input.rs

130 lines
3.5 KiB
Rust
Raw Normal View History

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
extern crate termion;
extern crate tui;
use std::io;
use std::sync::mpsc;
2018-05-06 10:59:24 +00:00
use std::thread;
2018-01-27 09:27:26 +00:00
use termion::event;
use termion::input::TermRead;
2018-08-12 22:27:56 +00:00
use tui::backend::AlternateScreenBackend;
use tui::layout::{Constraint, Direction, Layout, Rect};
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-01-27 09:27:26 +00:00
struct App {
size: Rect,
input: String,
messages: Vec<String>,
}
impl App {
fn new() -> App {
App {
size: Rect::default(),
input: String::new(),
messages: Vec::new(),
}
}
}
enum Event {
Input(event::Key),
}
fn main() {
// Terminal initialization
2018-08-12 22:27:56 +00:00
let backend = AlternateScreenBackend::new().unwrap();
2018-01-27 09:27:26 +00:00
let mut terminal = Terminal::new(backend).unwrap();
// Channels
let (tx, rx) = mpsc::channel();
let input_tx = tx.clone();
// Input
thread::spawn(move || {
let stdin = io::stdin();
for c in stdin.keys() {
let evt = c.unwrap();
input_tx.send(Event::Input(evt)).unwrap();
if evt == event::Key::Char('q') {
break;
}
}
});
// App
let mut app = App::new();
// First draw call
terminal.clear().unwrap();
terminal.hide_cursor().unwrap();
app.size = terminal.size().unwrap();
draw(&mut terminal, &app).unwrap();
2018-01-27 09:27:26 +00:00
loop {
let size = terminal.size().unwrap();
if app.size != size {
terminal.resize(size).unwrap();
app.size = size;
}
let evt = rx.recv().unwrap();
match evt {
Event::Input(input) => match input {
event::Key::Char('q') => {
break;
}
event::Key::Char('\n') => {
app.messages.push(app.input.drain(..).collect());
}
event::Key::Char(c) => {
app.input.push(c);
}
event::Key::Backspace => {
app.input.pop();
}
_ => {}
},
}
draw(&mut terminal, &app).unwrap();
2018-01-27 09:27:26 +00:00
}
terminal.show_cursor().unwrap();
terminal.clear().unwrap();
}
fn draw(t: &mut Terminal<AlternateScreenBackend>, app: &App) -> Result<(), io::Error> {
t.draw(|mut f| {
let chunks = Layout::default()
.direction(Direction::Vertical)
.margin(2)
.constraints([Constraint::Length(3), Constraint::Min(1)].as_ref())
2018-08-12 22:27:56 +00:00
.split(app.size);
Paragraph::new([Text::raw(&app.input)].iter())
.style(Style::default().fg(Color::Yellow))
.block(Block::default().borders(Borders::ALL).title("Input"))
2018-08-12 22:27:56 +00:00
.render(&mut f, chunks[0]);
List::new(
app.messages
.iter()
.enumerate()
2018-09-08 06:03:30 +00:00
.map(|(i, m)| Text::raw(format!("{}: {}", i, m))),
).block(Block::default().borders(Borders::ALL).title("Messages"))
2018-08-12 22:27:56 +00:00
.render(&mut f, chunks[1]);
})
2018-01-27 09:27:26 +00:00
}