Module rat_widget::masked_input

source ·
Expand description

Text input with an input mask.

  • Can do the usual insert/delete/move operations.

  • Text selection with keyboard + mouse

  • Scrolls with the cursor.

  • Modes for focus and valid.

  • Info-overlay for sub-fields without value.

  • Localization with format_num_pattern::NumberSymbols

  • Accepts an input mask:

    • 0: can enter digit, display as 0

    • 9: can enter digit, display as space

    • #: digit, plus or minus sign, display as space

    • -: sign

    • +: sign, positive is ‘+’, negative is ‘-’, not localized.

    • . and ,: decimal and grouping separators

    • H: must enter a hex digit, display as 0

    • h: can enter a hex digit, display as space

    • O: must enter an octal digit, display as 0

    • o: can enter an octal digit, display as space

    • D: must enter a decimal digit, display as 0

    • d: can enter a decimal digit, display as space

    • l: can enter letter, display as space

    • a: can enter letter or digit, display as space

    • c: can enter character or space, display as space

    • _: anything, display as space

    • <space>, :, ;, /: separator characters move the cursor when entered.

    • \: escapes the following character and uses it as a separator.

    • all other ascii characters a reserved.

    • other unicode characters can be used as separators without escaping.

  • Accepts a display overlay used instead of the default chars of the input mask.

use ratatui::widgets::StatefulWidget;
use rat_input::masked_input::{MaskedInput, MaskedInputState};

let date_focused = false;
let creditcard_focused = true;
let area = Rect::default();
let buf = Buffer::default();

let mut date_state = MaskedInputState::new();
date_state.set_mask("99/99/9999")?;
date_state.set_display_mask("mm/dd/yyyy");

let w_date = MaskedInput::default();
w_date.render(area, &mut buf, &mut date_state);
if date_focused {
    frame.set_cursor(date_state.cursor.x, date_state.cursor.y);
}

let mut creditcard_state = MaskedInputState::new();
creditcard_state.set_mask("dddd dddd dddd dddd")?;

let w_creditcard = MaskedInput::default();
w_creditcard.render(area, &mut buf, &mut creditcard_state);
if creditcard_focused {
    frame.set_cursor(creditcard_state.cursor.x, creditcard_state.cursor.y);
}

For event-handling call one of the HandleEvent implementations.

Structs§