├── Cargo.toml ├── COPYRIGHT ├── LICENSE-MIT ├── src ├── main.rs ├── fuzzy.rs ├── core.rs ├── persist.rs └── conductor.rs ├── readme.md └── LICENSE-APACHE /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | 3 | name = "hippo" 4 | version = "0.0.1" 5 | authors = [ "Nick Hamann " ] 6 | 7 | [[bin]] 8 | 9 | name = "hippo" 10 | 11 | [dependencies] 12 | docopt = "*" 13 | rustc-serialize = "*" 14 | rusqlite = "0.0.10" 15 | time = "~0.1.0" 16 | 17 | -------------------------------------------------------------------------------- /COPYRIGHT: -------------------------------------------------------------------------------- 1 | hippo is copyright 2015 Nick Hamann. 2 | 3 | Licensed under the Apache License, Version 2.0 or the MIT 5 | license , 6 | at your option. All files in the project carrying such 7 | notice may not be copied, modified, or distributed except 8 | according to those terms. 9 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | Copyright (c) 2015 Nick Hamann 2 | 3 | Permission is hereby granted, free of charge, to any 4 | person obtaining a copy of this software and associated 5 | documentation files (the "Software"), to deal in the 6 | Software without restriction, including without 7 | limitation the rights to use, copy, modify, merge, 8 | publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software 10 | is furnished to do so, subject to the following 11 | conditions: 12 | 13 | The above copyright notice and this permission notice 14 | shall be included in all copies or substantial portions 15 | of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF 18 | ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 19 | TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 20 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 21 | SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 22 | CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 23 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR 24 | IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 25 | DEALINGS IN THE SOFTWARE. 26 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate "rustc-serialize" as rustc_serialize; 2 | extern crate docopt; 3 | extern crate rusqlite; 4 | extern crate time; 5 | 6 | use docopt::Docopt; 7 | pub use time::Timespec; 8 | pub use conductor::{Item, ItemId, ItemSchedData, Review}; 9 | 10 | use conductor::Conductor; 11 | 12 | mod conductor; 13 | mod core; 14 | mod persist; 15 | mod fuzzy; 16 | 17 | static USAGE: &'static str = " 18 | Usage: 19 | hippo review [ | --id=] 20 | hippo add 21 | hippo edit 22 | hippo view 23 | hippo remove 24 | hippo list [--unreviewed --fuzzy] [] 25 | hippo (-h | --help) 26 | 27 | Options: 28 | -h, --help Show this screen. 29 | "; 30 | 31 | #[derive(RustcDecodable, Debug)] 32 | struct Args { 33 | arg_description: Option, 34 | arg_id: Option, 35 | arg_N: Option, 36 | arg_string: Option, 37 | flag_id: Option, 38 | flag_unreviewed: bool, 39 | flag_fuzzy: bool, 40 | cmd_add: bool, 41 | cmd_edit: bool, 42 | cmd_view: bool, 43 | cmd_remove: bool, 44 | cmd_list: bool, 45 | cmd_review: bool, 46 | } 47 | 48 | fn main() { 49 | let args: Args = Docopt::new(USAGE) 50 | .and_then(|d| d.decode()) 51 | .unwrap_or_else(|e| e.exit()); 52 | 53 | let default_review_num = 20; 54 | 55 | let cond = Conductor::new(); 56 | 57 | if args.cmd_add { 58 | let desc_string = args.arg_description.unwrap(); 59 | let desc = &desc_string[..]; 60 | cond.add_item(desc); 61 | 62 | } else if args.cmd_edit { 63 | let id = args.arg_id.unwrap().as_slice().parse().unwrap(); 64 | let desc_string = args.arg_description.unwrap(); 65 | let desc = &desc_string[..]; 66 | cond.edit_item(id, desc); 67 | 68 | } else if args.cmd_view { 69 | let id = args.arg_id.unwrap().as_slice().parse().unwrap(); 70 | cond.view_item(id); 71 | 72 | } else if args.cmd_remove { 73 | let id = args.arg_id.unwrap().as_slice().parse().unwrap(); 74 | cond.remove_item(id); 75 | 76 | } else if args.cmd_list { 77 | cond.list_items(args.arg_string, args.flag_unreviewed, args.flag_fuzzy); 78 | 79 | } else if args.cmd_review { 80 | match args.arg_N { 81 | Some(n) => cond.review(n.as_slice().parse().unwrap()), 82 | None => 83 | match args.flag_id { 84 | Some(id) => cond.review_item(id.as_slice().parse().unwrap()), 85 | None => cond.review(default_review_num), 86 | } 87 | } 88 | 89 | } else { 90 | println!("No command provided"); 91 | } 92 | } 93 | -------------------------------------------------------------------------------- /src/fuzzy.rs: -------------------------------------------------------------------------------- 1 | use std::ops::{Index, IndexMut}; 2 | use std::usize; 3 | 4 | struct MemoMatrix { 5 | rows: usize, 6 | cols: usize, 7 | v: Vec>, 8 | } 9 | 10 | impl MemoMatrix { 11 | fn new(rows: usize, cols: usize) -> Self { 12 | let mut v = Vec::with_capacity(rows*cols); 13 | for _ in 0..(rows*cols) { 14 | v.push(None); 15 | } 16 | MemoMatrix { rows: rows, cols: cols, v: v } 17 | } 18 | } 19 | 20 | impl Index<(usize, usize)> for MemoMatrix { 21 | type Output = Option; 22 | fn index<'a>(&'a self, index: &(usize, usize)) -> &'a Option { 23 | let (r, c) = *index; 24 | assert!(r < self.rows, "row index '{}' is out of bounds", r); 25 | assert!(c < self.cols, "column index '{}' is out of bounds", c); 26 | &self.v[r * self.cols + c] 27 | } 28 | } 29 | 30 | impl IndexMut<(usize, usize)> for MemoMatrix { 31 | fn index_mut<'a>(&'a mut self, index: &(usize, usize)) -> &'a mut Option { 32 | let (r, c) = *index; 33 | assert!(r < self.rows, "row index '{}' is out of bounds", r); 34 | assert!(c < self.cols, "column index '{}' is out of bounds", c); 35 | &mut self.v[r * self.cols + c] 36 | } 37 | } 38 | 39 | pub fn fuzzy_contains<'a, 'b>(p: &'a str, t: &'b str) -> bool { 40 | fuzzy_contains_err(p, t, 1) 41 | } 42 | 43 | fn fuzzy_contains_err<'a, 'b>(p: &'a str, t: &'b str, k: usize) -> bool { 44 | fuzzy_sub_dist(p, t) <= k 45 | } 46 | 47 | // returns the minimum edit distance between `p` and all substrings of `t` 48 | fn fuzzy_sub_dist<'a, 'b>(p: &'a str, t: &'b str) -> usize { 49 | let p_chars: Vec = p.chars().collect(); 50 | let t_chars: Vec = t.chars().collect(); 51 | 52 | // M(i, j) is the minimal cost of an edit sequence that turns p[..i] into t[..j] 53 | let n = t.chars().count(); 54 | let mut rect = MemoMatrix::new(p.chars().count() + 1, n + 1); 55 | 56 | let mut min = usize::MAX; 57 | for k in 0..(n+1) { 58 | let dist = ed_sub(&mut rect, &p_chars[..], &t_chars[..k]); 59 | if dist < min { 60 | min = dist; 61 | } 62 | } 63 | 64 | min 65 | } 66 | 67 | fn ed_sub<'a, 'b>(rect: &mut MemoMatrix, p: &'a [char], t: &'b [char]) -> usize { 68 | let (i, j) = (p.len(), t.len()); 69 | 70 | // check if this has already been computed and use it if so 71 | match rect[(i, j)] { 72 | Some(dist) => return dist, 73 | None => {}, 74 | } 75 | 76 | let dist = if i == 0 { 77 | 0 78 | } else if j == 0 { 79 | i 80 | } else { 81 | let (a, b) = (i-1, j-1); 82 | if p[a] == t[b] { 83 | ed_sub(rect, &p[..a], &t[..b]) 84 | } else { 85 | let v = vec![ 86 | ed_sub(rect, &p[..a], &t[..b]), 87 | ed_sub(rect, &p[..a], t), 88 | ed_sub(rect, p, &t[..b]) 89 | ]; 90 | v.into_iter().min().unwrap() + 1 91 | } 92 | }; 93 | 94 | rect[(i, j)] = Some(dist); 95 | dist 96 | } 97 | -------------------------------------------------------------------------------- /src/core.rs: -------------------------------------------------------------------------------- 1 | use std::num::Float; 2 | 3 | use super::{Item, ItemSchedData}; 4 | use time::now_utc; 5 | 6 | static INITIAL_FF: f64 = 3.0; 7 | static STEP_1_IRI: f64 = 2.0; 8 | static STEP_2_IRI: f64 = 4.0; 9 | 10 | fn days_to_seconds(days: f64) -> i64 { 11 | (days * 86400.0).round() as i64 12 | } 13 | 14 | pub fn init_item() -> ItemSchedData { 15 | ItemSchedData { 16 | last_reviewed: now_utc().to_timespec(), 17 | ff: INITIAL_FF, 18 | int_step: 1, 19 | iri: STEP_1_IRI, 20 | } 21 | } 22 | 23 | pub fn assess_item(data: &ItemSchedData, fam: u8) -> ItemSchedData { 24 | let int_step = if fam < 2 { 25 | 1 26 | } else { 27 | data.int_step + 1 28 | }; 29 | 30 | let iri = match int_step { 31 | 1 => STEP_1_IRI, 32 | 2 => STEP_2_IRI, 33 | _ => data.iri + data.ff 34 | }; 35 | 36 | // SM-2 algorithm says only to adjust if familiar was above a certain 37 | // threshold. haven't thought about what effect this has yet. 38 | // TODO: revisit 39 | 40 | let mut ff = data.ff + match fam { 41 | 0 => 0., 42 | 1 => 0., 43 | 2 => -0.32, 44 | 3 => -0.14, 45 | 4 => 0., 46 | 5 => 0.1, 47 | _ => unreachable!("Internal error: managed to reach unreachable code. I'm impressed.") 48 | }; 49 | 50 | if ff < 1.3 { 51 | ff = 1.3; 52 | } 53 | 54 | ItemSchedData { 55 | last_reviewed: now_utc().to_timespec(), 56 | ff: ff, 57 | int_step: int_step, 58 | iri: iri, 59 | } 60 | } 61 | 62 | 63 | pub fn list_display_item(item: Item) -> String { 64 | format!("{:3} : {}", item.id, item.desc) 65 | } 66 | 67 | pub fn full_display_item(item: Item) -> String { 68 | let dur = now_utc().to_timespec() - item.data.last_reviewed; 69 | format!("{:3} : {}\nLast reviewed: {} hours ago\nFF: {}\nint_step: {}\nIRI: {}", 70 | item.id, item.desc, dur.num_hours(), item.data.ff, 71 | item.data.int_step, item.data.iri) 72 | } 73 | 74 | // Given a vector of items, returns a new vector with only the items 75 | // that are in need of review 76 | pub fn filter_unreviewed_items(items: Vec) -> Vec { 77 | let curr_time = now_utc().to_timespec(); 78 | 79 | items.into_iter() 80 | .filter(|i| (curr_time - i.data.last_reviewed).num_seconds() > days_to_seconds(i.data.iri)) 81 | .collect() 82 | } 83 | 84 | fn sort_review_items(mut items: Vec) -> Vec { 85 | let curr_time = now_utc().to_timespec(); 86 | 87 | let exceeded_by = |it: &Item| -> i64 { 88 | (curr_time - it.data.last_reviewed).num_seconds() - days_to_seconds(it.data.iri) 89 | }; 90 | 91 | items.sort_by(|a, b| exceeded_by(b).cmp(&exceeded_by(a))); 92 | items 93 | } 94 | 95 | pub fn prepare_review_items(items: Vec) -> Vec { 96 | sort_review_items(filter_unreviewed_items(items)) 97 | } 98 | 99 | 100 | pub fn review_display_item(review_num: usize, item: &Item) -> String { 101 | format!("\n{:3} - {} (id: {})\n\n0-5 or 's' to skip item or 'q' to quit > ", 102 | review_num, item.desc, item.id) 103 | } 104 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | Hippo is a command-line program used to schedule the review of items using spaced repetition. Like other spaced-repetition software (Anki, Mnemosyne), the scheduling algorithm is based on the SM-2 algorithm. Unlike other spaced-repetition software, this is not flashcard-based. An "item" in hippo is just a description of the thing you want to review. The actual information to be reviewed is assumed to be elsewhere (in a text file somewhere, or in some note-taking software, or written down in a notebook, or maybe carved into clay tablets). 2 | 3 | # Usage 4 | 5 | Add an item with 6 | 7 | hippo add 8 | 9 | You can edit an item's description with 10 | 11 | hippo edit 12 | 13 | or view more an item's scheduling details by 14 | 15 | hippo view 16 | 17 | (the above details won't make much sense unless you understand the scheduling algorithm) or delete an item with 18 | 19 | hippo remove 20 | 21 | To review items, use 22 | 23 | hippo review [] 24 | 25 | where is an optional argument for the number of items to review. The default is 20. 26 | 27 | You can also review a specific item by using the `--id` flag: 28 | 29 | hippo review --id= 30 | 31 | You can also list all items with 32 | 33 | hippo list [] 34 | 35 | where is an optional search text argument. Only item with descriptions containing the string will be displayed. 36 | 37 | # Scheduling algorithm 38 | 39 | Each item has four fields that are used to make the review schedule: 40 | 41 | - *last_reviewed*, the timestamp when the item was last reviewed 42 | - *ff*, the *familarity factor* which is called "easiness factor" in the original SM-2 algorithm. Why did I change it? I dunno, personal taste? What does it matter? Maybe don't worry about it. 43 | - *iri*, the *inter-repetition interval*. This determines how much time should elapse between reviews of an item in days. 44 | - *int_step*, mostly a thing for tracking newly learned items. 45 | 46 | When a new item is added, the initial values of these fields are: 47 | 48 | - last_reviewed: the time it was created 49 | - ff: 2.5 50 | - iri: 1.0 51 | - int_step: 1 52 | 53 | So what happens when you do the `review` command is we first determine which items need to be reviewed by finding out which items have 54 | 55 | ( - last_reviewed) > iri * 86400 56 | 57 | (since last_reviewed is stored as a timestamp). Once we have the list, we sort the items to be reviewed so that they are in order "most urgently in need of review" to "least urgently in need of review". This is done by checking how much `(current time - last review time)` exceeds the IRI in seconds. 58 | 59 | Each item presents the description. You're supposed to review the material, and then rate on a scale of 0-5 how familiar the item feels, with 5 being most familiar and 0 being least. So if you think you have a great grasp of the item, rate it a 4 or a 5 and the scheduler will present that item less often. If you feel a bit shaky but still understand it, give is a 2 or a 3. If you totally forgot it, rate it 0 or 1. The significance of these is explained below. 60 | 61 | If we let `fam` be the 0-5 rating I've just described above, the logic for updating the 4 fields is given by: 62 | 63 | if fam < 2: 64 | int_step <-- 1 65 | else: 66 | int_step <-- int_step + 1 67 | 68 | if int_step == 1: 69 | iri <-- 1.0 70 | else if int_step == 2: 71 | iri <-- 3.0 72 | else: 73 | iri <-- iri + FF 74 | 75 | if fam == 2: 76 | FF <-- FF - 0.32 77 | else if fam == 3: 78 | FF <-- FF - 0.14 79 | else if fam == 5: 80 | FF <-- FF + 0.1 81 | 82 | if FF < 1.3: 83 | FF <-- 1.3 84 | 85 | last_reviewed <-- current time 86 | 87 | So for `fam` of 0 or 1, the int_step gets reset and it's like we are re-learning the item anew. Note that we also do not adjust the FF in this case, since FF only matters for items of int_step > 2. 88 | 89 | If `fam >= 2`, we update the familiarity factor. In the case of `fam = 2 or 3`, the FF gets adjusted downward `fam == 4` results in no adjustment, while `fam == 5` increases familiarity. 90 | 91 | There's also logic to ensure that FF never goes below 1.3 (this is in the original SM-2 algorithm and I've imported it unthinkingly. I haven't had the chance to test it yet). 92 | -------------------------------------------------------------------------------- /src/persist.rs: -------------------------------------------------------------------------------- 1 | use std::path::PathBuf; 2 | 3 | use rusqlite::SqliteConnection; 4 | use super::{Item, ItemId, ItemSchedData, Review}; 5 | 6 | static SQLITE_DBFILE: &'static str = "hippo.sqlite"; 7 | 8 | pub trait Persister { 9 | fn add_item(&self, desc: &str, data: ItemSchedData) -> Result; 10 | fn change_item_desc(&self, id: ItemId, desc: &str) -> Result<(), String>; 11 | fn update_item(&self, id: ItemId, data: ItemSchedData) -> Result<(), String>; 12 | fn get_item(&self, id: ItemId) -> Result; 13 | fn remove_item(&self, id: ItemId) -> Result<(), String>; 14 | fn get_items(&self) -> Result, String>; 15 | fn add_review(&self, review: Review) -> Result<(), String>; 16 | } 17 | 18 | 19 | pub struct SqlitePersister { 20 | conn: SqliteConnection 21 | } 22 | 23 | impl SqlitePersister { 24 | pub fn new(mut save_dir: PathBuf) -> Self { 25 | save_dir.push(SQLITE_DBFILE); 26 | let conn = SqliteConnection::open(&save_dir).unwrap(); 27 | conn.execute("CREATE TABLE if not exists items ( 28 | id integer primary key autoincrement, 29 | desc text NOT NULL, 30 | last_reviewed text NOT NULL, 31 | ff real NOT NULL, 32 | int_step integer NOT NULL, 33 | iri real NOT NULL)", &[]).unwrap(); 34 | 35 | conn.execute("CREATE TABLE if not exists reviews ( 36 | id integer primary key autoincrement, 37 | review_time text NOT NULL, 38 | item_id integer NOT NULL)", &[]).unwrap(); 39 | 40 | SqlitePersister { conn: conn } 41 | } 42 | } 43 | 44 | impl Persister for SqlitePersister { 45 | fn add_item(&self, desc: &str, data: ItemSchedData) -> Result { 46 | let sql = "INSERT INTO items 47 | (desc, last_reviewed, ff, int_step, iri) 48 | VALUES ($1, $2, $3, $4, $5)"; 49 | 50 | let res = self.conn.execute(sql, &[&desc, &data.last_reviewed, 51 | &data.ff, &data.int_step, &data.iri]); 52 | 53 | match res { 54 | Ok(_) => Ok(self.conn.last_insert_rowid()), 55 | Err(err) => Err(err.message), 56 | } 57 | } 58 | 59 | fn change_item_desc(&self, id: ItemId, desc: &str) -> Result<(), String> { 60 | let res = self.conn.execute("UPDATE items SET desc=$1 WHERE id=$2", 61 | &[&desc, &id]); 62 | match res { 63 | Ok(num) => 64 | if num > 0 { 65 | Ok(()) 66 | } else { 67 | Err("Item not found".to_string()) 68 | }, 69 | Err(err) => Err(err.message) 70 | } 71 | } 72 | 73 | fn update_item(&self, id: ItemId, data: ItemSchedData) -> Result<(), String> { 74 | let sql = "UPDATE items SET last_reviewed=$1, ff=$2, iri=$3, 75 | int_step=$4 WHERE id=$5"; 76 | let res = self.conn.execute(sql, &[&data.last_reviewed, &data.ff, 77 | &data.iri, &data.int_step, &id]); 78 | match res { 79 | Ok(num) => 80 | if num > 0 { 81 | Ok(()) 82 | } else { 83 | Err("Item not found".to_string()) 84 | }, 85 | Err(err) => Err(err.message) 86 | } 87 | } 88 | 89 | fn get_item(&self, id: ItemId) -> Result { 90 | let sql = "SELECT id, desc, last_reviewed, ff, int_step, iri 91 | FROM items WHERE id=$1"; 92 | let mut stmt = match self.conn.prepare(sql) { 93 | Ok(s) => s, 94 | Err(err) => return Err(err.message), 95 | }; 96 | 97 | let mut rows = match stmt.query(&[&id]) { 98 | Ok(s) => s, 99 | Err(err) => return Err(err.message), 100 | }; 101 | 102 | let row = match rows.next() { 103 | Some(r) => match r { 104 | Ok(row) => row, 105 | Err(err) => return Err(err.message), 106 | }, 107 | None => return Err("Item not found".to_string()), 108 | }; 109 | 110 | Ok(Item { 111 | id: row.get(0), 112 | desc: row.get(1), 113 | data: ItemSchedData { 114 | last_reviewed: row.get(2), 115 | ff: row.get(3), 116 | int_step: row.get(4), 117 | iri: row.get(5), 118 | }, 119 | }) 120 | } 121 | 122 | fn remove_item(&self, id: ItemId) -> Result<(), String> { 123 | let res = self.conn.execute("DELETE FROM items WHERE id=$1", &[&id]); 124 | match res { 125 | Ok(_) => Ok(()), 126 | Err(err) => Err(err.message) 127 | } 128 | } 129 | 130 | fn get_items(&self) -> Result, String> { 131 | let sql = "SELECT id, desc, last_reviewed, ff, int_step, iri 132 | FROM items"; 133 | let mut stmt = match self.conn.prepare(sql) { 134 | Ok(s) => s, 135 | Err(err) => return Err(err.message), 136 | }; 137 | 138 | let rows = match stmt.query(&[]) { 139 | Ok(s) => s, 140 | Err(err) => return Err(err.message), 141 | }; 142 | 143 | let mut v = vec![]; 144 | for row in rows { 145 | match row { 146 | Ok(row) => v.push( 147 | Item { 148 | id: row.get(0), 149 | desc: row.get(1), 150 | data: ItemSchedData { 151 | last_reviewed: row.get(2), 152 | ff: row.get(3), 153 | int_step: row.get(4), 154 | iri: row.get(5), 155 | }, 156 | }), 157 | Err(err) => return Err(err.message), 158 | } 159 | } 160 | 161 | Ok(v) 162 | } 163 | 164 | fn add_review(&self, review: Review) -> Result<(), String> { 165 | let sql = "INSERT INTO reviews (review_time, item_id) 166 | VALUES ($1, $2)"; 167 | 168 | let res = self.conn.execute(sql, &[&review.review_time, 169 | &review.item_id]); 170 | 171 | match res { 172 | Ok(_) => Ok(()), 173 | Err(err) => Err(err.message), 174 | } 175 | } 176 | } 177 | -------------------------------------------------------------------------------- /src/conductor.rs: -------------------------------------------------------------------------------- 1 | use std::old_io::stdin; 2 | use std::env; 3 | use std::path::PathBuf; 4 | use std::fs::{self, PathExt}; 5 | 6 | use super::Timespec; 7 | use persist::{Persister, SqlitePersister}; 8 | use core; 9 | use fuzzy::fuzzy_contains; 10 | 11 | // doesn't need to be signed, but sqlite uses i64 for rowids and 12 | // rusqlite only implements ToSql for i64 (not any other integer types) 13 | pub type ItemId = i64; 14 | 15 | pub struct Item { 16 | pub id: ItemId, 17 | pub desc: String, 18 | pub data: ItemSchedData, 19 | } 20 | 21 | pub struct ItemSchedData { 22 | pub last_reviewed: Timespec, 23 | pub ff: f64, 24 | pub int_step: i32, 25 | pub iri: f64, 26 | } 27 | 28 | pub struct Review { 29 | pub review_time: Timespec, 30 | pub item_id: ItemId, 31 | } 32 | 33 | pub struct Conductor { 34 | persister: P 35 | } 36 | 37 | impl Conductor { 38 | pub fn new() -> Self { 39 | let save_path = save_dir(); 40 | 41 | if !save_path.exists() { 42 | match fs::create_dir_all(&save_path) { 43 | Err(err) => panic!("Uh oh: {}", err), 44 | Ok(_) => {}, 45 | } 46 | } 47 | 48 | Conductor { persister: SqlitePersister::new(save_path) } 49 | } 50 | } 51 | 52 | impl Conductor

