From 2bc4a5e92aef51bd34a5b1a506c5edcee893d6ac Mon Sep 17 00:00:00 2001 From: wickerwaka Date: Sat, 30 Aug 2014 11:27:02 -0700 Subject: Center alignment for fmt Use '^' to specify center alignment in format strings. fmt!( "[{:^5s}]", "Hi" ) -> "[ Hi ]" fmt!( "[{:^5s}]", "H" ) -> "[ H ]" fmt!( "[{:^5d}]", 1i ) -> "[ 1 ]" fmt!( "[{:^5d}]", -1i ) -> "[ -1 ]" fmt!( "[{:^6d}]", 1i ) -> "[ 1 ]" fmt!( "[{:^6d}]", -1i ) -> "[ -1 ]" If the padding is odd then the padding on the right will be one character longer than the padding on the left. Tuples squashed --- src/libfmt_macros/lib.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'src/libfmt_macros') diff --git a/src/libfmt_macros/lib.rs b/src/libfmt_macros/lib.rs index 560681765cd..9272369f73c 100644 --- a/src/libfmt_macros/lib.rs +++ b/src/libfmt_macros/lib.rs @@ -81,6 +81,8 @@ pub enum Alignment { AlignLeft, /// The value will be aligned to the right. AlignRight, + /// The value will be aligned in the center. + AlignCenter, /// The value will take on a default alignment. AlignUnknown, } @@ -279,7 +281,7 @@ impl<'a> Parser<'a> { match self.cur.clone().next() { Some((_, c)) => { match self.cur.clone().skip(1).next() { - Some((_, '>')) | Some((_, '<')) => { + Some((_, '>')) | Some((_, '<')) | Some((_, '^')) => { spec.fill = Some(c); self.cur.next(); } @@ -293,6 +295,8 @@ impl<'a> Parser<'a> { spec.align = AlignLeft; } else if self.consume('>') { spec.align = AlignRight; + } else if self.consume('^') { + spec.align = AlignCenter; } // Sign flags if self.consume('+') { -- cgit 1.4.1-3-g733a5