X-Git-Url: https://git.armaanb.net/?a=blobdiff_plain;f=lightcards%2Frunner.py;h=0bd560072b3b2720afd77d795db3f2bd9bf8ac33;hb=eab52aa7f3cc154f25e578dc66de522259eacbd2;hp=2fa42f49e6cc214c92b82d2d1ea8a76fc825acb9;hpb=3c884626432e00720894132ec86e378f1b92d6ef;p=lightcards.git diff --git a/lightcards/runner.py b/lightcards/runner.py index 2fa42f4..0bd5600 100644 --- a/lightcards/runner.py +++ b/lightcards/runner.py @@ -2,35 +2,62 @@ # 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 def parse_args(): + """Parse command line arguments""" parser = argparse.ArgumentParser( description="Terminal flashcards from Markdown" ) - parser.add_argument("inp", metavar="input file", type=str, nargs=1) + parser.add_argument( + "-c", + "--config", + metavar="config_file", + type=str, + default="/dev/null", + help="specify custom config file", + ) + parser.add_argument( + "-V", + "--view", + metavar="1-3", + type=int, + choices=range(1, 4), + help="specify which view to start in", + ) + parser.add_argument("inp", metavar="input_files", type=str, nargs="+") + parser.add_argument( + "-l", + "--lenient", + action="store_true", + help="don't raise exception if tables are malformed", + ) + parser.add_argument( + "-t", + "--table", + metavar="num_table", + type=int, + help="specify which table to use if multiple are given", + ) parser.add_argument( "-a", "--alphabetize", action="store_true", help="alphabetize card order", ) - parser.add_argument( - "-f", "--flip", action="store_true", help="show second column first" - ) parser.add_argument( "-p", "--purge", action="store_true", - help="don't check cached info before starting", + help="delete cache before starting", ) parser.add_argument( "-r", "--reverse", action="store_true", help="reverse card order" @@ -39,48 +66,49 @@ def parse_args(): "-s", "--shuffle", action="store_true", help="shuffle card order" ) parser.add_argument( - "-v", "--version", action="version", version="lightcards 0.6.0" + "-v", + "--version", + action="version", + version=f"lightcards {pkg_resources.require('lightcards')[0].version}", ) 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()) - if cache: + cache = progress.dive(get_orig()[1]) + if cache and conf["cache"]: (stack) = cache # Manipulate deck - if args.shuffle: + if args.shuffle or conf["shuffle"]: shuffle(stack) - if args.alphabetize: - stack.sort() - if args.reverse: + if args.alphabetize or conf["alphabetize"]: + stack.sort(key=lambda x: x.front) + if args.reverse or conf["reverse"]: stack.reverse() - if args.flip: - for x in stack: - x[0], x[1] = x[1], x[0] - headers[0], headers[1] = headers[1], headers[0] - # Send to display - win = Display(stack, headers, idx) - 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(): @@ -90,9 +118,15 @@ def get_orig(): def main(args=sys.argv): args = parse_args() + conf = config.read_file(args.config) + + if not conf["debug"]: + sys.tracebacklimit = 0 + global headers, stack - (headers, stack) = parse.parse_html(parse.md2html(args.inp[0])) - show(args, stack, headers) + (headers, stack) = parse.parse_html(parse.md2html(args.inp), args, conf) + + show(args, stack, headers, conf) if __name__ == "__main__":