mirror of
https://github.com/Llewellynvdm/exa.git
synced 2024-11-26 13:56:27 +00:00
Simplify string padding
This commit is contained in:
parent
745bcce02a
commit
ee8c146ced
12
column.rs
12
column.rs
@ -42,23 +42,23 @@ impl Column {
|
||||
}
|
||||
|
||||
// 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.
|
||||
// it pick which end it puts the text on. It takes the amount of
|
||||
// padding to apply, rather than the width the text should end up,
|
||||
// because these strings are usually full of control characters.
|
||||
|
||||
impl Alignment {
|
||||
pub fn pad_string(&self, string: &String, string_length: uint, width: uint) -> String {
|
||||
pub fn pad_string(&self, string: &String, padding: uint) -> String {
|
||||
let mut str = String::new();
|
||||
match *self {
|
||||
Left => {
|
||||
str.push_str(string.as_slice());
|
||||
for _ in range(string_length, width) {
|
||||
for _ in range(0, padding) {
|
||||
str.push_char(' ');
|
||||
}
|
||||
}
|
||||
|
||||
Right => {
|
||||
for _ in range(string_length, width) {
|
||||
for _ in range(0, padding) {
|
||||
str.push_char(' ');
|
||||
}
|
||||
str.push_str(string.as_slice());
|
||||
|
5
exa.rs
5
exa.rs
@ -95,7 +95,7 @@ fn exa(options: &Options, print_header: bool, string: String) {
|
||||
.map(|n| lengths.iter().map(|row| *row.get(n)).max().unwrap())
|
||||
.collect();
|
||||
|
||||
for (field_lengths, row) in lengths.iter().zip(table.iter()) {
|
||||
for (field_widths, row) in lengths.iter().zip(table.iter()) {
|
||||
for (num, column) in options.columns.iter().enumerate() {
|
||||
if num != 0 {
|
||||
print!(" ");
|
||||
@ -105,7 +105,8 @@ fn exa(options: &Options, print_header: bool, string: String) {
|
||||
print!("{}", row.get(num));
|
||||
}
|
||||
else {
|
||||
print!("{}", column.alignment().pad_string(row.get(num), *field_lengths.get(num), *column_widths.get(num)));
|
||||
let padding = *column_widths.get(num) - *field_widths.get(num);
|
||||
print!("{}", column.alignment().pad_string(row.get(num), padding));
|
||||
}
|
||||
}
|
||||
print!("\n");
|
||||
|
Loading…
Reference in New Issue
Block a user