Skip to content

Commit

Permalink
Merge pull request #454 from zhiburt/patch/tabled/create-priority
Browse files Browse the repository at this point in the history
tabled/ Add Priority instead of a Priority*
  • Loading branch information
zhiburt authored Nov 20, 2024
2 parents 6afc4f7 + 60f2795 commit 15dbb36
Show file tree
Hide file tree
Showing 4 changed files with 256 additions and 25 deletions.
6 changes: 3 additions & 3 deletions tabled/examples/height.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
//! row heights irrespective of [`Padding`] or [`Margin`].
use tabled::{
settings::{peaker::PriorityMax, Height, Style},
settings::{peaker::Priority, Height, Style},
Table,
};

Expand All @@ -33,15 +33,15 @@ fn main() {

let table_ = table
.clone()
.with(Height::limit(4).priority(PriorityMax::default()))
.with(Height::limit(4).priority(Priority::max(true)))
.to_string();

println!("Table decrease height to 4\n");
println!("{table_}");

let table_ = table
.clone()
.with(Height::limit(0).priority(PriorityMax::default()))
.with(Height::limit(0).priority(Priority::max(true)))
.to_string();

println!("Table decrease height to 0\n");
Expand Down
4 changes: 2 additions & 2 deletions tabled/examples/terminal_size2/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use tabled::{
builder::Builder,
settings::{peaker::PriorityMax, Height, Settings, Width},
settings::{peaker::Priority, Height, Settings, Width},
Table,
};
use terminal_size::{terminal_size, Height as TerminalHeight, Width as TerminalWidth};
Expand All @@ -28,7 +28,7 @@ fn main() {
let (width, height) = get_terminal_size();

let term_size_settings = Settings::default()
.with(Width::wrap(width).priority(PriorityMax::default()))
.with(Width::wrap(width).priority(Priority::right(true)))
.with(Width::increase(width))
.with(Height::limit(height))
.with(Height::increase(height));
Expand Down
260 changes: 251 additions & 9 deletions tabled/src/settings/peaker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,260 @@ mod min;
mod none;
mod right;

/// A strategy of width function.
/// It determines the order how the function is applied.
pub trait Peaker {
/// This function returns a column index which will be changed.
/// Or `None` if no changes are necessary.
fn peak(&mut self, min_widths: &[usize], widths: &[usize]) -> Option<usize>;
}

pub use left::PriorityLeft;
pub use max::PriorityMax;
pub use min::PriorityMin;
pub use none::PriorityNone;
pub use right::PriorityRight;

// todo: Priority::max -->
/// A strategy of width function.
/// It determines the order how a function is applied.
///
/// For example which column we shall peak to truncate when doing width alogorithms.
pub trait Peaker {
/// This function returns an index which will be changed.
/// Or `None` if no changes are necessary.
///
/// When [`None`] returned the alogorithm must be stopped.
fn peak(&mut self, mins: &[usize], values: &[usize]) -> Option<usize>;
}

/// An abstract factory to construct different [`Peaker`] methods.
///
/// ```
/// # use tabled::{Table, settings::{Style, peaker::Priority, Width}};
/// # use testing_table::assert_table;
///
/// let data = [
/// ("1", "Hello", 100),
/// ("2", "World", 1000),
/// ];
///
/// let mut table = Table::new(data);
/// table.with(Style::modern());
/// table.with(Width::wrap(15).priority(Priority::max(false)));
///
/// let output = table.to_string();
///
/// assert_table!(
/// output,
/// "┌───┬────┬────┐"
/// "│ & │ &s │ i3 │"
/// "│ s │ tr │ 2 │"
/// "│ t │ │ │"
/// "│ r │ │ │"
/// "├───┼────┼────┤"
/// "│ 1 │ He │ 10 │"
/// "│ │ ll │ 0 │"
/// "│ │ o │ │"
/// "├───┼────┼────┤"
/// "│ 2 │ Wo │ 10 │"
/// "│ │ rl │ 00 │"
/// "│ │ d │ │"
/// "└───┴────┴────┘"
/// );
/// ```
#[derive(Debug, Clone, Copy, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct Priority;

impl Priority {
/// Returns a [`Peaker`] which goes over list one by one,
/// in order from left to right with no prioritization,
/// just peaking each value after another.
///
/// ```
/// # use tabled::{Table, settings::{Style, peaker::Priority, Width}};
/// # use testing_table::assert_table;
///
/// let data = [
/// ("1", "Hello", 100),
/// ("2", "World", 1000),
/// ];
///
/// let mut table = Table::new(data);
/// table.with(Style::modern());
/// table.with(Width::wrap(15).priority(Priority::none()));
///
/// let output = table.to_string();
///
/// assert_table!(
/// output,
/// "┌───┬────┬────┐"
/// "│ & │ &s │ i3 │"
/// "│ s │ tr │ 2 │"
/// "│ t │ │ │"
/// "│ r │ │ │"
/// "├───┼────┼────┤"
/// "│ 1 │ He │ 10 │"
/// "│ │ ll │ 0 │"
/// "│ │ o │ │"
/// "├───┼────┼────┤"
/// "│ 2 │ Wo │ 10 │"
/// "│ │ rl │ 00 │"
/// "│ │ d │ │"
/// "└───┴────┴────┘"
/// );
/// ```
pub fn none() -> PriorityNone {
PriorityNone::new()
}

/// Returns a [`Peaker`] which goes over list peacking a minimum value,
/// and prioritizing a chosen side when equal values are met.
///
/// ```
/// # use tabled::{Table, settings::{Style, peaker::Priority, Width}};
/// # use testing_table::assert_table;
///
/// let data = [
/// ("1", "Hello", 100),
/// ("2", "World", 1000),
/// ];
///
/// let mut table = Table::new(data);
/// table.with(Style::modern());
/// table.with(Width::wrap(15).priority(Priority::min(true)));
///
/// let output = table.to_string();
///
/// assert_table!(
/// output,
/// "┌──┬───────┬──┐"
/// "│ │ &str │ │"
/// "├──┼───────┼──┤"
/// "│ │ Hello │ │"
/// "├──┼───────┼──┤"
/// "│ │ World │ │"
/// "└──┴───────┴──┘"
/// );
/// ```
pub fn min(right: bool) -> PriorityMin {
PriorityMin::new(right)
}

/// Returns a [`Peaker`] which goes over list peacking a maximum value,
/// and prioritizing a chosen side when equal values are met.
///
/// ```
/// # use tabled::{Table, settings::{Style, peaker::Priority, Width}};
/// # use testing_table::assert_table;
///
/// let data = [
/// ("1", "Hello", 100),
/// ("2", "World", 1000),
/// ];
///
/// let mut table = Table::new(data);
/// table.with(Style::modern());
/// table.with(Width::wrap(15).priority(Priority::max(true)));
///
/// let output = table.to_string();
///
/// assert_table!(
/// output,
/// "┌────┬────┬───┐"
/// "│ &s │ &s │ i │"
/// "│ tr │ tr │ 3 │"
/// "│ │ │ 2 │"
/// "├────┼────┼───┤"
/// "│ 1 │ He │ 1 │"
/// "│ │ ll │ 0 │"
/// "│ │ o │ 0 │"
/// "├────┼────┼───┤"
/// "│ 2 │ Wo │ 1 │"
/// "│ │ rl │ 0 │"
/// "│ │ d │ 0 │"
/// "│ │ │ 0 │"
/// "└────┴────┴───┘"
/// );
/// ```
pub fn max(right: bool) -> PriorityMax {
PriorityMax::new(right)
}

/// Returns a [`Peaker`] which goes over list peacking a left most value as far as possible.
///
/// ```
/// # use tabled::{Table, settings::{Style, peaker::Priority, Width}};
/// # use testing_table::assert_table;
///
/// let data = [
/// ("1", "Hello", 100),
/// ("2", "World", 1000),
/// ];
///
/// let mut table = Table::new(data);
/// table.with(Style::modern());
/// table.with(Width::wrap(15).priority(Priority::left()));
///
/// let output = table.to_string();
///
/// assert_table!(
/// output,
/// "┌──┬───┬──────┐"
/// "│ │ & │ i32 │"
/// "│ │ s │ │"
/// "│ │ t │ │"
/// "│ │ r │ │"
/// "├──┼───┼──────┤"
/// "│ │ H │ 100 │"
/// "│ │ e │ │"
/// "│ │ l │ │"
/// "│ │ l │ │"
/// "│ │ o │ │"
/// "├──┼───┼──────┤"
/// "│ │ W │ 1000 │"
/// "│ │ o │ │"
/// "│ │ r │ │"
/// "│ │ l │ │"
/// "│ │ d │ │"
/// "└──┴───┴──────┘"
/// );
/// ```
pub fn left() -> PriorityLeft {
PriorityLeft::new()
}

/// Returns a [`Peaker`] which goes over list peacking a right most value as far as possible.
///
/// ```
/// # use tabled::{Table, settings::{Style, peaker::Priority, Width}};
/// # use testing_table::assert_table;
///
/// let data = [
/// ("1", "Hello", 100),
/// ("2", "World", 1000),
/// ];
///
/// let mut table = Table::new(data);
/// table.with(Style::modern());
/// table.with(Width::wrap(15).priority(Priority::right()));
///
/// let output = table.to_string();
///
/// assert_table!(
/// output,
/// "┌──────┬───┬──┐"
/// "│ &str │ & │ │"
/// "│ │ s │ │"
/// "│ │ t │ │"
/// "│ │ r │ │"
/// "├──────┼───┼──┤"
/// "│ 1 │ H │ │"
/// "│ │ e │ │"
/// "│ │ l │ │"
/// "│ │ l │ │"
/// "│ │ o │ │"
/// "├──────┼───┼──┤"
/// "│ 2 │ W │ │"
/// "│ │ o │ │"
/// "│ │ r │ │"
/// "│ │ l │ │"
/// "│ │ d │ │"
/// "└──────┴───┴──┘"
/// );
/// ```
pub fn right() -> PriorityRight {
PriorityRight::new()
}
}
11 changes: 0 additions & 11 deletions tabled/src/settings/width/wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,6 @@ fn chunks(s: &str, width: usize) -> Vec<String> {
return Vec::new();
}

println!("---> {:?}", s);

let mut prev_newline = false;
let mut buf = String::with_capacity(width);
let mut list = Vec::new();
Expand Down Expand Up @@ -324,8 +322,6 @@ fn chunks(s: &str, width: usize, prefix: &str, suffix: &str) -> Vec<String> {
let _ = write!(&mut line, "{}", text_style.start());

while !text_slice.is_empty() {
println!("... {} {}", width, line_width);

let available_space = width - line_width;

let part_width = get_text_width(text_slice);
Expand All @@ -349,13 +345,6 @@ fn chunks(s: &str, width: usize, prefix: &str, suffix: &str) -> Vec<String> {

let (lhs, rhs, (unknowns, split_char)) = split_string_at(text_slice, available_space);

println!(
"---------> {:?} {} {}",
text_slice, available_space, split_char
);
println!("---------> {:?} {}", lhs, get_text_width(lhs));
println!("---------> {:?}", rhs);

text_slice = &rhs[split_char..];

line.push_str(lhs);
Expand Down

0 comments on commit 15dbb36

Please sign in to comment.