mirror of
https://github.com/bootandy/dust.git
synced 2026-06-08 11:29:05 +03:00
Added json output function
This commit is contained in:
committed by
andy.boot
parent
a3dcab9454
commit
184ea1f956
+8
-1
@@ -238,4 +238,11 @@ pub fn build_cli() -> Command {
|
||||
.value_parser(value_parser!(String))
|
||||
.num_args(1..)
|
||||
)
|
||||
}
|
||||
.arg(
|
||||
Arg::new("output_json")
|
||||
.short('j')
|
||||
.long("output-json")
|
||||
.action(clap::ArgAction::SetTrue)
|
||||
.help("Output the directory tree as json to the current directory"),
|
||||
)
|
||||
}
|
||||
@@ -29,6 +29,7 @@ pub struct Config {
|
||||
pub depth: Option<usize>,
|
||||
pub bars_on_right: Option<bool>,
|
||||
pub stack_size: Option<usize>,
|
||||
pub output_json: Option<bool>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@@ -117,6 +118,9 @@ impl Config {
|
||||
from_cmd_line.copied()
|
||||
}
|
||||
}
|
||||
pub fn get_output_json(&self, options: &ArgMatches) -> bool {
|
||||
Some(true) == self.output_json || options.get_flag("output_json")
|
||||
}
|
||||
}
|
||||
|
||||
fn convert_min_size(input: &str) -> Option<usize> {
|
||||
|
||||
+22
@@ -39,6 +39,11 @@ use terminal_size::{terminal_size, Height, Width};
|
||||
use utils::get_filesystem_devices;
|
||||
use utils::simplify_dir_names;
|
||||
|
||||
use crate::node::Node;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use chrono::Local;
|
||||
|
||||
static DEFAULT_NUMBER_OF_LINES: usize = 30;
|
||||
static DEFAULT_TERMINAL_WIDTH: usize = 80;
|
||||
|
||||
@@ -228,6 +233,16 @@ fn main() {
|
||||
|
||||
let top_level_nodes = walk_it(simplified_dirs, &walk_data);
|
||||
|
||||
if config.get_output_json(&options) {
|
||||
let datetime = Local::now().format("%Y%m%d%H%M%S").to_string();
|
||||
let output_filename = format!("node-{}.json", datetime);
|
||||
let result = output_json(&output_filename, &top_level_nodes);
|
||||
match result {
|
||||
Ok(..) => {}
|
||||
Err(err) => { eprintln!("Error: {}", err) }
|
||||
}
|
||||
}
|
||||
|
||||
let tree = match summarize_file_types {
|
||||
true => get_all_file_types(&top_level_nodes, number_of_lines),
|
||||
false => {
|
||||
@@ -325,3 +340,10 @@ fn init_rayon(stack_size: &Option<usize>) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn output_json(output_filename: &str, top_level_nodes: &Vec<Node>) -> std::io::Result<()> {
|
||||
let serialized: String = serde_json::to_string(&top_level_nodes).unwrap();
|
||||
let mut file = File::create(output_filename)?;
|
||||
file.write_all(serialized.as_bytes())?;
|
||||
Ok(())
|
||||
}
|
||||
+2
-1
@@ -5,8 +5,9 @@ use crate::utils::is_filtered_out_due_to_regex;
|
||||
use regex::Regex;
|
||||
use std::cmp::Ordering;
|
||||
use std::path::PathBuf;
|
||||
use serde::Serialize;
|
||||
|
||||
#[derive(Debug, Eq, Clone)]
|
||||
#[derive(Debug, Eq, Clone, Serialize)]
|
||||
pub struct Node {
|
||||
pub name: PathBuf,
|
||||
pub size: u64,
|
||||
|
||||
Reference in New Issue
Block a user