console-rs / dialoguer

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

BUG - Input with utf8 chars #268

Closed finistseb closed 1 year ago

finistseb commented 1 year ago

When we enter utf8char with input.interact_text(), the behavior is buggy, the cursor moves in not accurate do to a bad string length calculation. The tail length is realized with tail.len() (bytes count where 1 utf8 char = 2 bytes) instead of tail.chars().count()

I have proposed a PR) in order to fix this issue. I'm a Rust developer for a relatively short time, so me bug analysis may be wrong or incomplete ;-)

Regards, Sebastien

finistseb commented 1 year ago

How to reproduce with this simple test program, enter the input "aaaaaa" and after insert a "é" in the middle of the string. The output is correct, but the display is wrong.

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

fn main() {
    let color_binding = ColorfulTheme::default();
    let value: String = Input::with_theme(&color_binding)
        .with_prompt("Enter parameter")
        .report(false)
        .allow_empty(false)
        .interact_text().unwrap();

    println!("{}", value);
}