Crate rat_text

Crate rat_text 

Source
Expand description

semver stable crates.io Documentation License License

This crate is a part of rat-salsa.

For examples see rat-text GitHub or an extended example mdedit.rs in rat-salsa GitHub.

§Text widgets for ratatui

Features for all widgets:

  • Undo/redo

  • Sync another widget

  • Support double-width characters

  • Range based text styling

  • Wrapped text

  • Clipboard trait to link to some clipboard implementation.

    There is no general solution for clipboards but this way you can integrate one of the many crates that try to do this.

  • Builtin scrolling. Uses rat-scrolled.

  • Lots of text manipulation functions.

    • Line/TextRange/byte indexes supported.
    • Grapheme iterator/cursor
    • byte-pos to TextPosition and vice versa.
    • Screen position to text position.

§TextInput

Single line text input widget.

// actual text is held in the state:
state.textinput.set_value("sample");

// render
TextInput::new()
    .style(Style::default().white().on_dark_gray())
    .select_style(Style::default().black().on_yellow())
    .render(txt_area, frame.buffer_mut(), &mut state.textinput);
    
// during event-handling
match text_input::handle_events(&mut state.textinput, true /*focused*/, event) {
    TextOutcome::Continue => { /* no handling */ }
    TextOutcome::Unchanged => { /* event recognized, but no changes */ }
    TextOutcome::Changed => { /* render required */ }
    TextOutcome::TextChanged => { /* actual edit */ }
}

§TextArea

Textarea with tendencies to being an editor.

Uses ropey as backend and iset to manage the text-styles.

  • Tab width/Tab expand to space.
  • Indent/dedent selection.
  • Newline starts at indent.
  • Mouse selection can work word-wise.
  • Decent speed even for large text (millons of lines and text-width ~100_000 tested).
  • Word-wrap mode.
  • Add Quotes/Braces/Brackets to selection.
// text is stored in the state
state.textarea.set_text("some text");

// render
TextArea::new()
  .block(Block::bordered())
  .vscroll(
      Scroll::new()
          .scroll_by(1)
          .policy(ScrollbarPolicy::Always),
  )
  .hscroll(Scroll::new().policy(ScrollbarPolicy::Always))
  .styles(istate.theme.textarea_style())
  .text_style([
      Style::new().red(),
      Style::new().underlined(),
      Style::new().green(),
      Style::new().on_yellow(),
  ])
  .render(layout[2], frame.buffer_mut(), &mut state.textarea);
  
// event-handling
match text_area::handle_events(&mut state.textarea, true /* focused */, event) {
    TextOutcome::Continue => { /* no handling */ }
    TextOutcome::Unchanged => { /* event recognized, but no changes */ }
    TextOutcome::Changed => { /* render required */ }
    TextOutcome::TextChanged => { /* actual edit */ }
}

There is an extended example mdedit.rs for TextArea in rat-salsa

§MaskedInput

Single line text input with a text-mask for allowed input.

  • Numeric
  • Decimal/Hexadecimal/Octal digits
  • Character/Character+Digits
  • Text separators

Nice to have for structured text input.

The widgets

  • DateInput and
  • NumberInput

use this as base.

§DateInput

DateInput with chrono format patterns.

§NumberInput

NumberInput with format_num_pattern backend. A bit similar to javas DecimalFormat.

§LineNumbers

Line numbers widget that can be combined with TextArea.

Modules§

clipboard
There are too many clipboard crates.
core
Core structs for text-editing. Used to implement the widgets.
date_input
Date-input widget using chrono
event
Event-handler traits and Keybindings.
line_number
Line numbers widget.
number_input
Number input widget
text_area
A text-area widget with text-styling abilities. And undo + clipboard support.
text_input
Text input widget.
text_input_mask
Text input widget with an input mask.
undo_buffer
Undo functionality.

Macros§

impl_screen_cursor
Create the implementation of HasScreenCursor for the given list of struct members.

Structs§

GlyphDeprecated
Data for rendering/mapping graphemes to screen coordinates.
Grapheme
One grapheme.
TextPosition
Text position.
TextRange
Exclusive range for text ranges.
TextStyle
Combined style for the widget.

Enums§

Locale
Locales matching the locales in glibc.
TextError
TextFocusGained
Behavior modifiers.
TextFocusLost
Behaviour modifiers.

Traits§

Cursor
Trait for a cursor (akin to an Iterator, not the blinking thing).
HasScreenCursor
Trait for accessing the screen-cursor.

Functions§

screen_cursor
Returns the screen_cursor for the first widget that returns one.

Type Aliases§

ipos_type
Row/Column type.
upos_type
Row/Column type.