Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed cell width issues when using ANSI color codes. #79

Merged
merged 2 commits into from
Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

use std::io::{Write, Error};
use std::string::ToString;
use unicode_width::UnicodeWidthStr;
use super::{Attr, Terminal, color};
use super::format::Alignment;
use super::utils::display_width;
use super::utils::print_align;

/// Represent a table cell containing a string.
Expand All @@ -26,7 +26,7 @@ impl Cell {
let content: Vec<String> = string.lines().map(|x| x.to_string()).collect();
let mut width = 0;
for cont in &content {
let l = UnicodeWidthStr::width(&cont[..]);
let l = display_width(&cont[..]);
if l > width {
width = l;
}
Expand Down
22 changes: 22 additions & 0 deletions src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,16 @@ impl TableFormat {
self.rborder = Some(border);
}

/// Set the character used for left table border
pub fn left_border(&mut self, border: char) {
self.lborder = Some(border);
}

/// Set the character used for right table border
pub fn right_border(&mut self, border: char) {
self.rborder = Some(border);
}

/// Set a line separator
pub fn separator(&mut self, what: LinePosition, separator: LineSeparator) {
*match what {
Expand Down Expand Up @@ -295,6 +305,18 @@ impl FormatBuilder {
self
}

/// Set the character used for left table border
pub fn left_border(mut self, border: char) -> Self {
self.format.left_border(border);
self
}

/// Set the character used for right table border
pub fn right_border(mut self, border: char) -> Self {
self.format.right_border(border);
self
}

/// Set a line separator format
pub fn separator(mut self, what: LinePosition, separator: LineSeparator) -> Self {
self.format.separator(what, separator);
Expand Down
32 changes: 31 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub fn print_align<T: Write + ?Sized>(out: &mut T,
size: usize,
skip_right_fill: bool)
-> Result<(), Error> {
let text_len = UnicodeWidthStr::width(text);
let text_len = display_width(text);
let mut nfill = if text_len < size { size - text_len } else { 0 };
let n = match align {
Alignment::LEFT => 0,
Expand All @@ -75,6 +75,36 @@ pub fn print_align<T: Write + ?Sized>(out: &mut T,
Ok(())
}

/// Return the display width of a unicode string.
/// This functions takes ANSI-escaped color codes into account.
pub fn display_width(text: &str) -> usize {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be even better with some tests

let width = UnicodeWidthStr::width(text);
let mut state = 0;
let mut hidden = 0;

for c in text.chars() {
state = match (state, c) {
(0, '\u{1b}') => 1,
(1, '[') => 2,
(1, _) => 0,
(2, 'm') => 3,
_ => state,
};

// We don't count escape characters as hidden as
// UnicodeWidthStr::width already considers them.
if state > 1 {
hidden += 1;
}

if state == 3 {
state = 0;
}
}

width - hidden
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down