This commit is contained in:
JMARyA 2024-02-13 09:12:16 +01:00
parent 8d8d90efd1
commit 8ea359d6df
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
5 changed files with 243 additions and 208 deletions

194
Cargo.lock generated
View file

@ -28,9 +28,9 @@ dependencies = [
[[package]]
name = "anstream"
version = "0.6.4"
version = "0.6.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2ab91ebe16eb252986481c5b62f6098f3b698a45e34b5b98200cf20dd2484a44"
checksum = "6e2e1ebcb11de5c03c67de28a7df593d32191b44939c482e97702baaaa6ab6a5"
dependencies = [
"anstyle",
"anstyle-parse",
@ -42,36 +42,36 @@ dependencies = [
[[package]]
name = "anstyle"
version = "1.0.4"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7079075b41f533b8c61d2a4d073c4676e1f8b249ff94a393b0595db304e0dd87"
checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc"
[[package]]
name = "anstyle-parse"
version = "0.2.2"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "317b9a89c1868f5ea6ff1d9539a69f45dffc21ce321ac1fd1160dfa48c8e2140"
checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.0"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b"
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
dependencies = [
"windows-sys 0.48.0",
"windows-sys",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.1"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0699d10d2f4d628a98ee7b57b289abbc98ff3bad977cb3152709d4bf2330628"
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
dependencies = [
"anstyle",
"windows-sys 0.48.0",
"windows-sys",
]
[[package]]
@ -88,9 +88,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.1"
version = "2.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
[[package]]
name = "bumpalo"
@ -115,32 +115,32 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.31"
version = "0.4.33"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38"
checksum = "9f13690e35a5e4ace198e7beea2895d29f3a9cc55015fcebe6336bd2010af9eb"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-targets 0.48.5",
"windows-targets 0.52.0",
]
[[package]]
name = "clap"
version = "4.4.10"
version = "4.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41fffed7514f420abec6d183b1d3acfd9099c79c3a10a06ade4f8203f1411272"
checksum = "80c21025abd42669a92efc996ef13cfb2c5c627858421ea58d5c3b331a6c134f"
dependencies = [
"clap_builder",
]
[[package]]
name = "clap_builder"
version = "4.4.9"
version = "4.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "63361bae7eef3771745f02d8d892bec2fee5f6e34af316ba556e7f97a7069ff1"
checksum = "458bf1f341769dfcf849846f65dffdf9146daa56bcd2a47cb4e1de9915567c99"
dependencies = [
"anstream",
"anstyle",
@ -150,9 +150,9 @@ dependencies = [
[[package]]
name = "clap_lex"
version = "0.6.0"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "702fc72eb24e5a1e48ce58027a675bc24edd52096d5397d4aea7c6dd9eca0bd1"
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
[[package]]
name = "colorchoice"
@ -174,9 +174,9 @@ dependencies = [
[[package]]
name = "core-foundation-sys"
version = "0.8.4"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e496a50fda8aacccc86d7529e2c1e0892dbd0f898a6b5645b5561b89c3210efa"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "crossterm"
@ -184,7 +184,7 @@ version = "0.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df"
dependencies = [
"bitflags 2.4.1",
"bitflags 2.4.2",
"crossterm_winapi",
"libc",
"parking_lot",
@ -223,9 +223,9 @@ dependencies = [
[[package]]
name = "env_logger"
version = "0.10.1"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95b3f3e67048839cb0d0781f445682a35113da7121f7c949db0e2be96a4fbece"
checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580"
dependencies = [
"humantime",
"is-terminal",
@ -240,16 +240,6 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5443807d6dff69373d433ab9ef5378ad8df50ca6298caf15de6e52e24aaf54d5"
[[package]]
name = "errno"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245"
dependencies = [
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "hashbrown"
version = "0.14.3"
@ -264,9 +254,9 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "hermit-abi"
version = "0.3.3"
version = "0.3.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d77f7ec81a6d05a3abb01ab6eb7590f6083d08449fe5a1c8b1e620283546ccb7"
checksum = "d0c62115964e08cb8039170eb33c1d0e2388a256930279edca206fff675f82c3"
[[package]]
name = "humantime"
@ -276,9 +266,9 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "iana-time-zone"
version = "0.1.58"
version = "0.1.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8326b86b6cff230b97d0d312a6c40a60726df3332e721f72a1b035f451663b20"
checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141"
dependencies = [
"android_system_properties",
"core-foundation-sys",
@ -299,9 +289,9 @@ dependencies = [
[[package]]
name = "indexmap"
version = "2.1.0"
version = "2.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d530e1a18b1cb4c484e6e34556a0d948706958449fca0cab753d649f2bce3d1f"
checksum = "824b2ae422412366ba479e8111fd301f7b5faece8149317bb81925979a53f520"
dependencies = [
"equivalent",
"hashbrown",
@ -309,33 +299,35 @@ dependencies = [
[[package]]
name = "is-terminal"
version = "0.4.9"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb0889898416213fab133e1d33a0e5858a48177452750691bde3666d0fdbaf8b"
checksum = "fe8f25ce1159c7740ff0b9b2f5cdf4a8428742ba7c112b9f20f22cd5219c7dab"
dependencies = [
"hermit-abi",
"rustix",
"windows-sys 0.48.0",
"libc",
"windows-sys",
]
[[package]]
name = "itoa"
version = "1.0.9"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]]
name = "js-sys"
version = "0.3.66"
version = "0.3.68"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca"
checksum = "406cda4b368d531c842222cf9d2600a9a4acce8d29423695379c6868a143a9ee"
dependencies = [
"wasm-bindgen",
]
[[package]]
name = "jsonfilter"
version = "0.1.0"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "79e41e2b969fe3568e7c235357ffdca59388bb77b54367ca7110d5df39a506b9"
dependencies = [
"regex",
"serde",
@ -344,15 +336,9 @@ dependencies = [
[[package]]
name = "libc"
version = "0.2.150"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89d92a4743f9a61002fae18374ed11e7973f530cb3a3255fb354818118b2203c"
[[package]]
name = "linux-raw-sys"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "969488b55f8ac402214f3f5fd243ebb7206cf82de60d3172994707a4bcc2b829"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "lock_api"
@ -372,7 +358,7 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]]
name = "mdq"
version = "0.1.0"
version = "0.2.0"
dependencies = [
"chrono",
"clap",
@ -390,24 +376,24 @@ dependencies = [
[[package]]
name = "memchr"
version = "2.6.4"
version = "2.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]]
name = "num-traits"
version = "0.2.17"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.18.0"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d"
checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]]
name = "parking_lot"
@ -488,19 +474,6 @@ version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "rustix"
version = "0.38.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc99bc2d4f1fed22595588a013687477aedf3cdcfb26558c559edb67b4d9b22e"
dependencies = [
"bitflags 2.4.1",
"errno",
"libc",
"linux-raw-sys",
"windows-sys 0.48.0",
]
[[package]]
name = "rustversion"
version = "1.0.14"
@ -509,9 +482,9 @@ checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4"
[[package]]
name = "ryu"
version = "1.0.15"
version = "1.0.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
checksum = "f98d2aa92eebf49b69786be48e4477826b256916e84a57ff2a4f21923b48eb4c"
[[package]]
name = "same-file"
@ -561,9 +534,9 @@ dependencies = [
[[package]]
name = "serde_yaml"
version = "0.9.27"
version = "0.9.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3cc7a1570e38322cfe4154732e5110f887ea57e22b76f4bfd32b5bdd3368666c"
checksum = "adf8a49373e98a4c5f0ceb5d05aa7c648d75f63774981ed95b7c7443bbd50c6e"
dependencies = [
"indexmap",
"itoa",
@ -574,15 +547,15 @@ dependencies = [
[[package]]
name = "smallvec"
version = "1.11.2"
version = "1.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4dccd0940a2dcdf68d092b8cbab7dc0ad8fa938bf95787e1b916b0e3d0e8e970"
checksum = "e6ecd384b10a64542d77071bd64bd7b231f4ed5940fba55e98c3de13824cf3d7"
[[package]]
name = "strsim"
version = "0.10.0"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01"
[[package]]
name = "strum"
@ -616,9 +589,9 @@ dependencies = [
[[package]]
name = "termcolor"
version = "1.4.0"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff1bc3d3f05aff0403e8ac0d92ced918ec05b666a43f83297ccef5bea8a3d449"
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
dependencies = [
"winapi-util",
]
@ -637,9 +610,9 @@ checksum = "e51733f11c9c4f72aa0c160008246859e340b00807569a0da0e7a1079b27ba85"
[[package]]
name = "unsafe-libyaml"
version = "0.2.9"
version = "0.2.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f28467d3e1d3c6586d8f25fa243f544f5800fec42d97032474e17222c2b75cfa"
checksum = "ab4c90930b95a82d00dc9e9ac071b4991924390d46cbd0dfe566148667605e4b"
[[package]]
name = "utf8parse"
@ -659,9 +632,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen"
version = "0.2.89"
version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e"
checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
@ -669,9 +642,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.89"
version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826"
checksum = "c9e7e1900c352b609c8488ad12639a311045f40a35491fb69ba8c12f758af70b"
dependencies = [
"bumpalo",
"log",
@ -684,9 +657,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.89"
version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2"
checksum = "b30af9e2d358182b5c7449424f017eba305ed32a7010509ede96cdc4696c46ed"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@ -694,9 +667,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.89"
version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
checksum = "642f325be6301eb8107a83d12a8ac6c1e1c54345a7ef1a9261962dfefda09e66"
dependencies = [
"proc-macro2",
"quote",
@ -707,9 +680,9 @@ dependencies = [
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.89"
version = "0.2.91"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f"
checksum = "4f186bd2dcf04330886ce82d6f33dd75a7bfcf69ecf5763b89fcde53b6ac9838"
[[package]]
name = "winapi"
@ -744,20 +717,11 @@ checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-core"
version = "0.51.1"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64"
checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
"windows-targets 0.52.0",
]
[[package]]

View file

@ -21,4 +21,4 @@ env_logger = "0.10.0"
log = "0.4.20"
chrono = "0.4.31"
csv = "1.3.0"
jsonfilter = { git = "https://git.hydrar.de/jmarya/jsonfilter" }
jsonfilter = "0.2.0"

View file

@ -1,6 +1,117 @@
use clap::{arg, command, ArgMatches};
pub fn get_args() -> ArgMatches {
use crate::quit_err;
pub struct Args {
pub root_dir: String,
pub output_json: bool,
pub no_header: bool,
pub limit: usize,
pub offset: usize,
pub ignoretags: bool,
pub sort_by: Option<String>,
pub group_by: Option<String>,
pub reversed: bool,
pub columns: Vec<String>,
pub headers: Vec<String>,
pub filters: serde_json::Value,
}
pub fn get_args() -> Args {
let args = get_args_match();
let root_dir = args.get_one::<String>("dir").unwrap();
let output_json = args.get_flag("json");
let no_header = args.get_flag("noheader");
let limit: usize = args
.get_one::<String>("limit")
.unwrap()
.parse()
.unwrap_or_else(|e| quit_err(e, "Limit is not a number"));
let offset: usize = args
.get_one::<String>("offset")
.unwrap()
.parse()
.unwrap_or_else(|e| quit_err(e, "Offset is not a number"));
let ignoretags: bool = args.get_flag("ignoretags");
let sort_by = args
.get_one::<String>("sortby")
.map(std::borrow::ToOwned::to_owned);
let group_by = args
.get_one::<String>("groupby")
.map(std::borrow::ToOwned::to_owned);
let reversed = args.get_flag("reverse");
let columns: Vec<_> = args
.get_many::<String>("column")
.unwrap()
.cloned()
.collect();
log::debug!("columns: {columns:?}");
let (columns, headers): (Vec<_>, Vec<_>) = columns
.into_iter()
.map(|x| {
let (column, header_rename) = x.split_once(':').unwrap_or((&x, &x));
(column.to_owned(), header_rename.to_owned())
})
.unzip();
if columns != headers {
log::debug!("renamed headers: {headers:?}");
}
let filters = args
.get_many::<String>("filter")
.map_or_else(std::vec::Vec::new, std::iter::Iterator::collect);
log::debug!("raw filters: {filters:?}");
let filters = if filters.len() == 1 {
let filter = filters.first().unwrap();
serde_json::from_str(filter)
.unwrap_or_else(|e| quit_err(e, &format!("filter '{filter}' could not be parsed")))
} else {
let filters: Vec<_> = filters
.iter()
.map(|x| {
serde_json::from_str::<serde_json::Value>(x)
.unwrap_or_else(|e| quit_err(e, &format!("filter '{x}' could not be parsed")))
})
.collect();
serde_json::json!({
"$and": filters
})
};
log::debug!("parsed filters: {filters:?}");
Args {
root_dir: root_dir.to_string(),
output_json,
no_header,
limit,
offset,
ignoretags,
sort_by,
group_by,
reversed,
columns,
headers,
filters,
}
}
fn get_args_match() -> ArgMatches {
command!()
.about("Query markdown files")
.arg(arg!([dir] "Directory to scan").required(true))

View file

@ -106,7 +106,7 @@ impl Index {
.as_mapping()
.unwrap()
.get("tags")
.map(|x| x.as_sequence().unwrap().clone())
.map(|x| x.as_sequence().unwrap_or(&Vec::new()).clone())
.unwrap_or_default();
let inline_tags = get_inline_tags(&content);
@ -123,6 +123,7 @@ impl Index {
.insert("tags".into(), unique_tags.into_iter().collect());
}
log::trace!("Adding {path} to Index");
let doc = Document { path, frontmatter };
i.documents.push(doc);
}
@ -149,15 +150,17 @@ impl Index {
scope.reverse();
}
let scope: Vec<_> = scope.into_iter().skip(offset).collect();
let scope = scope.into_iter().skip(offset);
let scope = if limit == 0 {
scope
if limit == 0 {
Self {
documents: scope.collect(),
}
} else {
scope.into_iter().take(limit).collect()
};
Self { documents: scope }
Self {
documents: scope.take(limit).collect(),
}
}
}
#[must_use]

View file

@ -4,98 +4,41 @@ use mdq::Index;
mod args;
pub fn quit_err(e: impl std::error::Error, msg: &str) -> ! {
eprintln!("Error: {msg}. {e}");
std::process::exit(1);
}
fn main() {
env_logger::init();
let args = args::get_args();
let root_dir = args.get_one::<String>("dir").unwrap();
let output_json = args.get_flag("json");
let no_header = args.get_flag("noheader");
let limit: usize = args.get_one::<String>("limit").unwrap().parse().unwrap();
let offset: usize = args.get_one::<String>("offset").unwrap().parse().unwrap();
let ignoretags: bool = args.get_flag("ignoretags");
let sort_by = args
.get_one::<String>("sortby")
.map(std::borrow::ToOwned::to_owned);
let group_by = args
.get_one::<String>("groupby")
.map(std::borrow::ToOwned::to_owned);
let reversed = args.get_flag("reverse");
let columns: Vec<_> = args
.get_many::<String>("column")
.unwrap()
.cloned()
.collect();
log::debug!("columns: {columns:?}");
let (columns, headers): (Vec<_>, Vec<_>) = columns
.into_iter()
.map(|x| {
let (column, header_rename) = x.split_once(':').unwrap_or((&x, &x));
(column.to_owned(), header_rename.to_owned())
})
.unzip();
if columns != headers {
log::debug!("renamed headers: {headers:?}");
let mut i = Index::new(&args.root_dir, args.ignoretags);
if !args.filters.is_null() {
i = i.filter_documents(&args.filters);
}
let filters = args
.get_many::<String>("filter")
.map_or_else(std::vec::Vec::new, std::iter::Iterator::collect);
i = i.apply(args.limit, args.offset, args.sort_by, args.reversed);
log::debug!("raw filters: {filters:?}");
let filters = if filters.len() == 1 {
serde_json::from_str(filters.first().unwrap()).unwrap()
} else {
let filters: Vec<_> = filters
.iter()
.map(|x| serde_json::from_str::<serde_json::Value>(x).unwrap())
.collect();
serde_json::json!({
"$and": filters
})
};
log::debug!("parsed filters: {filters:?}");
let mut i = Index::new(root_dir, ignoretags);
if !filters.is_null() {
i = i.filter_documents(&filters);
}
i = i.apply(limit, offset, sort_by, reversed);
if group_by.is_some() {
let grouped = i.group_by(&group_by.clone().unwrap());
if args.group_by.is_some() {
let grouped = i.group_by(&args.group_by.clone().unwrap());
let grouped: HashMap<_, _> = grouped
.into_iter()
.map(|(key, val)| (key, val.create_table_data(&columns)))
.map(|(key, val)| (key, val.create_table_data(&args.columns)))
.collect();
if output_json {
if args.output_json {
let mut data = serde_json::json!(
{
"columns": columns,
"groupby": group_by.unwrap(),
"columns": args.columns,
"groupby": args.group_by.unwrap(),
"results": grouped
}
);
if columns != headers {
if args.columns != args.headers {
data.as_object_mut()
.unwrap()
.insert("headers".into(), headers.into());
.insert("headers".into(), args.headers.into());
}
println!("{}", serde_json::to_string(&data).unwrap());
return;
@ -111,13 +54,20 @@ fn main() {
});
for group in grouped_keys {
println!("# {group}");
print_result(grouped.get(group).unwrap().clone(), &headers);
print_result(grouped.get(group).unwrap().clone(), &args.headers);
}
} else {
let mut first = true;
for (_, val) in grouped {
if first {
print_csv(val, if no_header { None } else { Some(&headers) });
print_csv(
val,
if args.no_header {
None
} else {
Some(&args.headers)
},
);
first = false;
continue;
}
@ -127,27 +77,34 @@ fn main() {
return;
}
let data = i.create_table_data(&columns);
let data = i.create_table_data(&args.columns);
if output_json {
if args.output_json {
let mut data = serde_json::json!(
{
"columns": columns,
"columns": args.columns,
"results": data
}
);
if columns != headers {
if args.columns != args.headers {
data.as_object_mut()
.unwrap()
.insert("headers".into(), headers.into());
.insert("headers".into(), args.headers.into());
}
println!("{}", serde_json::to_string(&data).unwrap());
return;
}
if std::io::stdout().is_terminal() {
print_result(data, &headers);
print_result(data, &args.headers);
} else {
print_csv(data, if no_header { None } else { Some(&headers) });
print_csv(
data,
if args.no_header {
None
} else {
Some(&args.headers)
},
);
}
}