Skip to content

Commit

Permalink
Implement compact output feature (#605) (#690)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexmaco authored Jan 5, 2021
1 parent 3e2e1ad commit 611de1f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
5 changes: 5 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Cli<'a> {
pub print_languages: bool,
pub sort: Option<Sort>,
pub types: Option<Vec<LanguageType>>,
pub compact: bool,
pub number_format: num_format::CustomFormat,
pub verbose: u64,
}
Expand Down Expand Up @@ -83,6 +84,8 @@ impl<'a> Cli<'a> {
(@arg types: -t --type
+takes_value
"Filters output by language type, seperated by a comma. i.e. -t=Rust,Markdown")
(@arg compact: -C --compact
"Do not print statistics about embedded languages.")
(@arg num_format_style: -n --("num-format")
possible_values(NumberFormatStyle::all())
conflicts_with[output]
Expand All @@ -106,6 +109,7 @@ impl<'a> Cli<'a> {
let no_ignore_vcs = matches.is_present("no_ignore_vcs");
let print_languages = matches.is_present("languages");
let verbose = matches.occurrences_of("verbose");
let compact = matches.is_present("compact");
let types = matches.value_of("types").map(|e| {
e.split(',')
.map(|t| t.parse::<LanguageType>())
Expand Down Expand Up @@ -151,6 +155,7 @@ impl<'a> Cli<'a> {
types,
verbose,
number_format,
compact,
};

debug!("CLI Config: {:#?}", cli);
Expand Down
6 changes: 3 additions & 3 deletions src/cli_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,18 @@ impl<W: Write> Printer<W> {
Ok(())
}

pub fn print_results<'a, I>(&mut self, languages: I) -> io::Result<()>
pub fn print_results<'a, I>(&mut self, languages: I, compact: bool) -> io::Result<()>
where
I: Iterator<Item = (&'a LanguageType, &'a Language)>,
{
let (a, b): (Vec<_>, Vec<_>) = languages
.filter(|(_, v)| !v.is_empty())
.partition(|(_, l)| l.children.is_empty());
.partition(|(_, l)| compact || l.children.is_empty());
let mut first = true;

for languages in &[&a, &b] {
for &(name, language) in *languages {
let has_children = !language.children.is_empty();
let has_children = !(compact || language.children.is_empty());
if first {
first = false;
} else if has_children || self.list_files {
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ fn main() -> Result<(), Box<dyn Error>> {
Sort::Lines => languages.sort_by(|a, b| b.1.lines().cmp(&a.1.lines())),
}

printer.print_results(languages.into_iter())?
printer.print_results(languages.into_iter(), cli.compact)?
} else {
printer.print_results(languages.iter())?
printer.print_results(languages.iter(), cli.compact)?
}

printer.print_total(languages)?;
Expand Down

0 comments on commit 611de1f

Please sign in to comment.