pub struct StringEditor { /* private fields */ }
Expand description
A StringEditor
instance wraps a String
and provides a way to edit it using a variety of commands.
It also keeps track of the cursor position, which can be used for rendering.
§Example
use string_cmd::StringEditor;
let mut editor = StringEditor::new();
// Insert text
editor.execute(Command::Insert('H'));
editor.execute(Command::Insert('e'));
editor.execute(Command::Insert('l'));
editor.execute(Command::Insert('l'));
editor.execute(Command::Insert('o'));
// Get the current text
let text = editor.get_text(); // "Hello"
// Get the cursor position
let cursor = editor.cursor_pos(); // 5
In most cases, you’ll want to use the StringEditor
in conjunction with a terminal event loop.
If you’re using crossterm
and want to use the default keybindings, check out the events
module
(requires the crossterm
feature).
Implementations§
Source§impl StringEditor
impl StringEditor
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new StringEditor
with an empty string.
Examples found in repository?
examples/demo.rs (line 11)
10fn main() -> std::io::Result<()> {
11 let mut editor = StringEditor::new();
12 let mut stdout = std::io::stdout();
13
14 println!("Enter text below:");
15 enable_raw_mode()?;
16
17 let input = loop {
18 if event::poll(Duration::from_millis(10))? {
19 let event = event::read()?;
20 if let event::Event::Key(key) = &event {
21 let ctrl = key.modifiers.contains(event::KeyModifiers::CONTROL);
22
23 match key.code {
24 KeyCode::Esc => break None,
25 KeyCode::Char('q') if ctrl => break None,
26 KeyCode::Enter => break Some(editor.get_text()),
27 _ => {}
28 }
29 }
30
31 if let Some(command) = event_to_command(&event) {
32 editor.execute(command);
33 }
34 }
35
36 execute!(
37 stdout,
38 cursor::MoveToColumn(0),
39 terminal::Clear(terminal::ClearType::CurrentLine)
40 )?;
41 print!("{}", editor.get_text());
42 execute!(stdout, cursor::MoveToColumn(editor.cursor_pos() as u16))?;
43 };
44
45 disable_raw_mode()?;
46 println!("\nGot input: {:?}", input);
47
48 Ok(())
49}
Sourcepub fn with_string(text: &str) -> Self
pub fn with_string(text: &str) -> Self
Create a new StringEditor
with a given string. Sets the cursor position to just after the end of the string.
Sourcepub fn get_text(&self) -> &str
pub fn get_text(&self) -> &str
Get the current text of the editor.
Examples found in repository?
examples/demo.rs (line 26)
10fn main() -> std::io::Result<()> {
11 let mut editor = StringEditor::new();
12 let mut stdout = std::io::stdout();
13
14 println!("Enter text below:");
15 enable_raw_mode()?;
16
17 let input = loop {
18 if event::poll(Duration::from_millis(10))? {
19 let event = event::read()?;
20 if let event::Event::Key(key) = &event {
21 let ctrl = key.modifiers.contains(event::KeyModifiers::CONTROL);
22
23 match key.code {
24 KeyCode::Esc => break None,
25 KeyCode::Char('q') if ctrl => break None,
26 KeyCode::Enter => break Some(editor.get_text()),
27 _ => {}
28 }
29 }
30
31 if let Some(command) = event_to_command(&event) {
32 editor.execute(command);
33 }
34 }
35
36 execute!(
37 stdout,
38 cursor::MoveToColumn(0),
39 terminal::Clear(terminal::ClearType::CurrentLine)
40 )?;
41 print!("{}", editor.get_text());
42 execute!(stdout, cursor::MoveToColumn(editor.cursor_pos() as u16))?;
43 };
44
45 disable_raw_mode()?;
46 println!("\nGot input: {:?}", input);
47
48 Ok(())
49}
Sourcepub fn cursor_pos(&self) -> usize
pub fn cursor_pos(&self) -> usize
Get the current cursor position.
Examples found in repository?
examples/demo.rs (line 42)
10fn main() -> std::io::Result<()> {
11 let mut editor = StringEditor::new();
12 let mut stdout = std::io::stdout();
13
14 println!("Enter text below:");
15 enable_raw_mode()?;
16
17 let input = loop {
18 if event::poll(Duration::from_millis(10))? {
19 let event = event::read()?;
20 if let event::Event::Key(key) = &event {
21 let ctrl = key.modifiers.contains(event::KeyModifiers::CONTROL);
22
23 match key.code {
24 KeyCode::Esc => break None,
25 KeyCode::Char('q') if ctrl => break None,
26 KeyCode::Enter => break Some(editor.get_text()),
27 _ => {}
28 }
29 }
30
31 if let Some(command) = event_to_command(&event) {
32 editor.execute(command);
33 }
34 }
35
36 execute!(
37 stdout,
38 cursor::MoveToColumn(0),
39 terminal::Clear(terminal::ClearType::CurrentLine)
40 )?;
41 print!("{}", editor.get_text());
42 execute!(stdout, cursor::MoveToColumn(editor.cursor_pos() as u16))?;
43 };
44
45 disable_raw_mode()?;
46 println!("\nGot input: {:?}", input);
47
48 Ok(())
49}
Source§impl StringEditor
impl StringEditor
Sourcepub fn execute(&mut self, command: Command)
pub fn execute(&mut self, command: Command)
Execute a command on the editor.
Examples found in repository?
examples/demo.rs (line 32)
10fn main() -> std::io::Result<()> {
11 let mut editor = StringEditor::new();
12 let mut stdout = std::io::stdout();
13
14 println!("Enter text below:");
15 enable_raw_mode()?;
16
17 let input = loop {
18 if event::poll(Duration::from_millis(10))? {
19 let event = event::read()?;
20 if let event::Event::Key(key) = &event {
21 let ctrl = key.modifiers.contains(event::KeyModifiers::CONTROL);
22
23 match key.code {
24 KeyCode::Esc => break None,
25 KeyCode::Char('q') if ctrl => break None,
26 KeyCode::Enter => break Some(editor.get_text()),
27 _ => {}
28 }
29 }
30
31 if let Some(command) = event_to_command(&event) {
32 editor.execute(command);
33 }
34 }
35
36 execute!(
37 stdout,
38 cursor::MoveToColumn(0),
39 terminal::Clear(terminal::ClearType::CurrentLine)
40 )?;
41 print!("{}", editor.get_text());
42 execute!(stdout, cursor::MoveToColumn(editor.cursor_pos() as u16))?;
43 };
44
45 disable_raw_mode()?;
46 println!("\nGot input: {:?}", input);
47
48 Ok(())
49}
Trait Implementations§
Source§impl Clone for StringEditor
impl Clone for StringEditor
Source§fn clone(&self) -> StringEditor
fn clone(&self) -> StringEditor
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for StringEditor
impl Debug for StringEditor
Source§impl Default for StringEditor
impl Default for StringEditor
Source§fn default() -> StringEditor
fn default() -> StringEditor
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for StringEditor
impl RefUnwindSafe for StringEditor
impl Send for StringEditor
impl Sync for StringEditor
impl Unpin for StringEditor
impl UnwindSafe for StringEditor
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more