зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-03 14:26:17 +03:00
at least 3 lines height to variable example and tweak some variable names
Этот коммит содержится в:
@@ -10,7 +10,7 @@ use tui::widgets::{Block, Borders};
|
||||
use tui::Terminal;
|
||||
use tui_textarea::{Input, Key, TextArea};
|
||||
|
||||
fn update(textarea: &mut TextArea) -> bool {
|
||||
fn validate(textarea: &mut TextArea) -> bool {
|
||||
if let Err(err) = textarea.lines()[0].parse::<f64>() {
|
||||
textarea.set_style(Style::default().fg(Color::LightRed));
|
||||
textarea.set_block(
|
||||
@@ -39,7 +39,7 @@ fn main() -> io::Result<()> {
|
||||
textarea.set_cursor_line_style(Style::default());
|
||||
let layout =
|
||||
Layout::default().constraints([Constraint::Length(3), Constraint::Min(1)].as_slice());
|
||||
let mut is_valid = update(&mut textarea);
|
||||
let mut is_valid = validate(&mut textarea);
|
||||
|
||||
loop {
|
||||
term.draw(|f| {
|
||||
@@ -64,7 +64,7 @@ fn main() -> io::Result<()> {
|
||||
input => {
|
||||
// TextArea::input returns if the input modified its text
|
||||
if textarea.input(input) {
|
||||
is_valid = update(&mut textarea);
|
||||
is_valid = validate(&mut textarea);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,10 @@ use crossterm::event::{DisableMouseCapture, EnableMouseCapture};
|
||||
use crossterm::terminal::{
|
||||
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
|
||||
};
|
||||
use std::cmp;
|
||||
use std::io;
|
||||
use tui::backend::CrosstermBackend;
|
||||
use tui::layout::{Constraint, Layout};
|
||||
use tui::layout::{Constraint, Direction, Layout};
|
||||
use tui::widgets::{Block, Borders};
|
||||
use tui::Terminal;
|
||||
use tui_textarea::{Input, Key, TextArea};
|
||||
@@ -27,9 +28,11 @@ fn main() -> io::Result<()> {
|
||||
|
||||
loop {
|
||||
term.draw(|f| {
|
||||
let len = textarea.lines().len() as u16 + 2; // + 2 for borders
|
||||
const MIN_HEIGHT: usize = 3;
|
||||
let height = cmp::max(textarea.lines().len(), MIN_HEIGHT) as u16 + 2; // + 2 for borders
|
||||
let chunks = Layout::default()
|
||||
.constraints([Constraint::Length(len), Constraint::Min(1)].as_slice())
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([Constraint::Length(height), Constraint::Min(0)].as_slice())
|
||||
.split(f.size());
|
||||
f.render_widget(textarea.widget(), chunks[0]);
|
||||
})?;
|
||||
|
||||
Ссылка в новой задаче
Block a user