Fix emoji length

This commit is contained in:
Thomas Forgione 2023-10-26 11:38:09 +02:00
parent a031445683
commit 14c6db5769
4 changed files with 33 additions and 12 deletions

7
Cargo.lock generated
View File

@ -48,6 +48,7 @@ version = "0.1.0"
dependencies = [ dependencies = [
"pty-process", "pty-process",
"termion", "termion",
"unicode-width",
] ]
[[package]] [[package]]
@ -110,6 +111,12 @@ dependencies = [
"redox_termios", "redox_termios",
] ]
[[package]]
name = "unicode-width"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.48.0" version = "0.48.0"

View File

@ -8,3 +8,4 @@ edition = "2021"
[dependencies] [dependencies]
pty-process = "0.4.0" pty-process = "0.4.0"
termion = "2.0.1" termion = "2.0.1"
unicode-width = "0.1.11"

View File

@ -90,13 +90,13 @@ impl<W: Write> Multiview<W> {
/// Renders all the tiles of the multiview. /// Renders all the tiles of the multiview.
pub fn render(&mut self) -> io::Result<()> { pub fn render(&mut self) -> io::Result<()> {
let now = Instant::now(); // let now = Instant::now();
if now.duration_since(self.last_render) < Duration::from_millis(20) { // if now.duration_since(self.last_render) < Duration::from_millis(20) {
return Ok(()); // return Ok(());
} // }
self.last_render = now; // self.last_render = now;
let mut buffer = vec![]; let mut buffer = vec![];
for i in 0..self.tiles.len() { for i in 0..self.tiles.len() {
for j in 0..self.tiles[0].len() { for j in 0..self.tiles[0].len() {

View File

@ -8,6 +8,8 @@ use std::thread;
use pty_process::blocking::Command; use pty_process::blocking::Command;
use pty_process::blocking::Pty; use pty_process::blocking::Pty;
use unicode_width::UnicodeWidthChar;
use termion::{color, cursor, style}; use termion::{color, cursor, style};
use crate::{utils, Msg}; use crate::{utils, Msg};
@ -252,6 +254,7 @@ impl Tile {
if c == '\x1b' { if c == '\x1b' {
self.counting = false; self.counting = false;
} }
match c { match c {
'\n' => { '\n' => {
self.stdout.last_mut().unwrap().push(c); self.stdout.last_mut().unwrap().push(c);
@ -267,7 +270,10 @@ impl Tile {
_ => { _ => {
self.stdout.last_mut().unwrap().push(c); self.stdout.last_mut().unwrap().push(c);
if self.counting { // Emoji variation selectors have no length
let is_variation_selector = c >= '\u{fe00}' && c <= '\u{fe0f}';
if self.counting && !is_variation_selector {
self.column_number += 1; self.column_number += 1;
if self.column_number == self.inner_size.0 + 1 { if self.column_number == self.inner_size.0 + 1 {
self.stdout.push(String::new()); self.stdout.push(String::new());
@ -380,7 +386,7 @@ impl Tile {
}; };
if c == '\x1b' { if c == '\x1b' {
let mut subbuffer = String::from(c); let mut subbuffer = vec![c];
loop { loop {
let next = match char_iter.next() { let next = match char_iter.next() {
@ -404,8 +410,8 @@ impl Tile {
} }
} }
match &subbuffer[0..3] { match (subbuffer.get(0), subbuffer.get(1), subbuffer.get(2)) {
"\x1b[K" => { (Some('\x1b'), Some('['), Some('K')) => {
if current_char_index < w { if current_char_index < w {
let mut spaces = String::new(); let mut spaces = String::new();
for _ in current_char_index..w { for _ in current_char_index..w {
@ -425,7 +431,7 @@ impl Tile {
)); ));
} }
} }
_ => buffer.push(subbuffer), _ => buffer.push(subbuffer.into_iter().collect()),
} }
continue; continue;
@ -442,6 +448,8 @@ impl Tile {
} }
buffer.push(spaces); buffer.push(spaces);
eprintln!("Clear {}", max_char_index);
line_index += 1; line_index += 1;
current_char_index = 0; current_char_index = 0;
max_char_index = 0; max_char_index = 0;
@ -465,8 +473,13 @@ impl Tile {
} }
_ => { _ => {
current_char_index += 1; // Emoji variation selectors have no length
max_char_index = std::cmp::max(max_char_index, current_char_index); let is_variation_selector = c >= '\u{fe00}' && c <= '\u{fe0f}';
if !is_variation_selector {
current_char_index += UnicodeWidthChar::width(c).unwrap_or(0) as u16;
max_char_index = std::cmp::max(max_char_index, current_char_index);
}
if current_char_index == w + 1 { if current_char_index == w + 1 {
line_index += 1; line_index += 1;