From 08281333002079ee65af35bf3f61abf7f81f7cc2 Mon Sep 17 00:00:00 2001 From: quininer kel Date: Wed, 10 May 2017 16:26:50 +0800 Subject: [PATCH] Fix TextCellContents cjk width --- src/output/cell.rs | 15 ++++++++++----- src/output/grid_details.rs | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/output/cell.rs b/src/output/cell.rs index 653f1a8..ef06b6e 100644 --- a/src/output/cell.rs +++ b/src/output/cell.rs @@ -1,5 +1,6 @@ //! The `TextCell` type for the details and lines views. +use std::iter::Sum; use std::ops::{Add, Deref, DerefMut}; use ansi_term::{Style, ANSIString, ANSIStrings}; @@ -163,11 +164,9 @@ impl TextCellContents { /// Calculates the width that a cell with these contents would take up, by /// counting the number of characters in each unformatted ANSI string. pub fn width(&self) -> DisplayWidth { - let sum = self.0.iter() - .map(|anstr| anstr.chars().count()) - .sum(); - - DisplayWidth(sum) + self.0.iter() + .map(|anstr| DisplayWidth::from(anstr.deref())) + .sum() } /// Promotes these contents to a full cell containing them alongside @@ -239,6 +238,12 @@ impl Add for DisplayWidth { } } +impl Sum for DisplayWidth { + fn sum(iter: I) -> Self where I: Iterator { + iter.fold(DisplayWidth(0), Add::add) + } +} + #[cfg(test)] mod width_unit_test { diff --git a/src/output/grid_details.rs b/src/output/grid_details.rs index a32336b..2b241d0 100644 --- a/src/output/grid_details.rs +++ b/src/output/grid_details.rs @@ -158,4 +158,4 @@ fn divide_rounding_up(a: usize, b: usize) -> usize { let mut result = a / b; if a % b != 0 { result += 1; } result -} \ No newline at end of file +}