зеркало из
https://github.com/glebtv/tui-textarea.git
synced 2026-09-08 00:15:50 +03:00
simplify UI structure of modal example
Этот коммит содержится в:
@@ -8,10 +8,8 @@ use std::fs;
|
|||||||
use std::io;
|
use std::io;
|
||||||
use std::io::BufRead;
|
use std::io::BufRead;
|
||||||
use tui::backend::CrosstermBackend;
|
use tui::backend::CrosstermBackend;
|
||||||
use tui::layout::{Constraint, Direction, Layout};
|
use tui::style::{Color, Modifier, Style};
|
||||||
use tui::style::{Modifier, Style};
|
use tui::widgets::{Block, Borders};
|
||||||
use tui::text::{Span, Spans};
|
|
||||||
use tui::widgets::{Block, Borders, Paragraph};
|
|
||||||
use tui::Terminal;
|
use tui::Terminal;
|
||||||
use tui_textarea::{CursorMove, Input, Key, Scrolling, TextArea};
|
use tui_textarea::{CursorMove, Input, Key, Scrolling, TextArea};
|
||||||
|
|
||||||
@@ -20,6 +18,22 @@ enum Mode {
|
|||||||
Insert,
|
Insert,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Mode {
|
||||||
|
fn help_message(&self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
Self::Normal => "type q to quit, type i to enter insert mode",
|
||||||
|
Self::Insert => "type Esc to back to normal mode",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn cursor_color(&self) -> Color {
|
||||||
|
match self {
|
||||||
|
Self::Normal => Color::Reset,
|
||||||
|
Self::Insert => Color::LightBlue,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for Mode {
|
impl fmt::Display for Mode {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
|
||||||
match self {
|
match self {
|
||||||
@@ -47,37 +61,19 @@ fn main() -> io::Result<()> {
|
|||||||
TextArea::default()
|
TextArea::default()
|
||||||
};
|
};
|
||||||
|
|
||||||
textarea.set_block(
|
|
||||||
Block::default()
|
|
||||||
.borders(Borders::ALL)
|
|
||||||
.title("Modal Example"),
|
|
||||||
);
|
|
||||||
|
|
||||||
let layout = Layout::default()
|
|
||||||
.direction(Direction::Vertical)
|
|
||||||
.constraints([Constraint::Min(1), Constraint::Length(1)].as_ref());
|
|
||||||
let mut mode = Mode::Normal;
|
let mut mode = Mode::Normal;
|
||||||
loop {
|
loop {
|
||||||
term.draw(|f| {
|
// Show help message and current mode in title of the block
|
||||||
let chunks = layout.split(f.size());
|
let title = format!("{} MODE ({})", mode, mode.help_message());
|
||||||
|
let block = Block::default().borders(Borders::ALL).title(title);
|
||||||
|
textarea.set_block(block);
|
||||||
|
|
||||||
f.render_widget(textarea.widget(), chunks[0]);
|
// Change the cursor color looking at current mode
|
||||||
|
let color = mode.cursor_color();
|
||||||
|
let style = Style::default().fg(color).add_modifier(Modifier::REVERSED);
|
||||||
|
textarea.set_cursor_style(style);
|
||||||
|
|
||||||
let (row, col) = textarea.cursor();
|
term.draw(|f| f.render_widget(textarea.widget(), f.size()))?;
|
||||||
let help = if let Mode::Normal = mode {
|
|
||||||
" type q for quit, type i to enter insert mode"
|
|
||||||
} else {
|
|
||||||
" type Esc to leave insert mode"
|
|
||||||
};
|
|
||||||
let status = Paragraph::new(Spans::from(vec![
|
|
||||||
Span::styled(
|
|
||||||
format!(" {} {},{} ", mode, row, col),
|
|
||||||
Style::default().add_modifier(Modifier::REVERSED),
|
|
||||||
),
|
|
||||||
Span::raw(help),
|
|
||||||
]));
|
|
||||||
f.render_widget(status, chunks[1]);
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let input = crossterm::event::read()?.into();
|
let input = crossterm::event::read()?.into();
|
||||||
match mode {
|
match mode {
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user