ui: add grapheme cluster support in text input

closes #60
embed
Manos Pitsidianakis 2019-03-18 12:45:33 +02:00
parent bf038428c2
commit 0b76307e30
Signed by: Manos Pitsidianakis
GPG Key ID: 73627C2F690DF710
8 changed files with 2037 additions and 73 deletions

View File

@ -5,20 +5,21 @@ authors = []
workspace = ".."
[dependencies]
xdg = "2.1.0"
xdg = "2.1.0" # >:c
serde = "1.0.71"
serde_derive = "1.0.71"
serde_json = "1.0"
config = "0.6"
chan = "0.1.21"
chan-signal = "0.3.1"
fnv = "1.0.3"
linkify = "0.3.1"
fnv = "1.0.3" # >:c
linkify = "0.3.1" # >:c
melib = { path = "../melib", version = "*" }
mime_apps = { path = "../../mime_apps", version = "*" }
nom = "3.2.0"
notify = "4.0.1"
notify-rust = "^3"
notify = "4.0.1" # >:c
notify-rust = "^3" # >:c
termion = "1.5.1"
bincode = "1.0.1"
uuid = { version = "0.6", features = ["serde", "v4"] }
unicode-segmentation = "1.1.0" # >:c

View File

@ -150,7 +150,7 @@ impl Component for ContactManager {
(
s,
match v {
Field::Text(v, _, _) | Field::TextArea(v, _) => v,
Field::Text(v, _) => v.as_str().to_string(),
Field::Choice(mut v, c) => v.remove(c),
},
)

View File

@ -299,7 +299,7 @@ impl Pager {
content
};
Pager {
text: text,
text,
cursor_pos: cursor_pos.unwrap_or(0),
height: content.size().1,
width: content.size().0,

View File

@ -18,18 +18,16 @@ impl Default for FormFocus {
pub enum Field {
Text(
String,
Cursor,
UText,
Option<(Box<Fn(&Context, &str) -> Vec<String> + Send>, AutoComplete)>,
),
Choice(Vec<String>, Cursor),
TextArea(String, Cursor),
}
impl Debug for Field {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Text(s, c, _) => fmt::Debug::fmt(s, f),
Text(s, _) => fmt::Debug::fmt(s, f),
k => fmt::Debug::fmt(k, f),
}
}
@ -39,15 +37,14 @@ use Field::*;
impl Default for Field {
fn default() -> Field {
Field::Text(String::new(), 0, None)
Field::Text(UText::new(String::new()), None)
}
}
impl Field {
fn as_str(&self) -> &str {
match self {
Text(ref s, _, _) => s,
TextArea(ref s, _) => s,
Text(ref s, _) => s.as_str(),
Choice(ref v, cursor) => {
if v.is_empty() {
""
@ -60,8 +57,7 @@ impl Field {
pub fn into_string(self) -> String {
match self {
Text(s, _, _) => s,
TextArea(s, _) => s,
Text(s, _) => s.into_string(),
Choice(mut v, cursor) => v.remove(cursor),
}
}
@ -75,26 +71,25 @@ impl Field {
) {
let upper_left = upper_left!(area);
match self {
Text(ref term, cursor, auto_complete_fn) => {
Text(ref term, auto_complete_fn) => {
change_colors(
grid,
(
pos_inc(upper_left, (*cursor, 0)),
(pos_inc(upper_left, (*cursor, 0))),
pos_inc(upper_left, (term.grapheme_pos(), 0)),
(pos_inc(upper_left, (term.grapheme_pos(), 0))),
),
Color::Default,
Color::Byte(248),
);
if term.chars().count() <= 2 {
if term.grapheme_len() <= 2 {
return;
}
if let Some((auto_complete_fn, auto_complete)) = auto_complete_fn {
let entries = auto_complete_fn(context, term);
let entries = auto_complete_fn(context, term.as_str());
auto_complete.set_suggestions(entries);
auto_complete.draw(grid, secondary_area, context);
}
}
TextArea(_, _) => {}
Choice(_, _cursor) => {}
}
}
@ -112,39 +107,35 @@ impl Component for Field {
);
}
fn process_event(&mut self, event: &mut UIEvent, _context: &mut Context) -> bool {
if let Text(ref mut s, ref mut cursor, Some((_, auto_complete))) = self {
match event.event_type {
UIEventType::InsertInput(Key::Char('\t')) => {
if let Some(suggestion) = auto_complete.get_suggestion() {
*s = suggestion;
*cursor = s.chars().count();
return true;
}
if let Text(ref mut s, Some((_, auto_complete))) = self {
if let UIEventType::InsertInput(Key::Char('\t')) = event.event_type {
if let Some(suggestion) = auto_complete.get_suggestion() {
*s = UText::new(suggestion);
let len = s.as_str().len().saturating_sub(1);
s.set_cursor(len);
return true;
}
_ => {}
}
}
match event.event_type {
UIEventType::InsertInput(Key::Up) => {
if let Text(_, _, Some((_, auto_complete))) = self {
if let Text(_, Some((_, auto_complete))) = self {
auto_complete.dec_cursor();
} else {
return false;
}
}
UIEventType::InsertInput(Key::Down) => {
if let Text(_, _, Some((_, auto_complete))) = self {
if let Text(_, Some((_, auto_complete))) = self {
auto_complete.inc_cursor();
} else {
return false;
}
}
UIEventType::InsertInput(Key::Right) => match self {
TextArea(ref s, ref mut cursor) | Text(ref s, ref mut cursor, _) => {
if *cursor < s.len() {
*cursor += 1;
}
Text(ref mut s, _) => {
s.cursor_inc();
}
Choice(ref vec, ref mut cursor) => {
*cursor = if *cursor == vec.len().saturating_sub(1) {
@ -155,12 +146,8 @@ impl Component for Field {
}
},
UIEventType::InsertInput(Key::Left) => match self {
TextArea(_, ref mut cursor) | Text(_, ref mut cursor, _) => {
if *cursor == 0 {
return false;
} else {
*cursor -= 1;
}
Text(ref mut s, _) => {
s.cursor_dec();
}
Choice(_, ref mut cursor) => {
if *cursor == 0 {
@ -170,27 +157,14 @@ impl Component for Field {
}
}
},
UIEventType::InsertInput(Key::Char(k)) => match self {
Text(ref mut s, ref mut cursor, _) | TextArea(ref mut s, ref mut cursor) => {
s.insert(*cursor, k);
*cursor += 1;
}
_ => {}
UIEventType::InsertInput(Key::Char(k)) => if let Text(ref mut s, _) = self {
s.insert_char(k);
},
UIEventType::InsertInput(Key::Backspace) => match self {
Text(ref mut s, ref mut cursor, ref mut auto_complete) => {
if *cursor > 0 {
*cursor -= 1;
s.remove(*cursor);
}
auto_complete
.as_mut()
.map(|ac| ac.1.set_suggestions(Vec::new()));
}
TextArea(ref mut s, ref mut cursor) => {
if *cursor > 0 {
*cursor -= 1;
s.remove(*cursor);
Text(ref mut s, auto_complete) => {
s.backspace();
if let Some(ac) = auto_complete.as_mut() {
ac.1.set_suggestions(Vec::new());
}
}
_ => {}
@ -268,11 +242,6 @@ impl FormWidget {
self.layout.push(value.0.clone());
self.fields.insert(value.0, Choice(value.1, 0));
}
pub fn push_text_area(&mut self, value: (String, String)) {
self.field_name_max_length = std::cmp::max(self.field_name_max_length, value.0.len());
self.layout.push(value.0.clone());
self.fields.insert(value.0, TextArea(value.1, 0));
}
pub fn push_cl(
&mut self,
value: (
@ -285,13 +254,16 @@ impl FormWidget {
self.layout.push(value.0.clone());
self.fields.insert(
value.0,
Text(value.1, 0, Some((value.2, AutoComplete::new(Vec::new())))),
Text(
UText::new(value.1),
Some((value.2, AutoComplete::new(Vec::new()))),
),
);
}
pub fn push(&mut self, value: (String, String)) {
self.field_name_max_length = std::cmp::max(self.field_name_max_length, value.0.len());
self.layout.push(value.0.clone());
self.fields.insert(value.0, Text(value.1, 0, None));
self.fields.insert(value.0, Text(UText::new(value.1), None));
}
pub fn insert(&mut self, index: usize, value: (String, Field)) {
@ -614,7 +586,7 @@ impl Component for AutoComplete {
);
context.dirty_areas.push_back(area);
}
fn process_event(&mut self, event: &mut UIEvent, _context: &mut Context) -> bool {
fn process_event(&mut self, _event: &mut UIEvent, _context: &mut Context) -> bool {
false
}
fn is_dirty(&self) -> bool {
@ -649,7 +621,7 @@ impl AutoComplete {
);
let width = content.cols();
for (i, e) in entries.iter().enumerate() {
let (x, _) = write_string_to_grid(
write_string_to_grid(
e,
&mut content,
Color::Byte(23),

View File

@ -21,6 +21,8 @@
extern crate serde;
use self::serde::de::Visitor;
use self::serde::{de, Deserialize, Deserializer};
extern crate unicode_segmentation;
use self::unicode_segmentation::UnicodeSegmentation;
#[macro_use]
mod position;
@ -28,8 +30,12 @@ mod position;
mod cells;
#[macro_use]
mod keys;
mod grapheme_clusters;
mod text_editing;
mod wcwidth;
pub use self::cells::*;
pub use self::grapheme_clusters::*;
pub use self::keys::*;
pub use self::position::*;
pub use self::text_editing::*;
pub use self::wcwidth::*;

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,131 @@
use super::*;
#[derive(Debug)]
pub struct UText {
content: String,
cursor_pos: usize,
grapheme_cursor_pos: usize,
}
impl UText {
pub fn new(content: String) -> Self {
UText {
content,
cursor_pos: 0,
grapheme_cursor_pos: 0,
}
}
pub fn set_cursor(&mut self, cursor_pos: usize) {
if cursor_pos >= self.content.len() {
return;
}
let (first, _) = self.content.split_at(cursor_pos);
self.grapheme_cursor_pos = split_graphemes(first).len();
self.cursor_pos = cursor_pos;
}
pub fn as_str(&self) -> &str {
self.content.as_str()
}
pub fn into_string(self) -> String {
self.content
}
pub fn grapheme_len(&self) -> usize {
split_graphemes(&self.content).len()
}
pub fn cursor_inc(&mut self) {
if self.cursor_pos >= self.content.len() {
return;
}
let (_, right) = std::dbg!(self.content.split_at(self.cursor_pos));
if let Some((_, graph)) = std::dbg!(next_grapheme(right)) {
self.cursor_pos += graph.len();
self.grapheme_cursor_pos += 1;
}
}
pub fn cursor_dec(&mut self) {
if self.cursor_pos == 0 {
return;
}
let (left, _) = std::dbg!(self.content.split_at(self.cursor_pos));
if let Some((_, graph)) = std::dbg!(last_grapheme(left)) {
self.cursor_pos -= graph.len();
self.grapheme_cursor_pos -= 1;
}
}
pub fn cursor_pos(&self) -> usize {
self.cursor_pos
}
pub fn grapheme_pos(&self) -> usize {
self.grapheme_cursor_pos
}
/*
* Insert code point `k` in position `self.cursor_pos`:
*
* before:
*
* self.content = xxxxxx....xxxxxxx;
* ^
* self.cursor_pos
*
* after:
*
* self.content = xxxxxx....xxxxkxxx;
* ^
* self.cursor_pos
*/
pub fn insert_char(&mut self, k: char) {
self.content.insert(self.cursor_pos, k);
self.cursor_pos += k.len_utf8();
self.grapheme_cursor_pos += 1;
}
/*
* remove grapheme cluster that ends on `self.cursor_pos`:
*
* before:
*
* self.content = xxxxxx....xxggxxx;
* ^
* self.cursor_pos
*
* after:
*
* self.content = xxxxxx....xxxxxx;
* ^
* self.cursor_pos
*/
pub fn backspace(&mut self) {
if self.content.is_empty() {
return;
}
let (offset, graph_len) = {
/*
* Split string at cursor_pos:
*/
let (left, _) = self.content.split_at(self.cursor_pos);
/*
* left = xxxxxx....xxgg;
* right = xxx;
*/
if let Some((offset, graph)) = std::dbg!(last_grapheme(left)) {
(offset, graph.len())
} else {
return;
}
};
self.cursor_dec();
self.content
.drain(std::dbg!(offset..offset + graph_len))
.count();
}
}

View File

@ -6,6 +6,9 @@
* Markus Kuhn -- 2001-09-08 -- public domain
*/
// TODO: Spacing widths
// Update to Unicode 12
#[macro_export]
macro_rules! big_if_true {
($a:expr) => {
@ -20,6 +23,76 @@ macro_rules! big_if_true {
type WChar = u32;
type Interval = (WChar, WChar);
pub struct CodePointsIterator<'a> {
rest: &'a [u8],
}
/*
* UTF-8 uses a system of binary prefixes, in which the high bits of each byte mark whether its a single byte, the beginning of a multi-byte sequence, or a continuation byte; the remaining bits, concatenated, give the code point index. This table shows how it works:
*
* UTF-8 (binary) Code point (binary) Range
* 0xxxxxxx xxxxxxx U+0000U+007F
* 110xxxxx 10yyyyyy xxxxxyyyyyy U+0080U+07FF
* 1110xxxx 10yyyyyy 10zzzzzz xxxxyyyyyyzzzzzz U+0800U+FFFF
* 11110xxx 10yyyyyy 10zzzzzz 10wwwwww xxxyyyyyyzzzzzzwwwwww U+10000U+10FFFF
*
*/
impl<'a> Iterator for CodePointsIterator<'a> {
type Item = WChar;
fn next(&mut self) -> Option<WChar> {
println!("rest = {:?}", self.rest);
if self.rest.is_empty() {
return None;
}
/* Input is UTF-8 valid strings, guaranteed by Rust's std */
if self.rest[0] & 0b1000_0000 == 0x0 {
let ret: WChar = self.rest[0] as WChar;
self.rest = &self.rest[1..];
return Some(ret);
}
if self.rest[0] & 0b1110_0000 == 0b1100_0000 {
let ret: WChar = (self.rest[0] as WChar & 0b0001_1111).rotate_left(6)
+ (self.rest[1] as WChar & 0b0111_1111);
self.rest = &self.rest[2..];
return Some(ret);
}
if self.rest[0] & 0b1111_0000 == 0b1110_0000 {
let ret: WChar = (self.rest[0] as WChar & 0b0000_0111).rotate_left(12)
+ (self.rest[1] as WChar & 0b0011_1111).rotate_left(6)
+ (self.rest[2] as WChar & 0b0011_1111);
self.rest = &self.rest[3..];
return Some(ret);
}
let ret: WChar = (self.rest[0] as WChar & 0b0000_0111).rotate_left(18)
+ (self.rest[1] as WChar & 0b0011_1111).rotate_left(12)
+ (self.rest[2] as WChar & 0b0011_1111).rotate_left(6)
+ (self.rest[3] as WChar & 0b0011_1111);
self.rest = &self.rest[4..];
Some(ret)
}
}
pub trait CodePointsIter {
fn code_points(&self) -> CodePointsIterator;
}
impl CodePointsIter for str {
fn code_points(&self) -> CodePointsIterator {
CodePointsIterator {
rest: self.as_bytes(),
}
}
}
impl CodePointsIter for &str {
fn code_points(&self) -> CodePointsIterator {
CodePointsIterator {
rest: self.as_bytes(),
}
}
}
/* auxiliary function for binary search in Interval table */
fn bisearch(ucs: WChar, table: &'static [Interval]) -> bool {
let mut min = 0;
@ -74,7 +147,7 @@ fn bisearch(ucs: WChar, table: &'static [Interval]) -> bool {
* in ISO 10646.
*/
fn wcwidth(ucs: WChar) -> Option<usize> {
pub fn wcwidth(ucs: WChar) -> Option<usize> {
/* sorted list of non-overlapping intervals of non-spacing characters */
let combining: &'static [Interval] = &[
(0x0300, 0x034E),
@ -235,7 +308,7 @@ fn wcswidth(mut pwcs: WChar, mut n: usize) -> Option<usize> {
* encodings who want to migrate to UCS. It is not otherwise
* recommended for general use.
*/
fn wcwidth_cjk(ucs: WChar) -> Option<usize> {
pub fn wcwidth_cjk(ucs: WChar) -> Option<usize> {
/* sorted list of non-overlapping intervals of East Asian Ambiguous
* characters */
let ambiguous: &'static [Interval] = &[