Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed --files argument to sort alphabetically #1059

Merged
merged 3 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading