提交 ee8c146c 编写于 作者: B Ben S

Simplify string padding

上级 745bcce0
......@@ -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());
......
......@@ -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");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册