1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
extern crate rand;
extern crate chrono;
extern crate ansi_term;
extern crate clap;

use std::fs::File;
use std::io::{Write,BufReader,BufRead,Error,ErrorKind};
use std::time::{SystemTime, UNIX_EPOCH};
use std::fmt;
use std::path::PathBuf;
use rand::prelude::Rng;
use chrono::NaiveDateTime;
use ansi_term::Colour;
use clap::{App,Arg};

#[derive(Clone)]
pub struct VocaSession {
    pub columns: Vec<String>,
    pub decks: Vec<String>,
    ///interval in minutes
    pub intervals: Vec<u32>,
    pub returntofirst: bool,
    filename: Option<String>,
    ///Configuration of columns to show for each side of the card
    pub showcolumns: Vec<Vec<u8>>,
    ///list delimiter
    pub listdelimiter: Option<String>,
    header: bool,
}

pub struct VocaData {
    pub session: VocaSession,
    pub cards: Vec<VocaCard>,
    pub comments: Vec<(usize,String)>,
}

pub struct VocaCard {
    pub fields: Vec<String>,
    pub due: Option<NaiveDateTime>,
    pub deck: u8
}

#[derive(Debug,Copy,Clone,PartialEq)]
pub enum PrintFormat {
    Plain,
    AnsiColour
}

impl VocaSession {
    pub fn common_arguments<'a,'b>() -> Vec<clap::Arg<'a,'b>> {
        let mut args: Vec<Arg> = Vec::new();
        args.push( Arg::with_name("showcolumns")
            .long("showcolumns")
            .short("-C")
            .help("Specify what columns to show on the card, comma separated list of column names, specify this multiple times to define multiple 'sides' of the card. The columns themselves are defined using --columns")
            .multiple(true)
            .takes_value(true)
        );
        args.push( Arg::with_name("decks")
            .long("decks")
            .short("-d")
            .help("Comma seperated list of deck names")
            .takes_value(true)
        );
        args.push( Arg::with_name("intervals")
            .long("intervals")
            .short("-i")
            .help("Comma seperated list of intervals for each respective deck (in minutes). Must contain as many items as --decks")
            .takes_value(true)
        );
        args.push( Arg::with_name("columns")
            .long("columns")
            .short("-c")
            .help("Comma separated list of column names.")
            .takes_value(true)
        );
        args.push( Arg::with_name("listdelimiter")
            .long("listdelimiter")
            .short("-l")
            .help("List delimiter to separate multiple alternatives within a field (recommended: | )")
            .takes_value(true)
        );
        args.push( Arg::with_name("returntofirst")
            .long("returntofirst")
            .short("-1")
            .help("When a card is demoted (e.g. answered incorrectly), demote it to the very first deck rather than the previous deck")
        );
        args
    }

    pub fn set_common_arguments<'a>(&mut self, args: &clap::ArgMatches<'a>) -> Result<(),Error> {
        if let Some(decks) = args.value_of("decks") {
            self.decks = decks.trim().split(",").map(|s| s.trim().to_owned()).collect();
        }
        if let Some(intervals) = args.value_of("intervals") {
            self.intervals = intervals.trim().split(",").map(|s| s.parse::<u32>().expect("parsing interval")).collect();
        }
        if let Some(columns) = args.value_of("columns") {
            self.columns = columns.trim().split(",").map(|s| s.trim().to_owned()).collect();
        }
        if let Some(listdelimiter) = args.value_of("listdelimiter") {
            self.listdelimiter = Some(listdelimiter.to_string());
        }
        if let Some(showcolumns) = args.values_of("showcolumns") {
            self.showcolumns.clear();
            for showcolumns in showcolumns {
                self.showcolumns.push(showcolumns.trim().split(",").map( |s|
                        self.columns.iter().enumerate() .find(|&r| r.1 == s.trim() )
                        .expect(format!("ERROR: showcolumns references a non-existing column: {}",s).as_str())
                        .0
                ).map(|n| n as u8 ).collect());
            }
        }
        if args.is_present("returntofirst") {
            self.returntofirst = true;
        }

        //sanity checks and defaults
        if self.decks.len() > 0 && self.intervals.is_empty() {

        } else if self.decks.len() != self.intervals.len() {
            return Err(Error::new(ErrorKind::InvalidData, "ERROR: intervals and decks have different length"));
        }

        if self.showcolumns.is_empty() {
            //default configuration: two sides
            self.showcolumns.push(vec!(0)); //first column on front side
            self.showcolumns.push((1..self.columns.len()).map(|n| n as u8).collect()); //other columns on back side
        }
        Ok(())
    }

    pub fn from_arguments(args: Vec<&str>) -> Result<Self,Error> {
        let mut vocasession = Self::default();
        let mut args = args.clone();
        args.insert(0,"metadata");
        let args = App::new("metadata").args(&Self::common_arguments()).get_matches_from(args);
        vocasession.set_common_arguments(&args)?;
        Ok(vocasession)
    }

    pub fn get_deck_by_name(&self, name: &str) -> Option<u8> {
        for (i, n) in self.decks.iter().enumerate() {
            if n == name {
                return Some(i as u8);
            }
        }
        None
    }
}

