ソースを参照

Right-align file sizes

Currently there's only one numeric column, and that's the file size, so it gets
special treatment.

I was originally going to have a folder file size field be filled up with '-'s
as far as it could go, leaving it entirely up to the column how its field gets
formatted. But then I saw just one '-' working just fine, so I left it like
that. In the first try, columns could do anything they want when padding a
string (including changing the padding character or just changing it entirely),
but now there's no point.
Ben S 11 年 前
コミット
e5e426fc60
3 ファイル変更47 行追加6 行削除
  1. 44 0
      column.rs
  2. 2 5
      exa.rs
  3. 1 1
      file.rs

+ 44 - 0
column.rs

@@ -5,3 +5,47 @@ pub enum Column {
     User(u64),
     Group,
 }
+
+// Each column can pick its own alignment. Usually, numbers are
+// right-aligned, and text is left-aligned.
+
+pub enum Alignment {
+    Left, Right,
+}
+
+impl Column {
+    pub fn alignment(&self) -> Alignment {
+        match *self {
+            FileSize(_) => Right,
+            _           => Left,
+        }
+    }
+}
+
+// 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.
+
+impl Alignment {
+    pub fn pad_string(&self, string: &String, string_length: uint, width: uint) -> String {
+        let mut str = String::new();
+        match *self {
+            Left => {
+                str.push_str(string.as_slice());
+                for _ in range(string_length, width) {
+                    str.push_char(' ');
+                }
+            }
+
+            Right => {
+                for _ in range(string_length, width) {
+                    str.push_char(' ');
+                }
+                str.push_str(string.as_slice());
+            },
+        }
+        return str;
+    }
+}
+

+ 2 - 5
exa.rs

@@ -74,16 +74,13 @@ fn exa(options: &Options, path: Path) {
 
     for (field_lengths, row) in lengths.iter().zip(table.iter()) {
         let mut first = true;
-        for ((column_length, cell), field_length) in column_widths.iter().zip(row.iter()).zip(field_lengths.iter()) {
+        for (((column_length, cell), field_length), column) in column_widths.iter().zip(row.iter()).zip(field_lengths.iter()).zip(options.columns.iter()) {  // this is getting messy
             if first {
                 first = false;
             } else {
                 print!(" ");
             }
-            print!("{}", cell.as_slice());
-            for _ in range(*field_length, *column_length) {
-                print!(" ");
-            }
+            print!("{}", column.alignment().pad_string(cell, *field_length, *column_length));
         }
         print!("\n");
     }

+ 1 - 1
file.rs

@@ -87,7 +87,7 @@ impl<'a> File<'a> {
         // Don't report file sizes for directories. I've never looked
         // at one of those numbers and gained any information from it.
         if self.stat.kind == io::TypeDirectory {
-            Black.bold().paint("---")
+            Black.bold().paint("-")
         } else {
             let size_str = if use_iec_prefixes {
                 format_IEC_bytes(self.stat.size)