summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorwickerwaka <martin.donlon@gmail.com>2014-08-30 11:27:02 -0700
committerwickerwaka <martin.donlon@gmail.com>2014-09-04 07:38:53 -0700
commit2bc4a5e92aef51bd34a5b1a506c5edcee893d6ac (patch)
tree3ae6b60182e140f23f3d1cc772349fb5eb29b3f1 /src/libcore
parent6d8b5c9f7d1347b715242a837fba87a01ae61d7e (diff)
downloadrust-2bc4a5e92aef51bd34a5b1a506c5edcee893d6ac.tar.gz
rust-2bc4a5e92aef51bd34a5b1a506c5edcee893d6ac.zip
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
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/fmt/mod.rs23
-rw-r--r--src/libcore/fmt/rt.rs2
2 files changed, 18 insertions, 7 deletions
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs
index 32663e5eb0f..7c4494358b1 100644
--- a/src/libcore/fmt/mod.rs
+++ b/src/libcore/fmt/mod.rs
@@ -461,19 +461,28 @@ impl<'a> Formatter<'a> {
         use char::Char;
         let align = match self.align {
             rt::AlignUnknown => default,
-            rt::AlignLeft | rt::AlignRight => self.align
+            _ => self.align
         };
-        if align == rt::AlignLeft {
-            try!(f(self));
-        }
+
+        let (pre_pad, post_pad) = match align {
+            rt::AlignLeft => (0u, padding),
+            rt::AlignRight | rt::AlignUnknown => (padding, 0u),
+            rt::AlignCenter => (padding / 2, (padding + 1) / 2),
+        };
+
         let mut fill = [0u8, ..4];
         let len = self.fill.encode_utf8(fill).unwrap_or(0);
-        for _ in range(0, padding) {
+
+        for _ in range(0, pre_pad) {
             try!(self.buf.write(fill.slice_to(len)));
         }
-        if align == rt::AlignRight {
-            try!(f(self));
+
+        try!(f(self));
+
+        for _ in range(0, post_pad) {
+            try!(self.buf.write(fill.slice_to(len)));
         }
+
         Ok(())
     }
 
diff --git a/src/libcore/fmt/rt.rs b/src/libcore/fmt/rt.rs
index 388084b9ed8..1f5449130ec 100644
--- a/src/libcore/fmt/rt.rs
+++ b/src/libcore/fmt/rt.rs
@@ -43,6 +43,8 @@ pub enum Alignment {
     AlignLeft,
     /// Indication that contents should be right-aligned.
     AlignRight,
+    /// Indication that contents should be center-aligned.
+    AlignCenter,
     /// No alignment was requested.
     AlignUnknown,
 }