X-Git-Url: https://git.armaanb.net/?a=blobdiff_plain;f=lightcards%2Frunner.py;h=852bf5bf64b8c921e832aae4541554b87fc48f67;hb=2203afc601a1a16f299bb8e3691a57e45c8230fc;hp=08004db3bd68d4e261fb080c7c777c7ae94b0fd3;hpb=dc6c791fb09b9e5aab0eec970fdca707cd70dc47;p=lightcards.git diff --git a/lightcards/runner.py b/lightcards/runner.py index 08004db..852bf5b 100644 --- a/lightcards/runner.py +++ b/lightcards/runner.py @@ -2,14 +2,13 @@ # Armaan Bhojwani 2021 import argparse -from curses import wrapper -import os +import curses import pkg_resources from random import shuffle import sys -from . import parse, progress -from .display import Display +from . import parse, progress, config +from .display import Display, CursesError from .deck import Status @@ -18,14 +17,21 @@ def parse_args(): parser = argparse.ArgumentParser( description="Terminal flashcards from Markdown" ) + parser.add_argument( + "-c", + "--config", + metavar="path", + type=str, + default="/dev/null", + 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)", + help="specify which view to start in", ) parser.add_argument("inp", metavar="input file", type=str, nargs=1) parser.add_argument( @@ -55,39 +61,41 @@ 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() cache = progress.dive(get_orig()[1]) - if cache: + if cache and conf["cache"]: (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() - # Send to display - win = Display(stack, headers, idx, args.view) - wrapper(win.run) + # Set view + if args.view: + view = args.view + else: + view = conf["default_view"] - -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])) + # Send to display + win = Display(stack, headers, idx, view, args, conf) + try: + curses.wrapper(win.run) + except curses.error as e: + raise CursesError() from e def get_orig(): @@ -97,9 +105,15 @@ def get_orig(): def main(args=sys.argv): 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__":