]> git.armaanb.net Git - lightcards.git/commitdiff
Add preliminary config file support
authorArmaan Bhojwani <me@armaanb.net>
Sat, 13 Feb 2021 16:45:33 +0000 (11:45 -0500)
committerArmaan Bhojwani <me@armaanb.net>
Sat, 13 Feb 2021 16:45:33 +0000 (11:45 -0500)
Still lots of work to be done on this feature, but it's a start!

README.md
config.py [new file with mode: 0644]
lightcards/config.py [new file with mode: 0644]
lightcards/display.py
lightcards/progress.py
lightcards/runner.py
man/lightcards.1
man/lightcards.1.md
setup.py

index 4dd7dc9e62f960bc86207e04e1bb1bbb58b3be2f..9284e1e212a47ea8cb765eee5f515302ca130808 100644 (file)
--- 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 (file)
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 (file)
index 0000000..56f7261
--- /dev/null
@@ -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
index d69bde7ab8926a4b6cbf4d1672e08ba0cb22cab5..8023ff37338fe273f00345cafe0591696fb189b8 100644 (file)
@@ -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"""
index 8956b63ab7043063850bb29adf837eb8f4fb2eb0..97fcf0c9ce68068db1f5f9914dbe49eb42f5e6e7 100644 (file)
@@ -5,6 +5,7 @@ import hashlib
 import os
 import pickle
 
+# TODO: Check for $XDG_CACHE_HOME
 dired = f"{os.path.expanduser('~')}/.cache/lightcards/"
 
 
index 69d5cbe09c1dd029d40e60b5af8a6f1fc8c1027a..6cb87d40d3f36c4db647e955cd3a70455b208044 100644 (file)
@@ -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__":
index d46d287a7dd7b21a8d47241971c32361c2096331..bf0ffa31c9bb23d3c5395db29d30538080db1719 100644 (file)
@@ -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
index 8ae4f6c4636314c952f924bcd156153a6609685e..a5173a71e285b96b2ad17f06083eb476519c0439 100644 (file)
@@ -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
 
index dfd549b3961e2ca4356826311b1449480d0ee3de..2373f3f82dcdcdbb2cc99b0f16cf33a22a1848b3 100644 (file)
--- 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"],
     },