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