#![warn(missing_docs,
unused_extern_crates,
unused_import_braces,
unused_qualifications)]
//! A formatted and aligned table printer written in rust
#[macro_use]
extern crate lazy_static;
use std::io::{self, Write, Error};
use std::fmt;
use std::iter::{FromIterator, IntoIterator};
use std::slice::{Iter, IterMut};
use std::ops::{Index, IndexMut};
pub use term::{Attr, color};
pub(crate) use term::{Terminal, stdout};
mod cell;
mod row;
pub mod format;
mod utils;
#[cfg(feature = "csv")]
pub mod csv;
#[cfg(feature = "evcxr")]
pub mod evcxr;
pub use row::Row;
pub use cell::Cell;
use format::{TableFormat, LinePosition, consts};
use utils::StringWriter;
/// An owned printable table
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct Table {
format: Box<TableFormat>,
titles: Box<Option<Row>>,
rows: Vec<Row>,
}
/// A borrowed immutable `Table` slice
/// A `TableSlice` is obtained by slicing a `Table` with the `Slice::slice` method.
///
/// # Examples
/// ```rust
/// # #[macro_use] extern crate prettytable;
/// use prettytable::{Table, Slice};
/// # fn main() {
/// let table = table![[1, 2, 3], [4, 5, 6], [7, 8, 9]];
/// let slice = table.slice(1..);
/// slice.printstd(); // Prints only rows 1 and 2
///
/// //Also supports other syntax :
/// table.slice(..);
/// table.slice(..2);
/// table.slice(1..3);
/// # }
/// ```
///
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct TableSlice<'a> {
format: &'a TableFormat,
titles: &'a Option<Row>,
rows: &'a [Row],
}
impl<'a> TableSlice<'a> {
/// Compute and return the number of column
// #[deprecated(since="0.8.0", note="Will become private in future release. See [issue #87](https://github.com/phsym/prettytable-rs/issues/87)")]
fn get_column_num(&self) -> usize {
let mut cnum = 0;
for r in self.rows {
let l = r.column_count();
if l > cnum {
cnum = l;
}
}
cnum
}
/// Get the number of rows
pub fn len(&self) -> usize {
self.rows.len()
}
/// Check if the table slice is empty
pub fn is_empty(&self) -> bool {
self.rows.is_empty()
}
/// Get an immutable reference to a row
pub fn get_row(&self, row: usize) -> Option<&Row> {
self.rows.get(row)
}
/// Get the width of the column at position `col_idx`.
/// Return 0 if the column does not exists;
fn get_column_width(&self, col_idx: usize) -> usize {
let mut width = match *self.titles {
Some(ref t) => t.get_column_width(col_idx, self.format),
None => 0,
};
for r in self.rows {
let l = r.get_column_width(col_idx, self.format);
if l > width {
width = l;
}
}
width
}
/// Get the width of all columns, and return a slice
/// with the result for each column
fn get_all_column_width(&self) -> Vec<usize> {
let colnum = self.get_column_num();
let mut col_width = vec![0usize; colnum];
for i in 0..colnum {
// TODO: calling "get_column_width()" in a loop is inefficient
col_width[i] = self.get_column_width(i);
}
col_width
}
/// Returns an iterator over the immutable cells of the column specified by `column`
pub fn column_iter(&self, column: usize) -> ColumnIter {
ColumnIter(self.rows.iter(), column)
}
/// Returns an iterator over immutable rows
pub fn row_iter(&self) -> Iter<Row> {
self.rows.iter()
}
/// Internal only
fn __print<T: Write + ?Sized, F>(&self, out: &mut T, f: F) -> Result<usize, Error>
where F: Fn(&Row, &mut T, &TableFormat, &[usize]) ->