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
|
|
|
|
2019-01-06 11:57:06 +00:00
|
|
|
use crate::util::event::{Event, Events};
|
2020-06-15 20:57:23 +00:00
|
|
|
use std::{error::Error, io};
|
|
|
|
use termion::{event::Key, input::MouseTerminal, raw::IntoRawMode, screen::AlternateScreen};
|
2020-03-13 01:02:51 +00:00
|
|
|
use tui::{
|
|
|
|
backend::TermionBackend,
|
|
|
|
layout::{Constraint, Direction, Layout},
|
2020-07-11 17:08:28 +00:00
|
|
|
style::{Color, Modifier, Style},
|
|
|
|
text::{Span, Spans, Text},
|
2020-05-10 13:44:30 +00:00
|
|
|
widgets::{Block, Borders, List, ListItem, Paragraph},
|
2020-03-13 01:02:51 +00:00
|
|
|
Terminal,
|
|
|
|
};
|
|
|
|
use unicode_width::UnicodeWidthStr;
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2020-01-19 17:41:00 +00:00
|
|
|
enum InputMode {
|
|
|
|
Normal,
|
|
|
|
Editing,
|
|
|
|
}
|
|
|
|
|
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,
|
2020-01-19 17:41:00 +00:00
|
|
|
/// Current input mode
|
|
|
|
input_mode: InputMode,
|
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(),
|
2020-01-19 17:41:00 +00:00
|
|
|
input_mode: InputMode::Normal,
|
2018-01-27 09:27:26 +00:00
|
|
|
messages: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 01:02:51 +00:00
|
|
|
fn main() -> Result<(), Box<dyn 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
|
2021-06-16 14:44:54 +00:00
|
|
|
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
|
2020-06-15 20:57:23 +00:00
|
|
|
terminal.draw(|f| {
|
2018-09-23 18:59:51 +00:00
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(2)
|
2020-01-19 17:41:00 +00:00
|
|
|
.constraints(
|
|
|
|
[
|
|
|
|
Constraint::Length(1),
|
|
|
|
Constraint::Length(3),
|
|
|
|
Constraint::Min(1),
|
|
|
|
]
|
|
|
|
.as_ref(),
|
|
|
|
)
|
2018-12-07 14:43:05 +00:00
|
|
|
.split(f.size());
|
2019-12-15 20:38:18 +00:00
|
|
|
|
2020-07-11 17:08:28 +00:00
|
|
|
let (msg, style) = match app.input_mode {
|
|
|
|
InputMode::Normal => (
|
|
|
|
vec![
|
|
|
|
Span::raw("Press "),
|
|
|
|
Span::styled("q", Style::default().add_modifier(Modifier::BOLD)),
|
|
|
|
Span::raw(" to exit, "),
|
|
|
|
Span::styled("e", Style::default().add_modifier(Modifier::BOLD)),
|
|
|
|
Span::raw(" to start editing."),
|
|
|
|
],
|
|
|
|
Style::default().add_modifier(Modifier::RAPID_BLINK),
|
|
|
|
),
|
|
|
|
InputMode::Editing => (
|
|
|
|
vec![
|
|
|
|
Span::raw("Press "),
|
|
|
|
Span::styled("Esc", Style::default().add_modifier(Modifier::BOLD)),
|
|
|
|
Span::raw(" to stop editing, "),
|
|
|
|
Span::styled("Enter", Style::default().add_modifier(Modifier::BOLD)),
|
|
|
|
Span::raw(" to record the message"),
|
|
|
|
],
|
|
|
|
Style::default(),
|
|
|
|
),
|
2020-01-19 17:41:00 +00:00
|
|
|
};
|
2020-07-11 17:08:28 +00:00
|
|
|
let mut text = Text::from(Spans::from(msg));
|
|
|
|
text.patch_style(style);
|
2020-05-10 13:44:30 +00:00
|
|
|
let help_message = Paragraph::new(text);
|
2019-12-15 20:38:18 +00:00
|
|
|
f.render_widget(help_message, chunks[0]);
|
|
|
|
|
2020-07-11 17:08:28 +00:00
|
|
|
let input = Paragraph::new(app.input.as_ref())
|
|
|
|
.style(match app.input_mode {
|
|
|
|
InputMode::Normal => Style::default(),
|
|
|
|
InputMode::Editing => Style::default().fg(Color::Yellow),
|
|
|
|
})
|
2019-12-15 20:38:18 +00:00
|
|
|
.block(Block::default().borders(Borders::ALL).title("Input"));
|
|
|
|
f.render_widget(input, chunks[1]);
|
2020-06-15 20:57:23 +00:00
|
|
|
match app.input_mode {
|
|
|
|
InputMode::Normal =>
|
|
|
|
// Hide the cursor. `Frame` does this by default, so we don't need to do anything here
|
|
|
|
{}
|
|
|
|
|
|
|
|
InputMode::Editing => {
|
|
|
|
// Make the cursor visible and ask tui-rs to put it at the specified coordinates after rendering
|
|
|
|
f.set_cursor(
|
|
|
|
// Put cursor past the end of the input text
|
|
|
|
chunks[1].x + app.input.width() as u16 + 1,
|
|
|
|
// Move one line down, from the border to the input line
|
|
|
|
chunks[1].y + 1,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-10 13:44:30 +00:00
|
|
|
let messages: Vec<ListItem> = app
|
2018-09-23 18:59:51 +00:00
|
|
|
.messages
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
2020-05-10 13:44:30 +00:00
|
|
|
.map(|(i, m)| {
|
|
|
|
let content = vec![Spans::from(Span::raw(format!("{}: {}", i, m)))];
|
|
|
|
ListItem::new(content)
|
|
|
|
})
|
|
|
|
.collect();
|
2019-12-15 20:38:18 +00:00
|
|
|
let messages =
|
|
|
|
List::new(messages).block(Block::default().borders(Borders::ALL).title("Messages"));
|
|
|
|
f.render_widget(messages, chunks[2]);
|
2018-09-23 18:59:51 +00:00
|
|
|
})?;
|
|
|
|
|
|
|
|
// Handle input
|
2020-05-16 22:51:57 +00:00
|
|
|
if let Event::Input(input) = events.next()? {
|
|
|
|
match app.input_mode {
|
2020-01-19 17:41:00 +00:00
|
|
|
InputMode::Normal => match input {
|
|
|
|
Key::Char('e') => {
|
|
|
|
app.input_mode = InputMode::Editing;
|
|
|
|
}
|
|
|
|
Key::Char('q') => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
|
|
|
InputMode::Editing => match input {
|
|
|
|
Key::Char('\n') => {
|
|
|
|
app.messages.push(app.input.drain(..).collect());
|
|
|
|
}
|
|
|
|
Key::Char(c) => {
|
|
|
|
app.input.push(c);
|
|
|
|
}
|
|
|
|
Key::Backspace => {
|
|
|
|
app.input.pop();
|
|
|
|
}
|
|
|
|
Key::Esc => {
|
|
|
|
app.input_mode = InputMode::Normal;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
2020-05-16 22:51:57 +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
|
|
|
}
|