add sortBy & reversed args

This commit is contained in:
JMARyA 2023-10-26 13:51:08 +02:00
parent 2bdeae1994
commit 0e79324393
Signed by: jmarya
GPG key ID: 901B2ADDF27C2263
4 changed files with 38 additions and 4 deletions

View file

@ -3,6 +3,12 @@ name = "mdq"
version = "0.1.0"
edition = "2021"
[profile.release]
strip = true
lto = true
codegen-units = 1
panic = "abort"
[dependencies]
clap = { version = "4.4.7", features = ["cargo"] }
regex = "1.10.2"

View file

@ -23,5 +23,7 @@ pub fn get_args() -> ArgMatches {
.required(false)
.default_value("file.title:Title"),
)
.arg(arg!(-s --sortby <KEY> "Sort results based on specified key").required(false))
.arg(arg!(-r --reverse "Reverse the results").required(false))
.get_matches()
}

View file

@ -79,10 +79,32 @@ impl Index {
/// Build a table with specified columns from index within specified scope
#[must_use]
pub fn select_columns(&self, col: &[String], limit: usize, offset: usize) -> Table {
pub fn select_columns(
&self,
col: &[String],
limit: usize,
offset: usize,
sort: Option<String>,
reverse: bool,
) -> Table {
let mut rows = vec![];
let scope: Vec<_> = self.documents.clone().into_iter().skip(offset).collect();
let mut scope = self.documents.clone();
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));
a.order_with(&b)
});
}
if reverse {
scope.reverse();
}
let scope: Vec<_> = scope.into_iter().skip(offset).collect();
let scope = if limit == 0 {
scope
@ -129,7 +151,7 @@ impl Index {
a = txd::DataType::String(a_str);
}
if !a.compare(f.1, b) {
if !a.compare(f.1, &b) {
is_included = false;
}
}

View file

@ -21,6 +21,10 @@ fn main() {
let offset: usize = args.get_one::<String>("offset").unwrap().parse().unwrap();
let sort_by = args.get_one::<String>("sortby").map(|x| x.to_owned());
let reversed = args.get_flag("reverse");
let columns: Vec<_> = args
.get_many::<String>("column")
.unwrap()
@ -51,7 +55,7 @@ fn main() {
i = i.filter_documents(&filters);
}
let data = i.select_columns(&columns, limit, offset);
let data = i.select_columns(&columns, limit, offset, sort_by, reversed);
if output_json {
let mut data = serde_json::json!(