]> git.armaanb.net Git - lightcards.git/commitdiff
Handle exceptions and exit codes better
authorArmaan Bhojwani <me@armaanb.net>
Fri, 12 Feb 2021 18:29:24 +0000 (13:29 -0500)
committerArmaan Bhojwani <me@armaanb.net>
Fri, 12 Feb 2021 18:32:48 +0000 (13:32 -0500)
lightcards/display.py
lightcards/parse.py
lightcards/runner.py
man/lightcards.1
man/lightcards.1.md

index 47399828e7361253ba75ea00b28151b178d45e07..8340c272cdc3349bdd30d4d9460108bed3c5217a 100644 (file)
@@ -19,6 +19,13 @@ def panel_create(x, y):
     return (win, panel)
 
 
+class CursesError(BaseException):
+    def __init__(self, message="lightcards: Curses error!"):
+        self.message = message
+        print(self.message)
+        sys.exit(3)
+
+
 class Help:
     def __init__(self, outer, mlines=21, mcols=52):
         """Initialize help screen"""
index 6fc2a759cf2ac152f636b9a376efa9ddf2d9db29..3a1306bfae98840ba312fc58deff160d667ceeb8 100644 (file)
@@ -13,7 +13,9 @@ def md2html(file):
     try:
         return markdown.markdown(open(file, "r").read(), extensions=["tables"])
     except FileNotFoundError:
-        sys.exit(f'lightcards: "{file}": No such file or directory')
+        raise Exception(
+            f'lightcards: "{file}": No such file or directory'
+        ) from None
 
 
 def parse_html(html):
@@ -29,11 +31,11 @@ def parse_html(html):
         for x in soup.find_all("tr"):
             outp.append(Card(tuple([clean_text(y) for y in x.find_all("td")])))
     except AttributeError:
-        sys.exit("lightcards: No table found")
+        raise Exception("lightcards: No table found") from None
 
     ths = soup.find_all("th")
     if len(ths) != 2:
-        sys.exit("lightcards: Headings malformed")
+        raise Exception("lightcards: Headings malformed")
 
     # Return a tuple of nested lists
     return ([clean_text(x) for x in ths], outp[1:])
index 08004db3bd68d4e261fb080c7c777c7ae94b0fd3..e0af7d9e24e00746ca683e49e0537aac67e98f57 100644 (file)
@@ -2,14 +2,14 @@
 # Armaan Bhojwani 2021
 
 import argparse
-from curses import wrapper
+import curses
 import os
 import pkg_resources
 from random import shuffle
 import sys
 
 from . import parse, progress
-from .display import Display
+from .display import Display, CursesError
 from .deck import Status
 
 
@@ -80,7 +80,10 @@ def show(args, stack, headers):
 
     # Send to display
     win = Display(stack, headers, idx, args.view)
-    wrapper(win.run)
+    try:
+        curses.wrapper(win.run)
+    except curses.error as e:
+        raise CursesError() from e
 
 
 def reparse():
@@ -96,6 +99,7 @@ def get_orig():
 
 
 def main(args=sys.argv):
+    sys.tracebacklimit = 0
     args = parse_args()
     global headers, stack
     (headers, stack) = parse.parse_html(parse.md2html(args.inp[0]))
index 47115f410ff3f92d62103ddf94bdeb09955df72b..37b3866eea7b206eb27867d399639ae7bf40d54e 100644 (file)
@@ -82,6 +82,9 @@ Parse error
 .TP
 \f[B]2\f[R]
 Invalid option
+.TP
+\f[B]3\f[R]
+Curses error
 .SH BUGS
 .PP
 https://lists.sr.ht/\[ti]armaan/public-inbox
index eb83e03df2f56e8fe5490c9668206c636887f739..78865eb874333166dfaf26ad5098ecdec331ef82 100644 (file)
@@ -83,6 +83,9 @@ Lightcards takes the first table from a valid Markdown or HTML file. Each row is
 **2**
 : Invalid option
 
+**3**
+: Curses error
+
 # BUGS
 https://lists.sr.ht/~armaan/public-inbox