From a4272400bf76321b62a4fa72b291a2ea9f52aa99 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Fri, 16 Jul 2021 20:55:52 +0300 Subject: [PATCH 1/3] Add width example Signed-off-by: Maxim Zhiburt --- examples/width.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 examples/width.rs diff --git a/examples/width.rs b/examples/width.rs new file mode 100644 index 00000000..f1ef6e36 --- /dev/null +++ b/examples/width.rs @@ -0,0 +1,18 @@ +//! The example can be run by this command +//! `cargo run --example width` + +use tabled::{Full, MaxWidth, Modify, Style, Table}; + +fn main() { + let data = [ + ["Hello World", "123123123231"], + ["Hello World", "zxczczxcxczxczxc"], + ["Hello World", "[[[[[[[[[[[[[[[[["], + ]; + + let table = Table::new(&data) + .with(Style::github_markdown()) + .with(Modify::new(Full).with(MaxWidth(10, "..."))); + + println!("{}", table); +} From 7a72c8c277ef236e80083370beac542a20f5b26f Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Fri, 16 Jul 2021 20:56:37 +0300 Subject: [PATCH 2/3] Improve documentation Signed-off-by: Maxim Zhiburt --- examples/default_array.rs | 2 +- examples/inline.rs | 2 +- src/alignment.rs | 2 +- src/disable.rs | 11 +- src/formating.rs | 25 ++-- src/indent.rs | 3 +- src/lib.rs | 261 +++++++++++++++++++++----------------- src/object.rs | 23 ++-- src/panel.rs | 14 +- src/style.rs | 46 ++++--- src/width.rs | 20 ++- 11 files changed, 238 insertions(+), 171 deletions(-) diff --git a/examples/default_array.rs b/examples/default_array.rs index dd214fc1..8a3fa1db 100644 --- a/examples/default_array.rs +++ b/examples/default_array.rs @@ -1,5 +1,5 @@ //! The example can be run by this command -//! `cargo run --example basic` +//! `cargo run --example default_array` use tabled::{Style, Table}; diff --git a/examples/inline.rs b/examples/inline.rs index 72271f53..09d48d1f 100644 --- a/examples/inline.rs +++ b/examples/inline.rs @@ -1,5 +1,5 @@ //! The example can be run by this command -//! `cargo run --example inline_structure` +//! `cargo run --example inline` use tabled::{ papergrid::AlignmentHorizontal, Alignment, Full, Head, Indent, Modify, Row, Style, Table, diff --git a/src/alignment.rs b/src/alignment.rs index 81db4165..5a1ae511 100644 --- a/src/alignment.rs +++ b/src/alignment.rs @@ -3,7 +3,7 @@ use crate::CellOption; use crate::Table; use papergrid::{AlignmentHorizontal, AlignmentVertical, Entity, Grid, Settings}; -/// Alignment represent a horizontal and vertical alignemt setting for a [Table]. +/// Alignment represent a horizontal and vertical alignemt setting for any cell on a [Table]. /// /// ```rust,no_run /// # use tabled::{Style, Alignment, Modify, Row, Table}; diff --git a/src/disable.rs b/src/disable.rs index ef06e23c..be16a08b 100644 --- a/src/disable.rs +++ b/src/disable.rs @@ -4,19 +4,20 @@ use crate::{bounds_to_usize, TableOption}; use papergrid::Grid; use std::ops::RangeBounds; -/// Disable represent a disable setting for a [Table]. +/// Disable removes particular rows/columns from a [Table]. /// /// ```rust,no_run /// # use tabled::{Disable, Table}; /// # let data: Vec<&'static str> = Vec::new(); /// let table = Table::new(&data).with(Disable::Row(..1)); /// ``` -/// #[derive(Debug)] pub enum Disable> { - /// Columns of the grid. Range is used to locate columns. + /// Columns of the grid. + /// Range is used to locate columns. Column(R), - /// Rows of the grid. Range is used to locate rows. + /// Rows of the grid. + /// Range is used to locate rows. Row(R), } @@ -34,7 +35,7 @@ impl> TableOption for Disable { let (x, y) = bounds_to_usize(range.start_bound(), range.end_bound(), grid.count_rows()); - // It's kindof a bad design that we must controll shift. + // It's kind of a bad design that we must controll shift. // It basically unveils an implementation... for (shifted, i) in (x..y).enumerate() { grid.remove_row(i - shifted); diff --git a/src/formating.rs b/src/formating.rs index 1ae5f364..7eab2efd 100644 --- a/src/formating.rs +++ b/src/formating.rs @@ -1,7 +1,7 @@ use crate::CellOption; use papergrid::{Entity, Grid, Settings}; -/// Format a structure which modifies a [Grid] +/// Formatting of particular cells on a [Grid]. /// /// # Example /// @@ -9,14 +9,14 @@ use papergrid::{Entity, Grid, Settings}; /// use tabled::{Table, Format, Row, Modify}; /// /// let data = vec![ -/// (0, "Grodno", true), -/// (1, "Minsk", true), -/// (2, "Hamburg", false), -/// (3, "Brest", true), +/// (0, "Grodno", true), +/// (1, "Minsk", true), +/// (2, "Hamburg", false), +/// (3, "Brest", true), /// ]; /// /// let table = Table::new(&data) -/// .with(Modify::new(Row(1..)).with(Format(|s| { format!(": {} :", s) }))) +/// .with(Modify::new(Row(1..)).with(Format(|s| format!(": {} :", s)))) /// .to_string(); /// /// assert_eq!(table, "+-------+-------------+-----------+\n\ @@ -53,14 +53,15 @@ where } } -/// Multiline a helper function for changing multiline content of cell by rows not as a whole. +/// Multiline a helper function for changing multiline content of cell. +/// Using this formatting applied for all rows not to a string as a whole. /// /// ```rust,no_run -/// use tabled::{Table, Format, multiline, Full, Modify}; -/// let data: Vec<&'static str> = Vec::new(); -/// let table = Table::new(&data) -/// .with(Modify::new(Full).with(Format(multiline(|s| format!("{}", s))))) -/// .to_string(); +/// use tabled::{Table, Format, multiline, Full, Modify}; +/// let data: Vec<&'static str> = Vec::new(); +/// let table = Table::new(&data) +/// .with(Modify::new(Full).with(Format(multiline(|s| format!("{}", s))))) +/// .to_string(); /// ``` pub fn multiline String>(f: F) -> Box String> { Box::new(move |s: &str| s.lines().map(|s| f(s)).collect::>().join("\n")) diff --git a/src/indent.rs b/src/indent.rs index 68527ee7..6328ea60 100644 --- a/src/indent.rs +++ b/src/indent.rs @@ -1,7 +1,7 @@ use crate::CellOption; use papergrid::{Entity, Grid, Settings}; -/// Indent is responsible for a left/right/top/bottom indent. +/// Indent is responsible for a left/right/top/bottom indent of particular cells. /// /// ```rust,no_run /// # use tabled::{Style, Indent, Row, Table, Modify}; @@ -12,6 +12,7 @@ use papergrid::{Entity, Grid, Settings}; pub struct Indent(usize, usize, usize, usize); impl Indent { + /// Construct's an Indent object. pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self { Self(left, right, top, bottom) } diff --git a/src/lib.rs b/src/lib.rs index 0033ab90..11923cb0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,56 +1,60 @@ //! An easy to use library for pretty print tables of Rust `struct`s and `enum`s. //! -//! # Get started +//! The library is based on a [Tabled] trait which is used to actually build tables. +//! It also provides an variate of dynamic settings for customization of a [Table]. //! -//! The common and probably the best way to begin is to annotate your type with -//! `#[derive(Tabled)]`. You can also implement it on your own as well. +//! [Table] can be build from vast majority of Rust's standart types. +//! +//! ## Examples +//! +//! If you wan't to build a table for your data. +//! Most likely a starting point is to anotate your type with `#[derive(Tabled)]`. //! //! ```rust -//! use tabled::{Tabled, Table}; +//! use tabled::{Tabled, Table}; //! -//! #[derive(Tabled)] -//! struct Language { -//! name: &'static str, -//! designed_by: &'static str, -//! invented_year: usize, -//! } +//! #[derive(Tabled)] +//! struct Language { +//! name: &'static str, +//! designed_by: &'static str, +//! invented_year: usize, +//! } //! -//! let languages = vec![ -//! Language{ -//! name: "C", -//! designed_by: "Dennis Ritchie", -//! invented_year: 1972 -//! }, -//! Language{ -//! name: "Rust", -//! designed_by: "Graydon Hoare", -//! invented_year: 2010 -//! }, -//! Language{ -//! name: "Go", -//! designed_by: "Rob Pike", -//! invented_year: 2009 -//! }, -//! ]; +//! let languages = vec![ +//! Language{ +//! name: "C", +//! designed_by: "Dennis Ritchie", +//! invented_year: 1972 +//! }, +//! Language{ +//! name: "Rust", +//! designed_by: "Graydon Hoare", +//! invented_year: 2010 +//! }, +//! Language{ +//! name: "Go", +//! designed_by: "Rob Pike", +//! invented_year: 2009 +//! }, +//! ]; //! -//! let table = Table::new(languages).to_string(); +//! let table = Table::new(languages).to_string(); //! -//! let expected = "+------+----------------+---------------+\n\ -//! | name | designed_by | invented_year |\n\ -//! +------+----------------+---------------+\n\ -//! | C | Dennis Ritchie | 1972 |\n\ -//! +------+----------------+---------------+\n\ -//! | Rust | Graydon Hoare | 2010 |\n\ -//! +------+----------------+---------------+\n\ -//! | Go | Rob Pike | 2009 |\n\ -//! +------+----------------+---------------+\n"; +//! let expected = "+------+----------------+---------------+\n\ +//! | name | designed_by | invented_year |\n\ +//! +------+----------------+---------------+\n\ +//! | C | Dennis Ritchie | 1972 |\n\ +//! +------+----------------+---------------+\n\ +//! | Rust | Graydon Hoare | 2010 |\n\ +//! +------+----------------+---------------+\n\ +//! | Go | Rob Pike | 2009 |\n\ +//! +------+----------------+---------------+\n"; //! -//! assert_eq!(table, expected); +//! assert_eq!(table, expected); //! ``` //! -//! We must to know what we print in the field -//! accordingly each field should implement `std::fmt::Display` -//! The example below is not compiled +//! Not all types can derive [Tabled] trait though. +//! The example below can't be compiled. //! //! ```rust,compile_fail //! # use tabled::Tabled; @@ -62,62 +66,80 @@ //! struct SomeOtherType; //! ``` //! -//! Most of the default types implements the trait out of the box. +//! We must know what we're up to print as a field. Because of this +//! each field must implement [std::fmt::Display]. +//! +//! ### Default implementations +//! +//! As I've already mentioned most of the default types implements the trait out of the box. +//! +//! This allows you to run the following code. //! //! ```rust -//! use tabled::{Tabled, Table}; -//! let some_numbers = [1, 2, 3]; -//! let table = Table::new(&some_numbers); -//! # let expected = "+-----+\n\ -//! # | i32 |\n\ -//! # +-----+\n\ -//! # | 1 |\n\ -//! # +-----+\n\ -//! # | 2 |\n\ -//! # +-----+\n\ -//! # | 3 |\n\ -//! # +-----+\n"; -//! # assert_eq!(table.to_string(), expected); +//! use tabled::{Tabled, Table}; +//! let table = Table::new(&[1, 2, 3]); +//! # let expected = "+-----+\n\ +//! # | i32 |\n\ +//! # +-----+\n\ +//! # | 1 |\n\ +//! # +-----+\n\ +//! # | 2 |\n\ +//! # +-----+\n\ +//! # | 3 |\n\ +//! # +-----+\n"; +//! # assert_eq!(table.to_string(), expected); //! ``` //! -//! You also can combine structures by means of tuples. +//! ### Combination of types via tuples +//! +//! Personally I consider this a feature which drives the library to shine. +//! You can combine any types that implements [Tabled] trait into one table. +//! +//! You can also see in this example a `#[header("name")]` usage which configures a header +//! of a table which will be printed. +//! You could change it dynamically as well. //! //! ```rust -//! use tabled::{Tabled, Table, Style}; +//! use tabled::{Tabled, Table, Style}; //! -//! #[derive(Tabled)] -//! enum Domain { -//! Security, -//! Embeded, -//! Frontend, -//! Unknown, -//! } +//! #[derive(Tabled)] +//! enum Domain { +//! Security, +//! Embeded, +//! Frontend, +//! Unknown, +//! } //! -//! #[derive(Tabled)] -//! struct Developer(#[header("name")] &'static str); +//! #[derive(Tabled)] +//! struct Developer(#[header("name")] &'static str); //! -//! let data = vec![ -//! (Developer("Terri Kshlerin"), Domain::Embeded), -//! (Developer("Catalina Dicki"), Domain::Security), -//! (Developer("Jennie Schmeler"), Domain::Frontend), -//! (Developer("Maxim Zhiburt"), Domain::Unknown), -//! ]; +//! let data = vec![ +//! (Developer("Terri Kshlerin"), Domain::Embeded), +//! (Developer("Catalina Dicki"), Domain::Security), +//! (Developer("Jennie Schmeler"), Domain::Frontend), +//! (Developer("Maxim Zhiburt"), Domain::Unknown), +//! ]; //! -//! let table = Table::new(data).with(Style::psql()).to_string(); +//! let table = Table::new(data).with(Style::psql()).to_string(); //! -//! assert_eq!( -//! table, -//! concat!( -//! " name | Security | Embeded | Frontend | Unknown \n", -//! "-----------------+----------+---------+----------+---------\n", -//! " Terri Kshlerin | | + | | \n", -//! " Catalina Dicki | + | | | \n", -//! " Jennie Schmeler | | | + | \n", -//! " Maxim Zhiburt | | | | + \n" -//! ) -//! ); +//! assert_eq!( +//! table, +//! concat!( +//! " name | Security | Embeded | Frontend | Unknown \n", +//! "-----------------+----------+---------+----------+---------\n", +//! " Terri Kshlerin | | + | | \n", +//! " Catalina Dicki | + | | | \n", +//! " Jennie Schmeler | | | + | \n", +//! " Maxim Zhiburt | | | | + \n" +//! ) +//! ); //! ``` //! +//! ## Settings +//! +//! You can find more examples of settings and attributes in +//! [README.md](https://github.com/zhiburt/tabled/blob/master/README.md) +//! use papergrid::{AlignmentHorizontal, Entity, Grid, Settings}; use std::fmt; @@ -142,12 +164,14 @@ pub use tabled_derive::Tabled; /// It's urgent that `header` len is equal to `fields` len. /// /// ```text -/// Self::headers().len() == self.fields().len() +/// Self::headers().len() == self.fields().len() /// ``` pub trait Tabled { - /// Fields must return a list of cell in a row + /// Fields method must return a list of cells. + /// + /// The cells will be placed in the same row, preserving the order. fn fields(&self) -> Vec; - /// Headers return a list of names for columns + /// Headers must return a list of column names. fn headers() -> Vec; } @@ -163,11 +187,9 @@ where } } -/// A trait for configuring a [Grid]. -/// -/// Mainly was created to be able to have a variadic set of parameters in a [the `table` macros](./macros.table.html) +/// A trait which is responsilbe for configuration of a [Grid]. pub trait TableOption { - /// Modification function of a [Grid] + /// The function modifies a [Grid] object. fn change(&self, grid: &mut Grid); } @@ -180,7 +202,8 @@ where } } -/// CellOption is trait for configuring a [Cell] which represented by 'row' and 'column' indexes. +/// A trait for configuring a [Cell] a single cell. +/// Where cell represented by 'row' and 'column' indexes. pub trait CellOption { /// Modification function of a [Cell] fn change_cell(&self, grid: &mut Grid, row: usize, column: usize); @@ -188,30 +211,28 @@ pub trait CellOption { /// Table structure provides an interface for building a table for types that implements [Tabled]. /// -/// # Example +/// To build a string representation of a table you must use a [std::fmt::Display]. +/// Or simply call `.to_string()` method. /// -/// ## Basic usage +/// ## Example +/// +/// ### Basic usage /// /// ```rust,no_run -/// use tabled::Table; -/// let data: Vec<&'static str> = Vec::new(); -/// let table = Table::new(data); -/// println!("{}", table); +/// use tabled::Table; +/// let table = Table::new(["Year", "2021"]); /// ``` /// -/// ## A list of settings -/// -/// It may take a list of arguments such as [Style] and [Alignment]. +/// ### With settings /// /// ```rust,no_run -/// use tabled::{Table, Style, Alignment, Full, Modify}; -/// let data = vec!["Hello", "2021"]; -/// let table = Table::new(&data) -/// .with(Style::psql()) -/// .with(Modify::new(Full).with(Alignment::left())); -/// println!("{}", table); +/// use tabled::{Table, Style, Alignment, Full, Modify}; +/// let data = vec!["Hello", "2021"]; +/// let table = Table::new(&data) +/// .with(Style::psql()) +/// .with(Modify::new(Full).with(Alignment::left())); +/// println!("{}", table); /// ``` -/// pub struct Table { grid: Grid, } @@ -224,7 +245,9 @@ impl Table { Self { grid } } - /// With is a generic function which applies options to the table. + /// With is a generic function which applies options to the [Table]. + /// + /// It applies settings immediately. pub fn with(mut self, option: O) -> Self where O: TableOption, @@ -240,8 +263,8 @@ impl fmt::Display for Table { } } -/// Modify structure provide a conviniet way for applying a set of [CellOption]s to the same object. -/// +/// Modify structure provide an abstraction, to be able to apply +/// a set of [CellOption]s to the same object. pub struct Modify { obj: O, modifiers: Vec>, @@ -259,8 +282,10 @@ where } } - /// With a generic function which stores a [CellOption] to apply it later to an [Object] + /// With a generic function which stores a [CellOption]. /// + /// The function *doesn't* changes a [Grid]. [Grid] will be changed + /// only after passing [Modify] object to [Table::with]. pub fn with(mut self, f: F) -> Self where F: CellOption + 'static, @@ -285,9 +310,9 @@ where } } -/// Build_grid function build a [Grid] from a data. -/// A [`table` macros](./macro.table.html) should be prefered over this function. -pub fn build_grid(iter: impl IntoIterator) -> Grid { +/// Building [Grid] from a data. +/// You must prefer [Table] over this function. +fn build_grid(iter: impl IntoIterator) -> Grid { let headers = T::headers(); let obj: Vec> = iter.into_iter().map(|t| t.fields()).collect(); @@ -345,9 +370,9 @@ macro_rules! tuple_table { tuple_table! { A } tuple_table! { A B } tuple_table! { A B C } -tuple_table! { A B C D} -tuple_table! { A B C D E} -tuple_table! { A B C D E F} +tuple_table! { A B C D } +tuple_table! { A B C D E } +tuple_table! { A B C D E F } macro_rules! default_table { ( $t:ty ) => { diff --git a/src/object.rs b/src/object.rs index edc4de63..158c6df2 100644 --- a/src/object.rs +++ b/src/object.rs @@ -10,7 +10,8 @@ pub trait Object: Sized { /// Cells returns a set of cordinates of cells fn cells(&self, count_rows: usize, count_columns: usize) -> Vec<(usize, usize)>; - /// And combines output of self with rhs object + /// Combines cells. + /// It doesn't repeat cells. fn and(self, rhs: O) -> Combination { Combination { lhs: self, @@ -19,7 +20,7 @@ pub trait Object: Sized { } } - /// Not excludes output of rhs from output + /// Excludes rhs cells from this cells. fn not(self, rhs: O) -> Combination { Combination { lhs: self, @@ -29,7 +30,7 @@ pub trait Object: Sized { } } -/// Head represent a row with column names +/// Head represents the row at the top of a [Table]. pub struct Head; impl Object for Head { @@ -38,7 +39,7 @@ impl Object for Head { } } -/// Head represent all cells on a [Grid] +/// Full represents all cells on a [Grid] pub struct Full; impl Object for Full { @@ -54,7 +55,7 @@ impl Object for Full { } } -/// Row denotes a set of cells on given rows on a [Grid] +/// Row denotes a set of cells on given rows on a [Grid]. pub struct Row>(pub R); impl> Object for Row { @@ -68,7 +69,7 @@ impl> Object for Row { } } -/// Column denotes a set of cells on given columns on a [Grid] +/// Column denotes a set of cells on given columns on a [Grid]. pub struct Column>(pub R); impl> Object for Column { @@ -82,7 +83,7 @@ impl> Object for Column { } } -/// Cell denotes a particular of cells on a [Grid]. +/// Cell denotes a particular cell on a [Grid]. pub struct Cell(pub usize, pub usize); impl Object for Cell { @@ -91,9 +92,10 @@ impl Object for Cell { } } +/// Combinator is a transformation function type Combinator = fn(Vec<(usize, usize)>, Vec<(usize, usize)>) -> Vec<(usize, usize)>; -/// Combination struct which allows a chain of objects +/// Combination struct used for chaning [Object]'s. pub struct Combination { lhs: L, rhs: R, @@ -112,6 +114,9 @@ where } } +/// Combines 2 sets of cells into one. +/// +/// Dublicates are removed from the output set. fn combine_cells(lhs: Vec<(usize, usize)>, rhs: Vec<(usize, usize)>) -> Vec<(usize, usize)> { lhs.into_iter() .chain(rhs.into_iter()) @@ -120,10 +125,12 @@ fn combine_cells(lhs: Vec<(usize, usize)>, rhs: Vec<(usize, usize)>) -> Vec<(usi .collect() } +/// Removes cells from fist set which are present in a second set. fn remove_cells(lhs: Vec<(usize, usize)>, rhs: Vec<(usize, usize)>) -> Vec<(usize, usize)> { lhs.into_iter().filter(|l| !rhs.contains(l)).collect() } +/// Converts a range bound to its indexes. pub(crate) fn bounds_to_usize( left: Bound<&usize>, right: Bound<&usize>, diff --git a/src/panel.rs b/src/panel.rs index 13b6f0c4..be01b605 100644 --- a/src/panel.rs +++ b/src/panel.rs @@ -1,10 +1,10 @@ -#[allow(unused)] -use crate::Disable; use crate::TableOption; use papergrid::{Entity, Grid, Settings}; +#[allow(unused)] use crate::Table; -/// Panel allows to add a custom panel to table. +/// Panel allows to add a Row which has 1 continues Cell to a [Table]. /// +/// See `examples/panel.rs`. #[derive(Debug)] pub struct Panel>(pub S, pub usize); @@ -20,8 +20,8 @@ impl> TableOption for Panel { } } -/// Header renders information at the top. -/// See [Panel] +/// Header inserts a [Panel] at the top. +/// See [Panel]. #[derive(Debug)] pub struct Header>(pub S); @@ -31,8 +31,8 @@ impl> TableOption for Header { } } -/// Footer renders information at the bottom. -/// See [Panel] +/// Footer renders a [Panel] at the bottom. +/// See [Panel]. #[derive(Debug)] pub struct Footer>(pub S); diff --git a/src/style.rs b/src/style.rs index d022d69b..1465fe4d 100644 --- a/src/style.rs +++ b/src/style.rs @@ -1,27 +1,25 @@ -use papergrid::{Border, Grid}; - +#[allow(unused)] +use crate::Table; use crate::TableOption; +use papergrid::{Border, Grid}; -/// Style is responsible for a look of a table. -/// -/// It's suppose to take only 1 type of [Line]s short or bordered. +/// Style is responsible for a look of a [Table]. /// /// # Example /// /// ```rust,no_run -/// use tabled::{Table, Style, style::Line}; -/// let data = vec!["Hello", "2021"]; -/// let table = Table::new(&data).with( -/// Style::noborder() -/// .frame_bottom(Some(Line::short('*', ' '))) -/// .split(Some(Line::short('*', ' '))) -/// .inner(' ') -/// ) -/// .to_string(); +/// use tabled::{Table, Style, style::Line}; +/// let data = vec!["Hello", "2021"]; +/// let table = Table::new(&data).with( +/// Style::noborder() +/// .frame_bottom(Some(Line::short('*', ' '))) +/// .split(Some(Line::short('*', ' '))) +/// .inner(' ') +/// ) +/// .to_string(); /// -/// println!("{}", table); +/// println!("{}", table); /// ``` -/// pub struct Style { frame: Frame, header_split_line: Option, @@ -149,37 +147,50 @@ impl Style { pseudo } + /// Left frame character. pub fn frame_left(mut self, frame: Option) -> Self { self.frame.left = frame; self } + /// Right frame character. pub fn frame_right(mut self, frame: Option) -> Self { self.frame.right = frame; self } + /// The header's top line. + /// + /// It's suppose that [Self::frame_bottom] and [Self::split] has the same type of [Line] short or bordered. pub fn frame_top(mut self, frame: Option) -> Self { self.frame.top = frame; self } + /// The footer's bottom line. + /// + /// It's suppose that [Self::frame_top] and [Self::split] has the same type of [Line] short or bordered. pub fn frame_bottom(mut self, frame: Option) -> Self { self.frame.bottom = frame; self } + /// The header's bottom line. pub fn header(mut self, line: Option) -> Self { self.header_split_line = line; self } + /// Row split line. + /// + /// [Self::frame_top] and [Self::frame_bottom] pub fn split(mut self, line: Option) -> Self { self.header_split_line = line.clone(); self.split = line; self } + /// Inner split character. pub fn inner(mut self, c: char) -> Self { self.inner_split_char = c; self @@ -195,6 +206,7 @@ impl Style { } } +/// Line represents a horizontal line on a [Table]. #[derive(Debug, Clone, Default)] pub struct Line { main: char, @@ -204,6 +216,7 @@ pub struct Line { } impl Line { + /// A line for frame styles. pub fn bordered(main: char, intersection: char, left: char, right: char) -> Self { Self { intersection, @@ -213,6 +226,7 @@ impl Line { } } + /// A line for no-frame styles. pub fn short(main: char, intersection: char) -> Self { Self { main, diff --git a/src/width.rs b/src/width.rs index c3da9247..8e64b6bd 100644 --- a/src/width.rs +++ b/src/width.rs @@ -1,7 +1,25 @@ use crate::CellOption; use papergrid::{Entity, Grid, Settings}; -/// Format a structure which modifies a [Grid] +/// Using MaxWidth you can set a max width of an object on a [Grid]. +/// +/// ## Example +/// +/// ``` +/// use tabled::{Full, MaxWidth, Modify, Style, Table}; +/// +/// let data = [ +/// "123456789", +/// "qwertyuiop[]", +/// "[[[[[[[[[[[[[[[[[", +/// ]; +/// +/// let table = Table::new(&data) +/// .with(Style::github_markdown()) +/// .with(Modify::new(Full).with(MaxWidth(5, "..."))); +/// ``` +/// +/// While working with colors you must setup `colors` feature. pub struct MaxWidth(pub usize, pub S) where S: AsRef; From f48d0df2a7a12f14bda1aee520e24c179dc9e603 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Fri, 16 Jul 2021 20:58:49 +0300 Subject: [PATCH 3/3] Fix cargo fmt Signed-off-by: Maxim Zhiburt --- src/formating.rs | 2 +- src/panel.rs | 3 ++- src/style.rs | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/formating.rs b/src/formating.rs index 7eab2efd..8733e4c3 100644 --- a/src/formating.rs +++ b/src/formating.rs @@ -10,7 +10,7 @@ use papergrid::{Entity, Grid, Settings}; /// /// let data = vec![ /// (0, "Grodno", true), -/// (1, "Minsk", true), +/// (1, "Minsk", true), /// (2, "Hamburg", false), /// (3, "Brest", true), /// ]; diff --git a/src/panel.rs b/src/panel.rs index be01b605..a11b903e 100644 --- a/src/panel.rs +++ b/src/panel.rs @@ -1,6 +1,7 @@ +#[allow(unused)] +use crate::Table; use crate::TableOption; use papergrid::{Entity, Grid, Settings}; -#[allow(unused)] use crate::Table; /// Panel allows to add a Row which has 1 continues Cell to a [Table]. /// diff --git a/src/style.rs b/src/style.rs index 1465fe4d..64ebd99f 100644 --- a/src/style.rs +++ b/src/style.rs @@ -160,7 +160,7 @@ impl Style { } /// The header's top line. - /// + /// /// It's suppose that [Self::frame_bottom] and [Self::split] has the same type of [Line] short or bordered. pub fn frame_top(mut self, frame: Option) -> Self { self.frame.top = frame; @@ -168,7 +168,7 @@ impl Style { } /// The footer's bottom line. - /// + /// /// It's suppose that [Self::frame_top] and [Self::split] has the same type of [Line] short or bordered. pub fn frame_bottom(mut self, frame: Option) -> Self { self.frame.bottom = frame;