зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-07 16:05:50 +03:00
use rust,ignore code fences in readme.md (fix #34)
so that we no longer need to add `--skip` on `cargo test`
Этот коммит содержится в:
@@ -1,3 +1,3 @@
|
|||||||
[alias]
|
[alias]
|
||||||
watch-check = ["watch", "-x", "clippy --features ratatui-crossterm,search --no-default-features --examples --tests", "-x", "clippy --features search --examples --tests"]
|
watch-check = ["watch", "-x", "clippy --features ratatui-crossterm,search --no-default-features --examples --tests", "-x", "clippy --features search --examples --tests"]
|
||||||
watch-test = ["watch", "-x", "test --features=search -- --skip=src/lib.rs", "-w", "src", "-w", "tests"]
|
watch-test = ["watch", "-x", "test --features=search", "-w", "src", "-w", "tests"]
|
||||||
|
|||||||
5
.github/workflows/ci.yml
поставляемый
5
.github/workflows/ci.yml
поставляемый
@@ -14,10 +14,7 @@ jobs:
|
|||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- uses: dtolnay/rust-toolchain@stable
|
- uses: dtolnay/rust-toolchain@stable
|
||||||
- uses: Swatinem/rust-cache@v2
|
- uses: Swatinem/rust-cache@v2
|
||||||
- run: cargo test --features=search -- --skip src/lib.rs
|
- run: cargo test --features=search
|
||||||
if: ${{ matrix.os != 'windows-latest' }}
|
|
||||||
- run: cargo test --features=search -- --skip src\lib.rs
|
|
||||||
if: ${{ matrix.os == 'windows-latest' }}
|
|
||||||
- run: cargo test --no-default-features --features=ratatui-crossterm,search -- --skip .rs
|
- run: cargo test --no-default-features --features=ratatui-crossterm,search -- --skip .rs
|
||||||
- run: cargo test --no-default-features --features=your-backend,search -- --skip .rs
|
- run: cargo test --no-default-features --features=your-backend,search -- --skip .rs
|
||||||
- run: cargo test --no-default-features --features=ratatui-your-backend,search -- --skip .rs
|
- run: cargo test --no-default-features --features=ratatui-your-backend,search -- --skip .rs
|
||||||
|
|||||||
@@ -21,11 +21,9 @@ Please ensure that all tests and linter checks passed on your branch before crea
|
|||||||
To run tests:
|
To run tests:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cargo test --features=search -- --skip src/lib.rs
|
cargo test --features=search
|
||||||
```
|
```
|
||||||
|
|
||||||
`--skip` is necessary since `cargo test` tries to run code blocks in [README file](./README.md).
|
|
||||||
|
|
||||||
To run linters:
|
To run linters:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
|
|||||||
44
README.md
44
README.md
@@ -172,7 +172,7 @@ Please select the correct version.
|
|||||||
|
|
||||||
## Minimal Usage
|
## Minimal Usage
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
use tui_textarea::TextArea;
|
use tui_textarea::TextArea;
|
||||||
use crossterm::event::{Event, read};
|
use crossterm::event::{Event, read};
|
||||||
|
|
||||||
@@ -255,13 +255,13 @@ If you don't want to use default key mappings, see the 'Advanced Usage' section.
|
|||||||
|
|
||||||
`TextArea` implements `Default` trait to create an editor instance with an empty text.
|
`TextArea` implements `Default` trait to create an editor instance with an empty text.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
let mut textarea = TextArea::default();
|
let mut textarea = TextArea::default();
|
||||||
```
|
```
|
||||||
|
|
||||||
`TextArea::new()` creates an editor instance with text lines passed as `Vec<String>`.
|
`TextArea::new()` creates an editor instance with text lines passed as `Vec<String>`.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
let mut lines: Vec<String> = ...;
|
let mut lines: Vec<String> = ...;
|
||||||
let mut textarea = TextArea::new(lines);
|
let mut textarea = TextArea::new(lines);
|
||||||
```
|
```
|
||||||
@@ -269,7 +269,7 @@ let mut textarea = TextArea::new(lines);
|
|||||||
`TextArea` implements `From<impl Iterator<Item=impl Into<String>>>`. `TextArea::from()` can create an editor instance
|
`TextArea` implements `From<impl Iterator<Item=impl Into<String>>>`. `TextArea::from()` can create an editor instance
|
||||||
from any iterators whose elements can be converted to `String`.
|
from any iterators whose elements can be converted to `String`.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
// Create `TextArea` from from `[&str]`
|
// Create `TextArea` from from `[&str]`
|
||||||
let mut textarea = TextArea::from([
|
let mut textarea = TextArea::from([
|
||||||
"this is first line",
|
"this is first line",
|
||||||
@@ -285,7 +285,7 @@ let mut textarea = TextARea::from(text.lines());
|
|||||||
`TextArea` also implements `FromIterator<impl Into<String>>`. `Iterator::collect()` can collect strings as an editor
|
`TextArea` also implements `FromIterator<impl Into<String>>`. `Iterator::collect()` can collect strings as an editor
|
||||||
instance. This allows to create `TextArea` reading lines from file efficiently using `io::BufReader`.
|
instance. This allows to create `TextArea` reading lines from file efficiently using `io::BufReader`.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
let file = fs::File::open(path)?;
|
let file = fs::File::open(path)?;
|
||||||
let mut textarea: TextArea = io::BufReader::new(file).lines().collect::<io::Result<_>>()?;
|
let mut textarea: TextArea = io::BufReader::new(file).lines().collect::<io::Result<_>>()?;
|
||||||
```
|
```
|
||||||
@@ -294,21 +294,21 @@ let mut textarea: TextArea = io::BufReader::new(file).lines().collect::<io::Resu
|
|||||||
|
|
||||||
`TextArea::lines()` returns text lines as `&[String]`. It borrows text contents temporarily.
|
`TextArea::lines()` returns text lines as `&[String]`. It borrows text contents temporarily.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
let text: String = textarea.lines().join("\n");
|
let text: String = textarea.lines().join("\n");
|
||||||
```
|
```
|
||||||
|
|
||||||
`TextArea::into_lines()` moves `TextArea` instance into text lines as `Vec<String>`. This can retrieve the text contents
|
`TextArea::into_lines()` moves `TextArea` instance into text lines as `Vec<String>`. This can retrieve the text contents
|
||||||
without any copy.
|
without any copy.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
let lines: Vec<String> = textarea.into_lines();
|
let lines: Vec<String> = textarea.into_lines();
|
||||||
```
|
```
|
||||||
|
|
||||||
Note that `TextArea` always contains at least one line. For example, an empty text means one empty line. This is because
|
Note that `TextArea` always contains at least one line. For example, an empty text means one empty line. This is because
|
||||||
any text file must end with newline.
|
any text file must end with newline.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
let textarea = TextArea::default();
|
let textarea = TextArea::default();
|
||||||
assert_eq!(textarea.into_lines(), [""]);
|
assert_eq!(textarea.into_lines(), [""]);
|
||||||
```
|
```
|
||||||
@@ -319,7 +319,7 @@ By default, `TextArea` does now show line numbers. To enable, set a style for re
|
|||||||
`TextArea::set_line_number_style()`. For example, the following renders line numbers in dark gray background
|
`TextArea::set_line_number_style()`. For example, the following renders line numbers in dark gray background
|
||||||
color.
|
color.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
use tui::style::{Style, Color};
|
use tui::style::{Style, Color};
|
||||||
|
|
||||||
let style = Style::default().bg(Color::DarkGray);
|
let style = Style::default().bg(Color::DarkGray);
|
||||||
@@ -332,7 +332,7 @@ By default, `TextArea` renders the line at cursor with underline so that users c
|
|||||||
is. To change the style of cursor line, use `TextArea::set_cursor_line_style()`. For example, the following styles the
|
is. To change the style of cursor line, use `TextArea::set_cursor_line_style()`. For example, the following styles the
|
||||||
cursor line with bold text.
|
cursor line with bold text.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
use tui::style::{Style, Modifier};
|
use tui::style::{Style, Modifier};
|
||||||
|
|
||||||
let style = Style::default().add_modifier(Modifier::BOLD);
|
let style = Style::default().add_modifier(Modifier::BOLD);
|
||||||
@@ -341,7 +341,7 @@ textarea.set_line_number_style(style);
|
|||||||
|
|
||||||
To disable cursor line style, set the default style as follows:
|
To disable cursor line style, set the default style as follows:
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
use tui::style::{Style, Modifier};
|
use tui::style::{Style, Modifier};
|
||||||
|
|
||||||
textarea.set_line_number_style(Style::default());
|
textarea.set_line_number_style(Style::default());
|
||||||
@@ -352,7 +352,7 @@ textarea.set_line_number_style(Style::default());
|
|||||||
The default tab width is 4. To change it, use `TextArea::set_tab_length()` method. The following sets 2 to tab width.
|
The default tab width is 4. To change it, use `TextArea::set_tab_length()` method. The following sets 2 to tab width.
|
||||||
Typing tab key inserts 2 spaces.
|
Typing tab key inserts 2 spaces.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
textarea.set_tab_length(2);
|
textarea.set_tab_length(2);
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -361,13 +361,13 @@ textarea.set_tab_length(2);
|
|||||||
By default, past 50 modifications are stored as edit history. The history is used for undo/redo. To change how many past
|
By default, past 50 modifications are stored as edit history. The history is used for undo/redo. To change how many past
|
||||||
edits are remembered, use `TextArea::set_max_histories()` method. The following remembers past 1000 changes.
|
edits are remembered, use `TextArea::set_max_histories()` method. The following remembers past 1000 changes.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
textarea.set_max_histories(1000);
|
textarea.set_max_histories(1000);
|
||||||
```
|
```
|
||||||
|
|
||||||
Setting 0 disables undo/redo.
|
Setting 0 disables undo/redo.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
textarea.set_max_histories(0);
|
textarea.set_max_histories(0);
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -383,7 +383,7 @@ the pattern from start of the file.
|
|||||||
Matches are highlighted in textarea. The text style to highlight matches can be changed with
|
Matches are highlighted in textarea. The text style to highlight matches can be changed with
|
||||||
`TextArea::set_search_style()`. Setting an empty string to `TextArea::set_search_pattern()` stops the text search.
|
`TextArea::set_search_style()`. Setting an empty string to `TextArea::set_search_pattern()` stops the text search.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
// Start text search matching to "hello" or "hi". This highlights matches in textarea but does not move cursor.
|
// Start text search matching to "hello" or "hi". This highlights matches in textarea but does not move cursor.
|
||||||
// `regex::Error` is returned on invalid pattern.
|
// `regex::Error` is returned on invalid pattern.
|
||||||
textarea.set_search_pattern("(hello|hi)").unwrap();
|
textarea.set_search_pattern("(hello|hi)").unwrap();
|
||||||
@@ -415,7 +415,7 @@ tui-textarea = { version = "*", features = ["search"] }
|
|||||||
|
|
||||||
To use `TextArea` for single-line input widget like `<input>` in HTML, ignore all key mappings which inserts newline.
|
To use `TextArea` for single-line input widget like `<input>` in HTML, ignore all key mappings which inserts newline.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
use crossterm::event::{Event, read};
|
use crossterm::event::{Event, read};
|
||||||
use tui_textarea::{Input, Key};
|
use tui_textarea::{Input, Key};
|
||||||
|
|
||||||
@@ -486,7 +486,7 @@ notify how to move the cursor.
|
|||||||
To define your own key mappings, simply call the above methods in your code instead of `TextArea::input()` method. The
|
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.
|
following example defines modal key mappings like Vim.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
use crossterm::event::{Event, read};
|
use crossterm::event::{Event, read};
|
||||||
use tui_textarea::{Input, Key, CursorMove, Scrolling};
|
use tui_textarea::{Input, Key, CursorMove, Scrolling};
|
||||||
|
|
||||||
@@ -531,7 +531,7 @@ If you don't want to use default key mappings, `TextArea::input_without_shortcut
|
|||||||
`TextArea::input()`. The method only handles very basic operations such as inserting/deleting single characters, tabs,
|
`TextArea::input()`. The method only handles very basic operations such as inserting/deleting single characters, tabs,
|
||||||
newlines.
|
newlines.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
match read()?.into() {
|
match read()?.into() {
|
||||||
// Handle your own key mappings here
|
// Handle your own key mappings here
|
||||||
// ...
|
// ...
|
||||||
@@ -560,7 +560,7 @@ backend into the `tui_textarea::Input` instance. Then `TextArea::input()` method
|
|||||||
In the following example, let's say `your_backend::KeyDown` is a key event type for your backend and
|
In the following example, let's say `your_backend::KeyDown` is a key event type for your backend and
|
||||||
`your_backend::read_next_key()` returns the next key event.
|
`your_backend::read_next_key()` returns the next key event.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
// In your backend implementation
|
// In your backend implementation
|
||||||
|
|
||||||
pub enum KeyDown {
|
pub enum KeyDown {
|
||||||
@@ -579,7 +579,7 @@ pub fn read_next_key() -> (KeyDown, bool, bool) {
|
|||||||
|
|
||||||
Then you can implement the logic to convert `your_backend::KeyDown` value into `tui_textarea::Input` value.
|
Then you can implement the logic to convert `your_backend::KeyDown` value into `tui_textarea::Input` value.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
use tui_textarea::{Input, Key};
|
use tui_textarea::{Input, Key};
|
||||||
use your_backend::KeyDown;
|
use your_backend::KeyDown;
|
||||||
|
|
||||||
@@ -600,7 +600,7 @@ key. An editor will do nothing with the key.
|
|||||||
|
|
||||||
Finally, convert your own backend's key input type into `tui_textarea::Input` and pass it to `TextArea::input()`.
|
Finally, convert your own backend's key input type into `tui_textarea::Input` and pass it to `TextArea::input()`.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
let mut textarea = ...;
|
let mut textarea = ...;
|
||||||
|
|
||||||
// Event loop
|
// Event loop
|
||||||
@@ -621,7 +621,7 @@ You don't need to do anything special. Create multiple `TextArea` instances and
|
|||||||
|
|
||||||
The following is an example to put two textarea widgets in application and manage the focus.
|
The following is an example to put two textarea widgets in application and manage the focus.
|
||||||
|
|
||||||
```rust
|
```rust,ignore
|
||||||
use tui_textarea::{TextArea, Input, Key};
|
use tui_textarea::{TextArea, Input, Key};
|
||||||
use crossterm::event::{Event, read};
|
use crossterm::event::{Event, read};
|
||||||
|
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user