1
0
Ben S 11 жил өмнө
parent
commit
ee8c146ced
2 өөрчлөгдсөн 9 нэмэгдсэн , 8 устгасан
  1. 6 6
      column.rs
  2. 3 2
      exa.rs

+ 6 - 6
column.rs

@@ -42,23 +42,23 @@ impl Column {
 }
 }
 
 
 // An Alignment is used to pad a string to a certain length, letting
 // An Alignment is used to pad a string to a certain length, letting
-// it pick which end it puts the text on. The length of the string is
-// passed in specifically because it needs to be the *unformatted*
-// length, rather than just the number of characters.
+// it pick which end it puts the text on. It takes the amount of
+// padding to apply, rather than the width the text should end up,
+// because these strings are usually full of control characters.
 
 
 impl Alignment {
 impl Alignment {
-    pub fn pad_string(&self, string: &String, string_length: uint, width: uint) -> String {
+    pub fn pad_string(&self, string: &String, padding: uint) -> String {
         let mut str = String::new();
         let mut str = String::new();
         match *self {
         match *self {
             Left => {
             Left => {
                 str.push_str(string.as_slice());
                 str.push_str(string.as_slice());
-                for _ in range(string_length, width) {
+                for _ in range(0, padding) {
                     str.push_char(' ');
                     str.push_char(' ');
                 }
                 }
             }
             }
 
 
             Right => {
             Right => {
-                for _ in range(string_length, width) {
+                for _ in range(0, padding) {
                     str.push_char(' ');
                     str.push_char(' ');
                 }
                 }
                 str.push_str(string.as_slice());
                 str.push_str(string.as_slice());

+ 3 - 2
exa.rs

@@ -95,7 +95,7 @@ fn exa(options: &Options, print_header: bool, string: String) {
         .map(|n| lengths.iter().map(|row| *row.get(n)).max().unwrap())
         .map(|n| lengths.iter().map(|row| *row.get(n)).max().unwrap())
         .collect();
         .collect();
 
 
-    for (field_lengths, row) in lengths.iter().zip(table.iter()) {
+    for (field_widths, row) in lengths.iter().zip(table.iter()) {
         for (num, column) in options.columns.iter().enumerate() {
         for (num, column) in options.columns.iter().enumerate() {
             if num != 0 {
             if num != 0 {
                 print!(" ");
                 print!(" ");
@@ -105,7 +105,8 @@ fn exa(options: &Options, print_header: bool, string: String) {
                 print!("{}", row.get(num));
                 print!("{}", row.get(num));
             }
             }
             else {
             else {
-                print!("{}", column.alignment().pad_string(row.get(num), *field_lengths.get(num), *column_widths.get(num)));
+                let padding = *column_widths.get(num) - *field_widths.get(num);
+                print!("{}", column.alignment().pad_string(row.get(num), padding));
             }
             }
         }
         }
         print!("\n");
         print!("\n");