pub struct Model {Show 14 fields
pub err: Option<String>,
pub prompt: String,
pub prompt_style: Style,
pub text_style: Style,
pub placeholder: String,
pub placeholder_style: Style,
pub cursor: Model,
pub cursor_mode: Mode,
pub width: i32,
pub key_map: KeyMap,
pub char_limit: i32,
pub echo_mode: EchoMode,
pub echo_character: char,
pub completion_style: Style,
/* private fields */
}Expand description
The main text input component model for Bubble Tea applications.
This struct represents a single-line text input field with support for:
- Cursor movement and text editing
- Input validation
- Auto-completion suggestions
- Different echo modes (normal, password, hidden)
- Horizontal scrolling for long text
- Customizable styling and key bindings
The model follows the Elm Architecture pattern used by Bubble Tea, with
separate Init(), Update(), and View() methods for state management.
§Examples
use bubbletea_widgets::textinput::{new, EchoMode};
use bubbletea_rs::Model;
// Create and configure a text input
let mut input = new();
input.focus();
input.set_placeholder("Enter your name...");
input.set_width(30);
input.set_char_limit(50);
// For password input
input.set_echo_mode(EchoMode::EchoPassword);
// With validation
input.set_validate(Box::new(|s: &str| {
if s.len() >= 3 {
Ok(())
} else {
Err("Must be at least 3 characters".to_string())
}
}));§Note
This struct matches the Go Model struct exactly for 1-1 compatibility.
Fields§
§err: Option<String>Err is an error that was not caught by a validator.
prompt: StringPrompt is the prompt to display before the text input.
prompt_style: StyleStyle for the prompt prefix.
text_style: StyleTextStyle is the style of the text as it’s being typed.
placeholder: StringPlaceholder is the placeholder text to display when the input is empty.
placeholder_style: StyleStyle for the placeholder text.
cursor: ModelCursor is the cursor model.
cursor_mode: ModeCursor rendering mode (blink/static/hidden).
width: i32Width is the maximum number of characters that can be displayed at once.
key_map: KeyMapKeyMap encodes the keybindings.
char_limit: i32CharLimit is the maximum number of characters this input will accept. 0 means no limit.
echo_mode: EchoModeEchoMode is the echo mode of the input.
echo_character: charEchoCharacter is the character to use for password fields.
completion_style: StyleCompletionStyle is the style of the completion suggestion.
Implementations§
Source§impl Model
impl Model
Sourcepub fn set_value(&mut self, s: &str)
pub fn set_value(&mut self, s: &str)
Sets the value of the text input.
This method replaces the entire content of the text input with the provided string. If a validation function is set, it will be applied to the new value.
§Arguments
s- The new string value to set
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.set_value("Hello, world!");
assert_eq!(input.value(), "Hello, world!");§Note
This method matches Go’s SetValue method exactly for compatibility.
Sourcepub fn position(&self) -> usize
pub fn position(&self) -> usize
Returns the current cursor position as a character index.
§Returns
The cursor position as a usize, where 0 is the beginning of the text
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.set_value("hello");
input.set_cursor(2);
assert_eq!(input.position(), 2);§Note
This method matches Go’s Position method exactly for compatibility.
Sourcepub fn set_cursor(&mut self, pos: usize)
pub fn set_cursor(&mut self, pos: usize)
Moves the cursor to the specified position.
If the position is beyond the end of the text, the cursor will be placed at the end. This method also handles overflow for horizontal scrolling when the text is wider than the display width.
§Arguments
pos- The new cursor position as a character index
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.set_value("hello world");
input.set_cursor(6); // Position after "hello "
assert_eq!(input.position(), 6);§Note
This method matches Go’s SetCursor method exactly for compatibility.
Sourcepub fn cursor_start(&mut self)
pub fn cursor_start(&mut self)
Moves the cursor to the beginning of the input field.
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.set_value("hello");
input.cursor_end();
input.cursor_start();
assert_eq!(input.position(), 0);§Note
This method matches Go’s CursorStart method exactly for compatibility.
Sourcepub fn cursor_end(&mut self)
pub fn cursor_end(&mut self)
Sourcepub fn focused(&self) -> bool
pub fn focused(&self) -> bool
Returns whether the text input currently has focus.
§Returns
true if the input is focused and will respond to key events, false otherwise
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
assert!(!input.focused());
input.focus();
assert!(input.focused());§Note
This method matches Go’s Focused method exactly for compatibility.
Sourcepub fn focus(&mut self) -> Cmd
pub fn focus(&mut self) -> Cmd
Sets focus on the text input, enabling it to receive key events.
When focused, the text input will display a cursor and respond to keyboard input. This method also focuses the cursor component which may return a command for cursor blinking.
§Returns
A Cmd that may be used to start cursor blinking animation
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
let cmd = input.focus();
assert!(input.focused());§Note
This method matches Go’s Focus method exactly for compatibility.
Sourcepub fn blur(&mut self)
pub fn blur(&mut self)
Removes focus from the text input, disabling key event handling.
When blurred, the text input will not respond to keyboard input and the cursor will not be visible.
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.focus();
assert!(input.focused());
input.blur();
assert!(!input.focused());§Note
This method matches Go’s Blur method exactly for compatibility.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Clears all text and resets the cursor to the beginning.
This method removes all text content and moves the cursor to position 0. It does not change other settings like placeholder text, validation, or styling.
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.set_value("some text");
input.reset();
assert_eq!(input.value(), "");
assert_eq!(input.position(), 0);§Note
This method matches Go’s Reset method exactly for compatibility.
Sourcepub fn set_suggestions(&mut self, suggestions: Vec<String>)
pub fn set_suggestions(&mut self, suggestions: Vec<String>)
Sets the list of available suggestions for auto-completion.
Suggestions will be filtered based on the current input and can be navigated using the configured key bindings (typically up/down arrows and tab to accept).
§Arguments
suggestions- A vector of strings that can be suggested to the user
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.set_suggestions(vec![
"apple".to_string(),
"application".to_string(),
"apply".to_string(),
]);
input.set_value("app");
// Now suggestions starting with "app" will be available§Note
This method matches Go’s SetSuggestions method exactly for compatibility.
Sourcepub fn set_placeholder(&mut self, placeholder: &str)
pub fn set_placeholder(&mut self, placeholder: &str)
Sourcepub fn set_width(&mut self, width: i32)
pub fn set_width(&mut self, width: i32)
Sets the display width of the input field in characters.
This controls how many characters are visible at once. If the text is longer than the width, it will scroll horizontally as the user types or moves the cursor.
§Arguments
width- The width in characters. Use 0 for no width limit.
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.set_width(20); // Show up to 20 characters at onceSourcepub fn set_echo_mode(&mut self, mode: EchoMode)
pub fn set_echo_mode(&mut self, mode: EchoMode)
Sets the echo mode for displaying typed characters.
§Arguments
mode- The echo mode to use:EchoNormal: Display characters as typed (default)EchoPassword: Display asterisks instead of actual charactersEchoNone: Don’t display any characters
§Examples
use bubbletea_widgets::textinput::{new, EchoMode};
let mut input = new();
input.set_echo_mode(EchoMode::EchoPassword);
input.set_value("secret");
// Text will appear as asterisks: ******Sourcepub fn set_char_limit(&mut self, limit: i32)
pub fn set_char_limit(&mut self, limit: i32)
Sets the maximum number of characters allowed in the input.
§Arguments
limit- Maximum number of characters. Use 0 for no limit.
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.set_char_limit(10); // Allow up to 10 characters
input.set_value("This is a very long string");
assert_eq!(input.value().len(), 10); // Truncated to 10 charactersSourcepub fn set_validate(&mut self, validate: ValidateFunc)
pub fn set_validate(&mut self, validate: ValidateFunc)
Sets a validation function that will be called whenever the input changes.
The validation function receives the current input value and should return
Ok(()) if the input is valid, or Err(message) if invalid.
§Arguments
validate- A function that takes a&strand returnsResult<(), String>
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.set_validate(Box::new(|s: &str| {
if s.contains('@') {
Ok(())
} else {
Err("Must contain @ symbol".to_string())
}
}));Sourcepub fn update(&mut self, msg: Msg) -> Option<Cmd>
pub fn update(&mut self, msg: Msg) -> Option<Cmd>
Processes a message and updates the text input state.
This method handles keyboard input, cursor movement, text editing operations, and clipboard operations. It should be called from your application’s update loop.
§Arguments
msg- The message to process (typically a key press or paste event)
§Returns
An optional Cmd that may need to be executed (e.g., for cursor blinking)
§Examples
use bubbletea_widgets::textinput::new;
use bubbletea_rs::{KeyMsg, Model};
use crossterm::event::{KeyCode, KeyModifiers};
let mut input = new();
input.focus();
// Simulate typing 'h'
let key_msg = KeyMsg {
key: KeyCode::Char('h'),
modifiers: KeyModifiers::NONE,
};
input.update(Box::new(key_msg));
assert_eq!(input.value(), "h");Source§impl Model
impl Model
Sourcepub fn available_suggestions(&self) -> Vec<String>
pub fn available_suggestions(&self) -> Vec<String>
Returns the complete list of available suggestions.
This returns all suggestions that were set via set_suggestions(),
regardless of the current input or filtering.
§Returns
A vector of all available suggestion strings
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.set_suggestions(vec!["apple".to_string(), "banana".to_string()]);
let suggestions = input.available_suggestions();
assert_eq!(suggestions.len(), 2);§Note
This method matches Go’s AvailableSuggestions method exactly.
Sourcepub fn matched_suggestions(&self) -> Vec<String>
pub fn matched_suggestions(&self) -> Vec<String>
Returns the list of suggestions that match the current input.
This returns only the suggestions that start with the current input value (case-insensitive matching). The list is updated automatically as the user types.
§Returns
A vector of suggestion strings that match the current input
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.set_suggestions(vec![
"apple".to_string(),
"application".to_string(),
"banana".to_string(),
]);
input.set_value("app");
let matched = input.matched_suggestions();
assert_eq!(matched.len(), 2); // "apple" and "application"§Note
This method matches Go’s MatchedSuggestions method exactly.
Sourcepub fn current_suggestion_index(&self) -> usize
pub fn current_suggestion_index(&self) -> usize
Returns the index of the currently selected suggestion.
This index refers to the position in the matched suggestions list (not the complete available suggestions list). Use up/down arrow keys or configured navigation bindings to change the selection.
§Returns
The zero-based index of the currently selected suggestion
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.set_suggestions(vec!["apple".to_string(), "application".to_string()]);
input.set_value("app");
assert_eq!(input.current_suggestion_index(), 0); // First suggestion selected§Note
This method matches Go’s CurrentSuggestionIndex method exactly.
Sourcepub fn current_suggestion(&self) -> String
pub fn current_suggestion(&self) -> String
Returns the text of the currently selected suggestion.
If no suggestions are available or the index is out of bounds, this returns an empty string.
§Returns
The currently selected suggestion as a String, or empty string if none
§Examples
use bubbletea_widgets::textinput::new;
let mut input = new();
input.set_suggestions(vec!["apple".to_string(), "application".to_string()]);
input.set_value("app");
assert_eq!(input.current_suggestion(), "apple");§Note
This method matches Go’s CurrentSuggestion method exactly.
Trait Implementations§
Source§impl Component for Model
impl Component for Model
Source§fn focus(&mut self) -> Option<Cmd>
fn focus(&mut self) -> Option<Cmd>
Sets the component to focused state.
This implementation wraps the existing focus() method to match the Component trait’s
expected signature of returning Option<Cmd> instead of Cmd.
§Returns
Some(Cmd) containing a cursor blink command, or None if no command is needed.
Auto Trait Implementations§
impl Freeze for Model
impl !RefUnwindSafe for Model
impl Send for Model
impl !Sync for Model
impl Unpin for Model
impl !UnwindSafe for Model
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
Source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
Source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
Source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
Source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
Source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
Source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
Source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle.Source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
Source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other into Self, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
Source§fn into_angle(self) -> U
fn into_angle(self) -> U
T.Source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
Source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters when converting.Source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self into C, using the provided parameters.Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
Source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self into T, while performing the appropriate scaling,
rounding and clamping.Source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
Source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors fails to cast.Source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more