From 827a1e11fdd47da2dd090f5ce99afe9a7cfbb434 Mon Sep 17 00:00:00 2001 From: Ben S Date: Tue, 3 Feb 2015 13:48:39 +0000 Subject: [PATCH] Make filename not a column FileName was always a special-cased column, as it was assumed to be the last column in the output. Now, it's explicitly marked as such. This allows the hash marks to be placed before the filename, rather than at the start of the line. --- src/column.rs | 2 -- src/file.rs | 38 ++++++++++++++------------------------ src/options.rs | 1 - src/output.rs | 32 +++++++++++++++----------------- 4 files changed, 29 insertions(+), 44 deletions(-) diff --git a/src/column.rs b/src/column.rs index 210e69e..74d449d 100644 --- a/src/column.rs +++ b/src/column.rs @@ -5,7 +5,6 @@ use ansi_term::Style; #[derive(PartialEq, Debug, Copy)] pub enum Column { Permissions, - FileName, FileSize(SizeFormat), Blocks, User, @@ -49,7 +48,6 @@ impl Column { pub fn header(&self) -> &'static str { match *self { Column::Permissions => "Permissions", - Column::FileName => "Name", Column::FileSize(_) => "Size", Column::Blocks => "Blocks", Column::User => "User", diff --git a/src/file.rs b/src/file.rs index 850a066..6d259c0 100644 --- a/src/file.rs +++ b/src/file.rs @@ -94,7 +94,6 @@ impl<'a> File<'a> { pub fn display(&self, column: &Column, users_cache: &mut U) -> Cell { match *column { Permissions => self.permissions_string(), - FileName => self.file_name_view(), FileSize(f) => self.file_size(f), HardLinks => self.hard_links(), Inode => self.inode(), @@ -110,15 +109,12 @@ impl<'a> File<'a> { /// /// It consists of the file name coloured in the appropriate style, /// with special formatting for a symlink. - pub fn file_name_view(&self) -> Cell { + pub fn file_name_view(&self) -> String { if self.stat.kind == io::FileType::Symlink { self.symlink_file_name_view() } else { - Cell { - length: 0, // This length is ignored (rightmost column) - text: self.file_colour().paint(&*self.name).to_string(), - } + self.file_colour().paint(&*self.name).to_string() } } @@ -130,7 +126,7 @@ impl<'a> File<'a> { /// an error, highlight the target and arrow in red. The error would /// be shown out of context, and it's almost always because the /// target doesn't exist. - fn symlink_file_name_view(&self) -> Cell { + fn symlink_file_name_view(&self) -> String { let name = &*self.name; let style = self.file_colour(); @@ -141,26 +137,20 @@ impl<'a> File<'a> { }; match self.target_file(&target_path) { - Ok(file) => Cell { - length: 0, // These lengths are never actually used... - text: format!("{} {} {}{}{}", - style.paint(name), - GREY.paint("=>"), - Cyan.paint(target_path.dirname_str().unwrap()), - Cyan.paint("/"), - file.file_colour().paint(file.name.as_slice())), - }, - Err(filename) => Cell { - length: 0, // ...because the rightmost column lengths are ignored! - text: format!("{} {} {}", - style.paint(name), - Red.paint("=>"), - Red.underline().paint(filename.as_slice())), - }, + Ok(file) => format!("{} {} {}{}{}", + style.paint(name), + GREY.paint("=>"), + Cyan.paint(target_path.dirname_str().unwrap()), + Cyan.paint("/"), + file.file_colour().paint(file.name.as_slice())), + Err(filename) => format!("{} {} {}", + style.paint(name), + Red.paint("=>"), + Red.underline().paint(filename.as_slice())), } } else { - Cell::paint(style, name) + style.paint(name).to_string() } } diff --git a/src/options.rs b/src/options.rs index d9d819e..f18083b 100644 --- a/src/options.rs +++ b/src/options.rs @@ -307,7 +307,6 @@ impl Columns { } } - columns.push(FileName); columns } } diff --git a/src/output.rs b/src/output.rs index 135b0b3..76fb077 100644 --- a/src/output.rs +++ b/src/output.rs @@ -30,7 +30,7 @@ impl View { /// The lines view literally just displays each file, line-by-line. fn lines_view(files: &[File]) { for file in files.iter() { - println!("{}", file.file_name_view().text); + println!("{}", file.file_name_view()); } } @@ -137,7 +137,8 @@ fn details_view(columns: &[Column], files: &[File], header: bool, tree: bool) { if header { let row = Row { depth: 0, - cells: columns.iter().map(|c| Cell::paint(Plain.underline(), c.header())).collect() + cells: columns.iter().map(|c| Cell::paint(Plain.underline(), c.header())).collect(), + name: Plain.underline().paint("Name").to_string() }; table.insert(0, row); @@ -148,25 +149,20 @@ fn details_view(columns: &[Column], files: &[File], header: bool, tree: bool) { .collect(); for row in table.iter() { - for _ in range(0, row.depth) { - print!("#"); - } - for (num, column) in columns.iter().enumerate() { - if num != 0 { - print!(" "); // Separator + let padding = column_widths[num] - row.cells[num].length; + print!("{} ", column.alignment().pad_string(&row.cells[num].text, padding)); + } + + if tree { + for _ in range(0, row.depth) { + print!("#"); } - if num == columns.len() - 1 { - // The final column doesn't need to have trailing spaces - print!("{}", row.cells[num].text); - } - else { - let padding = column_widths[num] - row.cells[num].length; - print!("{}", column.alignment().pad_string(&row.cells[num].text, padding)); - } + print!(" "); } - print!("\n"); + + print!("{}\n", row.name); } } @@ -176,6 +172,7 @@ fn get_files(columns: &[Column], cache: &mut OSUsers, recurse: bool, dest: &mut let row = Row { depth: depth, cells: columns.iter().map(|c| file.display(c, cache)).collect(), + name: file.file_name_view(), }; dest.push(row); @@ -191,4 +188,5 @@ fn get_files(columns: &[Column], cache: &mut OSUsers, recurse: bool, dest: &mut struct Row { pub depth: u8, pub cells: Vec, + pub name: String, }