impl Default for VocaSession {
    fn default() -> Self {
        VocaSession {
            columns: Vec::new(),
            decks: Vec::new(),
            intervals: Vec::new(),
            returntofirst: false,
            filename: None,
            showcolumns: Vec::new(),
            listdelimiter: None,
            header: false
        }
    }
}

impl VocaData {
    pub fn from_file(filename: &str) -> Result<Self,std::io::Error> {
        let file = File::open(filename)?;
        let reader = BufReader::new(file);
        let mut cards: Vec<VocaCard> = Vec::new();
        let mut comments: Vec<(usize,String)> = Vec::new();
        let mut header: bool = false;
        let mut columncount: u8 = 0;
        let mut metadata_args: Vec<String> = vec!();
        for (i, line) in reader.lines().enumerate() {
            let line = line?;
            if line.starts_with('#') {
                //metadata or comment
                if line.starts_with("#--") {
                    //metadata
                    if let Some((index,_)) = line.chars().enumerate().find(|&r| r.1 == ' ') {
                        metadata_args.push(format!("--{}",&line[3..index]).to_owned());
                        metadata_args.push(line[index+1..].to_owned());
                    } else {
                        metadata_args.push(format!("--{}",&line[3..]).to_owned());
                    }
                } else {
                    comments.push( (cards.len(), line) ); //we store the index so we can later serialise it in proper order again
                }
            } else if !line.is_empty() {
                let card = VocaCard::parse_line(&line)?;
                if i == 0 && !line.contains("deck#") && !line.contains("due@") && line == line.to_uppercase() {
                    metadata_args.push("--columns".to_owned());
                    metadata_args.push(card.fields.join(","));
                    header = true
                } else {
                    let length = card.fields.len() as u8;
                    if length > columncount {
                        columncount = length;
                    }
                    cards.push(card);
                }
            } else {
                //empty lines are considered comments for our purposes, we retain them in the output
                comments.push( (cards.len(), line) );
            }
        }
        if !metadata_args.contains(&"--columns".to_owned()) {
            //no column/header information provided, infer
            metadata_args.push("--columns".to_owned());
            metadata_args.push((1..=columncount).map(|n| format!("column#{}",n).to_owned()).collect::<Vec<String>>().join(",") );
        }
        let mut session = VocaSession::from_arguments(metadata_args.iter().map(|s| s.as_str()).collect())?;
        session.header = header;
        session.filename = Some(filename.to_owned());

        Ok(VocaData {
            cards: cards,
            session: session,
            comments: comments,
        })
    }


