feat: --files argument now sorts alphabetically (#1059)

* Fixed --files argument to sort alphabetically

* Deleted unnecessary clone().

* Ran cargo fmt
This commit is contained in:
Beiu Alexandru Dumitru 2024-05-13 14:00:40 +03:00 committed by GitHub
parent eae5e4c564
commit b0b7a38025
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 19 additions and 13 deletions

View file

@ -312,7 +312,12 @@ impl<W: Write> Printer<W> {
Ok(())
}
pub fn print_results<'a, I>(&mut self, languages: I, compact: bool) -> io::Result<()>
pub fn print_results<'a, I>(
&mut self,
languages: I,
compact: bool,
is_sorted: bool,
) -> io::Result<()>
where
I: Iterator<Item = (&'a LanguageType, &'a Language)>,
{
@ -337,16 +342,18 @@ impl<W: Write> Printer<W> {
if self.list_files {
self.print_subrow()?;
let mut reports: Vec<&Report> =
language.reports.iter().map(|report| &*report).collect();
if !is_sorted {
reports.sort_by(|&a, &b| a.name.cmp(&b.name));
}
if compact {
for report in &language.reports {
for &report in &reports {
writeln!(self.writer, "{:1$}", report, self.path_length)?;
}
} else {
let (a, b): (Vec<_>, Vec<_>) = language
.reports
.iter()
.partition(|r| r.stats.blobs.is_empty());
let (a, b): (Vec<&Report>, Vec<&Report>) =
reports.iter().partition(|&r| r.stats.blobs.is_empty());
for reports in &[&a, &b] {
let mut first = true;
for report in reports.iter() {

View file

@ -22,7 +22,6 @@ fn main() -> Result<(), Box<dyn Error>> {
Cli::print_supported_languages()?;
process::exit(0);
}
let config = cli.override_config(Config::from_config_files());
let mut languages = Languages::new();
@ -94,13 +93,13 @@ fn main() -> Result<(), Box<dyn Error>> {
printer.print_header()?;
let mut is_sorted = false;
if let Some(sort_category) = cli.sort.or(config.sort) {
for (_, ref mut language) in &mut languages {
language.sort_by(sort_category);
}
let mut languages: Vec<_> = languages.iter().collect();
match sort_category {
Sort::Blanks => languages.sort_by(|a, b| b.1.blanks.cmp(&a.1.blanks)),
Sort::Comments => languages.sort_by(|a, b| b.1.comments.cmp(&a.1.comments)),
@ -108,14 +107,14 @@ fn main() -> Result<(), Box<dyn Error>> {
Sort::Files => languages.sort_by(|a, b| b.1.reports.len().cmp(&a.1.reports.len())),
Sort::Lines => languages.sort_by(|a, b| b.1.lines().cmp(&a.1.lines())),
}
is_sorted = true;
if cli.sort_reverse {
printer.print_results(languages.into_iter().rev(), cli.compact)?;
printer.print_results(languages.into_iter().rev(), cli.compact, is_sorted)?;
} else {
printer.print_results(languages.into_iter(), cli.compact)?;
printer.print_results(languages.into_iter(), cli.compact, is_sorted)?;
}
} else {
printer.print_results(languages.iter(), cli.compact)?;
printer.print_results(languages.iter(), cli.compact, is_sorted)?;
}
printer.print_total(&languages)?;