Skip to content

Commit

Permalink
rename string_width_multiline into get_string_width
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiburt committed Jul 20, 2024
1 parent cfc078e commit 144bb0e
Show file tree
Hide file tree
Showing 21 changed files with 102 additions and 110 deletions.
4 changes: 2 additions & 2 deletions papergrid/src/dimension/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions papergrid/src/dimension/spanned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions papergrid/src/grid/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions papergrid/src/grid/iterable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -301,12 +301,12 @@ fn print_single_line_column<F: Write, C: ANSIFmt>(

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)
};
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand Down
14 changes: 7 additions & 7 deletions papergrid/src/grid/peekable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions papergrid/src/records/peekable_records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down
6 changes: 3 additions & 3 deletions papergrid/src/records/vec_records/cell_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -149,7 +149,7 @@ fn create_cell_info<S: AsRef<str>>(text: S) -> Text<S> {
// 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;
}

Expand All @@ -171,7 +171,7 @@ fn create_cell_info<S: AsRef<str>>(text: S) -> Text<S> {

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);
}
Expand Down
41 changes: 20 additions & 21 deletions papergrid/src/util/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -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]
Expand All @@ -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
Expand All @@ -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
);
}
Expand All @@ -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")]
{
Expand All @@ -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")]
Expand Down
2 changes: 1 addition & 1 deletion tabled/examples/shadow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ fn create_small_table(style: Borders<char>) -> 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")
Expand Down
5 changes: 2 additions & 3 deletions tabled/src/grid/records/into_records/truncate_records.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 2 additions & 2 deletions tabled/src/settings/measurement/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
};

Expand Down Expand Up @@ -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))))
})
}

Expand Down
Loading

0 comments on commit 144bb0e

Please sign in to comment.