Commit 1745f757 authored by Matthew Nicholson's avatar Matthew Nicholson
Browse files

update the example code with ctrl-c and ctrl-d handling

parent 30e07782
......@@ -2,29 +2,46 @@ extern crate liner;
use liner::Context;
use liner::KeyBindings;
use std::io::ErrorKind;
fn main() {
let mut con = Context::new();
loop {
let res = con.read_line("[prompt]$ ", &mut |_| {}).unwrap();
let res = con.read_line("[prompt]$ ", &mut |_| {});
if res.is_empty() {
break;
}
match res {
Ok(res) => {
match res.as_str() {
"emacs" => {
con.key_bindings = KeyBindings::Emacs;
println!("emacs mode");
}
"vi" => {
con.key_bindings = KeyBindings::Vi;
println!("vi mode");
}
"exit" => {
println!("exiting...");
break;
}
_ => {}
}
match res.as_str() {
"emacs" => {
con.key_bindings = KeyBindings::Emacs;
println!("emacs mode");
con.history.push(res.into()).unwrap();
}
"vi" => {
con.key_bindings = KeyBindings::Vi;
println!("vi mode");
Err(e) => {
match e.kind() {
// ctrl-c pressed
ErrorKind::Interrupted => {}
// ctrl-d pressed
ErrorKind::UnexpectedEof => {
println!("exiting...");
break;
}
_ => panic!("error: {:?}", e),
}
}
_ => {}
}
con.history.push(res.into()).unwrap();
}
}
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