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

Theme refactorings (basically move to old approach) && PaddingExpand #419

Merged
merged 20 commits into from
Jul 22, 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
4 changes: 2 additions & 2 deletions papergrid/examples/span_usage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use papergrid::{
},
dimension::{spanned::SpannedGridDimension, Estimate},
grid::peekable::PeekableGrid,
records::vec_records::{CellInfo, VecRecords},
records::vec_records::{Text, VecRecords},
};

fn main() {
Expand Down Expand Up @@ -49,7 +49,7 @@ fn main() {

let data = data
.iter()
.map(|row| row.iter().map(CellInfo::new).collect())
.map(|row| row.iter().map(Text::new).collect())
.collect();

let records = VecRecords::new(data);
Expand Down
9 changes: 7 additions & 2 deletions papergrid/src/ansi/ansi_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,23 @@ impl<'a> ANSIStr<'a> {
Self { prefix, suffix }
}

/// Constructs a new instance with suffix and prefix set to empty strings.
pub const fn empty() -> Self {
Self::new("", "")
}

/// Verifies if anything was actually set.
pub const fn is_empty(&self) -> bool {
self.prefix.is_empty() && self.suffix.is_empty()
}

/// Gets a reference to a prefix.
pub fn get_prefix(&self) -> &'a str {
pub const fn get_prefix(&self) -> &'a str {
self.prefix
}

/// Gets a reference to a suffix.
pub fn get_suffix(&self) -> &'a str {
pub const fn get_suffix(&self) -> &'a str {
self.suffix
}
}
Expand Down
20 changes: 14 additions & 6 deletions papergrid/src/config/sides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,19 @@ impl<T> Sides<T> {
where
T: Copy,
{
Self {
top: value,
bottom: value,
left: value,
right: value,
}
Self::new(value, value, value, value)
}

/// Creates a new object.
pub fn convert_into<T1>(self) -> Sides<T1>
where
T: Into<T1>,
{
Sides::new(
self.left.into(),
self.right.into(),
self.top.into(),
self.bottom.into(),
)
}
}
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
30 changes: 20 additions & 10 deletions papergrid/src/records/vec_records/cell_info.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
use core::fmt::Display;
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.
#[derive(Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
pub struct CellInfo<S> {
pub struct Text<S> {
text: S,
width: usize,
lines: Vec<StrWithWidth<'static>>,
}

impl<S> CellInfo<S> {
impl<S> Text<S> {
/// Creates a new instance of the structure.
pub fn new(text: S) -> Self
where
Expand All @@ -33,7 +34,7 @@ impl<S> CellInfo<S> {
}
}

impl<S> AsRef<str> for CellInfo<S>
impl<S> AsRef<str> for Text<S>
where
S: AsRef<str>,
{
Expand All @@ -42,7 +43,16 @@ where
}
}

impl<S> Cell for CellInfo<S>
impl<S> Display for Text<S>
where
S: Display,
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.text.fmt(f)
}
}

impl<S> Cell for Text<S>
where
S: AsRef<str>,
{
Expand Down Expand Up @@ -75,7 +85,7 @@ where
}
}

impl<S> Clone for CellInfo<S>
impl<S> Clone for Text<S>
where
S: Clone + AsRef<str>,
{
Expand Down Expand Up @@ -128,8 +138,8 @@ impl<'a> StrWithWidth<'a> {
}
}

fn create_cell_info<S: AsRef<str>>(text: S) -> CellInfo<S> {
let mut info = CellInfo {
fn create_cell_info<S: AsRef<str>>(text: S) -> Text<S> {
let mut info = Text {
text,
lines: vec![],
width: 0,
Expand All @@ -139,7 +149,7 @@ fn create_cell_info<S: AsRef<str>>(text: S) -> CellInfo<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 @@ -161,7 +171,7 @@ fn create_cell_info<S: AsRef<str>>(text: S) -> CellInfo<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
2 changes: 1 addition & 1 deletion papergrid/src/records/vec_records/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::ops::{Deref, DerefMut};
use super::PeekableRecords;

pub use cell::Cell;
pub use cell_info::{CellInfo, StrWithWidth};
pub use cell_info::{StrWithWidth, Text};

/// A [Records] implementation based on allocated buffers.
#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord)]
Expand Down
Loading
Loading