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

append_history adds blank line? #469

Closed
gillespiecd opened this issue Dec 4, 2020 · 2 comments · Fixed by #470
Closed

append_history adds blank line? #469

gillespiecd opened this issue Dec 4, 2020 · 2 comments · Fixed by #470
Labels

Comments

@gillespiecd
Copy link

gillespiecd commented Dec 4, 2020

If I use append_history after adding an entry, it seems to add an extra blank line after the entry, is this expected behavior?

I was hoping to use this instead of save_history in order to save on performance (my understanding is that it appends to the file instead of writing out a whole new one).

Code ran:

use rustyline::error::ReadlineError;
use rustyline::Editor;
use std::fs::File;
use std::io::{BufRead, BufReader};

fn main() -> Result<(), ReadlineError> {
    let mut rl = Editor::<()>::new();
    let path: &str = "history.txt";

    if rl.load_history(&path).is_err() {
        println!("No previous history.");
    }

    loop {
        match rl.readline("> ") {
            Ok(line) if line == "clear" => {
                let _ = File::create(&path);
                rl.clear_history();
            }
            Ok(line) if line == "exit" => {
                break;
            }
            Ok(line) if line == "history" => {
                let f = File::open(&path)?;
                for line in BufReader::new(f).lines().skip(1) {
                    println!("{}", line?);
                }
            }
            Ok(line) => {
                rl.add_history_entry(&line);
                let _ = rl.append_history(&path);
                println!("Line: {}", line);
            }
            Err(ReadlineError::Interrupted) => {
                println!("CTRL-C");
                break;
            }
            Err(ReadlineError::Eof) => {
                println!("CTRL-D");
                break;
            }
            Err(err) => {
                println!("Error: {:?}", err);
                break;
            }
        }
    }

    let _ = rl.save_history(path)?;

    Ok(())
}

File output after running a few lines, via bat history.txt:

───────┬────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: history.txt
───────┼────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ #V2
   2   │ a
   3   │
   4   │ b
   5   │
   6   │ c

@gwenn gwenn added the bug label Dec 4, 2020
gwenn added a commit to gwenn/rustyline that referenced this issue Dec 4, 2020
Do not generate blank line.
Fix kkawakam#469
@gwenn
Copy link
Collaborator

gwenn commented Dec 4, 2020

Thanks for the detailed bug report.
See #470

@gwenn gwenn closed this as completed in #470 Dec 4, 2020
@gwenn
Copy link
Collaborator

gwenn commented Dec 15, 2020

7.1.0 released.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants