Skip to main content

TextInput

Struct TextInput 

Source
pub struct TextInput { /* private fields */ }
Expand description

A single-line text input widget.

Implementations§

Source§

impl TextInput

Source

pub fn new() -> Self

Create a new empty text input.

Source

pub fn with_value(self, value: impl Into<String>) -> Self

Set the text value (builder).

Source

pub fn with_placeholder(self, placeholder: impl Into<String>) -> Self

Set the placeholder text (builder).

Source

pub fn with_mask(self, mask: char) -> Self

Set password mode with mask character (builder).

Source

pub fn with_max_length(self, max: usize) -> Self

Set maximum length in graphemes (builder).

Source

pub fn with_style(self, style: Style) -> Self

Set base style (builder).

Source

pub fn with_cursor_style(self, style: Style) -> Self

Set cursor style (builder).

Source

pub fn with_placeholder_style(self, style: Style) -> Self

Set placeholder style (builder).

Source

pub fn with_selection_style(self, style: Style) -> Self

Set selection style (builder).

Source

pub fn with_focused(self, focused: bool) -> Self

Set whether the input is focused (builder).

Source

pub fn value(&self) -> &str

Get the current value.

Source

pub fn set_value(&mut self, value: impl Into<String>)

Set the value, clamping cursor to valid range.

Source

pub fn clear(&mut self)

Clear all text.

Source

pub fn cursor(&self) -> usize

Get the cursor position (grapheme index).

Source

pub fn focused(&self) -> bool

Check if the input is focused.

Source

pub fn set_focused(&mut self, focused: bool)

Set focus state.

Source

pub fn cursor_position(&self, area: Rect) -> (u16, u16)

Get the cursor screen position relative to a render area.

Returns (x, y) where x is the column and y is the row. Useful for Frame::set_cursor().

Source

pub fn selected_text(&self) -> Option<&str>

Get selected text, if any.

Source

pub fn handle_event(&mut self, event: &Event) -> bool

Handle a terminal event.

Returns true if the state changed.

Source

pub fn insert_text(&mut self, text: &str)

Insert text at the current cursor position.

This method:

  • Replaces newlines and tabs with spaces.
  • Filters out other control characters.
  • Respects max_length (truncating if necessary).
  • Efficiently inserts the result in one operation.
Source

pub fn select_all(&mut self)

Select all text.

Source§

impl TextInput

Source

pub fn create_text_edit_command( &self, operation: TextEditOperation, ) -> Option<WidgetTextEditCmd>

Create an undo command for the given text edit operation.

This creates a command that can be added to a HistoryManager for undo/redo support. The command includes callbacks that will be called when the operation is undone or redone.

§Example
let mut input = TextInput::new();
let old_value = input.value().to_string();

// Perform the edit
input.set_value("new text");

// Create undo command
if let Some(cmd) = input.create_text_edit_command(TextEditOperation::SetValue {
    old_value,
    new_value: "new text".to_string(),
}) {
    history.push(cmd);
}
Source

pub fn undo_id(&self) -> UndoWidgetId

Get the undo widget ID.

This can be used to associate undo commands with this widget instance.

Trait Implementations§

Source§

impl Clone for TextInput

Source§

fn clone(&self) -> TextInput

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TextInput

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for TextInput

Source§

fn default() -> TextInput

Returns the “default value” for a type. Read more
Source§

impl TextInputUndoExt for TextInput

Source§

fn text_value(&self) -> &str

Get the current text value.
Source§

fn set_text_value(&mut self, value: &str)

Set the text value directly (for undo/redo).
Source§

fn cursor_position(&self) -> usize

Get the current cursor position.
Source§

fn set_cursor_position(&mut self, pos: usize)

Set the cursor position directly.
Source§

fn insert_text_at(&mut self, position: usize, text: &str)

Insert text at a position.
Source§

fn delete_text_range(&mut self, start: usize, end: usize)

Delete text at a range.
Source§

impl UndoSupport for TextInput

Source§

fn undo_widget_id(&self) -> UndoWidgetId

Get the widget’s unique ID for undo tracking.
Source§

fn create_snapshot(&self) -> Box<dyn Any + Send>

Create a snapshot of the current state for undo purposes. Read more
Source§

fn restore_snapshot(&mut self, snapshot: &dyn Any) -> bool

Restore state from a snapshot. Read more
Source§

impl Widget for TextInput

Source§

fn render(&self, area: Rect, frame: &mut Frame<'_>)

Render the widget into the frame at the given area. Read more
Source§

fn is_essential(&self) -> bool

Whether this widget is essential and should always render. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more