Save history in localStorage.
This commit is contained in:
parent
8816d76c53
commit
f79d092c5c
@ -1,5 +1,8 @@
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct History {
|
||||
pub entries: VecDeque<String>,
|
||||
pub max_size: usize,
|
||||
|
@ -490,9 +490,20 @@ impl Write for WritetermFunction {
|
||||
|
||||
impl Readline {
|
||||
pub fn new(prompt: String, write_fn: Box<dyn FnMut(&[u8])>, term_size: (u16, u16)) -> Self {
|
||||
let mut line_state = LineState::new(prompt, term_size);
|
||||
if let Some(existing_history) = web_sys::window().and_then(|w| {
|
||||
w.local_storage()
|
||||
.unwrap_or(None)
|
||||
.and_then(|ls| ls.get("term_history").unwrap_or(None))
|
||||
}) {
|
||||
if let Ok(history) = serde_json::from_str(&existing_history) {
|
||||
line_state.history = history;
|
||||
}
|
||||
}
|
||||
|
||||
Self {
|
||||
term: TerminalState::new(),
|
||||
line_state: LineState::new(prompt, term_size),
|
||||
line_state,
|
||||
write_term: WritetermFunction { f: write_fn },
|
||||
}
|
||||
}
|
||||
@ -508,9 +519,21 @@ impl Readline {
|
||||
pub fn readline(&mut self, input: &[u8]) -> Result<Vec<ReadlineEvent>, ReadlineError> {
|
||||
let evs = self.term.events_for_input(input)?;
|
||||
let mut results: Vec<ReadlineEvent> = Vec::new();
|
||||
|
||||
for ev in evs {
|
||||
results.extend(self.line_state.handle_event(ev, &mut self.write_term)?);
|
||||
}
|
||||
|
||||
if !results.is_empty() {
|
||||
if let Ok(json) = serde_json::to_string(&self.line_state.history) {
|
||||
web_sys::window().and_then(|w| {
|
||||
w.local_storage()
|
||||
.unwrap_or(None)
|
||||
.map(|ls| ls.set("term_history", &json))
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Ok(results)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user