diff options
author | pierresy <pierre-henri.symoneaux@alcatel-lucent.com> | 2016-02-14 19:23:36 +0100 |
---|---|---|
committer | pierresy <pierre-henri.symoneaux@alcatel-lucent.com> | 2016-02-14 19:23:36 +0100 |
commit | d6c64df750f8907c84b9e185884b657c1d0e0a06 (patch) | |
tree | 4ba896645a34c4a3d3d2e1b6b5dcadd441778c23 /src/utils.rs | |
parent | eaff73df9751b9c9163bca003f2be5a14ba40761 (diff) |
Don't fill the right part of final cell in a row when there is no right borer defined int the table format
Diffstat (limited to 'src/utils.rs')
-rw-r--r-- | src/utils.rs | 31 |
1 files changed, 25 insertions, 6 deletions
diff --git a/src/utils.rs b/src/utils.rs index 669b926..79f56b5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -45,7 +45,7 @@ impl Write for StringWriter { }
/// Align/fill a string and print it to `out`
-pub fn print_align<T: Write+?Sized>(out: &mut T, align: Alignment, text: &str, fill: char, size: usize) -> Result<(), Error> {
+pub fn print_align<T: Write+?Sized>(out: &mut T, align: Alignment, text: &str, fill: char, size: usize, skip_right_fill: bool) -> Result<(), Error> {
let text_len = UnicodeWidthStr::width(text);
let mut nfill = if text_len < size { size - text_len } else { 0 };
let n = match align {
@@ -58,7 +58,7 @@ pub fn print_align<T: Write+?Sized>(out: &mut T, align: Alignment, text: &str, f nfill -= n;
}
try!(out.write(text.as_bytes()));
- if nfill > 0 {
+ if nfill > 0 && ! skip_right_fill {
try!(out.write(&vec![fill as u8; nfill]));
}
return Ok(());
@@ -83,19 +83,38 @@ mod tests { #[test]
fn fill_align() {
let mut out = StringWriter::new();
- print_align(&mut out, Alignment::RIGHT, "foo", '*', 10).unwrap();
+ print_align(&mut out, Alignment::RIGHT, "foo", '*', 10, false).unwrap();
assert_eq!(out.as_string(), "*******foo");
let mut out = StringWriter::new();
- print_align(&mut out, Alignment::LEFT, "foo", '*', 10).unwrap();
+ print_align(&mut out, Alignment::LEFT, "foo", '*', 10, false).unwrap();
assert_eq!(out.as_string(), "foo*******");
let mut out = StringWriter::new();
- print_align(&mut out, Alignment::CENTER, "foo", '*', 10).unwrap();
+ print_align(&mut out, Alignment::CENTER, "foo", '*', 10, false).unwrap();
assert_eq!(out.as_string(), "***foo****");
let mut out = StringWriter::new();
- print_align(&mut out, Alignment::CENTER, "foo", '*', 1).unwrap();
+ print_align(&mut out, Alignment::CENTER, "foo", '*', 1, false).unwrap();
+ assert_eq!(out.as_string(), "foo");
+ }
+
+ #[test]
+ fn skip_right_fill() {
+ let mut out = StringWriter::new();
+ print_align(&mut out, Alignment::RIGHT, "foo", '*', 10, true).unwrap();
+ assert_eq!(out.as_string(), "*******foo");
+
+ let mut out = StringWriter::new();
+ print_align(&mut out, Alignment::LEFT, "foo", '*', 10, true).unwrap();
+ assert_eq!(out.as_string(), "foo");
+
+ let mut out = StringWriter::new();
+ print_align(&mut out, Alignment::CENTER, "foo", '*', 10, true).unwrap();
+ assert_eq!(out.as_string(), "***foo");
+
+ let mut out = StringWriter::new();
+ print_align(&mut out, Alignment::CENTER, "foo", '*', 1, false).unwrap();
assert_eq!(out.as_string(), "foo");
}
}
|