diff options
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | examples/csv.rs | 26 | ||||
-rw-r--r-- | src/lib.rs | 82 |
3 files changed, 94 insertions, 15 deletions
@@ -26,3 +26,4 @@ unicode-width = "^0.1" term = "^0.4"
lazy_static = "^0.2"
atty = "^0.1"
+csv = "^0.14"
diff --git a/examples/csv.rs b/examples/csv.rs new file mode 100644 index 0000000..df14173 --- /dev/null +++ b/examples/csv.rs @@ -0,0 +1,26 @@ +extern crate prettytable; +use prettytable::Table; + +/* + Following main function will print : + +---------+------+---------+ + | ABC | DEFG | HIJKLMN | + +---------+------+---------+ + | foobar | bar | foo | + +---------+------+---------+ + | foobar2 | bar2 | foo2 | + +---------+------+---------+ + + ABC,DEFG,HIJKLMN + foobar,bar,foo + foobar2,bar2,foo2 +*/ +fn main() { + let table = Table::from_csv_string("ABC,DEFG,HIJKLMN\n\ + foobar,bar,foo\n\ + foobar2,bar2,foo2").unwrap(); + table.printstd(); + + println!(""); + println!("{}", table.to_csv(Vec::new()).unwrap().into_string()); +} @@ -4,13 +4,14 @@ extern crate unicode_width;
extern crate term;
extern crate atty;
+extern crate csv;
#[macro_use] extern crate lazy_static;
-use std::io;
-use std::io::{Write, Error};
+use std::io::{self, Read, Write, Error};
use std::fmt;
+use std::path::Path;
use std::iter::{FromIterator, IntoIterator};
-use std::slice::{Iter, IterMut}; +use std::slice::{Iter, IterMut};
use std::ops::{Index, IndexMut};
use std::mem::transmute;
@@ -180,6 +181,26 @@ impl <'a> TableSlice<'a> { pub fn printstd(&self) {
self.print_tty(false);
}
+
+ /// Write the table to the specified writer.
+ pub fn to_csv<W: Write>(&self, w: W) -> csv::Result<csv::Writer<W>> {
+ self.to_csv_writer(csv::Writer::from_writer(w))
+ }
+
+ /// Write the table to the specified writer.
+ ///
+ /// This allows for format customisation.
+ pub fn to_csv_writer<W: Write>(&self, mut writer: csv::Writer<W>) -> csv::Result<csv::Writer<W>> {
+ for title in self.titles {
+ try!(writer.write(title.iter().map(|c| c.get_content())));
+ }
+ for row in self.rows {
+ try!(writer.write(row.iter().map(|c| c.get_content())));
+ }
+
+ try!(writer.flush());
+ Ok(writer)
+ }
}
impl <'a> IntoIterator for &'a TableSlice<'a> {
@@ -205,6 +226,25 @@ impl Table { }
}
+ /// Create a table from a CSV string
+ ///
+ /// For more customisability use `from_csv()`
+ pub fn from_csv_string(csv_s: &str) -> csv::Result<Table> {
+ Ok(Table::from_csv(&mut csv::Reader::from_string(csv_s).has_headers(false)))
+ }
+
+ /// Create a table from a CSV file
+ ///
+ /// For more customisability use `from_csv()`
+ pub fn from_csv_file<P: AsRef<Path>>(csv_p: P) -> csv::Result<Table> {
+ Ok(Table::from_csv(&mut try!(csv::Reader::from_file(csv_p)).has_headers(false)))
+ }
+
+ /// Create a table from a CSV reader
+ pub fn from_csv<R: Read>(reader: &mut csv::Reader<R>) -> Table {
+ Table::init(reader.records().map(|row| Row::new(row.unwrap().into_iter().map(|cell| Cell::new(&cell)).collect())).collect())
+ }
+
/// Change the table format. Eg : Separators
pub fn set_format(&mut self, format: TableFormat) {
*self.format = format;
@@ -287,17 +327,17 @@ impl Table { pub fn column_iter_mut(&mut self, column: usize) -> ColumnIterMut {
ColumnIterMut(self.rows.iter_mut(), column)
}
- - /// Returns an iterator over immutable rows - pub fn row_iter<'a>(&'a self) -> Iter<'a, Row> { - self.rows.iter() - } - - /// Returns an iterator over mutable rows - pub fn row_iter_mut<'a>(&'a mut self) -> IterMut<'a, Row> { - self.rows.iter_mut() - } - +
+ /// Returns an iterator over immutable rows
+ pub fn row_iter<'a>(&'a self) -> Iter<'a, Row> {
+ self.rows.iter()
+ }
+
+ /// Returns an iterator over mutable rows
+ pub fn row_iter_mut<'a>(&'a mut self) -> IterMut<'a, Row> {
+ self.rows.iter_mut()
+ }
+
/// Print the table to `out`
pub fn print<T: Write+?Sized>(&self, out: &mut T) -> Result<(), Error> {
self.as_ref().print(out)
@@ -330,6 +370,18 @@ impl Table { pub fn printstd(&self) {
self.as_ref().printstd();
}
+
+ /// Write the table to the specified writer.
+ pub fn to_csv<W: Write>(&self, w: W) -> csv::Result<csv::Writer<W>> {
+ self.as_ref().to_csv(w)
+ }
+
+ /// Write the table to the specified writer.
+ ///
+ /// This allows for format customisation.
+ pub fn to_csv_writer<W: Write>(&self, writer: csv::Writer<W>) -> csv::Result<csv::Writer<W>> {
+ self.as_ref().to_csv_writer(writer)
+ }
}
impl Index<usize> for Table {
@@ -615,7 +667,7 @@ mod tests { assert_eq!(table[1][1].get_content(), "bc");
table[1][1] = Cell::new("newval");
- assert_eq!(table[1][1].get_content(), "newval"); + assert_eq!(table[1][1].get_content(), "newval");
let out = "\
\u{0020}t1 t2 t3 \n\
|