Commit f226d608 authored by Sag0Sag0's avatar Sag0Sag0

Hopefully final changes

parent d6530865
......@@ -17,10 +17,12 @@ name = "liner"
[[bin]]
name = "liner_test"
path = "src/main.rs"
path = "src/examples/main.rs"
[dependencies]
bytecount = "0.3.1"
termion = "1.4.0"
unicode-width = "0.1.*"
[dev-dependencies]
regex = "1.0.0"
......@@ -3,7 +3,6 @@ extern crate termion;
extern crate regex;
use std::mem::replace;
use std::borrow::Cow;
use std::env::{args, current_dir};
use std::io;
......@@ -11,10 +10,10 @@ use liner::{Context, CursorPosition, Event, EventKind, FilenameCompleter};
use termion::color;
use regex::Regex;
fn highlight_dodo(s: &str) -> Cow<str> {
fn highlight_dodo(s: &str) -> String {
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()
reg_exp.replace_all(s, format.as_str()).to_string()
}
fn main() {
......@@ -33,7 +32,7 @@ fn main() {
loop {
let res = con.read_line("[prompt]$ ",
Box::new(highlight_dodo),
Some(Box::new(highlight_dodo)),
&mut |Event { editor, kind }| {
if let EventKind::BeforeComplete = kind {
let (_, pos) = editor.get_words_and_cursor_position();
......
use std::io::{self, stdin, stdout, Stdout, Write};
use std::borrow::Cow;
use termion::input::TermRead;
use termion::raw::{IntoRawMode, RawTerminal};
use super::*;
use keymap;
pub type ColorClosure = Box<Fn(&str) -> String>;
/// The default for `Context.word_divider_fn`.
pub fn get_buffer_words(buf: &Buffer) -> Vec<(usize, usize)> {
let mut res = Vec::new();
......@@ -71,7 +72,7 @@ impl Context {
pub fn read_line<P: Into<String>>(
&mut self,
prompt: P,
f:Box<Fn(&str) -> Cow<str>>,
f: Option<ColorClosure>,
mut handler: &mut EventHandler<RawTerminal<Stdout>>,
) -> io::Result<String> {
self.read_line_with_init_buffer(prompt, handler, f, Buffer::new())
......@@ -85,14 +86,14 @@ impl Context {
/// let line =
/// context.read_line_with_init_buffer("[prompt]$ ",
/// &mut |_| {},
/// Box::new(|s| s.into()),
/// Some(Box::new(|s| String::from(s))),
/// "some initial buffer");
/// ```
pub fn read_line_with_init_buffer<P: Into<String>, B: Into<Buffer>>(
&mut self,
prompt: P,
mut handler: &mut EventHandler<RawTerminal<Stdout>>,
f:Box<Fn(&str) -> Cow<str>>,
f: Option<ColorClosure>,
buffer: B,
) -> io::Result<String> {
let res = {
......
use std::cmp;
use std::io::{self, Write};
use std::borrow::Cow;
use termion::{self, clear, color, cursor};
use context::ColorClosure;
use Context;
use Buffer;
use event::*;
......@@ -63,7 +63,7 @@ pub struct Editor<'a, W: Write> {
// A closure that is evaluated just before we write to out.
// This allows us to do custom syntax highlighting and other fun stuff.
closure: Box<Fn(&str) -> Cow<str>>,
closure: Option<ColorClosure>,
// 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,
......@@ -114,7 +114,7 @@ impl<'a, W: Write> Editor<'a, W> {
pub fn new<P: Into<String>>(
out: W,
prompt: P,
f:Box<Fn(&str) -> Cow<str>>,
f: Option<ColorClosure>,
context: &'a mut Context
) -> io::Result<Self> {
Editor::new_with_init_buffer(out, prompt, f, context, Buffer::new())
......@@ -123,7 +123,7 @@ impl<'a, W: Write> Editor<'a, W> {
pub fn new_with_init_buffer<P: Into<String>, B: Into<Buffer>>(
out: W,
prompt: P,
f: Box<Fn(&str) -> Cow<str>>,
f: Option<ColorClosure>,
context: &'a mut Context,
buffer: B,
) -> io::Result<Self> {
......@@ -737,7 +737,10 @@ impl<'a, W: Write> Editor<'a, W> {
buf_num_remaining_bytes = 0;
} else {
buf_num_remaining_bytes -= line.len();
write!(self.out, "{}", (self.closure)(line))?;
match self.closure {
Some(ref f) => write!(self.out, "{}", f(line))?,
None => write!(self.out, "{}", line)?,
}
}
if i + 1 < lines.len() {
......@@ -808,7 +811,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(), Box::new(|s| s.into()), &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &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();
......@@ -821,7 +824,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(), Box::new(|s| s.into()), &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &mut context).unwrap();
ed.insert_str_after_cursor("let").unwrap();
assert_eq!(ed.cursor, 3);
......@@ -837,7 +840,7 @@ mod tests {
fn cursor_movement() {
let mut context = Context::new();
let out = Vec::new();
let mut ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &mut context).unwrap();
ed.insert_str_after_cursor("right").unwrap();
assert_eq!(ed.cursor, 5);
......@@ -850,7 +853,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(), Box::new(|s| s.into()), &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &mut context).unwrap();
ed.insert_str_after_cursor("right").unwrap();
assert_eq!(ed.cursor, 5);
......@@ -863,7 +866,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(), Box::new(|s| s.into()), &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &mut context).unwrap();
ed.insert_str_after_cursor("right").unwrap();
ed.cursor = 0;
......@@ -876,7 +879,7 @@ mod tests {
fn delete_until() {
let mut context = Context::new();
let out = Vec::new();
let mut ed = Editor::new(out, "prompt".to_owned(), Box::new(|s| s.into()), &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &mut context).unwrap();
ed.insert_str_after_cursor("right").unwrap();
ed.cursor = 4;
......@@ -889,7 +892,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(), Box::new(|s| s.into()), &mut context).unwrap();
let mut ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &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(), Box::new(|s| s.into()), &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &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(), Box::new(|s| s.into()), &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &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(), Box::new(|s| s.into()), &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &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(), Box::new(|s| s.into()), &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &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(), Box::new(|s| s.into()), &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &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(), Box::new(|s| s.into()), &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &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(), Box::new(|s| s.into()), &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &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(), Box::new(|s| s.into()), &mut context).unwrap();
let ed = Editor::new(out, "prompt".to_owned(), Some(Box::new(|s| String::from(s))), &mut context).unwrap();
let mut map = TestKeyMap::new(ed);
let res = map.handle_key(Ctrl('c'), &mut |_| {});
......
This diff is collapsed.
......@@ -80,4 +80,4 @@ pub fn remove_codes(input: &str) -> Cow<str> {
} else {
Cow::Borrowed(input)
}
}
}
\ 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