Commit 40cbc86d authored by stratact's avatar stratact

Rip apart History and refactor the API

parent 82c1715f
......@@ -20,17 +20,18 @@ fn highlight_dodo(s: &str) -> String {
fn main() {
let mut con = Context::new();
let history_file = args().nth(1);
match history_file {
Some(ref file_name) => println!("History file: {}", file_name),
None => println!("No history file"),
}
let history_file = match args().nth(1) {
Some(file_name) => {
println!("History file: {}", file_name);
file_name
}
None => {
eprintln!("No history file provided. Ending example early.");
return;
}
};
con.history.set_file_name(history_file);
// We set the file name, then check if we set it, and if we set it properly, we load it in
if con.history.file_name().is_some() {
con.history.load_history().unwrap();
}
con.history.set_file_name_and_load_history(history_file).unwrap();
loop {
// Reads the line, the first arg is the prompt, the second arg is a function called on every bit of text leaving liner, and the third is called on every key press
......@@ -94,7 +95,7 @@ fn main() {
_ => {}
}
// If we typed nothing, don't continue down to pushing to history
// If we typed nothing, don't continue down to pushing to history
if res.is_empty() {
break;
}
......@@ -115,7 +116,6 @@ fn main() {
_ => {
// Ensure that all writes to the history file
// are written before exiting due to error.
con.history.commit_history();
panic!("error: {:?}", e)
},
}
......@@ -126,5 +126,5 @@ fn main() {
}
// Ensure that all writes to the history file are written before exiting.
con.history.commit_history();
con.history.commit_to_file();
}
......@@ -19,16 +19,18 @@ fn highlight_dodo(s: &str) -> String {
fn main() {
let mut con = Context::new();
let history_file = args().nth(1);
match history_file {
Some(ref file_name) => println!("History file: {}", file_name),
None => println!("No history file"),
}
let history_file = match args().nth(1) {
Some(file_name) => {
println!("History file: {}", file_name);
file_name
}
None => {
eprintln!("No history file provided. Ending example early.");
return;
}
};
con.history.set_file_name(history_file);
if con.history.file_name().is_some() {
con.history.load_history().unwrap();
}
con.history.set_file_name_and_load_history(history_file).unwrap();
loop {
let res = con.read_line("[prompt]$ ",
......@@ -91,15 +93,12 @@ fn main() {
_ => {
// Ensure that all writes to the history file
// are written before exiting.
con.history.commit_history();
panic!("error: {:?}", e)
},
}
}
}
}
// Ensure that all writes to the history file are written before exiting.
con.history.commit_history();
}
\ No newline at end of file
con.history.commit_to_file();
}
......@@ -73,7 +73,7 @@ impl Context {
&mut self,
prompt: P,
f: Option<ColorClosure>,
mut handler: &mut EventHandler<RawTerminal<Stdout>>,
handler: &mut EventHandler<RawTerminal<Stdout>>,
) -> io::Result<String> {
self.read_line_with_init_buffer(prompt, handler, f, Buffer::new())
}
......@@ -92,7 +92,7 @@ impl Context {
pub fn read_line_with_init_buffer<P: Into<String>, B: Into<Buffer>>(
&mut self,
prompt: P,
mut handler: &mut EventHandler<RawTerminal<Stdout>>,
handler: &mut EventHandler<RawTerminal<Stdout>>,
f: Option<ColorClosure>,
buffer: B,
) -> io::Result<String> {
......@@ -105,13 +105,13 @@ impl Context {
}
};
self.revert_all_history();
//self.revert_all_history();
res
}
fn handle_keys<'a, T, W: Write, M: KeyMap<'a, W, T>>(
mut keymap: M,
mut handler: &mut EventHandler<W>,
handler: &mut EventHandler<W>,
) -> io::Result<String>
where
String: From<M>,
......
......@@ -112,8 +112,8 @@ macro_rules! cur_buf {
impl<'a, W: Write> Editor<'a, W> {
pub fn new<P: Into<String>>(
out: W,
prompt: P,
out: W,
prompt: P,
f: Option<ColorClosure>,
context: &'a mut Context
) -> io::Result<Self> {
......@@ -601,7 +601,7 @@ impl<'a, W: Write> Editor<'a, W> {
if self.show_autosuggestions {
{
let autosuggestion = self.current_autosuggestion().cloned();
let mut buf = self.current_buffer_mut();
let buf = self.current_buffer_mut();
if let Some(x) = autosuggestion {
buf.insert_from_buffer(&x);
}
......
This diff is collapsed.
......@@ -74,12 +74,12 @@ fn test_history_indexing() {
#[test]
fn test_in_memory_history_truncating() {
let mut h = History::new();
h.set_max_size(2);
h.set_max_buffers_size(2);
for _ in 0..4 {
h.push(Buffer::from("a")).unwrap();
h.push(Buffer::from("b")).unwrap();
}
h.commit_history();
h.commit_to_file();
assert_eq!(h.len(), 2);
}
......@@ -90,15 +90,15 @@ fn test_in_file_history_truncating() {
{
let mut h = History::new();
h.set_file_name(Some(String::from(tmp_file.to_string_lossy().into_owned())));
let _ = h.set_file_name_and_load_history(&tmp_file).unwrap();
h.set_max_file_size(5);
for _ in 0..20 {
h.push(Buffer::from("a")).unwrap();
for bytes in b'a'..b'z' {
h.push(Buffer::from(format!("{}", bytes as char))).unwrap();
}
h.commit_history();
h.commit_to_file();
}
let f = fs::File::open(tmp_file.clone()).unwrap();
let f = fs::File::open(&tmp_file).unwrap();
let r = BufReader::new(f);
let count = r.lines().count();
assert_eq!(count, 5);
......@@ -126,8 +126,7 @@ fn test_reading_from_file() {
f.write_all(TEXT.as_bytes()).unwrap();
}
let mut h = History::new();
h.set_file_name(Some(String::from(tmp_file.to_string_lossy().into_owned())));
let _ = h.load_history();
h.set_file_name_and_load_history(tmp_file);
assert_eq!(String::from(h.buffers[0].clone()), "a".to_string());
assert_eq!(String::from(h.buffers[1].clone()), "b".to_string());
assert_eq!(String::from(h.buffers[2].clone()), "c".to_string());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment