|
|
|
@ -29,10 +29,17 @@ use unicode_width::UnicodeWidthStr;
|
|
|
|
|
|
|
|
|
|
use crate::util::event::{Event, Events};
|
|
|
|
|
|
|
|
|
|
enum InputMode {
|
|
|
|
|
Normal,
|
|
|
|
|
Editing,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// App holds the state of the application
|
|
|
|
|
struct App {
|
|
|
|
|
/// Current value of the input box
|
|
|
|
|
input: String,
|
|
|
|
|
/// Current input mode
|
|
|
|
|
input_mode: InputMode,
|
|
|
|
|
/// History of recorded messages
|
|
|
|
|
messages: Vec<String>,
|
|
|
|
|
}
|
|
|
|
@ -41,6 +48,7 @@ impl Default for App {
|
|
|
|
|
fn default() -> App {
|
|
|
|
|
App {
|
|
|
|
|
input: String::new(),
|
|
|
|
|
input_mode: InputMode::Normal,
|
|
|
|
|
messages: Vec::new(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -55,7 +63,7 @@ fn main() -> Result<(), failure::Error> {
|
|
|
|
|
let mut terminal = Terminal::new(backend)?;
|
|
|
|
|
|
|
|
|
|
// Setup event handlers
|
|
|
|
|
let events = Events::new();
|
|
|
|
|
let mut events = Events::new();
|
|
|
|
|
|
|
|
|
|
// Create default app state
|
|
|
|
|
let mut app = App::default();
|
|
|
|
@ -66,12 +74,24 @@ fn main() -> Result<(), failure::Error> {
|
|
|
|
|
let chunks = Layout::default()
|
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
|
.margin(2)
|
|
|
|
|
.constraints([Constraint::Length(3), Constraint::Min(1)].as_ref())
|
|
|
|
|
.constraints(
|
|
|
|
|
[
|
|
|
|
|
Constraint::Length(1),
|
|
|
|
|
Constraint::Length(3),
|
|
|
|
|
Constraint::Min(1),
|
|
|
|
|
]
|
|
|
|
|
.as_ref(),
|
|
|
|
|
)
|
|
|
|
|
.split(f.size());
|
|
|
|
|
let help_message = match app.input_mode {
|
|
|
|
|
InputMode::Normal => "Press q to exit, e to start editing.",
|
|
|
|
|
InputMode::Editing => "Press Esc to stop editing, Enter to record the message",
|
|
|
|
|
};
|
|
|
|
|
Paragraph::new([Text::raw(help_message)].iter()).render(&mut f, chunks[0]);
|
|
|
|
|
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]);
|
|
|
|
|
.render(&mut f, chunks[1]);
|
|
|
|
|
let messages = app
|
|
|
|
|
.messages
|
|
|
|
|
.iter()
|
|
|
|
@ -79,34 +99,47 @@ fn main() -> Result<(), failure::Error> {
|
|
|
|
|
.map(|(i, m)| Text::raw(format!("{}: {}", i, m)));
|
|
|
|
|
List::new(messages)
|
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("Messages"))
|
|
|
|
|
.render(&mut f, chunks[1]);
|
|
|
|
|
.render(&mut f, chunks[2]);
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
// Put the cursor back inside the input box
|
|
|
|
|
write!(
|
|
|
|
|
terminal.backend_mut(),
|
|
|
|
|
"{}",
|
|
|
|
|
Goto(4 + app.input.width() as u16, 4)
|
|
|
|
|
Goto(4 + app.input.width() as u16, 5)
|
|
|
|
|
)?;
|
|
|
|
|
// stdout is buffered, flush it to see the effect immediately when hitting backspace
|
|
|
|
|
io::stdout().flush().ok();
|
|
|
|
|
|
|
|
|
|
// Handle input
|
|
|
|
|
match events.next()? {
|
|
|
|
|
Event::Input(input) => match input {
|
|
|
|
|
Key::Char('q') => {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Key::Char('\n') => {
|
|
|
|
|
app.messages.push(app.input.drain(..).collect());
|
|
|
|
|
}
|
|
|
|
|
Key::Char(c) => {
|
|
|
|
|
app.input.push(c);
|
|
|
|
|
}
|
|
|
|
|
Key::Backspace => {
|
|
|
|
|
app.input.pop();
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
Event::Input(input) => match app.input_mode {
|
|
|
|
|
InputMode::Normal => match input {
|
|
|
|
|
Key::Char('e') => {
|
|
|
|
|
app.input_mode = InputMode::Editing;
|
|
|
|
|
events.disable_exit_key();
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
events.enable_exit_key();
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
_ => {}
|
|
|
|
|
}
|
|
|
|
|