console-rs / dialoguer

Rust utility library for nice command line prompts and similar things
MIT License
1.33k stars 142 forks source link

In `Input`, cursor behavior is buggy when deleting a character when `with_prompt()` contains a newline #257

Open mirumirumi opened 1 year ago

mirumirumi commented 1 year ago

I found a strange behavior. I don't know if it is something that can be controlled by the crate, but I will report it.

Description

This works fine:

use dialoguer::{theme::ColorfulTheme, Input};

fn main() {
    let sss: String = Input::with_theme(&ColorfulTheme::default())
        .with_prompt("it works fine")
        .with_initial_text("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
        .interact_text()
        .unwrap();

    println!("{:?}", sss);
}

The cursor moves correctly even if the string being entered is deleted.

However, this does not work fine:

use dialoguer::{theme::ColorfulTheme, Input};

fn main() {
    let sss: String = Input::with_theme(&ColorfulTheme::default())
        .with_prompt("it does not \n work fine")  // <--- contains `\n`
        .with_initial_text("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
        .interact_text()
        .unwrap();

    println!("{:?}", sss);
}

image

Press the backspace key anywhere,

image

image

The movable range of the cursor is displaced. (The second image shows the left end of the cursor, which appears to actually have letters, but cannot be moved.)

I have checked everything with Windows Terminal, iTerm2 on Mac, and VS Code's built-in terminal.

Reproduction

https://github.com/mirumirumi/reproduction-dialoguer

Thank you.

mirumirumi commented 1 year ago

Additional information.

It appears that the bug is not whether there is a newline character in the prompt string, but whether the entire line, including the prompt and default string, is newlined on the terminal.

However, I do not know why the reproducibility was not 100% in this case (previous reports always reproduced).