Added help bar
[public/library.git] / librarian.py
1 #!/usr/bin/env python
2
3 import curses
4 import dbLayer as db
5 import browser
6 import bookForm
7 import helpBar
8
9 import bookData
10
11
12 stdscr=0
13 hb=0
14
15 menu_commands = [(' q','quit')]
16 browser_commands = [(' u','update'), (' d','delete'), (' q','quit')]
17
18 def menutest(s, l):
19     global stdscr
20     global hb
21     stdscr=s
22     curses.curs_set(0)
23     (rows,cols)=stdscr.getmaxyx()
24     bar = curses.newwin(1,cols-2,rows-1,1)
25     hb = helpBar.helpBar(bar)
26     hb.command=menu_commands
27     hb.refresh()
28     w = curses.newwin(10,40,(rows-10)/2, (cols-40)/2)
29
30     menu(w, l)
31     
32     curses.curs_set(1)
33
34 # item is a list of (string, callable) tuples
35 def menu(w, items):
36     w.keypad(1)
37     highlight=0
38     redrawMenu(w,items,highlight)
39
40     w.refresh()
41     ch=w.getch()
42     while (ch!=113 and ch!=27): # leave on q or ESC
43         if ch==curses.KEY_UP:
44             if highlight!=0:
45                 w.chgat(highlight,0, 0)
46                 highlight -= 1
47                 while(items[highlight][0]==""):
48                     highlight -=1
49                 w.chgat(highlight,0, curses.A_REVERSE)
50         if ch==curses.KEY_DOWN:
51             if highlight!=len(items)-1:
52                 w.chgat(highlight,0, 0)
53                 highlight += 1
54                 while(items[highlight][0]==""):
55                     highlight +=1
56                 w.chgat(highlight,0, curses.A_REVERSE)
57         if ch==curses.KEY_PPAGE:
58             w.chgat(highlight,0, 0)
59             highlight = 0
60             w.chgat(highlight,0, curses.A_REVERSE)
61         if ch==curses.KEY_NPAGE:
62             w.chgat(highlight,0, 0)
63             highlight = len(items)-1
64             w.chgat(highlight,0, curses.A_REVERSE)
65         if ch==114 or ch==10:
66             (s,f)=items[highlight]
67             f()
68             redrawMenu(w,items,highlight)
69         w.refresh()
70         ch = w.getch()
71
72 def redrawMenu(w,items,highlight):
73     i=0
74     for (mitem,fun) in items:
75         w.addstr(i,0, mitem)
76         i +=1
77     w.chgat(highlight, 0, curses.A_REVERSE)
78     w.refresh()
79     hb.commands=menu_commands
80     hb.refresh()
81
82
83 def addForm():
84     w=curses.newwin(1,1)
85     (my,mx)=stdscr.getmaxyx()
86     (r,c)=w.getmaxyx()
87     w.mvwin((my-r)/2,(mx-c)/2)
88     bf = bookForm.bookForm(w)
89     bf.lookup=bookData.openLibrary
90     bf.caption='Add a Book'
91     bf.blabel = 'Add'
92     book = bf.eventLoop()
93     bf.clear()
94     if len(book)!=0:
95         db.addBook(book)
96
97
98 def updateMenu():
99     w=curses.newwin(1,50,10,10)
100     w.addstr("I will be used to update or modify book records")
101     w.refresh()
102
103 def deleteMenu():
104     w=curses.newwin(1,50,10,10)
105     w.addstr("I will be used to delete book records")
106     w.refresh()
107
108 def browseMenu():
109     (my,mx)=stdscr.getmaxyx()
110     w=curses.newwin(20,80,(my-20)/2,(mx-80)/2)
111     b = browser.browserWindow(w)
112     hb.commands=browser_commands
113     hb.refresh()
114     b.startBrowser()
115     b.clear()
116
117
118 m = [("Browse Library", browseMenu),
119      ("Add Book or other item", addForm),
120      ("Modify/Update record", updateMenu),
121      ("Remove book from catalogue", deleteMenu),
122      ("",exit),
123      ("Exit", exit)]
124 curses.wrapper(menutest, m)