summaryrefslogtreecommitdiffstats
path: root/src/output
diff options
context:
space:
mode:
authorBen S <ogham@bsago.me>2015-08-25 15:04:15 +0100
committerBen S <ogham@bsago.me>2015-08-25 15:04:15 +0100
commit2a9b6fe93080ba00cf99c05db41076e843c8aae7 (patch)
tree7acbbeaeb8a335332fe4c85fa1a349b04c5dee0c /src/output
parent7deb08644ac85656c61f1917f2011fa72ca9eb86 (diff)
Display errors inline in the tree
When tree mode is active, this will print out errors as another form of child node in the tree, instead of in one big block before any output. The 'this' field now holds the io::Result of the readdir call, rather than only a *successful* result.
Diffstat (limited to 'src/output')
-rw-r--r--src/output/details.rs48
1 files changed, 42 insertions, 6 deletions
diff --git a/src/output/details.rs b/src/output/details.rs
index cb2f2ea..6be3b68 100644
--- a/src/output/details.rs
+++ b/src/output/details.rs
@@ -1,4 +1,7 @@
+use std::error::Error;
+use std::io;
use std::iter::repeat;
+use std::path::Path;
use std::string::ToString;
use colours::Colours;
@@ -98,11 +101,24 @@ impl Details {
// Use the filter to remove unwanted files *before* expanding
// them, so we don't examine any directories that wouldn't
// have their contents listed anyway.
- if let Some(ref dir) = file.this {
- let mut files = dir.files(true).flat_map(|f| f).collect();
-
- filter.transform_files(&mut files);
- self.add_files_to_table(table, &files, depth + 1);
+ match file.this {
+ Some(Ok(ref dir)) => {
+ let mut files = Vec::new();
+
+ for file_to_add in dir.files(true) {
+ match file_to_add {
+ Ok(f) => files.push(f),
+ Err((path, e)) => table.add_error(&e, depth + 1, false, Some(&*path)),
+ }
+ }
+
+ filter.transform_files(&mut files);
+ self.add_files_to_table(table, &files, depth + 1);
+ },
+ Some(Err(ref e)) => {
+ table.add_error(e, depth + 1, true, None);
+ },
+ None => {},
}
}
}
@@ -221,6 +237,24 @@ impl<U> Table<U> where U: Users {
self.rows.push(row);
}
+ pub fn add_error(&mut self, error: &io::Error, depth: usize, last: bool, path: Option<&Path>) {
+ let error_message = match path {
+ Some(path) => format!("<{}: {}>", path.display(), error),
+ None => format!("<{}>", error),
+ };
+
+ let row = Row {
+ depth: depth,
+ cells: None,
+ name: Cell::paint(self.colours.broken_arrow, &error_message),
+ last: last,
+ attrs: Vec::new(),
+ children: false,
+ };
+
+ self.rows.push(row);
+ }
+
/// Get the cells for the given file, and add the result to the table.
pub fn add_file(&mut self, file: &File, depth: usize, last: bool, links: bool) {
let cells = self.cells_for_file(file);
@@ -420,6 +454,8 @@ impl<U> Table<U> where U: Users {
.map(|n| self.rows.iter().map(|row| row.column_width(n)).max().unwrap_or(0))
.collect();
+ let total_width: usize = self.columns.len() + column_widths.iter().sum::<usize>();
+
for row in self.rows.iter() {
let mut cell = Cell::empty();
@@ -434,7 +470,7 @@ impl<U> Table<U> where U: Users {
}
}
else {
- cell.add_spaces(column_widths.len())
+ cell.add_spaces(total_width)
}
let mut filename = String::new();