Skip to content

Commit

Permalink
Merge branch 'vim'
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysd committed Nov 17, 2023
2 parents b2cee69 + fc08bab commit 0a5c190
Show file tree
Hide file tree
Showing 4 changed files with 440 additions and 439 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ name = "variable"
required-features = ["crossterm"]

[[example]]
name = "modal"
name = "vim"
required-features = ["crossterm"]

[[example]]
Expand Down
52 changes: 7 additions & 45 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,15 @@ cargo run --example variable

Simple textarea with variable height following the number of lines.

### [`modal`](./examples/modal.rs)
### [`vim`](./examples/vim.rs)

```sh
cargo run --example modal
cargo run --example vim
```

Simple modal text editor like `vi`.
Vim-like modal text editor. Vim emulation is implemented as a state machine.

<img src="https://raw.githubusercontent.com/rhysd/ss/master/tui-textarea/vim.gif" width=590 height=156 alt="Vim emulation example">

### [`popup_placeholder`](./examples/popup_placeholder.rs)

Expand Down Expand Up @@ -513,49 +515,9 @@ notify how to move the cursor.
| `textarea.scroll(Scrolling::HalfPageUp)` | Scroll up the viewport by half-page |
| `textarea.scroll((row, col))` | Scroll down the viewport to (row, col) position |

To define your own key mappings, simply call the above methods in your code instead of `TextArea::input()` method. The
following example defines modal key mappings like Vim.

```rust,ignore
use crossterm::event::{Event, read};
use tui_textarea::{Input, Key, CursorMove, Scrolling};
let mut textarea = ...;
enum Mode {
Normal,
Insert,
}
let mut mode = Mode::Normal;
// Event loop
loop {
// ...
match mode {
Mode::Normal => match read()?.into() {
Input { key: Key::Char('h'), .. } => textarea.move_cursor(CursorMove::Back),
Input { key: Key::Char('j'), .. } => textarea.move_cursor(CursorMove::Down),
Input { key: Key::Char('k'), .. } => textarea.move_cursor(CursorMove::Up),
Input { key: Key::Char('l'), .. } => textarea.move_cursor(CursorMove::Forward),
Input { key: Key::Char('i'), .. } => mode = Mode::Insert, // Enter insert mode
// ...Add more mappings
_ => {},
},
Mode::Insert => match read()?.into() {
Input { key: Key::Esc, .. } => {
mode = Mode::Normal; // Back to normal mode with Esc or Ctrl+C
}
input => {
textarea.input(input); // Use default key mappings in insert mode
}
},
}
}
```
To define your own key mappings, simply call the above methods in your code instead of `TextArea::input()` method.

See [`modal` example](./examples/modal.rs) for working example. It implements more Vim-like key mappings.
See the [`vim` example](./examples/vim.rs) for working example. It implements more Vim-like key modal mappings.

If you don't want to use default key mappings, `TextArea::input_without_shortcuts()` method can be used instead of
`TextArea::input()`. The method only handles very basic operations such as inserting/deleting single characters, tabs,
Expand Down
Loading

0 comments on commit 0a5c190

Please sign in to comment.