Browse Source

Restore xattrs to their long view column

Had to thread the value in at display-time to get it to only query the attributes once!

This isn't the nicest way to do it, but this *is* a bit of an edge-case (it's the only thing where a column depends on something that gets calculated later)
Ben S 10 years ago
parent
commit
5e1ff9cdcd
2 changed files with 33 additions and 18 deletions
  1. 24 17
      src/output/details.rs
  2. 9 1
      src/output/grid_details.rs

+ 24 - 17
src/output/details.rs

@@ -86,21 +86,28 @@ impl Details {
     /// is present.
     fn add_files_to_table<U: Users>(&self, table: &mut Table<U>, src: &[File], depth: usize) {
         for (index, file) in src.iter().enumerate() {
-            table.add_file(file, depth, index == src.len() - 1, true);
-
             let mut xattrs = Vec::new();
             let mut errors = Vec::new();
 
-            if self.xattr {
-                match file.path.attributes() {
-                    Ok(xs) => {
+            let has_xattrs = match file.path.attributes() {
+                Ok(xs) => {
+                    let r = !xs.is_empty();
+                    if self.xattr {
                         for xattr in xs {
                             xattrs.push(xattr);
                         }
-                    },
-                    Err(e) => errors.push((e, None)),
-                }
-            }
+                    }
+                    r
+                },
+                Err(e) => {
+                    if self.xattr {
+                        errors.push((e, None));
+                    }
+                    true
+                },
+            };
+
+            table.add_file(file, depth, index == src.len() - 1, true, has_xattrs);
 
             // There are two types of recursion that exa supports: a tree
             // view, which is dealt with here, and multiple listings, which is
@@ -290,8 +297,8 @@ impl<U> Table<U> where U: Users {
     }
 
     /// Get the cells for the given file, and add the result to the table.
-    fn add_file(&mut self, file: &File, depth: usize, last: bool, links: bool) {
-        let cells = self.cells_for_file(file);
+    fn add_file(&mut self, file: &File, depth: usize, last: bool, links: bool, xattrs: bool) {
+        let cells = self.cells_for_file(file, xattrs);
         self.add_file_with_cells(cells, file, depth, last, links)
     }
 
@@ -308,15 +315,15 @@ impl<U> Table<U> where U: Users {
 
     /// Use the list of columns to find which cells should be produced for
     /// this file, per-column.
-    pub fn cells_for_file(&mut self, file: &File) -> Vec<Cell> {
+    pub fn cells_for_file(&mut self, file: &File, xattrs: bool) -> Vec<Cell> {
         self.columns.clone().iter()
-                    .map(|c| self.display(file, c))
+                    .map(|c| self.display(file, c, xattrs))
                     .collect()
     }
 
-    fn display(&mut self, file: &File, column: &Column) -> Cell {
+    fn display(&mut self, file: &File, column: &Column, xattrs: bool) -> Cell {
         match *column {
-            Column::Permissions    => self.render_permissions(file.permissions()),
+            Column::Permissions    => self.render_permissions(file.permissions(), xattrs),
             Column::FileSize(fmt)  => self.render_size(file.size(), fmt),
             Column::Timestamp(t)   => self.render_time(file.timestamp(t)),
             Column::HardLinks      => self.render_links(file.links()),
@@ -328,7 +335,7 @@ impl<U> Table<U> where U: Users {
         }
     }
 
-    fn render_permissions(&self, permissions: f::Permissions) -> Cell {
+    fn render_permissions(&self, permissions: f::Permissions, xattrs: bool) -> Cell {
         let c = self.colours.perms;
         let bit = |bit, chr: &'static str, style: Style| {
             if bit { style.paint(chr) } else { self.colours.punctuation.paint("-") }
@@ -358,7 +365,7 @@ impl<U> Table<U> where U: Users {
             bit(permissions.other_execute, "x", c.other_execute),
         ];
 
-        if permissions.attribute {
+        if xattrs {
             columns.push(c.attribute.paint("@"));
         }
 

+ 9 - 1
src/output/grid_details.rs

@@ -5,6 +5,7 @@ use term_grid as grid;
 
 use column::{Column, Cell};
 use dir::Dir;
+use feature::xattr::FileAttributes;
 use file::File;
 use output::details::{Details, Table};
 use output::grid::Grid;
@@ -15,6 +16,13 @@ pub struct GridDetails {
     pub details: Details,
 }
 
+fn file_has_xattrs(file: &File) -> bool {
+    match file.path.attributes() {
+        Ok(attrs) => !attrs.is_empty(),
+        Err(_) => false,
+    }
+}
+
 impl GridDetails {
     pub fn view(&self, dir: Option<&Dir>, files: &[File]) {
         let columns_for_dir = match self.details.columns {
@@ -23,7 +31,7 @@ impl GridDetails {
         };
 
         let mut first_table = Table::with_options(self.details.colours, columns_for_dir.clone());
-        let cells: Vec<_> = files.iter().map(|file| first_table.cells_for_file(file)).collect();
+        let cells: Vec<_> = files.iter().map(|file| first_table.cells_for_file(file, file_has_xattrs(file))).collect();
 
         let mut last_working_table = self.make_grid(1, &*columns_for_dir, files, cells.clone());