2023-06-12 05:07:15 +00:00
|
|
|
use std::{error::Error, io};
|
|
|
|
|
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
|
2021-11-01 21:27:46 +00:00
|
|
|
use crossterm::{
|
2023-02-15 21:52:08 +00:00
|
|
|
event::{self, DisableMouseCapture, EnableMouseCapture, Event, KeyCode, KeyEventKind},
|
2021-11-01 21:27:46 +00:00
|
|
|
execute,
|
|
|
|
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
|
|
|
};
|
2023-07-10 22:59:01 +00:00
|
|
|
use ratatui::prelude::*;
|
2020-03-13 01:02:51 +00:00
|
|
|
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>> {
|
2021-11-01 21:27:46 +00:00
|
|
|
// setup terminal
|
|
|
|
enable_raw_mode()?;
|
|
|
|
let mut stdout = io::stdout();
|
|
|
|
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
|
|
|
let backend = CrosstermBackend::new(stdout);
|
2018-09-23 18:59:51 +00:00
|
|
|
let mut terminal = Terminal::new(backend)?;
|
2018-01-27 09:27:26 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// create app and run it
|
|
|
|
let app = App::default();
|
|
|
|
let res = run_app(&mut terminal, app);
|
2019-12-15 20:38:18 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
// restore terminal
|
|
|
|
disable_raw_mode()?;
|
|
|
|
execute!(
|
|
|
|
terminal.backend_mut(),
|
|
|
|
LeaveAlternateScreen,
|
|
|
|
DisableMouseCapture
|
|
|
|
)?;
|
|
|
|
terminal.show_cursor()?;
|
2019-12-15 20:38:18 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
if let Err(err) = res {
|
2023-05-22 03:46:02 +00:00
|
|
|
println!("{err:?}");
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
2020-06-15 20:57:23 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-06-15 20:57:23 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
fn run_app<B: Backend>(terminal: &mut Terminal<B>, mut app: App) -> io::Result<()> {
|
|
|
|
loop {
|
2021-12-23 17:46:25 +00:00
|
|
|
terminal.draw(|f| ui(f, &app))?;
|
2018-09-23 18:59:51 +00:00
|
|
|
|
2021-11-01 21:27:46 +00:00
|
|
|
if let Event::Key(key) = event::read()? {
|
2020-05-16 22:51:57 +00:00
|
|
|
match app.input_mode {
|
2021-11-01 21:27:46 +00:00
|
|
|
InputMode::Normal => match key.code {
|
|
|
|
KeyCode::Char('e') => {
|
2020-01-19 17:41:00 +00:00
|
|
|
app.input_mode = InputMode::Editing;
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
KeyCode::Char('q') => {
|
|
|
|
return Ok(());
|
2020-01-19 17:41:00 +00:00
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
2023-02-15 21:52:08 +00:00
|
|
|
InputMode::Editing if key.kind == KeyEventKind::Press => match key.code {
|
2021-11-01 21:27:46 +00:00
|
|
|
KeyCode::Enter => {
|
2020-01-19 17:41:00 +00:00
|
|
|
app.messages.push(app.input.drain(..).collect());
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
KeyCode::Char(c) => {
|
2020-01-19 17:41:00 +00:00
|
|
|
app.input.push(c);
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
KeyCode::Backspace => {
|
2020-01-19 17:41:00 +00:00
|
|
|
app.input.pop();
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
KeyCode::Esc => {
|
2020-01-19 17:41:00 +00:00
|
|
|
app.input_mode = InputMode::Normal;
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
},
|
2023-02-15 21:52:08 +00:00
|
|
|
_ => {}
|
2020-05-16 22:51:57 +00:00
|
|
|
}
|
2018-01-27 09:27:26 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-01 21:27:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ui<B: Backend>(f: &mut Frame<B>, app: &App) {
|
|
|
|
let chunks = Layout::default()
|
|
|
|
.direction(Direction::Vertical)
|
|
|
|
.margin(2)
|
|
|
|
.constraints(
|
|
|
|
[
|
|
|
|
Constraint::Length(1),
|
|
|
|
Constraint::Length(3),
|
|
|
|
Constraint::Min(1),
|
|
|
|
]
|
|
|
|
.as_ref(),
|
|
|
|
)
|
|
|
|
.split(f.size());
|
|
|
|
|
|
|
|
let (msg, style) = match app.input_mode {
|
|
|
|
InputMode::Normal => (
|
|
|
|
vec![
|
2023-07-01 09:14:16 +00:00
|
|
|
"Press ".into(),
|
|
|
|
"q".bold(),
|
|
|
|
" to exist, ".into(),
|
|
|
|
"e".bold(),
|
|
|
|
" to start editing.".bold(),
|
2021-11-01 21:27:46 +00:00
|
|
|
],
|
|
|
|
Style::default().add_modifier(Modifier::RAPID_BLINK),
|
|
|
|
),
|
|
|
|
InputMode::Editing => (
|
|
|
|
vec![
|
2023-07-01 09:14:16 +00:00
|
|
|
"Press ".into(),
|
|
|
|
"Esc".bold(),
|
|
|
|
" to stop editing, ".into(),
|
|
|
|
"Enter".bold(),
|
|
|
|
" to record the message".into(),
|
2021-11-01 21:27:46 +00:00
|
|
|
],
|
|
|
|
Style::default(),
|
|
|
|
),
|
|
|
|
};
|
2023-05-18 18:21:43 +00:00
|
|
|
let mut text = Text::from(Line::from(msg));
|
2021-11-01 21:27:46 +00:00
|
|
|
text.patch_style(style);
|
|
|
|
let help_message = Paragraph::new(text);
|
|
|
|
f.render_widget(help_message, chunks[0]);
|
|
|
|
|
2023-05-09 17:59:24 +00:00
|
|
|
let input = Paragraph::new(app.input.as_str())
|
2021-11-01 21:27:46 +00:00
|
|
|
.style(match app.input_mode {
|
|
|
|
InputMode::Normal => Style::default(),
|
|
|
|
InputMode::Editing => Style::default().fg(Color::Yellow),
|
|
|
|
})
|
|
|
|
.block(Block::default().borders(Borders::ALL).title("Input"));
|
|
|
|
f.render_widget(input, chunks[1]);
|
|
|
|
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 => {
|
2023-06-04 10:34:05 +00:00
|
|
|
// Make the cursor visible and ask ratatui to put it at the specified coordinates after
|
|
|
|
// rendering
|
2021-11-01 21:27:46 +00:00
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let messages: Vec<ListItem> = app
|
|
|
|
.messages
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.map(|(i, m)| {
|
2023-05-22 03:46:02 +00:00
|
|
|
let content = Line::from(Span::raw(format!("{i}: {m}")));
|
2021-11-01 21:27:46 +00:00
|
|
|
ListItem::new(content)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
let messages =
|
|
|
|
List::new(messages).block(Block::default().borders(Borders::ALL).title("Messages"));
|
|
|
|
f.render_widget(messages, chunks[2]);
|
2018-01-27 09:27:26 +00:00
|
|
|
}
|