    pub fn random_index(&self, rng: &mut impl Rng, deck: Option<u8>, due_only: bool, seen_only: bool) -> Option<(usize,usize)> {
        let mut indices: Vec<usize> = Vec::new();

        let now: NaiveDateTime = NaiveDateTime::from_timestamp(
            SystemTime::now().duration_since(UNIX_EPOCH).expect("Unable to get time").as_secs() as i64, 0
        );

        for (i, card) in self.cards.iter().enumerate() {
            if card.is_presentable(Some(&now), deck, due_only, seen_only) {
                indices.push(i);
            }
        }

        if !indices.is_empty() {
            return Some((indices[rng.gen_range(0, indices.len())], indices.len()));
        }
        None
    }


    pub fn next_index(&self, index: usize, deck: Option<u8>, due_only: bool, seen_only: bool, inclusive: bool) -> Option<(usize,usize)> {
        let now: NaiveDateTime = NaiveDateTime::from_timestamp(
            SystemTime::now().duration_since(UNIX_EPOCH).expect("Unable to get time").as_secs() as i64, 0
        );

        let mut next: Option<usize> = None;
        let mut count: usize = 0;
        for (i, card) in self.cards.iter().enumerate() {
            if (!inclusive && i > index) || (inclusive && i >= index) {
                if card.is_presentable(Some(&now), deck, due_only, seen_only) {
                    if next.is_none() {
                        next = Some(i);
                    } else {
                        count += 1;
                    }
                }
            }
        }
        next.map(|i| (i, count))
    }

    pub fn pick_card<'a>(&'a self, rng: &mut impl Rng, deck: Option<u8>, due_only: bool, seen_only: bool) -> Option<&'a VocaCard> {
        if let Some((choice,_)) = self.random_index(rng, deck, due_only, seen_only) {
            return Some(&self.cards[choice]);
        }
        None
    }

    pub fn pick_card_mut<'a>(&'a mut self, rng: &mut impl Rng, deck: Option<u8>, due_only: bool, seen_only: bool) -> Option<&'a mut VocaCard> {
        if let Some((choice,_)) = self.random_index(rng, deck, due_only, seen_only) {
            return Some(&mut self.cards[choice]);
        }
        None
    }

    pub fn pick_next_card<'a>(&'a self, index: usize, deck: Option<u8>, due_only: bool, seen_only: bool, inclusive: bool) -> Option<&'a VocaCard> {
        if let Some((choice,_)) = self.next_index(index, deck, due_only, seen_only, inclusive) {
            return Some(&self.cards[choice]);
        }
        None
    }

    pub fn pick_next_card_mut<'a>(&'a mut self, index: usize, deck: Option<u8>, due_only: bool, seen_only: bool, inclusive: bool) -> Option<&'a mut VocaCard> {
        if let Some((choice,_)) = self.next_index(index, deck, due_only, seen_only, inclusive) {
            return Some(&mut self.cards[choice]);
        }
        None
    }



    pub fn write(&self) -> Result<(),std::io::Error> {
        if self.session.filename.is_none() {
            return Err(std::io::Error::new(ErrorKind::InvalidData, "No filename configured"));
        }
        let mut file = std::fs::File::create(self.session.filename.as_ref().unwrap().as_str())?;
        //contents
        if self.session.header {
            file.write(self.session.columns.join("\t").as_bytes() )?;
            file.write(b"\n")?;
        }
        let mut nextcommentindex = if !self.comments.is_empty() { //initialise
            Some(self.comments[0].0)
        } else {
            None
        };
        for (i,card) in self.cards.iter().enumerate() {
            file.write(card.write_to_string().as_bytes())?;
            file.write(b"\n")?;
            if nextcommentindex.is_some() && i + 1 == nextcommentindex.unwrap() {
                for (commentindex, comment) in self.comments.iter() {
                    if *commentindex == i + 1 {
                        file.write(comment.as_bytes())?;
                        file.write(b"\n")?;
                        nextcommentindex = None; //reset
                    } else if *commentindex > i + 1 {
                        nextcommentindex = Some(*commentindex); //set for next
                        break;
                    }
                }
            }
        }
        //metadata last
        if !self.session.decks.is_empty() {
            file.write(b"#--decks ")?;
            file.write(self.session.decks.join(",").as_bytes() )?;
            file.write(b"\n")?;
        }
        if !self.session.intervals.is_empty() {
            file.write(b"#--intervals ")?;
            file.write(self.session.intervals.iter().map(|s| format!("{}",s)).collect::<Vec<String>>().join(",").as_bytes() )?;
            file.write(b"\n")?;
        }
        if let Some(listdelimiter) = &self.session.listdelimiter {
            file.write(b"#--listdelimiter ")?;
            file.write(listdelimiter.as_bytes())?;
            file.write(b"\n")?;
        }
        if self.session.returntofirst {
            file.write(b"#--returntofirst\n")?;
        }
        if !self.session.columns.is_empty() {
            if !self.session.header {
                file.write(b"#--columns ")?;
                file.write(self.session.columns.join(",").as_bytes() )?;
                file.write(b"\n")?;
            }
            for showcolumns in self.session.showcolumns.iter() {
                file.write(b"#--showcolumns ")?;
                file.write(showcolumns.iter().map(|n| format!("{}", self.session.columns[*n as usize]).to_string()).collect::<Vec<String>>().join(",").as_bytes() )?;
                file.write(b"\n")?;
            }
        }
        Ok(())
    }
}


