kkawakam / rustyline

Readline Implementation in Rust
https://crates.io/crates/rustyline/
MIT License
1.54k stars 176 forks source link

readline overrides stdout buffer #755

Closed linsyking closed 9 months ago

linsyking commented 9 months ago
fn main() {
    let mut rl = rustyline::DefaultEditor::new().unwrap();
    print!("hi");
    stdout().flush().unwrap();
    let s = rl.readline("> ");    
}

I was writing a unix shell and sometimes it needs to print a string without newline so I need to flush the stdout buffer.

stdout().flush().unwrap(); should print stdout buffer "hi", but when I add readline, nothing prints out.

gwenn commented 9 months ago

See check_cursor_position:

use rustyline::{Config, DefaultEditor, Result};
use std::io::stdout;
use std::io::Write;

fn main() -> Result<()> {
    let config = Config::builder()
        .check_cursor_position(true)
        .build();
    let mut rl = DefaultEditor::with_config(config)?;

    print!("hi");
    stdout().flush()?;
    let _ = rl.readline("> ")?;
    Ok(())
}