diff --git a/papergrid/src/dimension/compact.rs b/papergrid/src/dimension/compact.rs index dc3835fa..a2a2148c 100644 --- a/papergrid/src/dimension/compact.rs +++ b/papergrid/src/dimension/compact.rs @@ -7,7 +7,7 @@ use core::cmp::max; use crate::{ dimension::{Dimension, Estimate}, records::{IntoRecords, Records}, - util::string::{count_lines, string_width_multiline}, + util::string::{count_lines, get_string_width}, }; use crate::config::compact::CompactConfig; @@ -139,7 +139,7 @@ fn get_cell_height(cell: &str, cfg: &CompactConfig) -> usize { } fn get_cell_width(text: &str, cfg: &CompactConfig) -> usize { - let width = string_width_multiline(text); + let width = get_string_width(text); let pad = cfg.get_padding(); width + pad.left.size + pad.right.size diff --git a/papergrid/src/dimension/spanned.rs b/papergrid/src/dimension/spanned.rs index 249104f7..3a9ec315 100644 --- a/papergrid/src/dimension/spanned.rs +++ b/papergrid/src/dimension/spanned.rs @@ -11,7 +11,7 @@ use crate::{ config::Position, dimension::{Dimension, Estimate}, records::{IntoRecords, Records}, - util::string::{count_lines, string_dimension, string_width_multiline}, + util::string::{count_lines, get_string_dimension, get_string_width}, }; use crate::config::spanned::SpannedConfig; @@ -112,7 +112,7 @@ where } let text = cell.as_ref(); - let (height, width) = string_dimension(text); + let (height, width) = get_string_dimension(text); let pad = cfg.get_padding(pos.into()); let width = width + pad.left.size + pad.right.size; let height = height + pad.top.size + pad.bottom.size; @@ -274,7 +274,7 @@ fn adjust_column_range( fn get_cell_width(text: &str, cfg: &SpannedConfig, pos: Position) -> usize { let padding = get_cell_padding(cfg, pos); - let width = string_width_multiline(text); + let width = get_string_width(text); width + padding } diff --git a/papergrid/src/grid/compact.rs b/papergrid/src/grid/compact.rs index f2bdd3e2..9f1814ee 100644 --- a/papergrid/src/grid/compact.rs +++ b/papergrid/src/grid/compact.rs @@ -12,7 +12,7 @@ use crate::{ config::{AlignmentHorizontal, Borders, HorizontalLine, Indent, Sides}, dimension::Dimension, records::{IntoRecords, Records}, - util::string::string_width, + util::string::get_line_width, }; use crate::config::compact::CompactConfig; @@ -563,7 +563,7 @@ where { let available = width - (padding.left.space.size + padding.right.space.size); - let text_width = string_width(text); + let text_width = get_line_width(text); let (left, right) = if available > text_width { calculate_indent(alignment, text_width, available) } else { diff --git a/papergrid/src/grid/iterable.rs b/papergrid/src/grid/iterable.rs index cdc07842..a2c07cfd 100644 --- a/papergrid/src/grid/iterable.rs +++ b/papergrid/src/grid/iterable.rs @@ -14,7 +14,7 @@ use crate::{ config::{AlignmentHorizontal, AlignmentVertical, Formatting, Indent, Position, Sides}, dimension::Dimension, records::{IntoRecords, Records}, - util::string::{count_lines, get_lines, string_width, string_width_multiline, Lines}, + util::string::{count_lines, get_line_width, get_lines, get_string_width, Lines}, }; /// Grid provides a set of methods for building a text-based table. @@ -301,12 +301,12 @@ fn print_single_line_column( let (text, text_width) = if fmt.horizontal_trim && !text.is_empty() { let text = string_trim(text); - let width = string_width(&text); + let width = get_line_width(&text); (text, width) } else { let text = Cow::Borrowed(text); - let width = string_width_multiline(&text); + let width = get_string_width(&text); (text, width) }; @@ -854,7 +854,7 @@ where line }; - let line_width = string_width(&line); + let line_width = get_line_width(&line); let available_width = self.width - self.pad.left.size - self.pad.right.size; let (left, right) = if self.fmt.allow_lines_alignment { @@ -1303,11 +1303,11 @@ fn count_empty_lines(cell: &str) -> (usize, usize, usize) { fn get_text_width(text: &str, trim: bool) -> usize { if trim { get_lines(text) - .map(|line| string_width(line.trim())) + .map(|line| get_line_width(line.trim())) .max() .unwrap_or(0) } else { - string_width_multiline(text) + get_string_width(text) } } diff --git a/papergrid/src/grid/peekable.rs b/papergrid/src/grid/peekable.rs index 5ba26140..27b5a470 100644 --- a/papergrid/src/grid/peekable.rs +++ b/papergrid/src/grid/peekable.rs @@ -17,7 +17,7 @@ use crate::{ config::{AlignmentHorizontal, AlignmentVertical, Entity, Indent, Position, Sides}, dimension::Dimension, records::{ExactRecords, PeekableRecords, Records}, - util::string::string_width, + util::string::get_line_width, }; /// Grid provides a set of methods for building a text-based table. @@ -390,7 +390,7 @@ mod grid_basic { let line = records.get_line(pos, index); let (line, line_width) = if cfg.formatting.horizontal_trim { let line = string_trim(line); - let width = string_width(&line); + let width = get_line_width(&line); (line, width) } else { let width = records.get_line_width(pos, index); @@ -405,7 +405,7 @@ mod grid_basic { let cell_width = if cfg.formatting.horizontal_trim { (0..records.count_lines(pos)) .map(|i| records.get_line(pos, i)) - .map(|line| string_width(line.trim())) + .map(|line| get_line_width(line.trim())) .max() .unwrap_or_default() } else { @@ -909,7 +909,7 @@ mod grid_not_spanned { let line = records.get_line(pos, index); let (line, line_width) = if cfg.formatting.horizontal_trim { let line = string_trim(line); - let width = string_width(&line); + let width = get_line_width(&line); (line, width) } else { let width = records.get_line_width(pos, index); @@ -925,7 +925,7 @@ mod grid_not_spanned { let cell_width = if cfg.formatting.horizontal_trim { (0..records.count_lines(pos)) .map(|i| records.get_line(pos, i)) - .map(|line| string_width(line.trim())) + .map(|line| get_line_width(line.trim())) .max() .unwrap_or_default() } else { @@ -1775,7 +1775,7 @@ mod grid_spanned { let line = records.get_line(pos, index); let (line, line_width) = if text_cfg.formatting.horizontal_trim { let line = string_trim(line); - let width = string_width(&line); + let width = get_line_width(&line); (line, width) } else { let width = records.get_line_width(pos, index); @@ -1791,7 +1791,7 @@ mod grid_spanned { let cell_width = if text_cfg.formatting.horizontal_trim { (0..records.count_lines(pos)) .map(|i| records.get_line(pos, i)) - .map(|line| string_width(line.trim())) + .map(|line| get_line_width(line.trim())) .max() .unwrap_or_default() } else { diff --git a/papergrid/src/records/peekable_records.rs b/papergrid/src/records/peekable_records.rs index 528ba5f0..b8726f4f 100644 --- a/papergrid/src/records/peekable_records.rs +++ b/papergrid/src/records/peekable_records.rs @@ -17,12 +17,12 @@ pub trait PeekableRecords { /// Returns a width of a text of a cell by an index. fn get_width(&self, pos: Position) -> usize { - crate::util::string::string_width_multiline(self.get_text(pos)) + crate::util::string::get_string_width(self.get_text(pos)) } /// Returns a width of line of a text of a cell by an index. fn get_line_width(&self, pos: Position, line: usize) -> usize { - crate::util::string::string_width(self.get_line(pos, line)) + crate::util::string::get_line_width(self.get_line(pos, line)) } } diff --git a/papergrid/src/records/vec_records/cell_info.rs b/papergrid/src/records/vec_records/cell_info.rs index 7c41bdc5..a3495338 100644 --- a/papergrid/src/records/vec_records/cell_info.rs +++ b/papergrid/src/records/vec_records/cell_info.rs @@ -3,7 +3,7 @@ use std::{borrow::Cow, cmp::max}; use crate::{ records::vec_records::Cell, - util::string::{self, count_lines, get_lines, string_width}, + util::string::{self, count_lines, get_line_width, get_lines}, }; /// The struct is a [Cell] implementation which keeps width information pre allocated. @@ -149,7 +149,7 @@ fn create_cell_info>(text: S) -> Text { // We check if there's only 1 line in which case we don't allocate lines Vec let count_lines = count_lines(info.text.as_ref()); if count_lines < 2 { - info.width = string::string_width_multiline(info.text.as_ref()); + info.width = string::get_string_width(info.text.as_ref()); return info; } @@ -171,7 +171,7 @@ fn create_cell_info>(text: S) -> Text { info.lines = vec![StrWithWidth::new(Cow::Borrowed(""), 0); count_lines]; for (line, i) in get_lines(text).zip(info.lines.iter_mut()) { - i.width = string_width(&line); + i.width = get_line_width(&line); i.text = line; info.width = max(info.width, i.width); } diff --git a/papergrid/src/util/string.rs b/papergrid/src/util/string.rs index 7e7f4a88..a0b8cf32 100644 --- a/papergrid/src/util/string.rs +++ b/papergrid/src/util/string.rs @@ -6,7 +6,7 @@ /// Returns string width and count lines of a string. It's a combination of [`string_width_multiline`] and [`count_lines`]. #[cfg(feature = "std")] -pub fn string_dimension(text: &str) -> (usize, usize) { +pub fn get_string_dimension(text: &str) -> (usize, usize) { #[cfg(not(feature = "ansi"))] { let (lines, acc, max) = text.chars().fold((1, 0, 0), |(lines, acc, max), c| { @@ -24,13 +24,13 @@ pub fn string_dimension(text: &str) -> (usize, usize) { #[cfg(feature = "ansi")] { get_lines(text) - .map(|line| string_width(&line)) + .map(|line| get_line_width(&line)) .fold((0, 0), |(i, acc), width| (i + 1, acc.max(width))) } } /// Returns a string width. -pub fn string_width(text: &str) -> usize { +pub fn get_line_width(text: &str) -> usize { #[cfg(not(feature = "ansi"))] { unicode_width::UnicodeWidthStr::width(text) @@ -50,7 +50,7 @@ pub fn string_width(text: &str) -> usize { } /// Returns a max string width of a line. -pub fn string_width_multiline(text: &str) -> usize { +pub fn get_string_width(text: &str) -> usize { #[cfg(not(feature = "ansi"))] { text.lines() @@ -61,7 +61,7 @@ pub fn string_width_multiline(text: &str) -> usize { #[cfg(feature = "ansi")] { - text.lines().map(string_width).max().unwrap_or(0) + text.lines().map(get_line_width).max().unwrap_or(0) } } @@ -181,22 +181,19 @@ mod tests { fn string_width_emojie_test() { // ...emojis such as “joy”, which normally take up two columns when printed in a terminal // https://github.com/mgeisler/textwrap/pull/276 - assert_eq!(string_width("🎩"), 2); - assert_eq!(string_width("Rust 💕"), 7); - assert_eq!(string_width_multiline("Go 👍\nC 😎"), 5); + assert_eq!(get_line_width("🎩"), 2); + assert_eq!(get_line_width("Rust 💕"), 7); + assert_eq!(get_string_width("Go 👍\nC 😎"), 5); } #[cfg(feature = "ansi")] #[test] fn colored_string_width_test() { use owo_colors::OwoColorize; - assert_eq!(string_width(&"hello world".red().to_string()), 11); - assert_eq!( - string_width_multiline(&"hello\nworld".blue().to_string()), - 5 - ); - assert_eq!(string_width("\u{1b}[34m0\u{1b}[0m"), 1); - assert_eq!(string_width(&"0".red().to_string()), 1); + assert_eq!(get_line_width(&"hello world".red().to_string()), 11); + assert_eq!(get_string_width(&"hello\nworld".blue().to_string()), 5); + assert_eq!(get_line_width("\u{1b}[34m0\u{1b}[0m"), 1); + assert_eq!(get_line_width(&"0".red().to_string()), 1); } #[test] @@ -212,7 +209,7 @@ mod tests { #[test] fn string_width_multinline_for_link() { assert_eq!( - string_width_multiline( + get_string_width( "\u{1b}]8;;file:///home/nushell/asd.zip\u{1b}\\asd.zip\u{1b}]8;;\u{1b}\\" ), 7 @@ -223,7 +220,9 @@ mod tests { #[test] fn string_width_for_link() { assert_eq!( - string_width("\u{1b}]8;;file:///home/nushell/asd.zip\u{1b}\\asd.zip\u{1b}]8;;\u{1b}\\"), + get_line_width( + "\u{1b}]8;;file:///home/nushell/asd.zip\u{1b}\\asd.zip\u{1b}]8;;\u{1b}\\" + ), 7 ); } @@ -232,7 +231,7 @@ mod tests { #[test] fn string_dimension_test() { assert_eq!( - string_dimension("\u{1b}[37mnow is the time for all good men\n\u{1b}[0m"), + get_string_dimension("\u{1b}[37mnow is the time for all good men\n\u{1b}[0m"), { #[cfg(feature = "ansi")] { @@ -245,11 +244,11 @@ mod tests { } ); assert_eq!( - string_dimension("now is the time for all good men\n"), + get_string_dimension("now is the time for all good men\n"), (2, 32) ); - assert_eq!(string_dimension("asd"), (1, 3)); - assert_eq!(string_dimension(""), (1, 0)); + assert_eq!(get_string_dimension("asd"), (1, 3)); + assert_eq!(get_string_dimension(""), (1, 0)); } #[cfg(feature = "std")] diff --git a/tabled/examples/shadow.rs b/tabled/examples/shadow.rs index bde2cfeb..33637f0a 100644 --- a/tabled/examples/shadow.rs +++ b/tabled/examples/shadow.rs @@ -129,7 +129,7 @@ fn create_small_table(style: Borders) -> Table { // todo: very likely can be simplified fn create_main_table(message: &str) -> Table { - let (count_lines, message_width) = string::string_dimension(message); + let (count_lines, message_width) = string::get_string_dimension(message); let count_additional_separators = if count_lines > 2 { count_lines - 2 } else { 0 }; let left_table_space = (0..count_additional_separators) .map(|_| " ║ \n") diff --git a/tabled/src/grid/records/into_records/truncate_records.rs b/tabled/src/grid/records/into_records/truncate_records.rs index 1bdc6ac3..19f83f6c 100644 --- a/tabled/src/grid/records/into_records/truncate_records.rs +++ b/tabled/src/grid/records/into_records/truncate_records.rs @@ -2,8 +2,7 @@ use crate::{ grid::dimension::Dimension, grid::records::into_records::either_string::EitherString, - grid::records::IntoRecords, grid::util::string::string_width_multiline, - settings::width::Truncate, + grid::records::IntoRecords, grid::util::string::get_string_width, settings::width::Truncate, }; /// A records iterator which truncates all cells to a given width. @@ -88,7 +87,7 @@ where let width = self.dimension.get_width(self.iter_column); self.iter_column += 1; - let text_width = string_width_multiline(text_ref); + let text_width = get_string_width(text_ref); let is_small = text_width <= width; if is_small { Some(EitherString::Some(text)) diff --git a/tabled/src/settings/measurement/mod.rs b/tabled/src/settings/measurement/mod.rs index a5bdf1de..e0c5cfc6 100644 --- a/tabled/src/settings/measurement/mod.rs +++ b/tabled/src/settings/measurement/mod.rs @@ -4,7 +4,7 @@ use crate::{ grid::config::SpannedConfig, grid::dimension::SpannedGridDimension, grid::records::{ExactRecords, IntoRecords, PeekableRecords, Records}, - grid::util::string::{self, string_width_multiline}, + grid::util::string::{self, get_string_width}, settings::{Height, Width}, }; @@ -119,7 +119,7 @@ where { let (count_rows, count_cols) = (records.count_rows(), records.count_columns()); (0..count_rows).map(move |row| { - (0..count_cols).map(move |col| string_width_multiline(records.get_text((row, col)))) + (0..count_cols).map(move |col| get_string_width(records.get_text((row, col)))) }) } diff --git a/tabled/src/settings/themes/column_names.rs b/tabled/src/settings/themes/column_names.rs index 510f15c5..698abc82 100644 --- a/tabled/src/settings/themes/column_names.rs +++ b/tabled/src/settings/themes/column_names.rs @@ -8,7 +8,7 @@ use crate::{ vec_records::{Text, VecRecords}, ExactRecords, PeekableRecords, Records, Resizable, }, - util::string::string_width, + util::string::get_line_width, }, settings::{ object::{Column, Row}, @@ -278,7 +278,7 @@ fn set_column_text( let widths = names .iter() .enumerate() - .map(|(col, name)| (cmp::max(string_width(name), dims.get_width(col)))) + .map(|(col, name)| (cmp::max(get_line_width(name), dims.get_width(col)))) .collect::>(); dims.set_widths(widths.clone()); @@ -314,7 +314,7 @@ fn set_row_text( let heights = names .iter() .enumerate() - .map(|(row, name)| (cmp::max(string_width(name), dims.get_height(row)))) + .map(|(row, name)| (cmp::max(get_line_width(name), dims.get_height(row)))) .collect::>(); dims.set_heights(heights.clone()); @@ -399,16 +399,16 @@ fn get_color(colors: &Option>, i: usize) -> Option<&Color> { fn get_horizontal_indent(text: &str, align: AlignmentHorizontal, available: usize) -> usize { match align { AlignmentHorizontal::Left => 0, - AlignmentHorizontal::Right => available - string_width(text), - AlignmentHorizontal::Center => (available - string_width(text)) / 2, + AlignmentHorizontal::Right => available - get_line_width(text), + AlignmentHorizontal::Center => (available - get_line_width(text)) / 2, } } fn get_vertical_indent(text: &str, align: AlignmentVertical, available: usize) -> usize { match align { AlignmentVertical::Top => 0, - AlignmentVertical::Bottom => available - string_width(text), - AlignmentVertical::Center => (available - string_width(text)) / 2, + AlignmentVertical::Bottom => available - get_line_width(text), + AlignmentVertical::Center => (available - get_line_width(text)) / 2, } } diff --git a/tabled/src/settings/width/min_width.rs b/tabled/src/settings/width/min_width.rs index 31faed6a..7e7ac5ce 100644 --- a/tabled/src/settings/width/min_width.rs +++ b/tabled/src/settings/width/min_width.rs @@ -6,7 +6,7 @@ use crate::{ grid::config::{ColoredConfig, Entity}, grid::dimension::CompleteDimensionVecRecords, grid::records::{ExactRecords, IntoRecords, PeekableRecords, Records, RecordsMut}, - grid::util::string::{get_lines, string_width_multiline}, + grid::util::string::{get_lines, get_string_width}, settings::{ measurement::Measurement, peaker::{Peaker, PriorityNone}, @@ -119,7 +119,7 @@ where } let cell = records.get_text(pos); - let cell_width = string_width_multiline(cell); + let cell_width = get_string_width(cell); if cell_width >= width { continue; } @@ -191,12 +191,12 @@ where } fn increase_width(s: &str, width: usize, fill_with: char) -> String { - use crate::grid::util::string::string_width; + use crate::grid::util::string::get_line_width; use std::{borrow::Cow, iter::repeat}; get_lines(s) .map(|line| { - let length = string_width(&line); + let length = get_line_width(&line); if length < width { let mut line = line.into_owned(); diff --git a/tabled/src/settings/width/truncate.rs b/tabled/src/settings/width/truncate.rs index 5ca02fee..1b3e7fc4 100644 --- a/tabled/src/settings/width/truncate.rs +++ b/tabled/src/settings/width/truncate.rs @@ -9,7 +9,7 @@ use crate::{ config::{ColoredConfig, Entity, SpannedConfig}, dimension::CompleteDimensionVecRecords, records::{EmptyRecords, ExactRecords, IntoRecords, PeekableRecords, Records, RecordsMut}, - util::string::{string_width, string_width_multiline}, + util::string::{get_line_width, get_string_width}, }, settings::{ measurement::Measurement, @@ -216,7 +216,7 @@ where let text = records.get_text(pos); - let cell_width = string_width_multiline(text); + let cell_width = get_string_width(text); if available >= cell_width { continue; } @@ -284,7 +284,7 @@ fn need_suffix_color_preservation(_suffix: &Option>) -> bool } fn make_suffix<'a>(suffix: &'a TruncateSuffix<'_>, width: usize) -> (Cow<'a, str>, usize) { - let suffix_length = string_width(&suffix.text); + let suffix_length = get_line_width(&suffix.text); if width > suffix_length { return (Cow::Borrowed(suffix.text.as_ref()), width - suffix_length); } diff --git a/tabled/src/settings/width/wrap.rs b/tabled/src/settings/width/wrap.rs index 7a3efa1b..1b537474 100644 --- a/tabled/src/settings/width/wrap.rs +++ b/tabled/src/settings/width/wrap.rs @@ -9,7 +9,7 @@ use crate::{ config::{ColoredConfig, Entity}, dimension::CompleteDimensionVecRecords, records::{EmptyRecords, ExactRecords, IntoRecords, PeekableRecords, Records, RecordsMut}, - util::string::string_width_multiline, + util::string::get_string_width, }, settings::{ measurement::Measurement, @@ -151,7 +151,7 @@ where } let text = records.get_text(pos); - let cell_width = string_width_multiline(text); + let cell_width = get_string_width(text); if cell_width <= width { continue; } diff --git a/tabled/src/tables/compact.rs b/tabled/src/tables/compact.rs index 231ca078..a28a6547 100644 --- a/tabled/src/tables/compact.rs +++ b/tabled/src/tables/compact.rs @@ -76,7 +76,7 @@ use crate::{ into_records::{LimitColumns, LimitRows}, IntoRecords, IterRecords, }, - util::string::string_width, + util::string::get_line_width, CompactGrid, }, settings::{style::Style, TableOption}, @@ -256,7 +256,7 @@ where for row in mat.iter() { for (col, text) in row.iter().enumerate() { let text = text.as_ref(); - let text_width = string_width(text); + let text_width = get_line_width(text); width[col] = max(width[col], text_width); } } diff --git a/tabled/src/tables/extended.rs b/tabled/src/tables/extended.rs index 7a806e9a..ffc7bd83 100644 --- a/tabled/src/tables/extended.rs +++ b/tabled/src/tables/extended.rs @@ -48,7 +48,7 @@ //! assert_eq!(table, expected); //! ``` -use crate::grid::util::string::string_width; +use crate::grid::util::string::get_line_width; use crate::Tabled; use std::cell::RefCell; use std::fmt::{self, Debug, Display}; @@ -127,7 +127,7 @@ impl ExtendedTable { return false; } - let suffix_width = string_width(suffix); + let suffix_width = get_line_width(suffix); if max < suffix_width { return false; } @@ -137,7 +137,7 @@ impl ExtendedTable { let fields_max_width = self .fields .iter() - .map(|s| string_width(s)) + .map(|s| get_line_width(s)) .max() .unwrap_or_default(); @@ -208,14 +208,14 @@ impl Display for ExtendedTable { let max_field_width = fields .iter() - .map(|s| string_width(s)) + .map(|s| get_line_width(s)) .max() .unwrap_or_default(); let max_values_length = self .records .iter() - .map(|record| record.iter().map(|s| string_width(s)).max()) + .map(|record| record.iter().map(|s| get_line_width(s)).max()) .max() .unwrap_or_default() .unwrap_or_default(); diff --git a/tabled/src/tables/table_pool.rs b/tabled/src/tables/table_pool.rs index b1ee328c..991a98f9 100644 --- a/tabled/src/tables/table_pool.rs +++ b/tabled/src/tables/table_pool.rs @@ -251,7 +251,7 @@ mod print { dimension::{Dimension, DimensionPriority, Estimate, PoolTableDimension}, records::Records, util::string::{ - count_lines, get_lines, string_dimension, string_width, string_width_multiline, + count_lines, get_line_width, get_lines, get_string_dimension, get_string_width, }, }, settings::{Padding, Style, TableOption}, @@ -631,7 +631,7 @@ mod print { if lines_alignment { for line in get_lines(text) { - let line_width = string_width(&line); + let line_width = get_line_width(&line); let (left, right) = indent_horizontal(halignment, width, line_width); if border.has_left() { @@ -659,11 +659,11 @@ mod print { line_index += 1; } } else { - let text_width = string_width_multiline(text); + let text_width = get_string_width(text); let (left, _) = indent_horizontal(halignment, width, text_width); for line in get_lines(text) { - let line_width = string_width(&line); + let line_width = get_line_width(&line); let right = width - line_width - left; if border.has_left() { @@ -1465,7 +1465,7 @@ mod print { } fn str_dimension(text: &str, cfg: &CompactMultilineConfig) -> Dim { - let (count_lines, width) = string_dimension(text); + let (count_lines, width) = get_string_dimension(text); let w = width + get_padding_horizontal(cfg); let h = count_lines + get_padding_vertical(cfg); Dim::new(w, h) @@ -1590,7 +1590,7 @@ mod print { } let mut buf = String::new(); - let width = string_width_multiline(table); + let width = get_string_width(table); let top_color = color.top; let bottom_color = color.bottom; let left_color = color.left; diff --git a/tabled/src/util/string.rs b/tabled/src/util/string.rs index 7eadea8b..da632d68 100644 --- a/tabled/src/util/string.rs +++ b/tabled/src/util/string.rs @@ -180,7 +180,7 @@ pub(crate) fn strip_osc(text: &str) -> (String, Option) { mod tests { use super::*; - use crate::grid::util::string::string_width; + use crate::grid::util::string::get_line_width; #[cfg(feature = "ansi")] use owo_colors::{colors::Yellow, OwoColorize}; @@ -201,7 +201,7 @@ mod tests { assert_eq!(cut_str("🏳️🏳️", 0), ""); assert_eq!(cut_str("🏳️🏳️", 1), "🏳"); assert_eq!(cut_str("🏳️🏳️", 2), "🏳\u{fe0f}🏳"); - assert_eq!(string_width("🏳️🏳️"), string_width("🏳\u{fe0f}🏳\u{fe0f}")); + assert_eq!(get_line_width("🏳️🏳️"), get_line_width("🏳\u{fe0f}🏳\u{fe0f}")); assert_eq!(cut_str("🎓", 1), "�"); assert_eq!(cut_str("🎓", 2), "🎓"); @@ -257,8 +257,8 @@ mod tests { "\u{1b}[31;100m🏳\u{fe0f}🏳\u{1b}[39m\u{1b}[49m" ); assert_eq!( - string_width(&emojies), - string_width("\u{1b}[31;100m🏳\u{fe0f}🏳\u{fe0f}\u{1b}[39m\u{1b}[49m") + get_line_width(&emojies), + get_line_width("\u{1b}[31;100m🏳\u{fe0f}🏳\u{fe0f}\u{1b}[39m\u{1b}[49m") ); } diff --git a/tabled/tests/settings/margin_test.rs b/tabled/tests/settings/margin_test.rs index f103ed8e..3a29d02d 100644 --- a/tabled/tests/settings/margin_test.rs +++ b/tabled/tests/settings/margin_test.rs @@ -104,10 +104,7 @@ fn table_with_margin_and_max_width() { .with(Width::increase(50)) .to_string(); - assert_eq!( - tabled::grid::util::string::string_width_multiline(&table), - 50 - ); + assert_eq!(tabled::grid::util::string::get_string_width(&table), 50); assert_eq!( table, static_table!( diff --git a/tabled/tests/settings/width_test.rs b/tabled/tests/settings/width_test.rs index f26610ad..c06fd892 100644 --- a/tabled/tests/settings/width_test.rs +++ b/tabled/tests/settings/width_test.rs @@ -1,7 +1,7 @@ #![cfg(feature = "std")] use tabled::{ - grid::util::string::string_width_multiline, + grid::util::string::get_string_width, settings::{ formatting::{TabSize, TrimStrategy}, object::{Columns, Object, Rows, Segment}, @@ -877,7 +877,7 @@ fn total_width_big() { .with(MinWidth::new(80)) .to_string(); - assert_eq!(string_width_multiline(&table), 80); + assert_eq!(get_string_width(&table), 80); assert_eq!( table, static_table!( @@ -895,7 +895,7 @@ fn total_width_big() { .with(Settings::new(Width::truncate(80), Width::increase(80))) .to_string(); - assert_eq!(string_width_multiline(&table), 80); + assert_eq!(get_string_width(&table), 80); assert_eq!( table, static_table!( @@ -1366,7 +1366,7 @@ fn min_width_works_with_right_alignment() { ) .with(MinWidth::new(50)); - assert_eq!(string_width_multiline(&table.to_string()), 50); + assert_eq!(get_string_width(&table.to_string()), 50); assert_eq!( table.to_string(), static_table!( @@ -1458,7 +1458,7 @@ fn min_width_works_with_right_alignment() { "| |" ) ); - assert_eq!(string_width_multiline(&table.to_string()), 50); + assert_eq!(get_string_width(&table.to_string()), 50); table .with(Modify::new(Segment::all()).with(TrimStrategy::Horizontal)) @@ -1521,7 +1521,7 @@ fn min_width_with_span_1() { .with(MinWidth::new(100)) .to_string(); - assert_eq!(string_width_multiline(&table), 100); + assert_eq!(get_string_width(&table), 100); assert_eq!( table, static_table!( @@ -1549,7 +1549,7 @@ fn min_width_with_span_2() { .with(MinWidth::new(100)) .to_string(); - assert_eq!(string_width_multiline(&table), 100); + assert_eq!(get_string_width(&table), 100); assert_eq!( table, static_table!( @@ -1724,7 +1724,7 @@ fn max_width_truncate_with_big_span() { .with(Width::truncate(40)) .to_string(); - assert_eq!(string_width_multiline(&table), 40); + assert_eq!(get_string_width(&table), 40); assert_eq!( table, static_table!( @@ -1774,7 +1774,7 @@ fn max_width_truncate_with_big_span() { "| | 2-0 | Hello World With Big Line |" ) ); - assert_eq!(string_width_multiline(&table), 40); + assert_eq!(get_string_width(&table), 40); let table = Matrix::new(3, 3) .insert((2, 1), "Hello World With Big Line; Here we gooooooo") @@ -1785,7 +1785,7 @@ fn max_width_truncate_with_big_span() { .with(Width::truncate(40)) .to_string(); - assert_eq!(string_width_multiline(&table), 40); + assert_eq!(get_string_width(&table), 40); assert_eq!( table, static_table!( @@ -2211,7 +2211,7 @@ fn min_width_priority_max() { .with(MinWidth::new(60).priority(PriorityMax)) .to_string(); - assert_eq!(string_width_multiline(&table), 60); + assert_eq!(get_string_width(&table), 60); assert_eq!( table, static_table!( @@ -2231,7 +2231,7 @@ fn min_width_priority_min() { .with(MinWidth::new(60).priority(PriorityMin)) .to_string(); - assert_eq!(string_width_multiline(&table), 60); + assert_eq!(get_string_width(&table), 60); assert_eq!( table, static_table!( @@ -2273,7 +2273,7 @@ fn min_width_is_not_used_after_padding() { .with(Modify::new((0, 0)).with(Padding::new(2, 2, 0, 0))) .to_string(); - assert_eq!(string_width_multiline(&table), 40); + assert_eq!(get_string_width(&table), 40); assert_eq!( table, static_table!( @@ -2294,7 +2294,7 @@ fn min_width_is_used_after_margin() { .with(Width::increase(60)) .to_string(); - assert_eq!(string_width_multiline(&table), 60); + assert_eq!(get_string_width(&table), 60); assert_eq!( table, static_table!( @@ -2316,10 +2316,7 @@ fn wrap_keeping_words_0() { .with(Width::wrap(8).keep_words(true)) .to_string(); - assert_eq!( - tabled::grid::util::string::string_width_multiline(&table), - 8 - ); + assert_eq!(tabled::grid::util::string::get_string_width(&table), 8); assert_eq!( table, @@ -2612,7 +2609,7 @@ mod derived { "| \u{1b}[37m.4\u{1b}[39m | | | |" ) ); - assert_eq!(string_width_multiline(&table), 57); + assert_eq!(get_string_width(&table), 57); let table = Matrix::iter(&data) .with(Style::markdown()) @@ -2635,7 +2632,7 @@ mod derived { "| \u{1b}[37m.4\u{1b}[39m | | | |" ) ); - assert_eq!(string_width_multiline(&table), 57); + assert_eq!(get_string_width(&table), 57); } #[cfg(feature = "ansi")] @@ -2697,7 +2694,7 @@ mod derived { table, "| ver | published_d | is_act | major_feature |\n|-----|-------------|--------|--------------------------|\n| \u{1b}[31m0.2\u{1b}[39m | \u{1b}[48;2;8;10;30m\u{1b}[31m2021-06-23\u{1b}[39m\u{1b}[49m | true | \u{1b}[34;42m#[header(inline)] attrib\u{1b}[39m\u{1b}[49m |\n| \u{1b}[31m0.2\u{1b}[39m | \u{1b}[48;2;8;100;30m\u{1b}[32m2021-06-19\u{1b}[39m\u{1b}[49m | false | \u{1b}[33mAPI changes\u{1b}[39m |\n| \u{1b}[37m0.1\u{1b}[39m | \u{1b}[48;2;8;10;30m\u{1b}[31m2021-06-07\u{1b}[39m\u{1b}[49m | false | \u{1b}[31;40mdisplay_with attribute\u{1b}[0m |" ); - assert_eq!(string_width_multiline(&table), 57); + assert_eq!(get_string_width(&table), 57); } #[cfg(feature = "ansi")]