pub struct Table { /* private fields */ }
Expand description
A really powerful table formatter for text user interfaces.
Example:
use alinio::table::Table;
let data = vec![
vec!["First name", "Surname", "Telephone"],
vec!["John", "Smith", "04529834125"],
];
let mut table = Table::new(data, 40);
println!("{}\n---", table.render().unwrap().join("\n"))
This will print a table
Implementations
sourceimpl Table
impl Table
sourcepub fn new<T: Into<String>>(data: Vec<Vec<T>>, space: usize) -> Self
pub fn new<T: Into<String>>(data: Vec<Vec<T>>, space: usize) -> Self
Create new table with data and space.
data
is organized into rows, and then within those rows, there are columns.
If you are creating a blank table, you may need to specify the type that you wish to give
to the table in future. This is usually a String
.
Example:
use alinio::table::Table;
let table = Table::new::<String>(vec![], 10);
sourcepub fn set_priorities(&mut self, priorities: &[usize])
pub fn set_priorities(&mut self, priorities: &[usize])
Set the priorities for the columns. This allows you to control which columns to remove when space is limited. The higher the number of the column, the more important it is. For example:
use alinio::table::Table;
let data = vec![
vec!["First name", "Surname", "Telephone"],
vec!["John", "Smith", "04529834125"],
];
let mut table = Table::new(data, 24);
// First name is the most important, surname is least important, telephone is second most
// important.
// The higher the number, the more important the column
table.set_priorities(&[2, 0, 1]);
// We can't fit the whole table in 24 spaces, so it looks to see what it can remove.
// In this case, we can remove the surname column in order to make it fit, as the surname column
// has the lowest priority in the table.
println!("{}\n---", table.render().unwrap().join("\n"))
sourcepub fn set_alignment(&mut self, align: Align)
pub fn set_alignment(&mut self, align: Align)
Set the alignment of each cell.
sourcepub fn set_surround(&mut self, surround: bool)
pub fn set_surround(&mut self, surround: bool)
When surround
is true, padding will be applied to the sides of the table.
When surround
is false, the table will take the full width.
sourcepub fn set_space(&mut self, space: usize)
pub fn set_space(&mut self, space: usize)
Set the space between each cell. Use this if your terminal size updates.
sourcepub fn render(&self) -> Option<Vec<String>>
pub fn render(&self) -> Option<Vec<String>>
Render this table to rows of strings.
This will return None
if there is not enough space to fit the table.
sourcepub fn render_partial(&self, offset: usize) -> Option<Vec<String>>
pub fn render_partial(&self, offset: usize) -> Option<Vec<String>>
Only renders rows after offset
row. This is particularly useful if you have a table
that you wish to fit into a terminal with a height shorter than the table.
This will return None
if there is not enough space to fit the table, or if the offset is
out of bounds
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Table
impl Send for Table
impl Sync for Table
impl Unpin for Table
impl UnwindSafe for Table
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcepub fn borrow_mut(&mut self) -> &mut T
pub fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more