Skip to content

Commit

Permalink
Add summary information to output formats
Browse files Browse the repository at this point in the history
  • Loading branch information
YC authored and XAMPPRocky committed Aug 5, 2020
1 parent 2b5ab4f commit 64bb4e1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
7 changes: 1 addition & 6 deletions src/cli_utils.rs
Expand Up @@ -429,12 +429,7 @@ impl<W: Write> Printer<W> {
}

pub fn print_total(&mut self, languages: tokei::Languages) -> io::Result<()> {
let mut total = Language::new();

for (_, language) in languages {
total += language.summarise();
}

let total = languages.total();
self.print_row()?;
self.print_language(&total, "Total")?;
self.print_row()
Expand Down
12 changes: 10 additions & 2 deletions src/input.rs
@@ -1,4 +1,5 @@
use std::{collections::BTreeMap, error::Error, str::FromStr};
use serde_json::{json, Map};

use tokei::{Language, LanguageType, Languages};

Expand Down Expand Up @@ -83,12 +84,19 @@ macro_rules! supported_formats {
}

pub fn print(&self, languages: &Languages) -> Result<String, Box<dyn Error>> {
// To serde_json Map and add summary
let mut map = Map::new();
for (language_type, language) in languages.into_iter() {
map.insert(language_type.to_string(), json!(language));
}
map.insert(String::from("Total"), json!(languages.total()));

match *self {
Format::Json => Ok(serde_json::to_string(languages)?),
Format::Json => Ok(serde_json::to_string(&map)?),
$(
#[cfg(feature = $feature)] Format::$variant => {
let print= &{ $print_kode };
Ok(print(languages)?)
Ok(print(&map)?)
}
),+
}
Expand Down
9 changes: 9 additions & 0 deletions src/language/languages.rs
Expand Up @@ -96,6 +96,15 @@ impl Languages {
pub fn new() -> Self {
Languages::default()
}

/// Summary of the Languages struct.
pub fn total(self: &Languages) -> Language {
let mut total = Language::new();
for (_, language) in self {
total += language.summarise();
}
total
}
}

impl IntoIterator for Languages {
Expand Down

0 comments on commit 64bb4e1

Please sign in to comment.