diff options
author | Pierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com> | 2016-09-12 11:30:09 +0200 |
---|---|---|
committer | Pierre-Henri Symoneaux <pierre-henri.symoneaux@nokia.com> | 2016-09-12 11:30:09 +0200 |
commit | 3a8e694c8754c3ce2cdf842b56bee6e02fe14b71 (patch) | |
tree | 365c094077767664f3a5535134091178d8084668 | |
parent | 7ea0205b9546da5b4754d79641a68605a72dd3b8 (diff) |
Fixed wrong formatting of unicode separators
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | examples/formatting.rs | 22 | ||||
-rw-r--r-- | src/format.rs | 16 | ||||
-rw-r--r-- | src/lib.rs | 1 |
4 files changed, 34 insertions, 6 deletions
@@ -26,3 +26,4 @@ unicode-width = "^0.1" term = "^0.4"
lazy_static = "^0.2"
atty = "^0.1"
+encode_unicode = "^0.2"
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(())
}
}
@@ -5,6 +5,7 @@ extern crate unicode_width; extern crate term;
extern crate atty;
#[macro_use] extern crate lazy_static;
+extern crate encode_unicode;
use std::io;
use std::io::{Write, Error};
|