Parse utf8 chars in pancurses backend

Closes #76
This commit is contained in:
Alexandre Bury 2016-10-11 16:08:44 -07:00
parent 072c1d9b5c
commit 821d9f1783
4 changed files with 21 additions and 10 deletions

View File

@ -89,7 +89,7 @@ impl backend::Backend for Concrete {
// Is it a UTF-8 starting point? // Is it a UTF-8 starting point?
if 32 <= ch && ch <= 255 && ch != 127 { if 32 <= ch && ch <= 255 && ch != 127 {
Event::Char(utf8::read_char(ch as u8, || ncurses::getch() as u8) Event::Char(utf8::read_char(ch as u8, || Some(ncurses::getch() as u8))
.unwrap()) .unwrap())
} else { } else {
parse_ncurses_char(ch) parse_ncurses_char(ch)

View File

@ -5,6 +5,7 @@ use event::{Event, Key};
use self::super::find_closest; use self::super::find_closest;
use theme::{Color, ColorStyle, Effect}; use theme::{Color, ColorStyle, Effect};
use utf8;
pub struct Concrete { pub struct Concrete {
window: pancurses::Window, window: pancurses::Window,
@ -89,8 +90,22 @@ impl backend::Backend for Concrete {
// TODO: wait for a very short delay. If more keys are // TODO: wait for a very short delay. If more keys are
// pipelined, it may be an escape sequence. // pipelined, it may be an escape sequence.
pancurses::Input::Character('\u{1b}') => Event::Key(Key::Esc), pancurses::Input::Character('\u{1b}') => Event::Key(Key::Esc),
pancurses::Input::Character('\u{7f}') => Event::Key(Key::Backspace), pancurses::Input::Character('\u{7f}') => {
pancurses::Input::Character(c) => Event::Char(c), Event::Key(Key::Backspace)
}
pancurses::Input::Character(c) if 32 <= (c as u32) &&
(c as u32) <= 255 => {
Event::Char(utf8::read_char(c as u8, || {
self.window.getch().and_then(|i| match i {
pancurses::Input::Character(c) => {
Some(c as u8)
}
_ => None,
})
})
.unwrap())
}
pancurses::Input::Character(c) => Event::Unknown(c as i32),
// TODO: Some key combos are not recognized by pancurses, // TODO: Some key combos are not recognized by pancurses,
// but are sent as Unknown. We could still parse them here. // but are sent as Unknown. We could still parse them here.
pancurses::Input::Unknown(i) => Event::Unknown(i), pancurses::Input::Unknown(i) => Event::Unknown(i),

View File

@ -7,7 +7,7 @@ use std::char::from_u32;
/// ///
/// Returns an error if the stream is invalid utf-8. /// Returns an error if the stream is invalid utf-8.
pub fn read_char<F>(first: u8, next: F) -> Result<char, String> pub fn read_char<F>(first: u8, next: F) -> Result<char, String>
where F: Fn() -> u8 where F: Fn() -> Option<u8>
{ {
if first < 0x80 { if first < 0x80 {
return Ok(first as char); return Ok(first as char);
@ -28,7 +28,7 @@ pub fn read_char<F>(first: u8, next: F) -> Result<char, String>
// We already have one byte, now read the others. // We already have one byte, now read the others.
for _ in 1..n_bytes { for _ in 1..n_bytes {
let byte = next(); let byte = try!(next().ok_or("Missing UTF-8 byte".to_string()));
if byte & 0xC0 != 0x80 { if byte & 0xC0 != 0x80 {
return Err(format!("Found non-continuation byte after leading: \ return Err(format!("Found non-continuation byte after leading: \
{}", {}",

View File

@ -352,11 +352,7 @@ impl View for EditView {
fn on_event(&mut self, event: Event) -> EventResult { fn on_event(&mut self, event: Event) -> EventResult {
match event { match event {
Event::Char(ch) => { Event::Char(ch) => self.insert(ch),
// Find the byte index of the char at self.cursor
self.insert(ch);
}
// TODO: handle ctrl-key? // TODO: handle ctrl-key?
Event::Key(Key::Home) => self.cursor = 0, Event::Key(Key::Home) => self.cursor = 0,
Event::Key(Key::End) => self.cursor = self.content.len(), Event::Key(Key::End) => self.cursor = self.content.len(),