pgup, pgdn support, and returns a book thing
authorJohn Ladan <jladan@uwaterloo.ca>
Sat, 17 Mar 2012 02:24:15 +0000 (22:24 -0400)
committerJohn Ladan <jladan@uwaterloo.ca>
Sat, 17 Mar 2012 02:24:15 +0000 (22:24 -0400)
librarian.py

index 520b5d4..38a41c9 100755 (executable)
@@ -23,7 +23,7 @@ def menu(w, items):
 
     w.refresh()
     ch=w.getch()
-    while (ch!=113 or ch!=27): # leave on q or ESC
+    while (ch!=113 and ch!=27): # leave on q or ESC
         if ch==curses.KEY_UP:
             if highlight!=0:
                 w.chgat(highlight,0, 0)
@@ -38,6 +38,14 @@ def menu(w, items):
                 while(items[highlight][0]==""):
                     highlight +=1
                 w.chgat(highlight,0, curses.A_REVERSE)
+        if ch==curses.KEY_PPAGE:
+            w.chgat(highlight,0, 0)
+            highlight = 0
+            w.chgat(highlight,0, curses.A_REVERSE)
+        if ch==curses.KEY_NPAGE:
+            w.chgat(highlight,0, 0)
+            highlight = len(items)-1
+            w.chgat(highlight,0, curses.A_REVERSE)
         if ch==114 or ch==10:
             (s,f)=items[highlight]
             f()
@@ -68,6 +76,13 @@ def redrawForm(w, caption, items, buttonlabel, m):
     w.addstr(r,x-len(buttonlabel)-len("<cancel>")-6, "<cancel>  <"+buttonlabel+">")
     w.refresh()
 
+def lists_to_dict(l1, l2):
+    book = {}
+    for k,v in zip(l1,l2):
+        if v!="":
+            book[k.lower()]=v
+    return book
+
 
 #the final form for book data entry - takes caption and book info.
 def bookForm(caption, book, buttonlabel):
@@ -84,7 +99,7 @@ def bookForm(caption, book, buttonlabel):
             entries.append("")
     m+=4
 
-    w=curses.newwin(34,50,1,10)
+    w=curses.newwin(34,70,1,10)
     (y,x)=w.getmaxyx()
     w.keypad(1)
     redrawForm(w,caption,zip(labels,entries),buttonlabel,m)
@@ -121,6 +136,15 @@ def bookForm(caption, book, buttonlabel):
                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
                 cursor = len(entries[highlight])
                 w.move(r,m+cursor)
+        elif ch==curses.KEY_PPAGE:
+            w.chgat(r,m,x-m-2,curses.A_NORMAL)
+            highlight=0
+            b=-1
+            r=3+2*highlight
+            w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
+            cursor = len(entries[highlight])
+            w.move(r,m+cursor)
+            curses.curs_set(1)
         elif ch==curses.KEY_DOWN:
             if highlight >= len(labels) -1:
                 highlight = len(labels)
@@ -137,6 +161,15 @@ def bookForm(caption, book, buttonlabel):
                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
                 cursor = len(entries[highlight])
                 w.move(r,m+cursor)
+        elif ch==curses.KEY_NPAGE:
+            if highlight!=len(labels):
+                highlight = len(labels)
+                b += 1
+                b = min(b,1)
+                curses.curs_set(0)
+                w.chgat(r,m,x-m-2,curses.A_NORMAL)
+                r = y-3
+                w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE)
         elif ch==curses.KEY_LEFT:
             if highlight == len(labels):
                 w.chgat(r,bcol[b],bwidth[b],curses.A_NORMAL)
@@ -155,7 +188,7 @@ def bookForm(caption, book, buttonlabel):
                 if cursor < len(entries[highlight]):
                     cursor+=1
                     w.move(r,m+cursor)
-        elif ch>19 and ch<127:
+        elif ch>31 and ch<127:
             if highlight != len(labels):
                 entries[highlight]=entries[highlight][:cursor] + curses.keyname(ch) + entries[highlight][cursor:]
                 cursor+=1
@@ -163,12 +196,26 @@ def bookForm(caption, book, buttonlabel):
                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
                 w.move(r,m+cursor)
         elif ch==curses.KEY_BACKSPACE:
-            if highlight != len(labels):
+            if highlight != len(labels) and cursor!=0:
                 cursor-=1
                 entries[highlight]=entries[highlight][:cursor] + entries[highlight][cursor+1:]
                 w.addnstr(r,m, entries[highlight]+(" "*40), x-m-2)
                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
                 w.move(r,m+cursor)
+        elif ch==curses.KEY_DC:
+            if highlight != len(labels):
+                entries[highlight]=entries[highlight][:cursor] + entries[highlight][cursor+1:]
+                w.addnstr(r,m, entries[highlight]+(" "*40), x-m-2)
+                w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
+                w.move(r,m+cursor)
+        elif ch==curses.KEY_HOME:
+            if highlight != len(labels):
+                cursor=0
+                w.move(r,m+cursor)
+        elif ch==curses.KEY_END:
+            if highlight != len(labels):
+                cursor=len(entries[highlight])
+                w.move(r,m+cursor)
         elif ch==10:
             if b != -1:
                 if b == 0:
@@ -178,7 +225,7 @@ def bookForm(caption, book, buttonlabel):
                 elif b == 1:
                     w.clear()
                     w.refresh()
-                    return {"title": "I was 'added'"}
+                    return lists_to_dict(labels,entries)
             elif highlight == len(labels)-1:
                 highlight = len(labels)
                 b=0
@@ -201,13 +248,12 @@ def bookForm(caption, book, buttonlabel):
     curses.curs_set(0)
     w.clear()
     w.refresh()
-    return {"title":"this is what I returned"}
+    return {}
 
 
 def addForm():
     book = {"title":"A Book of Tests", "pages":"123"}
     book = bookForm("Add a book", book, "add")
-    stdscr.getch()
     bookForm("View the book", book, "done")