Skip to content

Commit

Permalink
Fix TextCellContents cjk width
Browse files Browse the repository at this point in the history
  • Loading branch information
quininer committed May 10, 2017
1 parent d2b1499 commit 0828133
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions src/output/cell.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -239,6 +238,12 @@ impl Add<usize> for DisplayWidth {
}
}

impl Sum for DisplayWidth {
fn sum<I>(iter: I) -> Self where I: Iterator<Item=Self> {
iter.fold(DisplayWidth(0), Add::add)
}
}


#[cfg(test)]
mod width_unit_test {
Expand Down
2 changes: 1 addition & 1 deletion src/output/grid_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 0828133

Please sign in to comment.