summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml3
-rw-r--r--examples/formatting.rs22
-rw-r--r--src/format.rs16
-rw-r--r--src/lib.rs37
4 files changed, 68 insertions, 10 deletions
diff --git a/Cargo.toml b/Cargo.toml
index 440f523..a3ab1bd 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -25,5 +25,6 @@ name = "prettytable"
unicode-width = "^0.1"
term = "^0.4"
lazy_static = "^0.2"
-atty = "^0.1"
+atty = "^0.2"
+encode_unicode = "^0.2"
csv = "^0.14"
diff --git a/examples/formatting.rs b/examples/formatting.rs
index 946d326..1ea0e18 100644
--- a/examples/formatting.rs
+++ b/examples/formatting.rs
@@ -63,4 +63,26 @@ fn main() {
.build()
);
table.printstd();
+
+ // Customized format with unicode
+ // Example to print
+ // ┌─────────────┬────────────┐
+ // | Title 1 | Title 2 |
+ // ├─────────────┼────────────┤
+ // | Value 1 | Value 2 |
+ // ├─────────────┼────────────┤
+ // | Value three | Value four |
+ // └─────────────┴────────────┘
+ println!("With unicode:");
+ table.set_format(
+ format::FormatBuilder::new()
+ .column_separator('|')
+ .borders('|')
+ .separators( &[format::LinePosition::Top], format::LineSeparator::new('─', '┬', '┌', '┐'))
+ .separators( &[format::LinePosition::Intern], format::LineSeparator::new('─', '┼', '├', '┤'))
+ .separators( &[format::LinePosition::Bottom], format::LineSeparator::new('─', '┴', '└', '┘'))
+ .padding(1, 1)
+ .build()
+ );
+ table.printstd();
}
diff --git a/src/format.rs b/src/format.rs
index d0d816e..49e1a96 100644
--- a/src/format.rs
+++ b/src/format.rs
@@ -1,6 +1,8 @@
//! Define table formatting utilities
-use std::io::{Write, Error};
+use std::io::{Write, Error};
+
+use encode_unicode::Utf8Char;
use super::utils::NEWLINE;
@@ -53,17 +55,19 @@ impl LineSeparator {
/// Print a full line separator to `out`. `col_width` is a slice containing the width of each column
pub fn print<T: Write+?Sized>(&self, out: &mut T, col_width: &[usize], colsep: bool, lborder: bool, rborder: bool) -> Result<(), Error> {
if lborder {
- try!(out.write_all(&[self.ljunc as u8]));
+ try!(out.write_all(Utf8Char::from(self.ljunc).as_bytes()));
}
let mut iter = col_width.into_iter().peekable();
while let Some(width) = iter.next() {
- try!(out.write_all(&vec![self.line as u8; width+2]));
+ for _ in 0..width+2 {
+ try!(out.write_all(Utf8Char::from(self.line).as_bytes()));
+ }
if colsep && iter.peek().is_some() {
- try!(out.write_all(&[self.junc as u8]));
+ try!(out.write_all(Utf8Char::from(self.junc).as_bytes()));
}
}
if rborder {
- try!(out.write_all(&[self.rjunc as u8]));
+ try!(out.write_all(Utf8Char::from(self.rjunc).as_bytes()));
}
out.write_all(NEWLINE)
}
@@ -185,7 +189,7 @@ impl TableFormat {
/// Print a column separator or a table border
pub fn print_column_separator<T: Write+?Sized>(&self, out: &mut T, pos: ColumnPosition) -> Result<(), Error> {
match self.get_column_separator(pos) {
- Some(s) => out.write_all(&[s as u8]),
+ Some(s) => out.write_all(Utf8Char::from(s).as_bytes()),
None => Ok(())
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 96eafe3..0d2f756 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,11 +1,10 @@
-//! <a href="https://github.com/phsym/prettytable-rs"><img style="position: absolute; top: 0; left: 0; border: 0;" src="https://camo.githubusercontent.com/121cd7cbdc3e4855075ea8b558508b91ac463ac2/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f6c6566745f677265656e5f3030373230302e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_left_green_007200.png"></a>
-//! <style>.sidebar { margin-top: 53px }</style>
//! A formatted and aligned table printer written in rust
extern crate unicode_width;
extern crate term;
extern crate atty;
extern crate csv;
#[macro_use] extern crate lazy_static;
+extern crate encode_unicode;
use std::io::{self, Read, Write, Error};
use std::fmt;
@@ -162,7 +161,7 @@ impl <'a> TableSlice<'a> {
/// # Panic
/// Panic if writing to standard output fails
pub fn print_tty(&self, force_colorize: bool) {
- let r = match (stdout(), atty::is() || force_colorize) {
+ let r = match (stdout(), atty::is(atty::Stream::Stdout) || force_colorize) {
(Some(mut o), true) => self.print_term(&mut *o),
_ => self.print(&mut io::stdout()),
};
@@ -567,6 +566,7 @@ mod tests {
use Slice;
use row::Row;
use cell::Cell;
+ use format;
use format::consts::{FORMAT_NO_LINESEP, FORMAT_NO_COLSEP, FORMAT_CLEAN};
#[test]
@@ -708,6 +708,37 @@ mod tests {
assert_eq!(out, table.slice(1..4).to_string().replace("\r\n", "\n"));
}
+ #[test]
+ fn test_unicode_separators() {
+ let mut table = Table::new();
+ table.set_format(
+ format::FormatBuilder::new()
+ .column_separator('|')
+ .borders('|')
+ .separators( &[format::LinePosition::Top], format::LineSeparator::new('─', '┬', '┌', '┐'))
+ .separators( &[format::LinePosition::Intern], format::LineSeparator::new('─', '┼', '├', '┤'))
+ .separators( &[format::LinePosition::Bottom], format::LineSeparator::new('─', '┴', '└', '┘'))
+ .padding(1, 1)
+ .build()
+ );
+ table.add_row(Row::new(vec![Cell::new("1"), Cell::new("1"), Cell::new("1")]));
+ table.add_row(Row::new(vec![Cell::new("2"), Cell::new("2"), Cell::new("2")]));
+ table.set_titles(Row::new(vec![Cell::new("t1"), Cell::new("t2"), Cell::new("t3")]));
+ let out = "\
+┌────┬────┬────┐
+| t1 | t2 | t3 |
+├────┼────┼────┤
+| 1 | 1 | 1 |
+├────┼────┼────┤
+| 2 | 2 | 2 |
+└────┴────┴────┘
+";
+ println!("{}", out);
+ println!("____");
+ println!("{}", table.to_string().replace("\r\n", "\n"));
+ assert_eq!(out, table.to_string().replace("\r\n", "\n"));
+ }
+
mod csv {
use Table;
use row::Row;