From fc8f8d6c6a49fbac5579af2b317f624b81d911db Mon Sep 17 00:00:00 2001 From: Ben S Date: Sun, 22 Jun 2014 07:40:40 +0100 Subject: [PATCH] Add number of hard links column This is behaviour cribbed from ls. Even though I've never personally used it, I'm sure someone has a use for it. --- column.rs | 2 ++ file.rs | 3 ++- options.rs | 16 ++++++++++------ 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/column.rs b/column.rs index cf7a5b8..5adc798 100644 --- a/column.rs +++ b/column.rs @@ -4,6 +4,7 @@ pub enum Column { FileSize(bool), User(u64), Group, + HardLinks, } // Each column can pick its own alignment. Usually, numbers are @@ -17,6 +18,7 @@ impl Column { pub fn alignment(&self) -> Alignment { match *self { FileSize(_) => Right, + HardLinks => Right, _ => Left, } } diff --git a/file.rs b/file.rs index 0ff0b9b..b354731 100644 --- a/file.rs +++ b/file.rs @@ -2,7 +2,7 @@ use colours::{Plain, Style, Black, Red, Green, Yellow, Blue, Purple, Cyan, Fixed use std::io::{fs, IoResult}; use std::io; -use column::{Column, Permissions, FileName, FileSize, User, Group}; +use column::{Column, Permissions, FileName, FileSize, User, Group, HardLinks}; use format::{format_metric_bytes, format_IEC_bytes}; use unix::Unix; use sort::SortPart; @@ -94,6 +94,7 @@ impl<'a> File<'a> { Permissions => self.permissions_string(), FileName => self.file_name(), FileSize(use_iec) => self.file_size(use_iec), + HardLinks => Red.paint(self.stat.unstable.nlink.to_str().as_slice()), // Display the ID if the user/group doesn't exist, which // usually means it was deleted but its files weren't. diff --git a/options.rs b/options.rs index 45ff75c..9f234f7 100644 --- a/options.rs +++ b/options.rs @@ -2,7 +2,7 @@ extern crate getopts; use file::File; use std::cmp::lexical_ordering; -use column::{Column, Permissions, FileName, FileSize, User, Group}; +use column::{Column, Permissions, FileName, FileSize, User, Group, HardLinks}; use unix::get_current_user_id; use std::ascii::StrAsciiExt; @@ -35,6 +35,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("l", "links", "show number of hard links"), getopts::optflag("r", "reverse", "reverse order of files"), getopts::optopt("s", "sort", "field to sort by", "WORD"), ]; @@ -52,11 +53,14 @@ impl Options { } fn columns(matches: getopts::Matches) -> Vec { - let mut columns = vec![ - Permissions, - FileSize(matches.opt_present("binary")), - User(get_current_user_id()), - ]; + let mut columns = vec![Permissions]; + + if matches.opt_present("links") { + columns.push(HardLinks); + } + + columns.push(FileSize(matches.opt_present("binary"))); + columns.push(User(get_current_user_id())); if matches.opt_present("group") { columns.push(Group);