From dd109cb4a1b49f920e36e5732417c4a3d5582821 Mon Sep 17 00:00:00 2001 From: Armaan Bhojwani Date: Sat, 13 Feb 2021 11:45:33 -0500 Subject: [PATCH] Add preliminary config file support Still lots of work to be done on this feature, but it's a start! --- README.md | 3 +++ config.py | 18 +++++++++++++++++ lightcards/config.py | 44 ++++++++++++++++++++++++++++++++++++++++++ lightcards/display.py | 35 +++++++++++++++++++-------------- lightcards/progress.py | 1 + lightcards/runner.py | 11 ++++++++--- man/lightcards.1 | 6 ++++++ man/lightcards.1.md | 6 ++++++ setup.py | 13 ++++++++++++- 9 files changed, 118 insertions(+), 19 deletions(-) create mode 100644 config.py create mode 100644 lightcards/config.py diff --git a/README.md b/README.md index 4dd7dc9..9284e1e 100644 --- a/README.md +++ b/README.md @@ -24,5 +24,8 @@ See `lightcards --help` or `man lightcards` for usage information. ## Input file `contrib/example.md` is an example input file. Lightcards takes the first table from a valid Markdown or HTML file. Each row is a card, and the two columns are the front and back. +## Configuration +An example config file is provided at `/usr/share/doc/lightcards/config.py`, or `~/.local/share/doc/lightcards/config.py` if installed without root privileges. Copy it to `~/.config/lightcards/config.py` or `/etc/lightcards/config.py` and modify the given values. + ## License Copyright Armaan Bhojwani 2021, MIT license diff --git a/config.py b/config.py new file mode 100644 index 0000000..64296bb --- /dev/null +++ b/config.py @@ -0,0 +1,18 @@ +############### +# KEYBINDINGS # +############### + +card_prev = ["h", "KEY_LEFT"] +card_next = ["l", "KEY_RIGHT"] +card_flip = ["j", "k", "KEY_UP", "KEY_DOWN"] +card_star = ["i", "/"] +card_first = ["0", "^", "KEY_HOME"] +card_last = ["$", "KEY_END"] + +help_disp = ["H", "?"] + +menu_disp = "m" + +view_one = "1" +view_two = "2" +view_three = "3" diff --git a/lightcards/config.py b/lightcards/config.py new file mode 100644 index 0000000..56f7261 --- /dev/null +++ b/lightcards/config.py @@ -0,0 +1,44 @@ +# Parse lightcards config file +# Armaan Bhojwani 2021 + +import sys +from pathlib import Path +import os + + +class ConfigException(BaseException): + def __init__(self, message): + self.message = message + print(f"lightcards: {self.message}") + sys.exit(4) + + +def find_file(file): + file = str(file) + local_xdg = f"{os.path.expanduser('~')}/{os.environ.get('XDG_CACHE_HOME')}/lightcards/config.py" + local = f"{os.path.expanduser('~')}/.config/lightcards/config.py" + world = "/etc/lightcards/config.py" + world_default = "/usr/share/doc/lightcards/config.py" + local_default = ( + f"{os.path.expanduser('~')}/.local/share/doc/lightcards/config.py" + ) + + if os.path.exists(file): + return file + elif os.path.exists(local_xdg): + return local_xdg + elif os.path.exists(local): + return local + elif os.path.exists(world): + return world + elif os.path.exists(world_default): + return world_default + elif os.path.exists(local_default): + return local_default + + +def read_file(file): + config = {} + exec(Path(str(find_file(file))).read_text(), {}, config) + + return config diff --git a/lightcards/display.py b/lightcards/display.py index d69bde7..8023ff3 100644 --- a/lightcards/display.py +++ b/lightcards/display.py @@ -9,7 +9,7 @@ import sys import textwrap import time -from . import runner, progress, parse +from . import runner, progress, parse, config def panel_create(x, y): @@ -69,8 +69,8 @@ class Help: self.panel.hide() text = [ - "Welcome to runner. Here are some keybindings", - "to get you started:", + "Welcome to Lightcards. Here are the default", + "keybindings to get you started:", "", "h, left previous card", "l, right next card", @@ -231,12 +231,13 @@ class Menu: class Display: - def __init__(self, stack, headers, obj, view, input_file): + def __init__(self, stack, headers, obj, view, args): self.stack = stack self.headers = headers self.obj = obj self.view = view - self.input_file = input_file + self.input_file = args.inp[0] + self.config = config.read_file(args.config) def run(self, stdscr): """Set important options that require stdscr before starting""" @@ -470,37 +471,41 @@ class Display: key = self.win.getkey() if key == "q": self.leave() - elif key in ["h", "KEY_LEFT"]: + elif key in self.config["card_prev"]: self.obj.back() self.current_card().side = 0 self.disp_card() - elif key in ["l", "KEY_RIGHT"]: + elif key in self.config["card_next"]: if self.obj.index + 1 == len(self.stack): self.menu_obj.disp() else: self.obj.forward(self.stack) self.current_card().side = 0 self.disp_card() - elif key in ["j", "k", "KEY_UP", "KEY_DOWN"] and self.view != 3: + elif key in self.config["card_flip"] and self.view != 3: self.current_card().flip() self.disp_card() - elif key in ["i", "/"]: + elif key in self.config["card_star"]: self.current_card().toggleStar() self.disp_card() - elif key in ["0", "^", "KEY_HOME"]: + elif key in self.config["card_first"]: self.obj.index = 0 self.current_card().side = 0 self.disp_card() - elif key in ["$", "KEY_END"]: + elif key in self.config["card_last"]: self.obj.index = len(self.stack) - 1 self.current_card().side = 0 self.disp_card() - elif key in ["H", "?"]: + elif key in self.config["help_disp"]: self.help_obj.disp() - elif key == "m": + elif key in self.config["menu_disp"]: self.menu_obj.disp() - elif key in ["1", "2", "3", "4"]: - self.view = int(key) + elif key in self.config["view_one"]: + self.view = 1 + elif key in self.config["view_two"]: + self.view = 2 + elif key in self.config["view_three"]: + self.view = 3 def disp_sidebar(self): """Display a sidebar with the starred terms""" diff --git a/lightcards/progress.py b/lightcards/progress.py index 8956b63..97fcf0c 100644 --- a/lightcards/progress.py +++ b/lightcards/progress.py @@ -5,6 +5,7 @@ import hashlib import os import pickle +# TODO: Check for $XDG_CACHE_HOME dired = f"{os.path.expanduser('~')}/.cache/lightcards/" diff --git a/lightcards/runner.py b/lightcards/runner.py index 69d5cbe..6cb87d4 100644 --- a/lightcards/runner.py +++ b/lightcards/runner.py @@ -18,6 +18,12 @@ 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", @@ -95,9 +101,8 @@ def main(args=sys.argv): sys.tracebacklimit = 0 args = parse_args() global headers, stack - input_file = args.inp[0] - (headers, stack) = parse.parse_html(parse.md2html(input_file)) - show(args, stack, headers, input_file) + (headers, stack) = parse.parse_html(parse.md2html(args.inp[0])) + show(args, stack, headers, args) if __name__ == "__main__": diff --git a/man/lightcards.1 b/man/lightcards.1 index d46d287..bf0ffa3 100644 --- a/man/lightcards.1 +++ b/man/lightcards.1 @@ -37,6 +37,9 @@ Shuffle card order .TP \f[B]-V\f[R] [1-3], \f[B]--view\f[R] [1-3] Specify startup view +.TP +\f[B]-c\f[R], \f[B]--config\f[R] +Specify a custom config file .SH KEYS .TP \f[B]l\f[R], \f[B]right\f[R] @@ -85,6 +88,9 @@ Invalid option .TP \f[B]3\f[R] Curses error +.TP +\f[B]4\f[R] +Config error .SH BUGS .PP https://lists.sr.ht/\[ti]armaan/public-inbox diff --git a/man/lightcards.1.md b/man/lightcards.1.md index 8ae4f6c..a5173a7 100644 --- a/man/lightcards.1.md +++ b/man/lightcards.1.md @@ -39,6 +39,9 @@ lightcards \[options\] \[input file\] **-V** \[1-3\], **\--view** \[1-3\] : Specify startup view +**-c**, **\--config** +: Specify a custom config file + # KEYS **l**, **right** : Next card @@ -86,6 +89,9 @@ Lightcards takes the first table from a valid Markdown or HTML file. Each row is **3** : Curses error +**4** +: Config error + # BUGS https://lists.sr.ht/~armaan/public-inbox diff --git a/setup.py b/setup.py index dfd549b..2373f3f 100644 --- a/setup.py +++ b/setup.py @@ -17,7 +17,18 @@ setup( license="MIT", packages=["lightcards"], install_requires=["beautifulsoup4", "markdown"], - data_files=[("man/man1", ["man/lightcards.1"])], + data_files=[ + ("man/man1", ["man/lightcards.1"]), + ( + "share/doc/lightcards/", + [ + "./config.py", + "./README.md", + "man/lightcards.1.md", + "./LICENSE", + ], + ), + ], entry_points={ "console_scripts": ["lightcards=lightcards:main"], }, -- 2.39.2