Skip to content

cfg-out csv feature #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ rust:
- stable
- beta
- nightly
script:
- cargo build --verbose
- cargo test --verbose
- cargo build --verbose --no-default-features
- cargo test --verbose --no-default-features
env:
global:
- secure: k+5s8j7arJSoqS/7BnX7vBEXb1csFsn/cr+WCxRQtlV7bK8JkQ/3t3E1MCUpCSHJLb6K+GlRSkN6tWkhPVUpYA57J7+bSADJ2cAWBq2ArMubXMkMl/t7ibuOArGggDRLulYZ83kDZEkVcMs3QyAv7cGvSMnj6VehTeUrZsIreHmNGJnpsxuXqsfaHhiToWkO/KTRGHOuro7xQczCKzV54g7NAfIgWvcy3T5zVpkaNZWGd/BaRvkBRP8fZpqNBQSlG3Unq3q6wWIeLIJd3QWAQCrzDDMNIbiwsU/KHOJfVvvDFDJF/rzn1EwVvkWRQmT+GtPmLDCRV5OD4hmjVyEtdFU1aLaxxeQBNdSUb3SsDbnUkfyX+WgHEAYRxRAOGW8vhA7+9gaMI2fStkc5JwAcfrZxKkDd9YsUX4iYNk207zsRz/5M6gTWCw2e7jLj9kUGMiTy+008TRxAjSNbN9sl+FRMH5BPMDlgDM4Ohp1+JRq0Mfu1qT6hoYXb+AoRvHijw9HoqtaU2lTamuSN6+LFNJ0CDt2Qhy4jn+Dmp5ZlivcUVzpQpdZoPG00BnLK6YfYoCF9gFX194TM2T6ljhYGaL7ITZI9Cz4qMxD3r459aGz8sUAcTkSbTRMGpTb4fJVfvCgsP2IDfKO7WS/W4SbCzYMh7PfpQg03BAvld0y69O8=
Expand Down
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]

name = "prettytable-rs"
version = "0.6.4"
version = "0.6.5"
description = "A library for printing pretty formatted tables in terminal"
homepage = "https://github.com/phsym/prettytable-rs"
repository = "https://github.com/phsym/prettytable-rs"
Expand All @@ -12,7 +12,7 @@ keywords = ["tab", "table", "format", "pretty", "print"]
license = "BSD-3-Clause"

[features]
default = ["win_crlf"]
default = ["win_crlf", "csv"]
win_crlf = []

[[bin]]
Expand All @@ -27,4 +27,4 @@ term = "^0.4"
lazy_static = "^0.2"
atty = "^0.2"
encode_unicode = "^0.2"
csv = "^0.14"
csv = { version = "^0.14", optional = true }
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ build_script:

test_script:
- cargo test --verbose
- cargo test --verbose --no-default-features
7 changes: 6 additions & 1 deletion examples/csv.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
extern crate prettytable;
use prettytable::Table;

/*
Following main function will print :
Expand All @@ -15,7 +14,10 @@ use prettytable::Table;
foobar,bar,foo
foobar2,bar2,foo2
*/
#[cfg(feature = "csv")]
fn main() {
use prettytable::Table;

let table = Table::from_csv_string("ABC,DEFG,HIJKLMN\n\
foobar,bar,foo\n\
foobar2,bar2,foo2").unwrap();
Expand All @@ -24,3 +26,6 @@ fn main() {
println!("");
println!("{}", table.to_csv(Vec::new()).unwrap().into_string());
}

#[cfg(not(feature = "csv"))]
fn main() {}
14 changes: 13 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@
extern crate unicode_width;
extern crate term;
extern crate atty;
#[cfg(feature = "csv")]
extern crate csv;
#[macro_use] extern crate lazy_static;
extern crate encode_unicode;

use std::io::{self, Read, Write, Error};
use std::io::{self, Write, Error};
#[cfg(feature = "csv")]
use std::io::Read;
use std::fmt;
#[cfg(feature = "csv")]
use std::path::Path;
use std::iter::{FromIterator, IntoIterator};
use std::slice::{Iter, IterMut};
Expand Down Expand Up @@ -182,13 +186,15 @@ impl <'a> TableSlice<'a> {
}

/// Write the table to the specified writer.
#[cfg(feature = "csv")]
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.
#[cfg(feature = "csv")]
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())));
Expand Down Expand Up @@ -228,18 +234,21 @@ impl Table {
/// Create a table from a CSV string
///
/// For more customisability use `from_csv()`
#[cfg(feature = "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()`
#[cfg(feature = "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
#[cfg(feature = "csv")]
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())
}
Expand Down Expand Up @@ -371,13 +380,15 @@ impl Table {
}

/// Write the table to the specified writer.
#[cfg(feature = "csv")]
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.
#[cfg(feature = "csv")]
pub fn to_csv_writer<W: Write>(&self, writer: csv::Writer<W>) -> csv::Result<csv::Writer<W>> {
self.as_ref().to_csv_writer(writer)
}
Expand Down Expand Up @@ -739,6 +750,7 @@ mod tests {
assert_eq!(out, table.to_string().replace("\r\n", "\n"));
}

#[cfg(feature = "csv")]
mod csv {
use Table;
use row::Row;
Expand Down