impl VocaCard {
    pub fn parse_line(line: &str) -> Result<VocaCard, std::io::Error> {
        let mut begin = 0;
        let mut fields: Vec<String> =  Vec::new();
        let mut deck: u8 = 0;
        let mut due: Option<NaiveDateTime> = None;
        let length = line.chars().count();
        for (j, (i, c)) in line.char_indices().enumerate() {
            if (j == length -1) || (c == '\t')  {
                //handle previous column
                let value = &line[begin..if j == length - 1 {
                    line.len()
                } else {
                    i
                }];
                if value.starts_with("deck#") {
                    if let Ok(num) = &value[5..].parse::<u8>() {
                        deck = *num - 1;
                    }
                } else if value.starts_with("due@") {
                    due = match NaiveDateTime::parse_from_str(&value[4..], "%Y-%m-%d %H:%M:%S") {
                        Ok(dt) => Some(dt),
                        Err(e) => {
                            return Err(std::io::Error::new(ErrorKind::InvalidData, format!("Unable to parse due date: {}",e)));
                        }
                    };
                } else {
                    if value == "-" { //empty field placeholder
                        fields.push(String::new());
                    } else {
                        fields.push(value.trim().to_owned());
                    }
                }
                begin = i+1
            }
        }
        Ok( VocaCard {
            fields: fields,
            due: due,
            deck: deck
        })
    }

    pub fn write_to_string(&self) -> String {
        let mut result: String = String::new();
        for (i, field) in self.fields.iter().enumerate() {
            if i > 0 {
                result += "\t";
            }
            if field.is_empty() {
                result += "-";
            } else {
                result += field;
            }
        }
        if self.deck > 0 {
            result = format!("{}\tdeck#{}",result, self.deck + 1);
        }
        if let Some(due) = self.due {
            result = format!("{}\tdue@{}",result, due.format("%Y-%m-%d %H:%M:%S").to_string().as_str() );
        }
        result
    }

    pub fn move_to_deck(&mut self, deck: u8, session: &VocaSession) -> bool {
        if deck >= session.decks.len() as u8 {
            return false;
        }
        if let Some(interval) = session.intervals.get(deck as usize) {
            self.due = Some(NaiveDateTime::from_timestamp(
                    SystemTime::now().duration_since(UNIX_EPOCH).expect("Unable to get time").as_secs() as i64 + (interval * 60) as i64, 0
            ));
        }
        self.deck = deck;
        true
    }

    pub fn promote(&mut self, session: &VocaSession) -> bool {
        if ((self.deck+1) as usize) < session.decks.len() {
            self.move_to_deck(self.deck+1, session);
            true
        } else {
            self.move_to_deck(self.deck, session);
            false
        }
    }

    pub fn demote(&mut self, session: &VocaSession)  -> bool {
        if self.deck > 0 && !session.returntofirst {
            self.move_to_deck(self.deck-1, session);
            true
        } else {
            self.move_to_deck(0, session);
            false
        }
    }

