Commit 36780114 authored by Sag0Sag0's avatar Sag0Sag0

Attempted to work on Cow stuff

parent d65b41f1
use std::io::{self, stdin, stdout, Stdout, Write};
use std::borrow::Cow;
use termion::input::TermRead;
use termion::raw::{IntoRawMode, RawTerminal};
......@@ -67,13 +68,13 @@ 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<P: Into<String>, F: 'static>(
pub fn read_line<'a, P: Into<String>, F: 'static>(
&mut self,
prompt: P,
f: F,
mut handler: &mut EventHandler<RawTerminal<Stdout>>,
) -> io::Result<String>
where for<'r> F: (Fn(&'r str,) -> String) {
where F: Fn(&'a str) -> Cow<'a, str> {
self.read_line_with_init_buffer(prompt, handler, f, Buffer::new())
}
......@@ -88,14 +89,14 @@ impl Context {
/// |s| {String::from(s)},
/// "some initial buffer");
/// ```
pub fn read_line_with_init_buffer<P: Into<String>, B: Into<Buffer>, F: 'static>(
pub fn read_line_with_init_buffer<'a, P: Into<String>, B: Into<Buffer>, F: 'static>(
&mut self,
prompt: P,
mut handler: &mut EventHandler<RawTerminal<Stdout>>,
f: F,
buffer: B,
) -> io::Result<String>
where for<'r> F: Fn(&'r str) -> String + 'static {
where F: Fn(&'a str) -> Cow<'a, str> {
let res = {
let stdout = stdout().into_raw_mode().unwrap();
let ed = try!(Editor::new_with_init_buffer(stdout, prompt, f, self, buffer));
......
use std::cmp;
use std::io::{self, Write};
use std::borrow::Cow;
use termion::{self, clear, color, cursor};
use Context;
......@@ -59,7 +60,10 @@ pub struct Editor<'a, W: Write> {
prompt: String,
out: W,
context: &'a mut Context,
closure: Box<Fn(&str) -> String>,
// 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>>,
// 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,
......@@ -106,25 +110,31 @@ macro_rules! cur_buf {
}
}
impl<'a, 'b, W: Write> Editor<'a, W> {
pub fn new<P: Into<String>, F: 'static>(out: W, prompt: P, f: F, context: &'a mut Context) -> io::Result<Self>
where for<'r> F: Fn(&'r str) -> String + 'static {
impl<'a, W: Write> Editor<'a, W> {
//pub fn echo<>
pub fn new<P: Into<String>, F>(
out: W,
prompt: P,
f: F,
context: &'a mut Context
) -> io::Result<Self>
where F: Fn(&'a str) -> Cow<'a, str> {
Editor::new_with_init_buffer(out, prompt, f, context, Buffer::new())
}
pub fn new_with_init_buffer<P: Into<String>, B: Into<Buffer>, F: 'static>(
pub fn new_with_init_buffer<P: Into<String>, B: Into<Buffer>, F>(
out: W,
prompt: P,
f: F,
context: &'a mut Context,
buffer: B,
) -> io::Result<Self>
where for<'r> F: Fn(&'r str) -> String + 'static {
where F: Into<Fn(&'a str) -> Cow<'a, str>> {
let mut ed = Editor {
prompt: prompt.into(),
cursor: 0,
out: out,
closure: Box::new(f),
closure: Box::new(f.into()),
new_buf: buffer.into(),
cur_history_loc: None,
context: context,
......@@ -771,7 +781,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| {String::from(s)}, &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), |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();
......@@ -782,8 +792,9 @@ mod tests {
#[test]
fn move_cursor_left() {
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(), |s| {String::from(s)}, &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), closure, &mut context).unwrap();
ed.insert_str_after_cursor("let").unwrap();
assert_eq!(ed.cursor, 3);
......
......@@ -3,6 +3,7 @@ extern crate termion;
extern crate regex;
use std::mem::replace;
use std::borrow::Cow;
use std::env::{args, current_dir};
use std::io;
......@@ -10,13 +11,13 @@ use liner::{Context, CursorPosition, Event, EventKind, FilenameCompleter};
use termion::color;
use regex::Regex;
fn highlight_dodo(s: &str) -> String {
fn highlight_dodo(s: &'static str) -> Cow<'a, 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()).to_string()
reg_exp.replace_all(s, format.as_str()).into()
}
fn main() {
fn main<'a>() {
let mut con = Context::new();
let history_file = args().nth(1);
......@@ -30,8 +31,10 @@ fn main() {
con.history.load_history().unwrap();
}
let h = |s: &str| highlight_dodo(s);
loop {
let res = con.read_line("[prompt]$ ",
let res = con.read_line("[prompt]$ ",
highlight_dodo,
&mut |Event { editor, kind }| {
if let EventKind::BeforeComplete = kind {
......@@ -102,4 +105,4 @@ fn main() {
// Ensure that all writes to the history file are written before exiting.
con.history.commit_history();
}
}
\ No newline at end of file
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