зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-08 08:25:51 +03:00
add an example for simple textarea with variable height
Этот коммит содержится в:
@@ -32,3 +32,7 @@ required-features = ["crossterm"]
|
|||||||
[[example]]
|
[[example]]
|
||||||
name = "single_line"
|
name = "single_line"
|
||||||
required-features = ["crossterm"]
|
required-features = ["crossterm"]
|
||||||
|
|
||||||
|
[[example]]
|
||||||
|
name = "variable"
|
||||||
|
required-features = ["crossterm"]
|
||||||
|
|||||||
@@ -51,6 +51,7 @@ cargo run --example minimal
|
|||||||
- [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
|
- [multi](./examples/multi.rs): Two split textareas in a screen and switch them
|
||||||
- [single_line](./examples/single_line.rs): Single line input form with float number validation
|
- [single_line](./examples/single_line.rs): Single line input form with float number validation
|
||||||
|
- [variable](./examples/variable.rs): Simple textarea with variable height following the number of lines
|
||||||
- [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
|
||||||
|
|||||||
54
examples/variable.rs
Обычный файл
54
examples/variable.rs
Обычный файл
@@ -0,0 +1,54 @@
|
|||||||
|
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, Layout};
|
||||||
|
use tui::widgets::{Block, Borders};
|
||||||
|
use tui::Terminal;
|
||||||
|
use tui_textarea::{Input, Key, TextArea};
|
||||||
|
|
||||||
|
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.set_block(
|
||||||
|
Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.title("Textarea with Variable Height"),
|
||||||
|
);
|
||||||
|
|
||||||
|
loop {
|
||||||
|
term.draw(|f| {
|
||||||
|
let len = textarea.lines().len() as u16 + 2; // + 2 for borders
|
||||||
|
let chunks = Layout::default()
|
||||||
|
.constraints([Constraint::Length(len), Constraint::Min(1)].as_slice())
|
||||||
|
.split(f.size());
|
||||||
|
f.render_widget(textarea.widget(), chunks[0]);
|
||||||
|
})?;
|
||||||
|
match Input::from(crossterm::event::read()?) {
|
||||||
|
Input { key: Key::Esc, .. } => break,
|
||||||
|
input => {
|
||||||
|
textarea.input(input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
disable_raw_mode()?;
|
||||||
|
crossterm::execute!(
|
||||||
|
term.backend_mut(),
|
||||||
|
LeaveAlternateScreen,
|
||||||
|
DisableMouseCapture
|
||||||
|
)?;
|
||||||
|
term.show_cursor()?;
|
||||||
|
|
||||||
|
println!("Lines: {:?}", textarea.lines());
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
Ссылка в новой задаче
Block a user