A widget for entering a single line of text in a dialog for Ratatui.
It also includes a function for creating a centered Rect to place the dialog in.
Based on https://github.com/ratatui/ratatui/blob/v0.26.1/examples/user_input.rs.
NOTE: The widget only works with the crossterm backend. Adding others is not a priority at this time.
Examples
For a full (but minimal) app demonstrating use, clone the repository and run cargo run --example basic.
You can also check out my app taskfinder for
more extensive use.
use tui_dialog::{Dialog, centered_rect};
pub struct App {
dialog: Dialog
text: String,
exit: bool,
}
let mut app = App {
dialog: Dialog::default(),
text: "Hello world!".to_string(),
exit: false,
};
while !app.exit {
terminal.draw(|frame| render(frame, app))?;
match event::read()? {
Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
if app.dialog.open {
app.dialog.key_action(&key_event.code);
if app.dialog.submitted {
app.text = app.dialog.submitted_input.clone();
}
} else {
match key_event.code {
KeyCode::Char('q') => app.exit = true,
KeyCode::Char('d') => app.dialog.open = true,
_ => (),
}
}
}
_ => (),
};
}
fn render(frame: &mut Frame, app: &mut App) {
let dialog_area = centered_rect(frame.area(), 45, 5, 0, 0);
frame.render_widget(app.dialog, dialog_area);
}