Added help bar
[public/library.git] / librarian.py
index f051507..3672715 100755 (executable)
@@ -1,10 +1,30 @@
 #!/usr/bin/env python
 
 import curses
+import dbLayer as db
+import browser
+import bookForm
+import helpBar
 
-def menutest(stdscr, l):
+import bookData
+
+
+stdscr=0
+hb=0
+
+menu_commands = [(' q','quit')]
+browser_commands = [(' u','update'), (' d','delete'), (' q','quit')]
+
+def menutest(s, l):
+    global stdscr
+    global hb
+    stdscr=s
     curses.curs_set(0)
     (rows,cols)=stdscr.getmaxyx()
+    bar = curses.newwin(1,cols-2,rows-1,1)
+    hb = helpBar.helpBar(bar)
+    hb.command=menu_commands
+    hb.refresh()
     w = curses.newwin(10,40,(rows-10)/2, (cols-40)/2)
 
     menu(w, l)
@@ -15,82 +35,65 @@ def menutest(stdscr, l):
 def menu(w, items):
     w.keypad(1)
     highlight=0
-    for (mitem,fun) in items:
-        w.addstr(highlight,0, mitem)
-        highlight +=1
+    redrawMenu(w,items,highlight)
 
-    highlight=0
-    w.chgat(highlight, 0, curses.A_REVERSE)
     w.refresh()
     ch=w.getch()
-    while (ch!=113): # leave on q
+    while (ch!=113 and ch!=27): # leave on q or ESC
         if ch==curses.KEY_UP:
             if highlight!=0:
                 w.chgat(highlight,0, 0)
                 highlight -= 1
+                while(items[highlight][0]==""):
+                    highlight -=1
                 w.chgat(highlight,0, curses.A_REVERSE)
         if ch==curses.KEY_DOWN:
             if highlight!=len(items)-1:
                 w.chgat(highlight,0, 0)
                 highlight += 1
+                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()
+            redrawMenu(w,items,highlight)
         w.refresh()
         ch = w.getch()
 
-# items is a list of (label, value) pairs
-def drawForm(w, title, items, highlight):
-    w.addstr(1,1,title)
-    r=3;
-    m=0;
-    for l, v in items:
-        m = max(len(l),m)
-    m+=3
-    for l, v in items:
-        c = m-len(l)-2
-        w.addstr(r,c,l+":")
-        w.addstr(r,m,v)
-        r+=2
-    return m
-
-
-def addMenu():
-    formdata = [("ISBN","112733"),
-                ("Title", "Poo"),
-                ("Author","")]
-    w=curses.newwin(10,50,10,10)
-    (y,x)=w.getmaxyx()
-    w.border()
-    w.keypad(1)
-    curses.curs_set(1)
-    highlight=0
-    r=3
-    m = drawForm(w,"Add a Book", formdata, highlight)
-    w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
-    w.move(r,m+len(formdata[highlight][1]))
-    ch = w.getch()
-    while (ch!=113):
-        if ch==curses.KEY_UP:
-            if highlight!=0:
-                w.chgat(r,m,x-m-2,curses.A_NORMAL)
-                highlight -= 1
-                r=3+2*highlight
-                w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
-                w.move(r,m+len(formdata[highlight][1]))
-        if ch==curses.KEY_DOWN:
-            if highlight != len(formdata) -1:
-                w.chgat(r,m,x-m-2,curses.A_NORMAL)
-                highlight += 1
-                r=3+2*highlight
-                w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
-                w.move(r,m+len(formdata[highlight][1]))
-        w.refresh()
-        ch = w.getch()
-
-    curses.curs_set(0)
+def redrawMenu(w,items,highlight):
+    i=0
+    for (mitem,fun) in items:
+        w.addstr(i,0, mitem)
+        i +=1
+    w.chgat(highlight, 0, curses.A_REVERSE)
     w.refresh()
+    hb.commands=menu_commands
+    hb.refresh()
+
+
+def addForm():
+    w=curses.newwin(1,1)
+    (my,mx)=stdscr.getmaxyx()
+    (r,c)=w.getmaxyx()
+    w.mvwin((my-r)/2,(mx-c)/2)
+    bf = bookForm.bookForm(w)
+    bf.lookup=bookData.openLibrary
+    bf.caption='Add a Book'
+    bf.blabel = 'Add'
+    book = bf.eventLoop()
+    bf.clear()
+    if len(book)!=0:
+        db.addBook(book)
+
 
 def updateMenu():
     w=curses.newwin(1,50,10,10)
@@ -103,12 +106,19 @@ def deleteMenu():
     w.refresh()
 
 def browseMenu():
-    w=curses.newwin(1,50,10,10)
-    w.addstr("I will be used to browse book records")
-    w.refresh()
+    (my,mx)=stdscr.getmaxyx()
+    w=curses.newwin(20,80,(my-20)/2,(mx-80)/2)
+    b = browser.browserWindow(w)
+    hb.commands=browser_commands
+    hb.refresh()
+    b.startBrowser()
+    b.clear()
+
 
 m = [("Browse Library", browseMenu),
-     ("Add Book or other item", addMenu),
+     ("Add Book or other item", addForm),
      ("Modify/Update record", updateMenu),
-     ("Remove book from catalogue", deleteMenu)]
+     ("Remove book from catalogue", deleteMenu),
+     ("",exit),
+     ("Exit", exit)]
 curses.wrapper(menutest, m)