0

I'm writing an ncurses app with Rust.

I want the user to be able to input Unicode (UTF-8) characters. Besides printing them to screen, I am going to build up a search string from the characters.

Here's a minimal example:

use ncurses::*;

fn main() {
    initscr();
    loop {
        let input = get_wch();
        match input.unwrap() {
            WchResult::Char(ch) => { 
                match ch {
                    27 => break,
                    _ => { mvaddstr(0, 0, &format!("spam {}", ch)); }
                }
            },
            WchResult::KeyCode(code) => { mvaddstr(0, 0, &format!("eggs {}", code)); }
        }
    }
    endwin();
}

I was told that if I want to read UTF-8 characters, I should use get_wch(), however when I input ć, what is printed is ~G. For ASCII characters, it prints spam <ch>.

How do I handle Unicode properly in an ncurses app?

adder
  • 3,512
  • 1
  • 16
  • 28

1 Answers1

0

Have a look here: https://github.com/jeaye/ncurses-rs/blob/master/examples/ex_7.rs.

get_wchar() gets your a single wide character, meaning a single Unicode Scalar Value stored in an i32. Unicode Scalar Value is what's often referred to as "character" in Unicode contexts. UTF-8 encodes each of these "characters" into 1 to 4 Bytes. The corresponding Rust Type is char. To convert it, you should cast it to u32 and use the char::from_u32() method. This returns you an Option (since not all of u32 values are valid Unicode Scalar Values). After unwrapping, char's Display trait should now take care to encode it into UTF-8 and put it into the output stream.

sannaj
  • 360
  • 2
  • 8