Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfixes/consistency improvements for multiline stuff #334

Merged
merged 4 commits into from Mar 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions examples/example.rs
Expand Up @@ -5,19 +5,22 @@ use rustyline::config::OutputStreamType;
use rustyline::error::ReadlineError;
use rustyline::highlight::{Highlighter, MatchingBracketHighlighter};
use rustyline::highlight::{PromptInfo};
use rustyline::validate::{self, Validator, MatchingBracketValidator};
use rustyline::hint::{Hinter, HistoryHinter};
use rustyline::{Cmd, CompletionType, Config, Context, EditMode, Editor, KeyPress};
use rustyline_derive::{Helper, Validator};
use rustyline_derive::Helper;

#[derive(Helper, Validator)]
#[derive(Helper)]
struct MyHelper {
completer: FilenameCompleter,
highlighter: MatchingBracketHighlighter,
validator: MatchingBracketValidator,
hinter: HistoryHinter,
colored_prompt: String,
continuation_prompt: String,
}


impl Completer for MyHelper {
type Candidate = Pair;

Expand Down Expand Up @@ -71,6 +74,16 @@ impl Highlighter for MyHelper {
}
}

impl Validator for MyHelper {
fn validate(&self, ctx: &mut validate::ValidationContext) -> rustyline::Result<validate::ValidationResult> {
self.validator.validate(ctx)
}

fn validate_while_typing(&self) -> bool {
self.validator.validate_while_typing()
}
}

// To debug rustyline:
// RUST_LOG=rustyline=debug cargo run --example example 2> debug.log
fn main() -> rustyline::Result<()> {
Expand All @@ -87,6 +100,7 @@ fn main() -> rustyline::Result<()> {
hinter: HistoryHinter {},
colored_prompt: " 0> ".to_owned(),
continuation_prompt: "\x1b[1;32m...> \x1b[0m".to_owned(),
validator: MatchingBracketValidator::new(),
};
let mut rl = Editor::with_config(config);
rl.set_helper(Some(h));
Expand Down
18 changes: 18 additions & 0 deletions src/edit.rs
Expand Up @@ -451,6 +451,24 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> {
}
}

/// Move cursor to the start of the buffer.
pub fn edit_move_buffer_start(&mut self) -> Result<()> {
if self.line.move_buffer_start() {
self.move_cursor()
} else {
Ok(())
}
}

/// Move cursor to the end of the buffer.
pub fn edit_move_buffer_end(&mut self) -> Result<()> {
if self.line.move_buffer_end() {
self.move_cursor()
} else {
Ok(())
}
}

pub fn edit_kill(&mut self, mvt: &Movement) -> Result<()> {
if self.line.kill(mvt) {
self.refresh_line()
Expand Down
60 changes: 57 additions & 3 deletions src/history.rs
Expand Up @@ -110,6 +110,10 @@ impl History {
}
}

// New multiline-aware history files start with `#V2\n` and have newlines
// and backslashes escaped in them.
const FILE_VERSION_V2: &'static str = "#V2";

/// Save the history in the specified file.
// TODO append_history
// http://cnswww.cns.cwru.edu/php/chet/readline/history.html#IDX30
Expand All @@ -127,10 +131,12 @@ impl History {
let file = f?;
fix_perm(&file);
let mut wtr = BufWriter::new(file);
wtr.write_all(Self::FILE_VERSION_V2.as_bytes())?;
for entry in &self.entries {
wtr.write_all(entry.as_bytes())?;
wtr.write_all(b"\n")?;
wtr.write_all(entry.replace('\\', "\\\\").replace('\n', "\\n").as_bytes())?;
}
wtr.write_all(b"\n")?;
// https://github.com/rust-lang/rust/issues/32677#issuecomment-204833485
wtr.flush()?;
Ok(())
Expand All @@ -145,8 +151,26 @@ impl History {

let file = File::open(&path)?;
let rdr = BufReader::new(file);
for line in rdr.lines() {
self.add(line?); // TODO truncate to MAX_LINE
let mut lines = rdr.lines();
let mut v2 = false;
if let Some(first) = lines.next() {
let line = first?;
if line == Self::FILE_VERSION_V2 {
v2 = true;
} else {
self.add(line);
}
}
for line in lines {
let line = if v2 {
line?.replace("\\n", "\n").replace("\\\\", "\\")
} else {
line?
};
if line.is_empty() {
continue;
}
self.add(line); // TODO truncate to MAX_LINE
}
Ok(())
}
Expand Down Expand Up @@ -315,11 +339,41 @@ mod tests {
#[test]
fn save() {
let mut history = init();
assert!(history.add("line\nfour \\ abc"));
let td = tempdir::TempDir::new_in(&Path::new("."), "histo").unwrap();
let history_path = td.path().join(".history");

history.save(&history_path).unwrap();
let mut history2 = History::new();
history2.load(&history_path).unwrap();
for (a, b) in history.entries.iter().zip(history2.entries.iter()) {
assert_eq!(a, b);
}

td.close().unwrap();
}

#[test]
fn load_legacy() {
use std::io::Write;
let td = tempdir::TempDir::new_in(&Path::new("."), "histo").unwrap();
let history_path = td.path().join(".history_v1");
{
let mut legacy = std::fs::File::create(&history_path).unwrap();
// Some data we'd accidentally corrupt if we got the version wrong
let data = b"\
test\\n \\abc \\123\n\
123\\n\\\\n\n\
abcde
";
legacy.write_all(data).unwrap();
legacy.flush().unwrap();
}
let mut history = History::new();
history.load(&history_path).unwrap();
assert_eq!(history.entries[0], "test\\n \\abc \\123");
assert_eq!(history.entries[1], "123\\n\\\\n");
assert_eq!(history.entries[2], "abcde");
td.close().unwrap();
}

Expand Down
8 changes: 8 additions & 0 deletions src/keymap.rs
Expand Up @@ -255,6 +255,11 @@ pub enum Movement {
LineUp(RepeatCount),
/// move to the same column on the next line
LineDown(RepeatCount),
WholeBuffer, // not really a movement
/// beginning-of-buffer
BeginningOfBuffer,
/// end-of-buffer
EndOfBuffer,
}

impl Movement {
Expand All @@ -278,6 +283,9 @@ impl Movement {
Movement::ForwardChar(previous) => Movement::ForwardChar(repeat_count(previous, new)),
Movement::LineUp(previous) => Movement::LineUp(repeat_count(previous, new)),
Movement::LineDown(previous) => Movement::LineDown(repeat_count(previous, new)),
Movement::WholeBuffer => Movement::WholeBuffer,
Movement::BeginningOfBuffer => Movement::BeginningOfBuffer,
Movement::EndOfBuffer => Movement::EndOfBuffer,
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Expand Up @@ -620,6 +620,14 @@ fn readline_edit<H: Helper>(
Cmd::Move(Movement::LineDown(n)) => {
s.edit_move_line_down(n)?;
}
Cmd::Move(Movement::BeginningOfBuffer) => {
// Move to the start of the buffer.
s.edit_move_buffer_start()?
}
Cmd::Move(Movement::EndOfBuffer) => {
// Move to the end of the buffer.
s.edit_move_buffer_end()?
}
Cmd::DowncaseWord => {
// lowercase word after point
s.edit_word(WordAction::LOWERCASE)?
Expand Down