]> git.armaanb.net Git - lightcards.git/blob - lightcards/lightcards.py
ed1916d371ae9d833af41435772c5ed2680c8a70
[lightcards.git] / lightcards / lightcards.py
1 # Markdown flashcard utility
2 # Armaan Bhojwani 2021
3
4 import argparse
5 from curses import wrapper
6 import os
7 from random import shuffle
8 import sys
9
10 from . import parse, progress
11 from .display import Display
12 from .deck import Status
13
14
15 def parse_args():
16     parser = argparse.ArgumentParser(
17         description="Terminal flashcards from Markdown")
18     parser.add_argument("inp",
19                         metavar="input file",
20                         type=str,
21                         nargs=1)
22     parser.add_argument("-a", "--alphabetize",
23                         action='store_true',
24                         help="alphabetize card order")
25     parser.add_argument("-f", "--flip",
26                         action='store_true',
27                         help="show second column first")
28     parser.add_argument("-p", "--purge",
29                         action='store_true',
30                         help="don't check cached info before starting")
31     # TODO: don't require input file when using  -P
32     parser.add_argument("-P", "--purge-all",
33                         action='store_true',
34                         help="don't check cached info before starting")
35     parser.add_argument("-r", "--reverse",
36                         action='store_true',
37                         help="reverse card order")
38     parser.add_argument("-s", "--shuffle",
39                         action='store_true',
40                         help="shuffle card order")
41     parser.add_argument("-v", "--version",
42                         action='version',
43                         version="lightcards 0.4.0")
44     return parser.parse_args()
45
46
47 def show(args, stack, headers):
48     """
49     Get objects from cache, manipulate deck according to passed arguments, and
50     send it to the display functions
51     """
52     # Purge caches if asked
53     if args.purge:
54         progress.purge(stack)
55     if args.purge_all:
56         progress.purge_all()
57
58     # Check for caches
59     ida = progress.dive(stack)
60     if ida:
61         (idx, stack, headers) = ida
62     else:
63         idx = Status()
64
65     # Manipulate deck
66     if args.shuffle:
67         shuffle(stack)
68     if args.alphabetize:
69         stack.sort()
70     if args.reverse:
71         stack.reverse()
72     if args.flip:
73         for x in stack:
74             x[0], x[1] = x[1], x[0]
75         headers[0], headers[1] = headers[1], headers[0]
76
77     # Send to display
78     win = Display(stack, headers, idx)
79     wrapper(win.run)
80
81
82 def reparse():
83     """Parse arguments and input file again"""
84     args = parse_args()
85     os.system(f"$EDITOR {args.inp[0]}"),
86     return parse.parse_html(parse.md2html(args.inp[0]))
87
88
89 def get_orig():
90     """Return original header and stack"""
91     return((headers, stack))
92
93
94 def main(args=sys.argv):
95     args = parse_args()
96     global headers, stack
97     (headers, stack) = parse.parse_html(parse.md2html(args.inp[0]))
98     show(args, stack, headers)
99
100
101 if __name__ == "__main__":
102     main()