Crate edtui

Crate edtui 

Source
Expand description

§EdTUI

Crate Badge Continuous Integration Deps Status License Badge

§Overview

EdTUI is a text editor widget for the Ratatui ecosystem. It is designed to provide a user experience inspired by Vim. Edtui is developed to be used as an editor in ratatui apps. It is not supposed to be a stand-alone code editor.

Instantiate the state and render the view:

use edtui::{EditorState, EditorTheme, EditorView};
use ratatui::widgets::Widget;

let mut state = EditorState::default();
EditorView::new(&mut state)
        .theme(EditorTheme::default())
        .wrap(true) // line wrapping
        .render(area, buf);

Handle events:

use edtui::EditorEventHandler;

let mut event_handler = EditorEventHandler::default();
event_handler.on_key_event(key_event, &mut state);

§Features

  • Vim-like keybindings and editing modes for efficient text manipulation.
  • Copy paste using the systems clipboard.
  • Line wrapping.
  • Syntax highlighting.
  • Mouse support.

§Demo

§Keybindings

EdTUI offers a set of keybindings similar to Vim. Here are some of the most common keybindings:

§Normal Mode:
KeybindingDescription
iEnter Insert mode
vEnter Visual mode
h, j, k, lNavigate left, down, up, and right
wMove forward to the start of a word
eMove forward to the end of a word
bMove backward to the start of a word
ctrl+dJump a half page down
ctrl+uJump a half page up
xDelete the character under the cursor
u, ctrl+rUndo/Redo last action
EscEscape Visual mode
0Move cursor to start of line
_Move cursor to first non-blank character
$Move cursor to end of line
ggMove cursor to the first row
G Move cursor to the last row
%Move cursor to closing/opening bracket
aAppend after the cursor
AAppend at the end of the line
oAdd a new line below and enter Insert mode
OAdd a new line above and enter Insert mode
JJoin current line with the line below
dDelete the selection (Visual mode)
ddDelete the current line
DDelete to the end of the line
viwSelect between word.
ciwChange between word.
vi + ", ', (, [ or {Select between delimiter ", ', (, [ or {
ci + ", ', (, [ or {Change between delimiter ", ', (, [ or {
uUndo the last change
rRedo the last undone action
yCopy the selected text in visual mode
yyCopy the current line in normal mode
pPaste the copied text
HomeMove cursor to start of line
EndMove cursor to end of line
§Insert Mode:
KeybindingDescription
EscReturn to Normal mode
BackspaceDelete the previous character
EnterInsert line break
ArrowsNavigation
HomeMove cursor to start of line
EndMove cursor to end of line
ctrl+uDelete until first character

For more keybindings and customization options, refer to the code.

§Mouse Support

Edtui includes mouse support for selecting text:

let event_handler = EditorEvent::default();
event_handler.on_mouse_event(mouse_event, &mut state);
// or handle both key and mouse event
event_handler.on_event(event, &mut state);

§Syntax highlighting

Syntax highlighting was added in version 0.8.4.

Edtui offers a number of custom themes, see SyntaxHighlighter::theme for a complete list. If you want to use a custom theme, see SyntaxHighlighter::custom_theme. Check syntect for more details about themes and extensions.

 use edtui::EditorState;
 use edtui::EditorView;
 use edtui::SyntaxHighlighter;

 let theme_name = "dracula";
 let extension = "rs";
 let syntax_highlighter = SyntaxHighlighter::new(theme_name, extension);
 EditorView::new(&mut EditorState::default())
         .syntax_highlighter(Some(syntax_highlighter))
         .render(area, buf);

§Paste Support

If you want to enable paste (via ctrl+y or cmd+y) you must explicitly enable it at the start of your app:

use ratatui::crossterm::event::EnableBracketedPaste;
let mut stdout = std::io::stdout();
ratatui::crossterm::xecute!(stdout, EnableBracketedPaste);

and disable it during cleanup:

use ratatui::crossterm::event::DisableBracketedPaste;
ratatui::crossterm::execute!(std::io::stdout(), DisableBracketedPaste);

See examples/app/term.rs for a an example.

§Roadmap

  • Support termwiz and termion
  • Display line numbers
  • Remap keybindings

Re-exports§

pub use events::EditorEventHandler;
pub use syntect;

Modules§

actions
Editor actions such as move, insert, delete
clipboard
The editors clipboard
events

Structs§

EditorInputDeprecated
EditorState
Represents the state of an editor.
EditorStatusLine
An optional status line for Editor.
EditorTheme
The theme data of the Editor.
EditorView
Creates the view for the editor. EditorView and EditorState are the core classes of edtui.
Index2
An index representing a specific position in a 2d jagged array.
RowIndex
An index representing a specific row in a jagged array.
SyntaxHighlighter
Syntax highlighter settings including theme and syntax.

Enums§

EditorMode
The editor mode.

Type Aliases§

Lines
A data structure that contains chars organized in rows and columns