mirror of
https://github.com/Llewellynvdm/exa.git
synced 2025-04-08 00:11:51 +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
|
// 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
|
// it pick which end it puts the text on. It takes the amount of
|
||||||
// passed in specifically because it needs to be the *unformatted*
|
// padding to apply, rather than the width the text should end up,
|
||||||
// length, rather than just the number of characters.
|
// because these strings are usually full of control characters.
|
||||||
|
|
||||||
impl Alignment {
|
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();
|
let mut str = String::new();
|
||||||
match *self {
|
match *self {
|
||||||
Left => {
|
Left => {
|
||||||
str.push_str(string.as_slice());
|
str.push_str(string.as_slice());
|
||||||
for _ in range(string_length, width) {
|
for _ in range(0, padding) {
|
||||||
str.push_char(' ');
|
str.push_char(' ');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Right => {
|
Right => {
|
||||||
for _ in range(string_length, width) {
|
for _ in range(0, padding) {
|
||||||
str.push_char(' ');
|
str.push_char(' ');
|
||||||
}
|
}
|
||||||
str.push_str(string.as_slice());
|
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())
|
.map(|n| lengths.iter().map(|row| *row.get(n)).max().unwrap())
|
||||||
.collect();
|
.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() {
|
for (num, column) in options.columns.iter().enumerate() {
|
||||||
if num != 0 {
|
if num != 0 {
|
||||||
print!(" ");
|
print!(" ");
|
||||||
@ -105,7 +105,8 @@ fn exa(options: &Options, print_header: bool, string: String) {
|
|||||||
print!("{}", row.get(num));
|
print!("{}", row.get(num));
|
||||||
}
|
}
|
||||||
else {
|
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");
|
print!("\n");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user