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 alignment issues #48

Merged
merged 1 commit into from
Jul 2, 2024
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
7 changes: 0 additions & 7 deletions cli-table/src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ impl TableStruct {
&self.format,
&color_spec,
)?;
println(&mut buffers)?;

if let Some(ref title) = self.title {
let title_dimension = row_dimensions.next().unwrap();
Expand All @@ -148,8 +147,6 @@ impl TableStruct {
&color_spec,
)?
}

println(&mut buffers)?;
}

let mut rows = self.rows.iter().zip(row_dimensions).peekable();
Expand All @@ -175,10 +172,6 @@ impl TableStruct {
&color_spec,
)?,
}

if rows.peek().is_some() {
println(&mut buffers)?;
}
}

buffers.into_vec()
Expand Down
74 changes: 48 additions & 26 deletions cli-table/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,64 @@
use std::io::{Result, Write};

use termcolor::{ColorSpec, WriteColor};
use unicode_width::UnicodeWidthStr;

use crate::{
buffers::Buffers,
table::{Dimension as TableDimension, HorizontalLine, TableFormat, VerticalLine},
};

/// NOTE: `display_width()` is ported from https://github.com/phsym/prettytable-rs
const ESC: char = '\x1b';

/// NOTE: `display_width()` is ported from https://docs.rs/ansi-width/0.1.0/src/ansi_width/lib.rs.html#9-55
///
/// Return the display width of a unicode string.
/// This functions takes ANSI-escaped color codes into account.
pub(crate) fn display_width(text: &str) -> usize {
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;
let mut width = 0;
let mut chars = text.chars();

// This lint is a false positive, because we use the iterator later, leading to
// ownership issues if we follow the lint.
#[allow(clippy::while_let_on_iterator)]
while let Some(c) = chars.next() {
// ESC starts escape sequences, so we need to take characters until the
// end of the escape sequence.
if c == ESC {
let Some(c) = chars.next() else {
break;
};
match c {
// String terminator character: ends other sequences
// We probably won't encounter this but it's here for completeness.
// Or for if we get passed invalid codes.
'\\' => {
// ignore
}
// Control Sequence Introducer: continue until `\x40-\x7C`
'[' => while !matches!(chars.next(), Some('\x40'..='\x7C') | None) {},
// Operating System Command: continue until ST
']' => {
let mut last = c;
while let Some(new) = chars.next() {
if new == '\x07' || (new == '\\' && last == ESC) {
break;
}
last = new;
}
}
// We don't know what character it is, best bet is to fall back to unicode width
// The ESC is assumed to have 0 width in this case.
_ => {
width += unicode_width::UnicodeWidthChar::width(c).unwrap_or(0);
}
}
} else {
// If it's a normal character outside an escape sequence, use the
// unicode width.
width += unicode_width::UnicodeWidthChar::width(c).unwrap_or(0);
}
}

width - hidden
width
}

pub fn transpose<T>(v: Vec<Vec<T>>) -> Vec<Vec<T>> {
Expand Down Expand Up @@ -104,6 +124,8 @@ pub(crate) fn print_horizontal_line(
}
}
}

println(buffers)?;
}

Ok(())
Expand Down
Loading