]> git.armaanb.net Git - lightcards.git/blobdiff - lightcards/display.py
Set index to 0 before saving if at end of set
[lightcards.git] / lightcards / display.py
index 940d15efadae8e254c3e14a722ed0dd85a075ecd..b4076e57a5e8ac20a6d262c43c2d467d93f75421 100644 (file)
@@ -6,7 +6,7 @@ from random import shuffle
 import sys
 import textwrap
 
-from . import lightcards
+from . import lightcards, progress
 
 
 class Display():
@@ -23,12 +23,19 @@ class Display():
         curses.init_pair(3, curses.COLOR_YELLOW, 0)
         self.get_key()
 
+    def leave(self):
+        if self.obj.getIdx() == len(self.stack):
+            self.obj.setIdx(0)
+
+        progress.dump((self.obj, self.stack, self.headers), self.stack)
+        sys.exit(0)
+
     def disp_bar(self):
         """
         Display the statusbar at the bottom of the screen with progress, star
         status, and card side.
         """
-        (mlines, mcols) = self.win.getmaxyx()
+        (mlines, _) = self.win.getmaxyx()
 
         # Calculate percent done
         if len(self.stack) <= 1:
@@ -58,7 +65,6 @@ class Display():
         self.win.insstr(bar_end, curses.color_pair(1))
 
     def menu_print(self, string, err=False):
-        (mlines, mcols) = self.win.getmaxyx()
         self.win.clear()
         if err:
             color = curses.color_pair(2)
@@ -73,7 +79,7 @@ class Display():
             key = self.win.getkey()
             if key == "q":
                 if len(self.stack) == self.obj.getIdx():
-                    sys.exit(0)
+                    self.leave()
                 elif len(self.stack) < self.obj.getIdx():
                     self.obj.setIdx(0)
                 self.get_key()
@@ -120,12 +126,16 @@ class Display():
                 self.obj.setIdx(0)
                 self.get_key()
 
-    def disp_menu(self, keygrab=True):
+    def disp_menu(self, keygrab=True, quit=False):
         """
         Display a menu once the end of the deck has been reached, offering
         multiple options on how to continue.
         """
 
+        quit_text = "[q]: back"
+        if quit:
+            quit_text = "[q]: quit"
+
         self.win.addstr("LIGHTCARDS MENU", curses.color_pair(1) +
                         curses.A_BOLD)
         self.win.hline(1, 0, curses.ACS_HLINE, 15)
@@ -137,13 +147,13 @@ class Display():
                         "[d]: star all\n" +
                         "[s]: update stack to include starred only\n\n" +
                         "[r]: restart\n" +
-                        "[q]: back")
+                        quit_text)
 
         if keygrab:
             self.menu_grab()
 
     def wrap_width(self):
-        (mlines, mcols) = self.win.getmaxyx()
+        (_, mcols) = self.win.getmaxyx()
         wrap_width = mcols
         if mcols > 80:
             wrap_width = 80
@@ -156,9 +166,9 @@ class Display():
         side.
         """
         self.win.clear()
-        (mlines, mcols) = self.win.getmaxyx()
+        (_, mcols) = self.win.getmaxyx()
         if self.obj.getIdx() == len(self.stack):
-            self.disp_menu()
+            self.disp_menu(quit=True)
         else:
             # If on the back of the card, show the content of the front side in
             # the header
@@ -226,7 +236,7 @@ class Display():
         while True:
             key = self.win.getkey()
             if key == "q":
-                sys.exit(0)
+                self.leave()
             elif key in ["l", "KEY_RIGHT"]:
                 self.obj.forward(self.stack)
                 self.obj.setSide(0)