Skip to content

Commit

Permalink
refactored print_adjusted macro to function
Browse files Browse the repository at this point in the history
  • Loading branch information
snapdgn committed Sep 12, 2022
1 parent 0aeade3 commit ab01d91
Showing 1 changed file with 59 additions and 24 deletions.
83 changes: 59 additions & 24 deletions src/uu/stat/src/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,41 @@ fn pad_and_print(result: &str, left: bool, width: usize, padding: Padding) {
};
}

macro_rules! print_adjusted {
($str: ident, $left: expr, $width: expr, $padding: expr) => {
let field_width = cmp::max($width, $str.len());
pad_and_print(&$str, $left, field_width, $padding);
};
($str: ident, $left: expr, $need_prefix: expr, $prefix: expr, $width: expr, $padding: expr) => {
let mut field_width = cmp::max($width, $str.len());
if $need_prefix {
field_width -= $prefix.len();
fn print_adjusted(
s: &str,
left: bool,
need_prefix: Option<bool>,
prefix: Option<&str>,
width: usize,
padding: Padding,
) {
let mut field_width = cmp::max(width, s.len());
if let Some(p) = prefix {
if let Some(needprefix) = need_prefix {
if needprefix {
field_width -= p.len();
}
}
pad_and_print(&$str, $left, field_width, $padding);
};
pad_and_print(s, left, field_width, padding);
} else {
pad_and_print(s, left, field_width, padding);
}
}

//macro_rules! print_adjusted {
//($str: ident, $left: expr, $width: expr, $padding: expr) => {
//let field_width = cmp::max($width, $str.len());
//pad_and_print(&$str, $left, field_width, $padding);
//};
//($str: ident, $left: expr, $need_prefix: expr, $prefix: expr, $width: expr, $padding: expr) => {
//let mut field_width = cmp::max($width, $str.len());
//if $need_prefix {
//field_width -= $prefix.len();
//}
//pad_and_print(&$str, $left, field_width, $padding);
//};
//}

static ABOUT: &str = "Display file or file system status.";
const USAGE: &str = "{} [OPTION]... FILE...";

Expand Down Expand Up @@ -286,7 +307,7 @@ fn print_it(arg: &str, output_type: &OutputType, flag: u8, width: usize, precisi
} else {
arg
};
print_adjusted!(s, left_align, width, Padding::Space);
print_adjusted(s, left_align, None, None, width, Padding::Space);
}
OutputType::Integer => {
let arg = if has!(flag, F_GROUP) {
Expand All @@ -296,7 +317,14 @@ fn print_it(arg: &str, output_type: &OutputType, flag: u8, width: usize, precisi
};
let min_digits = cmp::max(precision, arg.len() as i32) as usize;
let extended: Cow<str> = extend_digits(arg.as_ref(), min_digits);
print_adjusted!(extended, left_align, has_sign, prefix, width, padding_char);
print_adjusted(
extended.as_ref(),
left_align,
Some(has_sign),
Some(prefix),
width,
padding_char,
);
}
OutputType::Unsigned => {
let arg = if has!(flag, F_GROUP) {
Expand All @@ -306,30 +334,37 @@ fn print_it(arg: &str, output_type: &OutputType, flag: u8, width: usize, precisi
};
let min_digits = cmp::max(precision, arg.len() as i32) as usize;
let extended: Cow<str> = extend_digits(arg.as_ref(), min_digits);
print_adjusted!(extended, left_align, width, padding_char);
print_adjusted(
extended.as_ref(),
left_align,
None,
None,
width,
padding_char,
);
}
OutputType::UnsignedOct => {
let min_digits = cmp::max(precision, arg.len() as i32) as usize;
let extended: Cow<str> = extend_digits(arg, min_digits);
print_adjusted!(
extended,
print_adjusted(
extended.as_ref(),
left_align,
should_alter,
prefix,
Some(should_alter),
Some(prefix),
width,
padding_char
padding_char,
);
}
OutputType::UnsignedHex => {
let min_digits = cmp::max(precision, arg.len() as i32) as usize;
let extended: Cow<str> = extend_digits(arg, min_digits);
print_adjusted!(
extended,
print_adjusted(
extended.as_ref(),
left_align,
should_alter,
prefix,
Some(should_alter),
Some(prefix),
width,
padding_char
padding_char,
);
}
_ => unreachable!(),
Expand Down

0 comments on commit ab01d91

Please sign in to comment.