    pub fn print(&self, side: u8, session: &VocaSession, format: PrintFormat, wraplist: bool) -> Result<(), std::fmt::Error> {
        let output = self.fields_to_str(side, session, wraplist)?;
        for (index, output) in output {
            match format {
                PrintFormat::Plain => println!("{}",output),
                PrintFormat::AnsiColour => {
                    match index {
                        0 => println!("{}",Colour::Green.paint(output).to_string()),
                        1 => println!("{}",Colour::Cyan.paint(output).to_string()),
                        2 => println!("{}",Colour::Yellow.paint(output).to_string()),
                        3 => println!("{}",Colour::Purple.paint(output).to_string()),
                        4 => println!("{}",Colour::Blue.paint(output).to_string()),
                        _ => println!("{}",output),
                    }
                }
            }
        }
        Ok(())
    }

    pub fn fields_to_str(&self, side: u8, session: &VocaSession, wraplist: bool) -> Result<Vec<(u8,&str)>, std::fmt::Error> {
        if let Some(showcolumns) = session.showcolumns.get(side as usize) {
            let mut output: Vec<(u8,&str)> = Vec::new();
            for showcolumn in showcolumns.iter() {
                let lines = self.field_to_str(*showcolumn, session, wraplist)?;
                for line in lines {
                    output.push((*showcolumn, line));
                }
            }
            Ok(output)
        } else {
            Err(fmt::Error)
        }
    }


    pub fn field_to_str(&self, index: u8, session: &VocaSession, wraplist: bool) -> Result<Vec<&str>, std::fmt::Error> {
        if let Some(field) = self.fields.get(index as usize) {
            let output: Vec<&str> = if let Some(listdelimiter) = &session.listdelimiter {
                if wraplist {
                    field.split(listdelimiter.as_str()).collect()
                } else {
                    vec!(field.as_str())
                }
            } else {
                vec!(field.as_str())
            };
            Ok(output)
        } else {
            Ok(Vec::new()) //empty string
        }
    }

    pub fn is_presentable(&self, now: Option<&NaiveDateTime>, deck: Option<u8>, due_only: bool, seen_only: bool) -> bool {
        let now: NaiveDateTime = match now {
            Some(dt) => *dt,
            None => NaiveDateTime::from_timestamp( SystemTime::now().duration_since(UNIX_EPOCH).expect("Unable to get time").as_secs() as i64, 0),
        };
        if deck.is_none() || self.deck == deck.unwrap() {
            if self.due.is_none() && seen_only {
                return false;
            }
            if !due_only || (due_only && (self.due.is_none() || self.due.unwrap() < now)) {
                return true;
            }
        }
        false
    }
}

pub fn load_files(files: Vec<&str>, force: bool) -> Vec<VocaData> {
    let mut datasets: Vec<VocaData> = Vec::new();

    for filename in files.iter() {
        if !PathBuf::from(filename).exists() {
            eprintln!("ERROR: Specified input file not does exist: {}", filename);
            std::process::exit(1);
        } else {
            match VocaData::from_file(filename) {
                Ok(mut data) => {
                    if !datasets.is_empty() {
                        if data.session.columns != datasets[0].session.columns {
                            eprintln!("ERROR: columns of {} differ from those in the first loaded file, unable to load together.", filename);
                            std::process::exit(1);
                        }
                        if data.session.decks != datasets[0].session.decks {
                            if force || data.session.decks.is_empty() {
                                data.session.decks = datasets[0].session.decks.clone();
                                data.session.intervals = datasets[0].session.intervals.clone();
                            } else {
                                eprintln!("ERROR: decks of {} differ from those in the first loaded file, refusing to load together (use --force to force it)", filename);
                                std::process::exit(1);
                            }
                        }
                    }
                    datasets.push(data);
                }
                Err(err) => {
                    eprintln!("ERROR loading {}: {}", filename, err);
                    std::process::exit(1);
                }
            }
        }
    }

    datasets
}