Commit 23992c25 authored by Sag0Sag0's avatar Sag0Sag0

Fixed problems involving returning Cow<>s

parent 36780114
......@@ -68,13 +68,12 @@ impl Context {
/// The output is stdout.
/// The returned line has the newline removed.
/// Before returning, will revert all changes to the history buffers.
pub fn read_line<'a, P: Into<String>, F: 'static>(
pub fn read_line<'a, P: Into<String>>(
&mut self,
prompt: P,
f: F,
f: Box<for<'r> Fn(&'r str) -> Cow<'_, str>>,
mut handler: &mut EventHandler<RawTerminal<Stdout>>,
) -> io::Result<String>
where F: Fn(&'a str) -> Cow<'a, str> {
) -> io::Result<String> {
self.read_line_with_init_buffer(prompt, handler, f, Buffer::new())
}
......@@ -86,17 +85,16 @@ impl Context {
/// let line =
/// context.read_line_with_init_buffer("[prompt]$ ",
/// &mut |_| {},
/// |s| {String::from(s)},
/// Box::new(|s| s.into()),
/// "some initial buffer");
/// ```
pub fn read_line_with_init_buffer<'a, P: Into<String>, B: Into<Buffer>, F: 'static>(
pub fn read_line_with_init_buffer<'a, P: Into<String>, B: Into<Buffer>>(
&mut self,
prompt: P,
mut handler: &mut EventHandler<RawTerminal<Stdout>>,
f: F,
f: Box<for<'r> Fn(&'r str) -> Cow<'_, str>>,
buffer: B,
) -> io::Result<String>
where F: Fn(&'a str) -> Cow<'a, str> {
) -> io::Result<String> {
let res = {
let stdout = stdout().into_raw_mode().unwrap();
let ed = try!(Editor::new_with_init_buffer(stdout, prompt, f, self, buffer));
......
......@@ -62,8 +62,8 @@ pub struct Editor<'a, W: Write> {
context: &'a mut Context,
// A closure that is evaluated just before we write to out.
// This allows us to do custom syntax highlighting.
closure: Box<Fn(&'a str) -> Cow<'a, str>>,
// This allows us to do custom syntax highlighting and other fun stuff.
closure: Box<for<'r> Fn(&'r str) -> Cow<'_, str>>,
// The location of the cursor. Note that the cursor does not lie on a char, but between chars.
// So, if `cursor == 0` then the cursor is before the first char,
......@@ -110,31 +110,28 @@ macro_rules! cur_buf {
}
}
impl<'a, W: Write> Editor<'a, W> {
//pub fn echo<>
pub fn new<P: Into<String>, F>(
impl<'a, 'b, W: Write> Editor<'a, W> {
pub fn new<P: Into<String>>(
out: W,
prompt: P,
f: F,
f: Box<for<'r> Fn(&'r str) -> Cow<'_, str>>,
context: &'a mut Context
) -> io::Result<Self>
where F: Fn(&'a str) -> Cow<'a, str> {
) -> io::Result<Self> {
Editor::new_with_init_buffer(out, prompt, f, context, Buffer::new())
}
pub fn new_with_init_buffer<P: Into<String>, B: Into<Buffer>, F>(
pub fn new_with_init_buffer<P: Into<String>, B: Into<Buffer>>(
out: W,
prompt: P,
f: F,
f: Box<for<'r> Fn(&'r str) -> Cow<'_, str>>,
context: &'a mut Context,
buffer: B,
) -> io::Result<Self>
where F: Into<Fn(&'a str) -> Cow<'a, str>> {
) -> io::Result<Self> {
let mut ed = Editor {
prompt: prompt.into(),
cursor: 0,
out: out,
closure: Box::new(f.into()),
closure: f,
new_buf: buffer.into(),
cur_history_loc: None,
context: context,
......@@ -781,7 +778,7 @@ mod tests {
fn delete_all_after_cursor_undo() {
let mut context = Context::new();
let out = Vec::new();
let mut ed = Editor::new(out, "prompt".to_owned(), |s| {s.into()}, &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
ed.insert_str_after_cursor("delete all of this").unwrap();
ed.move_cursor_to_start_of_line().unwrap();
ed.delete_all_after_cursor().unwrap();
......@@ -794,7 +791,7 @@ mod tests {
let mut context = Context::new();
let closure = |s: &str| {String::from(s)};
let out = Vec::new();
let mut ed = Editor::new(out, "prompt".to_owned(), closure, &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
ed.insert_str_after_cursor("let").unwrap();
assert_eq!(ed.cursor, 3);
......@@ -810,7 +807,7 @@ mod tests {
fn cursor_movement() {
let mut context = Context::new();
let out = Vec::new();
let mut ed = Editor::new(out, "prompt".to_owned(), |s| {String::from(s)}, &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
ed.insert_str_after_cursor("right").unwrap();
assert_eq!(ed.cursor, 5);
......@@ -823,7 +820,7 @@ mod tests {
fn delete_until_backwards() {
let mut context = Context::new();
let out = Vec::new();
let mut ed = Editor::new(out, "prompt".to_owned(), |s| {String::from(s)}, &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
ed.insert_str_after_cursor("right").unwrap();
assert_eq!(ed.cursor, 5);
......@@ -836,7 +833,7 @@ mod tests {
fn delete_until_forwards() {
let mut context = Context::new();
let out = Vec::new();
let mut ed = Editor::new(out, "prompt".to_owned(), |s| {String::from(s)}, &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
ed.insert_str_after_cursor("right").unwrap();
ed.cursor = 0;
......@@ -849,7 +846,7 @@ mod tests {
fn delete_until() {
let mut context = Context::new();
let out = Vec::new();
let mut ed = Editor::new(out, "prompt".to_owned(), |s| {String::from(s)}, &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
ed.insert_str_after_cursor("right").unwrap();
ed.cursor = 4;
......@@ -862,7 +859,7 @@ mod tests {
fn delete_until_inclusive() {
let mut context = Context::new();
let out = Vec::new();
let mut ed = Editor::new(out, "prompt".to_owned(), |s| {String::from(s)}, &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
ed.insert_str_after_cursor("right").unwrap();
ed.cursor = 4;
......
......@@ -211,7 +211,7 @@ mod tests {
fn enter_is_done() {
let mut context = Context::new();
let out = Vec::new();
let ed = Editor::new(out, "prompt".to_owned(), |s| {String::from(s)}, &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
let mut map = Emacs::new(ed);
map.ed.insert_str_after_cursor("done").unwrap();
assert_eq!(map.ed.cursor(), 4);
......@@ -226,7 +226,7 @@ mod tests {
fn move_cursor_left() {
let mut context = Context::new();
let out = Vec::new();
let ed = Editor::new(out, "prompt".to_owned(), |s| {String::from(s)}, &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
let mut map = Emacs::new(ed);
map.editor_mut().insert_str_after_cursor("let").unwrap();
assert_eq!(map.ed.cursor(), 3);
......@@ -241,7 +241,7 @@ mod tests {
fn move_word() {
let mut context = Context::new();
let out = Vec::new();
let ed = Editor::new(out, "prompt".to_owned(), |s| {String::from(s)}, &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
let mut map = Emacs::new(ed);
map.editor_mut().insert_str_after_cursor("abc def ghi").unwrap();
assert_eq!(map.ed.cursor(), 11);
......@@ -261,7 +261,7 @@ mod tests {
fn cursor_movement() {
let mut context = Context::new();
let out = Vec::new();
let ed = Editor::new(out, "prompt".to_owned(), |s| {String::from(s)}, &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
let mut map = Emacs::new(ed);
map.ed.insert_str_after_cursor("right").unwrap();
assert_eq!(map.ed.cursor(), 5);
......@@ -276,7 +276,7 @@ mod tests {
fn ctrl_h() {
let mut context = Context::new();
let out = Vec::new();
let ed = Editor::new(out, "prompt".to_owned(), |s| {String::from(s)}, &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
let mut map = Emacs::new(ed);
map.ed.insert_str_after_cursor("not empty").unwrap();
......
......@@ -99,7 +99,7 @@ mod tests {
fn ctrl_d_empty() {
let mut context = Context::new();
let out = Vec::new();
let ed = Editor::new(out, "prompt".to_owned(), |s| {String::from(s)}, &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
let mut map = TestKeyMap::new(ed);
let res = map.handle_key(Ctrl('d'), &mut |_| {});
......@@ -112,7 +112,7 @@ mod tests {
fn ctrl_d_non_empty() {
let mut context = Context::new();
let out = Vec::new();
let ed = Editor::new(out, "prompt".to_owned(), |s| {String::from(s)}, &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
let mut map = TestKeyMap::new(ed);
map.ed.insert_str_after_cursor("not empty").unwrap();
......@@ -125,7 +125,7 @@ mod tests {
fn ctrl_c() {
let mut context = Context::new();
let out = Vec::new();
let ed = Editor::new(out, "prompt".to_owned(), |s| {String::from(s)}, &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
let mut map = TestKeyMap::new(ed);
let res = map.handle_key(Ctrl('c'), &mut |_| {});
......
This diff is collapsed.
......@@ -11,13 +11,13 @@ use liner::{Context, CursorPosition, Event, EventKind, FilenameCompleter};
use termion::color;
use regex::Regex;
fn highlight_dodo(s: &'static str) -> Cow<'a, str> {
fn highlight_dodo<'r>(s: &'r str) -> Cow<'_, str> {
let reg_exp = Regex::new("(?P<k>dodo)").unwrap();
let format = format!("{}$k{}", color::Fg(color::Red), color::Fg(color::Reset));
reg_exp.replace_all(s, format.as_str()).into()
}
fn main<'a>() {
fn main() {
let mut con = Context::new();
let history_file = args().nth(1);
......@@ -31,11 +31,9 @@ fn main<'a>() {
con.history.load_history().unwrap();
}
let h = |s: &str| highlight_dodo(s);
loop {
let res = con.read_line("[prompt]$ ",
highlight_dodo,
Box::new(highlight_dodo),
&mut |Event { editor, kind }| {
if let EventKind::BeforeComplete = kind {
let (_, pos) = editor.get_words_and_cursor_position();
......
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