Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
blueforesticarus committed Oct 7, 2022
1 parent 20edf20 commit 2d53b1b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 40 deletions.
25 changes: 10 additions & 15 deletions examples/inner_style.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use comfy_table::{Table, Row, Cell, ContentArrangement};
use comfy_table::{Cell, ContentArrangement, Row, Table};

fn main() {
let mut table = Table::new();
Expand All @@ -7,12 +7,11 @@ fn main() {
table.set_width(85);

let mut row = Row::new();
row.add_cell(Cell::new(
format!("hello{}cell1", console::style("123\n456").dim().blue())
));
row.add_cell(Cell::new(
"cell2"
));
row.add_cell(Cell::new(format!(
"hello{}cell1",
console::style("123\n456").dim().blue()
)));
row.add_cell(Cell::new("cell2"));

table.add_row(row);

Expand All @@ -26,13 +25,9 @@ fn main() {
table.add_row(row);

let mut row = Row::new();
row.add_cell(Cell::new(
"cell5"
));
row.add_cell(Cell::new(
"cell6"
));
row.add_cell(Cell::new("cell5"));
row.add_cell(Cell::new("cell6"));
table.add_row(row);

println!("{}", table);
}
println!("{}", table);
}
5 changes: 1 addition & 4 deletions src/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@ impl Cell {
#[allow(clippy::needless_pass_by_value)]
pub fn new<T: ToString>(content: T) -> Self {
let content = content.to_string();
let mut split_content : Vec<String> = content
.split('\n')
.map(ToString::to_string)
.collect();
let mut split_content: Vec<String> = content.split('\n').map(ToString::to_string).collect();

// corrects ansi codes so style is terminated and resumed around the split
fix_style_in_split_str(&mut split_content);
Expand Down
5 changes: 3 additions & 2 deletions src/utils/formatting/content_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ pub fn format_row(
// we are truncate the line, so we might cuttoff a ansi code
let stripped = strip_ansi_codes(last_line).to_string();
last_line.clear();
last_line.push_str( &stripped );
last_line.push_str(&stripped);

// Don't do anything if the collumn is smaller then 6 characters
let width: usize = info.content_width.into();
Expand Down Expand Up @@ -183,7 +183,8 @@ pub fn format_row(
#[allow(unused_variables)]
fn align_line(table: &Table, info: &ColumnDisplayInfo, cell: &Cell, mut line: String) -> String {
let content_width = info.content_width;
let remaining: usize = usize::from(content_width).saturating_sub(console::measure_text_width(&line));
let remaining: usize =
usize::from(content_width).saturating_sub(console::measure_text_width(&line));

// Apply the styling before aligning the line, if the user requests it.
// That way non-delimiter whitespaces won't have stuff like underlines.
Expand Down
38 changes: 19 additions & 19 deletions src/utils/formatting/content_split.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ fn split_long_word(allowed_width: usize, word: &str) -> (String, String) {
let mut escape_count = 0;
let mut tail = String::with_capacity(word.len());

for (val, is_esc) in iter.by_ref(){
for (val, is_esc) in iter.by_ref() {
if is_esc {
escapes.push(val);
if val == reset {
Expand All @@ -183,25 +183,25 @@ fn split_long_word(allowed_width: usize, word: &str) -> (String, String) {
true => 0,
false => val.width(),
};

if current_len + len <= allowed_width {
head.push_str(val);
current_len += len;

if ! is_esc {
if !is_esc {
//allows popping unneeded escape codes later
last_txt = head.len();
escape_count = escapes.len();
}
}else{
assert!(! is_esc);
} else {
assert!(!is_esc);
let mut char_iter = val.chars().peekable();
while let Some(c) = char_iter.peek() {
let character_width = c.width().unwrap_or(0);
if allowed_width < current_len + character_width {
break;
}

current_len += character_width;
let c = char_iter.next().unwrap();
head.push(c);
Expand All @@ -211,15 +211,15 @@ fn split_long_word(allowed_width: usize, word: &str) -> (String, String) {
escape_count = escapes.len();
}

head.truncate(last_txt);// cut off dangling escape codes since they should have no effect
head.truncate(last_txt); // cut off dangling escape codes since they should have no effect
if escape_count != 0 {
head.push_str(reset.as_str());
}

for esc in escapes{
for esc in escapes {
tail.push_str(esc);
}
let remaining : String = char_iter.collect();
let remaining: String = char_iter.collect();
tail.push_str(&remaining);
break;
}
Expand All @@ -232,28 +232,28 @@ fn split_long_word(allowed_width: usize, word: &str) -> (String, String) {
/// Fixes ansi escape codes in a split string
/// 1. Adds reset code to the end of each substring if needed.
/// 2. Keeps track of previous substring's escape codes and inserts them in later substrings to continue style
pub fn fix_style_in_split_str(words: &mut [String]){
let mut escapes : Vec<String> = Vec::new();
pub fn fix_style_in_split_str(words: &mut [String]) {
let mut escapes: Vec<String> = Vec::new();
let reset = crossterm::style::Attribute::Reset.to_string();

for word in words {
if ! escapes.is_empty(){
if !escapes.is_empty() {
word.insert_str(0, escapes.join("").as_str())
}

let iter = console::AnsiCodeIterator::new(word)
.filter(|(_,is_esc)| *is_esc)
.map(|v|v.0);
for esc in iter{
.filter(|(_, is_esc)| *is_esc)
.map(|v| v.0);
for esc in iter {
if esc == reset {
escapes.clear()
}else{
} else {
escapes.push(esc.to_string())
}
}

if ! escapes.is_empty(){
if !escapes.is_empty() {
word.push_str(&reset);
}
};
}
}
}

0 comments on commit 2d53b1b

Please sign in to comment.