Skip to content
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
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct Config {
function: bool,
metrics: bool,
output: String,
pretty: bool,
line_start: Option<usize>,
line_end: Option<usize>,
preproc_lock: Option<Arc<Mutex<PreprocResults>>>,
Expand Down Expand Up @@ -80,6 +81,7 @@ fn act_on_file(language: Option<LANG>, path: PathBuf, cfg: Config) -> std::io::R
} else if cfg.metrics {
let cfg = MetricsCfg {
path,
pretty: cfg.pretty,
output_path: if cfg.output.is_empty() {
None
} else {
Expand Down Expand Up @@ -309,6 +311,11 @@ fn main() {
.default_value("")
.takes_value(true),
)
.arg(
Arg::with_name("pretty")
.help("Dump a pretty json file")
.long("pr"),
)
.arg(
Arg::with_name("output")
.help("Output file/directory")
Expand Down Expand Up @@ -430,6 +437,7 @@ fn main() {
(None, None)
};

let pretty = matches.is_present("pretty");
let output = matches.value_of("output").unwrap().to_string();
let output_is_dir = PathBuf::from(output.clone()).is_dir();
if metrics && !output.is_empty() && !output_is_dir {
Expand Down Expand Up @@ -468,6 +476,7 @@ fn main() {
count_filter,
function,
metrics,
pretty,
output: output.clone(),
line_start,
line_end,
Expand Down
16 changes: 13 additions & 3 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,8 +351,17 @@ impl<'a> FuncSpace<'a> {
writeln!(stdout, "{}", val)
}

fn dump_json(&self, path: &PathBuf, output_path: &PathBuf) -> std::io::Result<()> {
let json_data = serde_json::to_string_pretty(&self).unwrap();
fn dump_json(
&self,
path: &PathBuf,
output_path: &PathBuf,
pretty: bool,
) -> std::io::Result<()> {
let json_data = if pretty {
serde_json::to_string_pretty(&self).unwrap()
} else {
serde_json::to_string(&self).unwrap()
};

let mut file = path.as_path().file_name().unwrap().to_os_string();
file.push(".json");
Expand Down Expand Up @@ -453,6 +462,7 @@ pub fn metrics<'a, T: TSParserTrait>(parser: &'a T, path: &'a PathBuf) -> Option

pub struct MetricsCfg {
pub path: PathBuf,
pub pretty: bool,
pub output_path: Option<PathBuf>,
}

Expand All @@ -465,7 +475,7 @@ impl Callback for Metrics {
fn call<T: TSParserTrait>(cfg: Self::Cfg, parser: &T) -> Self::Res {
if let Some(space) = metrics(parser, &cfg.path) {
if let Some(output_path) = cfg.output_path {
space.dump_json(&cfg.path, &output_path)
space.dump_json(&cfg.path, &output_path, cfg.pretty)
} else {
space.dump_root()
}
Expand Down