1
0
зеркало из https://github.com/glebtv/tui-textarea.git synced 2026-08-28 11:36:17 +03:00

refactor text selection and add more tests (fix #6)

Этот коммит содержится в:
rhysd
2023-11-12 17:39:48 +09:00
родитель 5a2649abe9
Коммит a54ce36d22
11 изменённых файлов: 1338 добавлений и 918 удалений

Просмотреть файл

@@ -248,7 +248,7 @@ impl<'a> Editor<'a> {
Span::raw(" to save, "),
Span::styled("^G", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to search, "),
Span::styled("^X", Style::default().add_modifier(Modifier::BOLD)),
Span::styled("^T", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to switch buffer"),
])
};
@@ -314,7 +314,7 @@ impl<'a> Editor<'a> {
..
} => break,
Input {
key: Key::Char('x'),
key: Key::Char('t'),
ctrl: true,
..
} => {

Просмотреть файл

@@ -60,7 +60,7 @@ fn main() -> io::Result<()> {
} else {
TextArea::default()
};
textarea.set_selection_style(Style::default().bg(Color::Red));
let mut mode = Mode::Normal;
loop {
// Show help message and current mode in title of the block
@@ -75,41 +75,32 @@ fn main() -> io::Result<()> {
term.draw(|f| f.render_widget(textarea.widget(), f.size()))?;
let input: Input = crossterm::event::read()?.into();
// since this sample doesnt use input a lot of the time
// it must call should_select
textarea.should_select(&input);
let input = crossterm::event::read()?.into();
match mode {
Mode::Normal => match input {
// Mappings in normal mode
// note that the navigation keys can be pressed with or without shift
// if the user is selecting text
// so we have to look for 'h' and 'H' etc.
Input {
key: Key::Char('h' | 'H'),
key: Key::Char('h'),
..
} => textarea.move_cursor(CursorMove::Back),
Input {
key: Key::Char('j' | 'J'),
key: Key::Char('j'),
..
} => textarea.move_cursor(CursorMove::Down),
Input {
key: Key::Char('k' | 'K'),
key: Key::Char('k'),
..
} => textarea.move_cursor(CursorMove::Up),
Input {
key: Key::Char('l' | 'L'),
key: Key::Char('l'),
..
} => textarea.move_cursor(CursorMove::Forward),
Input {
key: Key::Char('w' | 'W'),
key: Key::Char('w'),
..
} => textarea.move_cursor(CursorMove::WordForward),
Input {
key: Key::Char('b' | 'B'),
key: Key::Char('b'),
ctrl: false,
..
} => textarea.move_cursor(CursorMove::WordBack),
@@ -140,13 +131,6 @@ fn main() -> io::Result<()> {
} => {
textarea.paste();
}
Input {
key: Key::Char('y'),
ctrl: false,
..
} => {
textarea.copy();
}
Input {
key: Key::Char('u'),
ctrl: false,
@@ -182,8 +166,6 @@ fn main() -> io::Result<()> {
key: Key::Char('A'),
..
} => {
// stop selection is called to prevent 'A' being interpreted as selecting
textarea.stop_selection();
textarea.move_cursor(CursorMove::End);
mode = Mode::Insert;
}
@@ -199,8 +181,6 @@ fn main() -> io::Result<()> {
key: Key::Char('O'),
..
} => {
// stop selection is called to prevent 'O' being interpreted as selecting
textarea.stop_selection();
textarea.move_cursor(CursorMove::Head);
textarea.insert_newline();
textarea.move_cursor(CursorMove::Up);
@@ -210,7 +190,6 @@ fn main() -> io::Result<()> {
key: Key::Char('I'),
..
} => {
textarea.stop_selection();
textarea.move_cursor(CursorMove::Head);
mode = Mode::Insert;
}
@@ -248,6 +227,33 @@ fn main() -> io::Result<()> {
ctrl: true,
..
} => textarea.scroll(Scrolling::PageUp),
Input {
key: Key::Char('v'),
ctrl: false,
..
} => textarea.start_selection(),
Input {
key: Key::Char('V'),
ctrl: false,
..
} => {
textarea.move_cursor(CursorMove::Head);
textarea.start_selection();
textarea.move_cursor(CursorMove::End);
}
Input { key: Key::Esc, .. } => textarea.cancel_selection(),
Input {
key: Key::Char('y'),
ctrl: false,
..
} if textarea.is_selecting() => textarea.copy(),
Input {
key: Key::Char('d'),
ctrl: false,
..
} if textarea.is_selecting() => {
textarea.cut();
}
_ => {}
},
Mode::Insert => match input {

Просмотреть файл

@@ -250,7 +250,7 @@ impl<'a> Editor<'a> {
Span::raw(" to save, "),
Span::styled("^G", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to search, "),
Span::styled("^X", Style::default().add_modifier(Modifier::BOLD)),
Span::styled("^T", Style::default().add_modifier(Modifier::BOLD)),
Span::raw(" to switch buffer"),
])
};
@@ -316,7 +316,7 @@ impl<'a> Editor<'a> {
..
} => break,
Input {
key: Key::Char('x'),
key: Key::Char('t'),
ctrl: true,
..
} => {