summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
l---------bin/kgrep1
-rw-r--r--src/bin/khaleesi.rs8
-rw-r--r--src/grep.rs34
-rw-r--r--src/lib.rs1
-rw-r--r--src/utils.rs11
5 files changed, 55 insertions, 0 deletions
diff --git a/bin/kgrep b/bin/kgrep
new file mode 120000
index 0000000..27627ca
--- /dev/null
+++ b/bin/kgrep
@@ -0,0 +1 @@
+kgeneric \ No newline at end of file
diff --git a/src/bin/khaleesi.rs b/src/bin/khaleesi.rs
index bbb52cd..3a6750e 100644
--- a/src/bin/khaleesi.rs
+++ b/src/bin/khaleesi.rs
@@ -14,6 +14,7 @@ use khaleesi::list;
use khaleesi::seq;
use khaleesi::utils;
use khaleesi::unroll;
+use khaleesi::grep;
use khaleesi::defaults::*;
use std::env;
@@ -41,6 +42,7 @@ fn main() {
"cal" => cal::printcal(),
"dbg" => cal::dbg(),
"list" => action_list(&args[2..]),
+ "grep" => action_grep(&args[2..]),
"seq" => action_sequence(&args[2..]),
"unroll" => action_unroll(&args[2..]),
_ => print_usage(&args[0])
@@ -65,6 +67,12 @@ fn action_list(args: &[String]) {
}
}
+fn action_grep(args: &[String]) {
+ if let Some(mut input) = default_input() {
+ grep::grep(&mut input, &args);
+ }
+}
+
fn action_sort(args: &[String]) {
if args.len() == 0 {
if let Some(mut input) = default_input() {
diff --git a/src/grep.rs b/src/grep.rs
new file mode 100644
index 0000000..d8a65d0
--- /dev/null
+++ b/src/grep.rs
@@ -0,0 +1,34 @@
+use utils;
+use icalwrap::IcalVCalendar;
+
+pub fn grep(filenames: &mut Iterator<Item = String>, args: &[String]) {
+ if args.len() == 0 {
+ println!("Usage: grep term+");
+ return;
+ }
+
+ let cals = utils::iterate_calendars_from_files(filenames);
+
+ let predicate = predicate_contains_term(&args[0]);
+ let cals = cals.filter(predicate);
+
+ utils::print_cals(cals);
+}
+
+fn predicate_contains_term(term: &str) -> impl Fn(&IcalVCalendar) -> bool {
+ let term = term.to_lowercase();
+ move |cal| {
+ let event = cal.get_first_event();
+ if let Some(summary) = event.get_summary() {
+ if summary.to_lowercase().contains(&term) {
+ return true;
+ }
+ }
+ if let Some(description) = event.get_description() {
+ if description.to_lowercase().contains(&term) {
+ return true;
+ }
+ }
+ false
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index b77dccf..6b5088b 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -11,6 +11,7 @@ pub mod sort;
pub mod unroll;
pub mod utils;
pub mod bucketable;
+pub mod grep;
#[cfg(test)]
pub mod testdata;
diff --git a/src/utils.rs b/src/utils.rs
index 5edad7c..a6ec25f 100644
--- a/src/utils.rs
+++ b/src/utils.rs
@@ -88,6 +88,11 @@ pub fn read_calendar_from_file(filepath: &str) -> Result<IcalVCalendar, String>
read_calendar_from_path(path)
}
+pub fn iterate_calendars_from_files(filenames: impl Iterator<Item = String>) -> impl Iterator<Item = IcalVCalendar> {
+ let cals = filenames.map(|f| read_calendar_from_file(&f));
+ cals.filter_map(|cal| cal.ok())
+}
+
pub fn read_calendars_from_files(files: &mut Iterator<Item = String>) -> Result<Vec<IcalVCalendar>, String> {
let result: Result<Vec<IcalVCalendar>, String> = files.map(|file| read_calendar_from_file(&file)).collect();
return result
@@ -120,3 +125,9 @@ pub fn get_bucket_for_date(date: &Date<Local>) -> String {
);
bucket
}
+
+pub fn print_cals(cals: impl Iterator<Item = IcalVCalendar>) {
+ for cal in cals {
+ println!("{}", cal.get_path_as_string());
+ }
+}