Skip to content

[bug] 中文输入乱码问题 #134

Description

@asmcos

下面代码是一个普通获取中文输入的 代码,导致

    let stdin = io::stdin();
    let mut reader = BufReader::new(stdin);
    let mut line = String::new();

    eprintln!("stdin-line-test: Tokio stdin + read_line "); 

    loop {
        line.clear();
        print!("› ");
        std::io::stdout().flush()?;

        let n = reader.read_line(&mut line).await?;
        if n == 0 {
            eprintln!("(EOF)");
            break;
        }

        let trimmed = line.trim_end_matches(['\n', '\r']);
        if trimmed == "quit" || trimmed == "exit" {
            break;
        }

会出现

› [ 20.124599 0:4 starry_kernel::pseudofs::dev::tty::terminal::ldisc:181] Ignored echo char: 0xe4
[ 20.128822 0:4 starry_kernel::pseudofs::dev::tty::terminal::ldisc:181] Ignored echo char: 0xbd
[ 20.129233 0:4 starry_kernel::pseudofs::dev::tty::terminal::ldisc:181] Ignored echo char: 0xa0

查找源代码: 181行

            other => {
                //warn!("Ignored echo char: {other:#x}");
                self.writer.write(&[ch]);
            }

直接把打印换成输出 就可以接受中文输入了。

从代码看出来是中文输入后的显示问题。 可能会在不能显示中文的环境中会显示乱码。

但是替代方案 打印 也没有解决显示问题。

我猜测原作者 是为了让用户去调试? 可以加调试开关,默认就是 self.writer.write(&[ch]); 打开开关后 可以 打印:warn!("Ignored echo char: {other:#x}");

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions