зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-07 16:05:50 +03:00
add multi example
Этот коммит содержится в:
@@ -24,3 +24,7 @@ required-features = ["termion"]
|
|||||||
[[example]]
|
[[example]]
|
||||||
name = "editor"
|
name = "editor"
|
||||||
required-features = ["crossterm"]
|
required-features = ["crossterm"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "multi"
|
||||||
|
required-features = ["crossterm"]
|
||||||
|
|||||||
13
README.md
13
README.md
@@ -33,8 +33,8 @@ If you're using tui-rs with [termion][], enable `termion` feature instead of `cr
|
|||||||
|
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tui = { version = "*", default-features=false, features=["termion"] }
|
tui = { version = "*", default-features = false, features = ["termion"] }
|
||||||
tui-textarea = { version = "*", default-features=false, features=["termion"] }
|
tui-textarea = { version = "*", default-features = false, features = ["termion"] }
|
||||||
```
|
```
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
@@ -49,6 +49,7 @@ cargo run --example minimal
|
|||||||
|
|
||||||
- [minimal](./examples/minimal.rs): Minimal usage with [crossterm][] support
|
- [minimal](./examples/minimal.rs): Minimal usage with [crossterm][] support
|
||||||
- [termion](./examples/termion.rs): Minimal usage with [termion][] support
|
- [termion](./examples/termion.rs): Minimal usage with [termion][] support
|
||||||
|
- [multi](./examples/multi.rs): Two split textareas in a screen and switch them
|
||||||
- [editor](./examples/editor.rs): Simple text editor to edit multiple files
|
- [editor](./examples/editor.rs): Simple text editor to edit multiple files
|
||||||
|
|
||||||
## Minimal Usage
|
## Minimal Usage
|
||||||
@@ -379,8 +380,8 @@ supports it. In the case, support for neither crossterm nor termion is necessary
|
|||||||
|
|
||||||
```toml
|
```toml
|
||||||
[dependencies]
|
[dependencies]
|
||||||
tui = { version = "*", default-features=false }
|
tui = { version = "*", default-features = false }
|
||||||
tui-textarea = { version = "*", default-features=false }
|
tui-textarea = { version = "*", default-features = false }
|
||||||
```
|
```
|
||||||
|
|
||||||
`tui_textarea::Input` is a type for backend-agnostic key input. What you need to do is converting key event in your own
|
`tui_textarea::Input` is a type for backend-agnostic key input. What you need to do is converting key event in your own
|
||||||
@@ -480,7 +481,7 @@ loop {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
See [`editor` example](./examples/editor.rs) for working example.
|
See [`multi` example](./examples/multi.rs) and [`editor` example](./examples/editor.rs) for working example.
|
||||||
|
|
||||||
## Contributing to tui-textarea
|
## Contributing to tui-textarea
|
||||||
|
|
||||||
@@ -498,7 +499,7 @@ tui-textarea is distributed under [The MIT License](./LICENSE.txt).
|
|||||||
[crate]: https://crates.io/crates/tui-textarea
|
[crate]: https://crates.io/crates/tui-textarea
|
||||||
[tui-rs]: https://github.com/fdehau/tui-rs
|
[tui-rs]: https://github.com/fdehau/tui-rs
|
||||||
[termion]: https://docs.rs/termion/latest/termion/
|
[termion]: https://docs.rs/termion/latest/termion/
|
||||||
[crosster]: https://docs.rs/crossterm/latest/crossterm/
|
[crossterm]: https://docs.rs/crossterm/latest/crossterm/
|
||||||
[doc]: https://docs.rs/tui-textarea/latest/tui_textarea
|
[doc]: https://docs.rs/tui-textarea/latest/tui_textarea
|
||||||
[tui-backend]: https://docs.rs/tui/latest/tui/backend/trait.Backend.html
|
[tui-backend]: https://docs.rs/tui/latest/tui/backend/trait.Backend.html
|
||||||
[repo]: https://github.com/rhysd/tui-textarea
|
[repo]: https://github.com/rhysd/tui-textarea
|
||||||
|
|||||||
88
examples/multi.rs
Обычный файл
88
examples/multi.rs
Обычный файл
@@ -0,0 +1,88 @@
|
|||||||
|
use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
|
||||||
|
use crossterm::terminal::{
|
||||||
|
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
|
||||||
|
};
|
||||||
|
use std::io;
|
||||||
|
use tui::backend::CrosstermBackend;
|
||||||
|
use tui::layout::{Constraint, Direction, Layout};
|
||||||
|
use tui::style::{Color, Modifier, Style};
|
||||||
|
use tui::widgets::{Block, Borders};
|
||||||
|
use tui::Terminal;
|
||||||
|
use tui_textarea::{Input, Key, TextArea};
|
||||||
|
|
||||||
|
fn inactivate(textarea: &mut TextArea<'_>) {
|
||||||
|
textarea.set_cursor_line_style(Style::default());
|
||||||
|
textarea.set_cursor_style(Style::default());
|
||||||
|
let b = textarea
|
||||||
|
.block()
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_else(|| Block::default().borders(Borders::ALL));
|
||||||
|
textarea.set_block(
|
||||||
|
b.style(Style::default().fg(Color::DarkGray))
|
||||||
|
.title(" Inactive (^X to switch) "),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn activate(textarea: &mut TextArea<'_>) {
|
||||||
|
textarea.set_cursor_line_style(Style::default().add_modifier(Modifier::UNDERLINED));
|
||||||
|
textarea.set_cursor_style(Style::default().add_modifier(Modifier::REVERSED));
|
||||||
|
let b = textarea
|
||||||
|
.block()
|
||||||
|
.cloned()
|
||||||
|
.unwrap_or_else(|| Block::default().borders(Borders::ALL));
|
||||||
|
textarea.set_block(b.style(Style::default()).title(" Active "));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() -> io::Result<()> {
|
||||||
|
let stdout = io::stdout();
|
||||||
|
let mut stdout = stdout.lock();
|
||||||
|
enable_raw_mode()?;
|
||||||
|
crossterm::execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
|
||||||
|
let backend = CrosstermBackend::new(stdout);
|
||||||
|
let mut term = Terminal::new(backend)?;
|
||||||
|
|
||||||
|
let mut textarea = [TextArea::default(), TextArea::default()];
|
||||||
|
|
||||||
|
let layout = Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)].as_ref());
|
||||||
|
|
||||||
|
let mut which = 0;
|
||||||
|
activate(&mut textarea[0]);
|
||||||
|
inactivate(&mut textarea[1]);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
term.draw(|f| {
|
||||||
|
let chunks = layout.split(f.size());
|
||||||
|
for (textarea, chunk) in textarea.iter().zip(chunks) {
|
||||||
|
let widget = textarea.widget();
|
||||||
|
f.render_widget(widget, chunk);
|
||||||
|
}
|
||||||
|
})?;
|
||||||
|
match Input::from(crossterm::event::read()?) {
|
||||||
|
Input { key: Key::Esc, .. } => break,
|
||||||
|
Input {
|
||||||
|
key: Key::Char('x'),
|
||||||
|
ctrl: true,
|
||||||
|
..
|
||||||
|
} => {
|
||||||
|
inactivate(&mut textarea[which]);
|
||||||
|
which = (which + 1) % 2;
|
||||||
|
activate(&mut textarea[which]);
|
||||||
|
}
|
||||||
|
input => {
|
||||||
|
textarea[which].input(input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
disable_raw_mode()?;
|
||||||
|
crossterm::execute!(
|
||||||
|
term.backend_mut(),
|
||||||
|
LeaveAlternateScreen,
|
||||||
|
DisableMouseCapture
|
||||||
|
)?;
|
||||||
|
term.show_cursor()?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Ссылка в новой задаче
Block a user