add sortBy & reversed args
This commit is contained in:
parent
2bdeae1994
commit
0e79324393
4 changed files with 38 additions and 4 deletions
|
@ -3,6 +3,12 @@ name = "mdq"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
[profile.release]
|
||||||
|
strip = true
|
||||||
|
lto = true
|
||||||
|
codegen-units = 1
|
||||||
|
panic = "abort"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.4.7", features = ["cargo"] }
|
clap = { version = "4.4.7", features = ["cargo"] }
|
||||||
regex = "1.10.2"
|
regex = "1.10.2"
|
||||||
|
|
|
@ -23,5 +23,7 @@ pub fn get_args() -> ArgMatches {
|
||||||
.required(false)
|
.required(false)
|
||||||
.default_value("file.title:Title"),
|
.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()
|
.get_matches()
|
||||||
}
|
}
|
||||||
|
|
28
src/lib.rs
28
src/lib.rs
|
@ -79,10 +79,32 @@ impl Index {
|
||||||
|
|
||||||
/// Build a table with specified columns from index within specified scope
|
/// Build a table with specified columns from index within specified scope
|
||||||
#[must_use]
|
#[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 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 {
|
let scope = if limit == 0 {
|
||||||
scope
|
scope
|
||||||
|
@ -129,7 +151,7 @@ impl Index {
|
||||||
a = txd::DataType::String(a_str);
|
a = txd::DataType::String(a_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
if !a.compare(f.1, b) {
|
if !a.compare(f.1, &b) {
|
||||||
is_included = false;
|
is_included = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,6 +21,10 @@ fn main() {
|
||||||
|
|
||||||
let offset: usize = args.get_one::<String>("offset").unwrap().parse().unwrap();
|
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
|
let columns: Vec<_> = args
|
||||||
.get_many::<String>("column")
|
.get_many::<String>("column")
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -51,7 +55,7 @@ fn main() {
|
||||||
i = i.filter_documents(&filters);
|
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 {
|
if output_json {
|
||||||
let mut data = serde_json::json!(
|
let mut data = serde_json::json!(
|
||||||
|
|
Loading…
Add table
Reference in a new issue