Right-align file sizes

Currently there's only one numeric column, and that's the file size, so it gets
special treatment.

I was originally going to have a folder file size field be filled up with '-'s
as far as it could go, leaving it entirely up to the column how its field gets
formatted. But then I saw just one '-' working just fine, so I left it like
that. In the first try, columns could do anything they want when padding a
string (including changing the padding character or just changing it entirely),
but now there's no point.
This commit is contained in:
Ben S 2014-06-03 21:14:35 +01:00
parent 2d41333117
commit e5e426fc60
3 changed files with 47 additions and 6 deletions

View File

@ -5,3 +5,47 @@ pub enum Column {
User(u64),
Group,
}
// Each column can pick its own alignment. Usually, numbers are
// right-aligned, and text is left-aligned.
pub enum Alignment {
Left, Right,
}
impl Column {
pub fn alignment(&self) -> Alignment {
match *self {
FileSize(_) => Right,
_ => Left,
}
}
}
// An Alignment is used to pad a string to a certain length, letting
// it pick which end it puts the text on. The length of the string is
// passed in specifically because it needs to be the *unformatted*
// length, rather than just the number of characters.
impl Alignment {
pub fn pad_string(&self, string: &String, string_length: uint, width: uint) -> String {
let mut str = String::new();
match *self {
Left => {
str.push_str(string.as_slice());
for _ in range(string_length, width) {
str.push_char(' ');
}
}
Right => {
for _ in range(string_length, width) {
str.push_char(' ');
}
str.push_str(string.as_slice());
},
}
return str;
}
}

7
exa.rs
View File

@ -74,16 +74,13 @@ fn exa(options: &Options, path: Path) {
for (field_lengths, row) in lengths.iter().zip(table.iter()) {
let mut first = true;
for ((column_length, cell), field_length) in column_widths.iter().zip(row.iter()).zip(field_lengths.iter()) {
for (((column_length, cell), field_length), column) in column_widths.iter().zip(row.iter()).zip(field_lengths.iter()).zip(options.columns.iter()) { // this is getting messy
if first {
first = false;
} else {
print!(" ");
}
print!("{}", cell.as_slice());
for _ in range(*field_length, *column_length) {
print!(" ");
}
print!("{}", column.alignment().pad_string(cell, *field_length, *column_length));
}
print!("\n");
}

View File

@ -87,7 +87,7 @@ impl<'a> File<'a> {
// Don't report file sizes for directories. I've never looked
// at one of those numbers and gained any information from it.
if self.stat.kind == io::TypeDirectory {
Black.bold().paint("---")
Black.bold().paint("-")
} else {
let size_str = if use_iec_prefixes {
format_IEC_bytes(self.stat.size)