brush_core/shell/
readline.rs1use crate::{error, extensions, variables::ShellVariable};
4
5impl<SE: extensions::ShellExtensions> crate::Shell<SE> {
6 pub fn set_edit_buffer(&mut self, contents: String, cursor: usize) -> Result<(), error::Error> {
13 self.env
14 .set_global("READLINE_LINE", ShellVariable::new(contents))?;
15
16 self.env
17 .set_global("READLINE_POINT", ShellVariable::new(cursor.to_string()))?;
18
19 Ok(())
20 }
21
22 pub fn pop_edit_buffer(&mut self) -> Result<Option<(String, usize)>, error::Error> {
25 let line = self
26 .env
27 .unset("READLINE_LINE")?
28 .map(|line| line.value().to_cow_str(self).to_string());
29
30 let point = self
31 .env
32 .unset("READLINE_POINT")?
33 .and_then(|point| point.value().to_cow_str(self).parse::<usize>().ok())
34 .unwrap_or(0);
35
36 if let Some(line) = line {
37 Ok(Some((line, point)))
38 } else {
39 Ok(None)
40 }
41 }
42}