Added support for wide characters such as cjk and emoji

This commit is contained in:
nilninull 2020-09-07 02:00:15 +09:00 committed by Ferran Basora
parent 9b677a67c6
commit d57e594737
3 changed files with 7 additions and 3 deletions

1
Cargo.lock generated
View File

@ -147,6 +147,7 @@ dependencies = [
"clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
"regex 1.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "regex 1.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
"termion 1.5.5 (registry+https://github.com/rust-lang/crates.io-index)", "termion 1.5.5 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]
[[package]] [[package]]

View File

@ -13,6 +13,7 @@ termion = "1.5"
regex = "1.3.1" regex = "1.3.1"
clap = "2.33.0" clap = "2.33.0"
base64 = "0.11.0" base64 = "0.11.0"
unicode-width = "0.1.7"
[[bin]] [[bin]]
name = "thumbs" name = "thumbs"

View File

@ -8,6 +8,8 @@ use termion::raw::IntoRawMode;
use termion::screen::AlternateScreen; use termion::screen::AlternateScreen;
use termion::{color, cursor}; use termion::{color, cursor};
use unicode_width::UnicodeWidthStr;
pub struct View<'a> { pub struct View<'a> {
state: &'a mut state::State<'a>, state: &'a mut state::State<'a>,
skip: usize, skip: usize,
@ -110,7 +112,7 @@ impl<'a> View<'a> {
// Find long utf sequences and extract it from mat.x // Find long utf sequences and extract it from mat.x
let line = &self.state.lines[mat.y as usize]; let line = &self.state.lines[mat.y as usize];
let prefix = &line[0..mat.x as usize]; let prefix = &line[0..mat.x as usize];
let extra = prefix.len() - prefix.chars().count(); let extra = prefix.width_cjk() - prefix.chars().count();
let offset = (mat.x as u16) - (extra as u16); let offset = (mat.x as u16) - (extra as u16);
let text = self.make_hint_text(mat.text); let text = self.make_hint_text(mat.text);
@ -126,9 +128,9 @@ impl<'a> View<'a> {
if let Some(ref hint) = mat.hint { if let Some(ref hint) = mat.hint {
let extra_position = match self.position { let extra_position = match self.position {
"right" => text.len() - hint.len(), "right" => text.width_cjk() - hint.len(),
"off_left" => 0 - hint.len(), "off_left" => 0 - hint.len(),
"off_right" => text.len(), "off_right" => text.width_cjk(),
_ => 0, _ => 0,
}; };