stat: remove extend_digits and print_adjusted in favor of format!

This commit is contained in:
Terts Diepraam 2022-11-17 01:25:10 +01:00
parent 3499bdaeac
commit 82464b703a

View file

@ -21,10 +21,10 @@ use clap::{crate_version, Arg, ArgAction, ArgMatches, Command};
use std::borrow::Cow; use std::borrow::Cow;
use std::convert::AsRef; use std::convert::AsRef;
use std::ffi::{OsStr, OsString}; use std::ffi::{OsStr, OsString};
use std::fs;
use std::os::unix::fs::{FileTypeExt, MetadataExt}; use std::os::unix::fs::{FileTypeExt, MetadataExt};
use std::os::unix::prelude::OsStrExt; use std::os::unix::prelude::OsStrExt;
use std::path::Path; use std::path::Path;
use std::{cmp, fs, iter};
const ABOUT: &str = "Display file or file system status."; const ABOUT: &str = "Display file or file system status.";
const USAGE: &str = "{} [OPTION]... FILE..."; const USAGE: &str = "{} [OPTION]... FILE...";
@ -61,22 +61,6 @@ fn check_bound(slice: &str, bound: usize, beg: usize, end: usize) -> UResult<()>
Ok(()) Ok(())
} }
/// pads the string with zeroes if supplied min is greater
/// then the length of the string, else returns the original string
fn extend_digits(string: &str, min: usize) -> Cow<'_, str> {
if min > string.len() {
let mut pad = String::with_capacity(min);
iter::repeat('0')
.take(min - string.len())
.map(|_| pad.push('0'))
.all(|_| true);
pad.push_str(string);
pad.into()
} else {
string.into()
}
}
enum Padding { enum Padding {
Zero, Zero,
Space, Space,
@ -100,30 +84,6 @@ fn pad_and_print(result: &str, left: bool, width: usize, padding: Padding) {
}; };
} }
/// prints the adjusted string after padding
/// `left` flag specifies the type of alignment of the string
/// `width` is the supplied padding width of the string needed
/// `prefix` & `need_prefix` are Optional, which adjusts the `field_width` accordingly, where
/// `field_width` is the max of supplied `width` and size of string
/// `padding`, specifies type of padding, which is '0' or ' ' in this case.
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(true) = need_prefix {
field_width -= p.len();
}
pad_and_print(s, left, field_width, padding);
} else {
pad_and_print(s, left, field_width, padding);
}
}
#[derive(Debug)] #[derive(Debug)]
pub enum OutputType { pub enum OutputType {
Str(String), Str(String),
@ -282,15 +242,13 @@ fn print_it(output: &OutputType, flags: Flags, width: usize, precision: Option<u
Padding::Space Padding::Space
}; };
let has_sign = flags.sign || flags.space;
match output { match output {
OutputType::Str(s) => { OutputType::Str(s) => {
let s = match precision { let s = match precision {
Some(p) if p < s.len() => &s[..p], Some(p) if p < s.len() => &s[..p],
_ => s, _ => s,
}; };
print_adjusted(s, flags.left, None, None, width, Padding::Space); pad_and_print(s, flags.left, width, Padding::Space);
} }
OutputType::Integer(num) => { OutputType::Integer(num) => {
let num = num.to_string(); let num = num.to_string();
@ -299,57 +257,44 @@ fn print_it(output: &OutputType, flags: Flags, width: usize, precision: Option<u
} else { } else {
Cow::Borrowed(num.as_str()) Cow::Borrowed(num.as_str())
}; };
let extended = extend_digits(&arg, precision.unwrap_or(0)); let prefix = if flags.sign {
let prefix = if flags.sign { "+" } else { " " }; "+"
print_adjusted( } else if flags.space {
extended.as_ref(), " "
flags.left, } else {
Some(has_sign), ""
Some(prefix), };
width, let extended = format!(
padding_char, "{prefix}{arg:0>precision$}",
precision = precision.unwrap_or(0)
); );
pad_and_print(&extended, flags.left, width, padding_char);
} }
OutputType::Unsigned(num) => { OutputType::Unsigned(num) => {
let num = num.to_string(); let num = num.to_string();
let arg = if flags.group { let s = if flags.group {
group_num(&num) group_num(&num)
} else { } else {
Cow::Borrowed(num.as_str()) Cow::Borrowed(num.as_str())
}; };
let extended = extend_digits(&arg, precision.unwrap_or(0)); let s = format!("{s:0>precision$}", precision = precision.unwrap_or(0));
print_adjusted( pad_and_print(&s, flags.left, width, padding_char);
extended.as_ref(),
flags.left,
None,
None,
width,
padding_char,
);
} }
OutputType::UnsignedOct(num) => { OutputType::UnsignedOct(num) => {
let num = format!("{:o}", num); let prefix = if flags.alter { "0" } else { "" };
let extended = extend_digits(&num, precision.unwrap_or(0)); let s = format!(
print_adjusted( "{prefix}{num:0>precision$o}",
extended.as_ref(), precision = precision.unwrap_or(0)
flags.left,
Some(flags.alter),
Some("0"),
width,
padding_char,
); );
pad_and_print(&s, flags.left, width, padding_char);
} }
OutputType::UnsignedHex(num) => { OutputType::UnsignedHex(num) => {
let num = format!("{:x}", num); let prefix = if flags.alter { "0x" } else { "" };
let extended = extend_digits(&num, precision.unwrap_or(0)); let s = format!(
print_adjusted( "{prefix}{num:0>precision$x}",
extended.as_ref(), precision = precision.unwrap_or(0)
flags.left,
Some(flags.alter),
Some("0x"),
width,
padding_char,
); );
pad_and_print(&s, flags.left, width, padding_char);
} }
OutputType::Unknown => print!("?"), OutputType::Unknown => print!("?"),
} }