{ 53 | pub fn add_item(&self, desc: &str) { 54 | let init_data = core::init_item(); 55 | match self.persister.add_item(desc, init_data) { 56 | Ok(id) => println!("Item {} has been added", id), 57 | Err(err) => println!("{}", err), 58 | } 59 | } 60 | 61 | pub fn edit_item(&self, id: ItemId, desc: &str) { 62 | match self.persister.change_item_desc(id, desc) { 63 | Ok(_) => println!("Item {}'s description has been updated", id), 64 | Err(e) => println!("{}", e), 65 | } 66 | } 67 | 68 | pub fn view_item(&self, id: ItemId) { 69 | match self.persister.get_item(id) { 70 | Ok(item) => println!("{}", core::full_display_item(item)), 71 | Err(e) => println!("{}", e), 72 | } 73 | } 74 | 75 | pub fn remove_item(&self, id: ItemId) { 76 | match self.persister.remove_item(id) { 77 | Ok(_) => println!("Item {} has been removed", id), 78 | Err(e) => println!("{}", e), 79 | } 80 | } 81 | 82 | pub fn list_items(&self, search: Option, unreviewed: bool, fuzzy: bool) { 83 | match self.persister.get_items() { 84 | Ok(mut items) => { 85 | // only show items in need of review 86 | if unreviewed { 87 | items = core::filter_unreviewed_items(items); 88 | } 89 | 90 | match search { 91 | Some(text) => 92 | // Barf. 93 | if fuzzy { 94 | for item in items { 95 | if fuzzy_contains(text.as_slice(), item.desc.as_slice()) { 96 | println!("{}", core::list_display_item(item)); 97 | } 98 | } 99 | } else { 100 | for item in items { 101 | if item.desc.contains(text.as_slice()) { 102 | println!("{}", core::list_display_item(item)); 103 | } 104 | } 105 | }, 106 | None => 107 | for item in items { 108 | println!("{}", core::list_display_item(item)); 109 | }, 110 | } 111 | } 112 | Err(e) => println!("{}", e), 113 | } 114 | } 115 | 116 | pub fn review(&self, n: usize) { 117 | match self.persister.get_items() { 118 | Ok(items) => self.do_review(core::prepare_review_items(items), n), 119 | Err(e) => println!("{}", e), 120 | } 121 | } 122 | 123 | pub fn review_item(&self, id: ItemId) { 124 | match self.persister.get_item(id) { 125 | Ok(item) => self.do_review(vec![item], 1), 126 | Err(e) => println!("{}", e), 127 | } 128 | 129 | } 130 | 131 | fn do_review(&self, items: Vec, n: usize) { 132 | let mut reviewed = 0; 133 | let mut items_iter = items.iter(); 134 | 135 | while reviewed < n { 136 | let item = match items_iter.next() { 137 | Some(item) => item, 138 | None => { println!("\nNo more items to review"); return }, 139 | }; 140 | 141 | let mut inp: char; 142 | 143 | // loop for getting user input 144 | loop { 145 | print!("{}", core::review_display_item(reviewed+1, item)); 146 | let input = stdin().read_line() 147 | .ok().expect("Failed to read input"); 148 | let input = input.as_slice().trim(); 149 | 150 | if input.len() == 1 && "qs012345".find(input.char_at(0)).is_some() { 151 | inp = input.char_at(0); 152 | break; 153 | } else { 154 | println!("\nInvalid input"); 155 | } 156 | } 157 | 158 | match inp { 159 | 'q' => break, 160 | 's' => continue, 161 | n => { 162 | let fam = ((n as isize) - ('0' as isize)) as u8; 163 | let new_item_data = core::assess_item(&item.data, fam); 164 | let time = new_item_data.last_reviewed; 165 | match self.persister.update_item(item.id, new_item_data) { 166 | Err(e) => {println!("{}", e); return }, 167 | _ => {}, 168 | } 169 | 170 | let rev = Review { 171 | item_id: item.id, 172 | review_time: time 173 | }; 174 | 175 | match self.persister.add_review(rev) { 176 | Err(e) => {println!("{}", e); return }, 177 | _ => { reviewed += 1; }, 178 | } 179 | }, 180 | } 181 | 182 | } 183 | 184 | println!("\nFinished reviewing."); 185 | } 186 | } 187 | 188 | 189 | fn save_dir() -> PathBuf { 190 | // On Linux we will try to use 191 | // 192 | // $XDG_DATA_HOME/hippo 193 | // 194 | // or 195 | // 196 | // $HOME/.local/share/ 197 | // 198 | // as a fallback. (If both don't exist (somehow?), use the current folder) 199 | // 200 | // On Windows I guess I should use %AppData%? I don't intend to implement 201 | // this just yet. 202 | 203 | match env::var("XDG_DATA_HOME") { 204 | Ok(dir) => { let mut p = PathBuf::new(&dir); p.push("hippo"); p }, 205 | Err(_) => 206 | match env::var("HOME") { 207 | Ok(dir) => { 208 | let mut p = PathBuf::new(&dir); 209 | p.push(".local"); 210 | p.push("share"); 211 | p.push("hippo"); 212 | p 213 | }, 214 | Err(_) => PathBuf::new(""), 215 | }, 216 | } 217 | } 218 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------