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