From 2a2f55557de6b4fa04b2a8d2394cbcf14bfbe736 Mon Sep 17 00:00:00 2001 From: JMARyA Date: Mon, 30 Oct 2023 16:38:21 +0100 Subject: [PATCH] refactor --- Cargo.lock | 5 +++-- Cargo.toml | 2 +- src/args.rs | 2 +- src/lib.rs | 18 ++++++++++++++---- src/main.rs | 22 +++++++++++++++++++--- 5 files changed, 38 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4aaafb2..e24d87c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -481,9 +481,9 @@ checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f" [[package]] name = "rustix" -version = "0.38.20" +version = "0.38.21" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "67ce50cb2e16c2903e30d1cbccfd8387a74b9d4c938b6a4c5ec6cc7556f7a8a0" +checksum = "2b426b0506e5d50a7d8dafcf2e81471400deb602392c7dd110815afb4eaf02a3" dependencies = [ "bitflags 2.4.1", "errno", @@ -617,6 +617,7 @@ dependencies = [ [[package]] name = "txd" version = "0.1.0" +source = "git+https://git.hydrar.de/jmarya/txd#06f05e4a32c2bddf3e5f6347c96354feb109034f" dependencies = [ "chrono", "serde", diff --git a/Cargo.toml b/Cargo.toml index 1ec7827..9fb32be 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ regex = "1.10.2" serde = "1.0.189" serde_yaml = "0.9.25" walkdir = "2.4.0" -txd = { path = "../txd" } +txd = { git = "https://git.hydrar.de/jmarya/txd" } serde_json = "1.0.107" comfy-table = "7.1.0" env_logger = "0.10.0" diff --git a/src/args.rs b/src/args.rs index 72b9c3d..5c9809e 100644 --- a/src/args.rs +++ b/src/args.rs @@ -19,7 +19,7 @@ pub fn get_args() -> ArgMatches { ) .arg(arg!(-f --filter ... "Filter to apply to the documents").required(false)) .arg( - arg!(-c --column ... "Specify output columns") + arg!(-c --column ... "Specify output columns. You can rename the text displayed in the header using the `:` character like this: VariableName:OutputName") .required(false) .default_value("file.title:Title"), ) diff --git a/src/lib.rs b/src/lib.rs index 282240a..f1018cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,10 +86,20 @@ impl Index { if let Some(sort) = sort { scope.sort_by(|a, b| { - let a = txd::parse(&a.get_key(&sort)); - let b = txd::parse(&b.get_key(&sort)); + let a_str = a.get_key(&sort); + let b_str = b.get_key(&sort); + let mut a = txd::parse(&a_str); + let mut b = txd::parse(&b_str); - a.order_with(&b) + log::debug!("Trying to order {a:?} and {b:?}",); + + if !a.same_as(&b) { + log::debug!("trying to cast a to string because of different types"); + a = txd::DataType::String(a_str); + b = txd::DataType::String(b_str); + } + + a.order_with(&b).unwrap() }); } @@ -169,7 +179,7 @@ impl Index { a = txd::DataType::String(a_str); } - if !a.compare(f.1, &b) { + if !a.compare(f.1, &b).unwrap() { is_included = false; } } diff --git a/src/main.rs b/src/main.rs index 5664764..92851ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,7 +70,7 @@ fn main() { i = i.apply(limit, offset, sort_by, reversed); if group_by.is_some() { - let grouped = i.group_by(&group_by.unwrap()); + let grouped = i.group_by(&group_by.clone().unwrap()); let grouped: HashMap<_, _> = grouped .into_iter() .map(|(key, val)| (key, val.create_table_data(&columns))) @@ -80,6 +80,7 @@ fn main() { let mut data = serde_json::json!( { "columns": columns, + "groupby": group_by.unwrap(), "results": grouped } ); @@ -93,9 +94,24 @@ fn main() { } if std::io::stdout().is_terminal() { - for (group, val) in grouped { + let mut grouped_keys = grouped.iter().map(|(key, _)| key).collect::>(); + grouped_keys.sort_by(|a_str, b_str| { + let mut a = txd::parse(a_str); + let mut b = txd::parse(b_str); + + log::debug!("Trying to order {a:?} and {b:?}",); + + if !a.same_as(&b) { + log::debug!("trying to cast a to string because of different types"); + a = txd::DataType::String(a_str.to_string()); + b = txd::DataType::String(b_str.to_string()); + } + + a.order_with(&b).unwrap() + }); + for group in grouped_keys { println!("# {group}"); - print_result(val, &headers); + print_result(grouped.get(group).unwrap().to_vec(), &headers); } } else { let mut first = true;