Skip to content

Commit

Permalink
feat: --files argument now sorts alphabetically (#1059)
Browse files Browse the repository at this point in the history
* Fixed --files argument to sort alphabetically

* Deleted unnecessary clone().

* Ran cargo fmt
  • Loading branch information
Alexe1289 authored May 13, 2024
1 parent eae5e4c commit b0b7a38
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
21 changes: 14 additions & 7 deletions src/cli_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)>,
{
Expand All @@ -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() {
Expand Down
11 changes: 5 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -94,28 +93,28 @@ 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)),
Sort::Code => languages.sort_by(|a, b| b.1.code.cmp(&a.1.code)),
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)?;
Expand Down

0 comments on commit b0b7a38

Please sign in to comment.