1
0
Эх сурвалжийг харах

Add header row with -h option

Ben S 11 жил өмнө
parent
commit
038143682c
3 өөрчлөгдсөн 22 нэмэгдсэн , 1 устгасан
  1. 13 0
      column.rs
  2. 6 1
      exa.rs
  3. 3 0
      options.rs

+ 13 - 0
column.rs

@@ -26,6 +26,19 @@ impl Column {
             _           => Left,
         }
     }
+
+    pub fn header(&self) -> &'static str {
+        match *self {
+            Permissions => "Permissions",
+            FileName => "Name",
+            FileSize(_) => "Size",
+            Blocks => "Blocks",
+            User(_) => "User",
+            Group => "Group",
+            HardLinks => "Links",
+            Inode => "inode",
+        }
+    }
 }
 
 // An Alignment is used to pad a string to a certain length, letting

+ 6 - 1
exa.rs

@@ -8,6 +8,7 @@ use file::File;
 use dir::Dir;
 use options::Options;
 use unix::Unix;
+use colours::Plain;
 
 pub mod colours;
 pub mod column;
@@ -72,10 +73,14 @@ fn exa(options: &Options, print_header: bool, string: String) {
 
     let mut cache = Unix::empty_cache();
 
-    let table: Vec<Vec<String>> = files.iter()
+    let mut table: Vec<Vec<String>> = files.iter()
         .map(|f| options.columns.iter().map(|c| f.display(c, &mut cache)).collect())
         .collect();
 
+    if options.header {
+        table.unshift(options.columns.iter().map(|c| Plain.underline().paint(c.header())).collect());
+    }
+
     // Each column needs to have its invisible colour-formatting
     // characters stripped before it has its width calculated, or the
     // width will be incorrect and the columns won't line up properly.

+ 3 - 0
options.rs

@@ -16,6 +16,7 @@ pub struct Options {
     pub reverse: bool,
     pub dirs: Vec<String>,
     pub columns: Vec<Column>,
+    pub header: bool,
 }
 
 impl SortField {
@@ -35,6 +36,7 @@ impl Options {
             getopts::optflag("a", "all", "show dot-files"),
             getopts::optflag("b", "binary", "use binary prefixes in file sizes"),
             getopts::optflag("g", "group", "show group as well as user"),
+            getopts::optflag("h", "header", "show a header row at the top"),
             getopts::optflag("i", "inode", "show each file's inode number"),
             getopts::optflag("l", "links", "show number of hard links"),
             getopts::optflag("r", "reverse", "reverse order of files"),
@@ -47,6 +49,7 @@ impl Options {
             Ok(matches) => Ok(Options {
                 showInvisibles: matches.opt_present("all"),
                 reverse: matches.opt_present("reverse"),
+                header: matches.opt_present("header"),
                 sortField: matches.opt_str("sort").map(|word| SortField::from_word(word)).unwrap_or(Name),
                 dirs: matches.free.clone(),
                 columns: Options::columns(matches),