]> git.armaanb.net Git - lightcards.git/blobdiff - lightcards/runner.py
Add menu keybindings to config
[lightcards.git] / lightcards / runner.py
index e0af7d9e24e00746ca683e49e0537aac67e98f57..73e275875138b93ec7f9edcd05c767b8312dfe45 100644 (file)
@@ -3,12 +3,11 @@
 
 import argparse
 import curses
-import os
 import pkg_resources
 from random import shuffle
 import sys
 
-from . import parse, progress
+from . import parse, progress, config
 from .display import Display, CursesError
 from .deck import Status
 
@@ -18,13 +17,18 @@ def parse_args():
     parser = argparse.ArgumentParser(
         description="Terminal flashcards from Markdown"
     )
+    parser.add_argument(
+        "-c",
+        "--config",
+        type=str,
+        help="specify custom config file",
+    )
     parser.add_argument(
         "-V",
         "--view",
         metavar="view",
         type=int,
         choices=range(1, 4),
-        default=1,
         help="specify which view to start in (default = 1)",
     )
     parser.add_argument("inp", metavar="input file", type=str, nargs=1)
@@ -55,14 +59,14 @@ def parse_args():
     return parser.parse_args()
 
 
-def show(args, stack, headers):
+def show(args, stack, headers, conf):
     """
     Get objects from cache, manipulate deck according to passed arguments, and
     send it to the display functions
     """
     # Purge caches if asked
     if args.purge:
-        progress.purge(stack)
+        progress.purge(get_orig()[1])
 
     # Check for caches
     idx = Status()
@@ -71,39 +75,43 @@ def show(args, stack, headers):
         (stack) = cache
 
     # Manipulate deck
-    if args.shuffle:
+    if args.shuffle or conf["shuffle"]:
         shuffle(stack)
-    if args.alphabetize:
+    if args.alphabetize or conf["alphabetize"]:
         stack.sort(key=lambda x: x.front)
-    if args.reverse:
+    if args.reverse or conf["reverse"]:
         stack.reverse()
 
+    # Set view
+    if args.view:
+        view = args.view
+    else:
+        view = conf["default_view"]
+
     # Send to display
-    win = Display(stack, headers, idx, args.view)
+    win = Display(stack, headers, idx, view, args, conf)
     try:
         curses.wrapper(win.run)
     except curses.error as e:
         raise CursesError() from e
 
 
-def reparse():
-    """Parse arguments and input file again"""
-    args = parse_args()
-    os.system(f"$EDITOR {args.inp[0]}"),
-    return parse.parse_html(parse.md2html(args.inp[0]))
-
-
 def get_orig():
     """Return original header and stack"""
     return (headers, stack)
 
 
 def main(args=sys.argv):
-    sys.tracebacklimit = 0
     args = parse_args()
+    conf = config.read_file(args.config)
+
     global headers, stack
     (headers, stack) = parse.parse_html(parse.md2html(args.inp[0]))
-    show(args, stack, headers)
+
+    if not conf["debug"]:
+        sys.tracebacklimit = 0
+
+    show(args, stack, headers, conf)
 
 
 if __name__ == "__main__":