diff --git a/src/cli.rs b/src/cli.rs index c68b2bd..2af055b 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -74,7 +74,7 @@ pub fn parse() -> Args { .short("o") .long("output") .value_name("OUTPUT") - .possible_values(&["table", "html", "markdown"]) + .possible_values(&["table", "html", "markdown", "json"]) .default_value("table") .max_values(1) .hide_default_value(true) diff --git a/src/output.rs b/src/output.rs index 739b805..4722396 100644 --- a/src/output.rs +++ b/src/output.rs @@ -7,6 +7,7 @@ pub enum Format { Table, Html, Markdown, + Json, } impl FromStr for Format { @@ -16,6 +17,7 @@ impl FromStr for Format { "table" => Ok(Format::Table), "html" => Ok(Format::Html), "markdown" => Ok(Format::Markdown), + "json" => Ok(Format::Json), _ => Err(()), } } @@ -64,6 +66,7 @@ impl Output { Format::Table => self.table(&mut data), Format::Html => self.html(&mut data), Format::Markdown => self.markdown(&mut data), + Format::Json => self.json(&mut data), }; println!("{}", data.join("\n")); @@ -191,4 +194,44 @@ impl Output { format_size(self.total_size) )); } + + fn json(&self, data: &mut Vec) { + let mut items = Vec::new(); + for item in &self.data { + items.push(format!( + r#"{{ + "language": "{}", + "code": {}, + "comment": {}, + "blank": {}, + "file": {}, + "size": "{}" + }}"#, + item.language, + item.code, + item.comment, + item.blank, + item.file, + format_size(item.size) + )); + } + data.push(format!( + r#"{{ + "total": {{ + "code": {}, + "comment": {}, + "blank": {}, + "file": {}, + "size": "{}" + }}, + "items": [{}] +}}"#, + self.total_code, + self.total_comment, + self.total_blank, + self.total_file, + format_size(self.total_size), + items.join(", ") + )); + } }