left and right keys implemented in the form
[public/library.git] / librarian.py
1 #!/usr/bin/env python
2
3 import curses
4
5 stdscr=0
6
7 def menutest(s, l):
8     global stdscr
9     stdscr=s
10     curses.curs_set(0)
11     (rows,cols)=stdscr.getmaxyx()
12     w = curses.newwin(10,40,(rows-10)/2, (cols-40)/2)
13
14     menu(w, l)
15     
16     curses.curs_set(1)
17
18 # item is a list of (string, callable) tuples
19 def menu(w, items):
20     w.keypad(1)
21     highlight=0
22     redrawMenu(w,items,highlight)
23
24     w.refresh()
25     ch=w.getch()
26     while (ch!=113): # leave on q
27         if ch==curses.KEY_UP:
28             if highlight!=0:
29                 w.chgat(highlight,0, 0)
30                 highlight -= 1
31                 while(items[highlight][0]==""):
32                     highlight -=1
33                 w.chgat(highlight,0, curses.A_REVERSE)
34         if ch==curses.KEY_DOWN:
35             if highlight!=len(items)-1:
36                 w.chgat(highlight,0, 0)
37                 highlight += 1
38                 while(items[highlight][0]==""):
39                     highlight +=1
40                 w.chgat(highlight,0, curses.A_REVERSE)
41         if ch==114 or ch==10:
42             (s,f)=items[highlight]
43             f()
44             redrawMenu(w,items,highlight)
45         w.refresh()
46         ch = w.getch()
47
48 def redrawMenu(w,items,highlight):
49     i=0
50     for (mitem,fun) in items:
51         w.addstr(i,0, mitem)
52         i +=1
53     w.chgat(highlight, 0, curses.A_REVERSE)
54     w.refresh()
55
56 # items is a list of (label, value) pairs
57 def redrawForm(w, caption, items, buttonlabel, m):
58     (y,x)=w.getmaxyx()
59     w.border()
60     curses.curs_set(1)
61     w.addstr(1,1,caption)
62     r=3
63     for l,v in items:
64         c = m-len(l)-2
65         w.addstr(r,c,l+":")
66         w.addstr(r,m,v)
67         r+=2
68     w.addstr(r,x-len(buttonlabel)-len("<cancel>")-6, "<cancel>  <"+buttonlabel+">")
69     w.refresh()
70
71
72 #the final form for book data entry - takes caption and book info.
73 def bookForm(caption, book, buttonlabel):
74     labels = ["ISBN", "LCCN", "Title", "Subtitle", "Authors", "Edition",
75               "Publisher", "Publish Date", "Publish Year", "Publish Month", "Publish location",
76               "Pages", "Pagination", "Weight"]
77     entries = []
78     m = 0
79     for l in labels:
80         m = max(len(l),m)
81         if l.lower() in book:
82             entries.append(book[l.lower()])
83         else:
84             entries.append("")
85     m+=4
86
87     w=curses.newwin(34,50,1,10)
88     (y,x)=w.getmaxyx()
89     w.keypad(1)
90     redrawForm(w,caption,zip(labels,entries),buttonlabel,m)
91     bcol = [x-len(buttonlabel)-len("<cancel>")-6, x-len(buttonlabel)-4]
92     bwidth = [8,len(buttonlabel)+2]
93     
94     highlight=0
95     b = -1
96     r=3
97     w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
98     w.move(r,m+len(entries[highlight]))
99     cursor = len(entries[highlight])
100     ch = w.getch()
101     while (ch!=113):
102         if ch==curses.KEY_UP:
103             if highlight == len(labels):
104                 w.chgat(r,bcol[b],bwidth[b],curses.A_NORMAL)
105                 highlight = len(labels)-1
106                 b = -1
107                 r=3+2*highlight
108                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
109                 cursor = len(entries[highlight])
110                 w.move(r,m+cursor)
111                 curses.curs_set(1)
112             elif highlight!=0:
113                 w.chgat(r,m,x-m-2,curses.A_NORMAL)
114                 highlight -= 1
115                 r=3+2*highlight
116                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
117                 cursor = len(entries[highlight])
118                 w.move(r,m+cursor)
119         elif ch==curses.KEY_DOWN:
120             if highlight >= len(labels) -1:
121                 highlight = len(labels)
122                 b += 1
123                 b = min(b,1)
124                 curses.curs_set(0)
125                 w.chgat(r,m,x-m-2,curses.A_NORMAL)
126                 r = y-3
127                 w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE)
128             else:
129                 w.chgat(r,m,x-m-2,curses.A_NORMAL)
130                 highlight += 1
131                 r=3+2*highlight
132                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
133                 cursor = len(entries[highlight])
134                 w.move(r,m+cursor)
135         elif ch==curses.KEY_LEFT:
136             if highlight == len(labels):
137                 w.chgat(r,bcol[b],bwidth[b],curses.A_NORMAL)
138                 b=0
139                 w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE)
140             else:
141                 if cursor>0:
142                     cursor-=1
143                     w.move(r,m+cursor)
144         elif ch==curses.KEY_RIGHT:
145             if highlight == len(labels):
146                 w.chgat(r,bcol[b],bwidth[b],curses.A_NORMAL)
147                 b=1
148                 w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE)
149             else:
150                 if cursor < len(entries[highlight]):
151                     cursor+=1
152                     w.move(r,m+cursor)
153         elif ch==10:
154             if b != -1:
155                 if b == 0:
156                     w.clear()
157                     w.refresh()
158                     return {}
159                 elif b == 1:
160                     w.clear()
161                     w.refresh()
162                     return {"title": "I was 'added'"}
163             elif highlight == len(labels)-1:
164                 highlight = len(labels)
165                 b=0
166                 curses.curs_set(0)
167                 w.chgat(r,m,x-m-2,curses.A_NORMAL)
168                 r = y-3
169                 w.chgat(r,bcol[b],bwidth[b],curses.A_REVERSE)
170             else:
171                 w.chgat(r,m,x-m-2,curses.A_NORMAL)
172                 highlight += 1
173                 r=3+2*highlight
174                 w.chgat(r,m,x-m-2,curses.A_UNDERLINE)
175                 cursor = len(entries[highlight])
176                 w.move(r,m+cursor)
177
178
179         w.refresh()
180         ch = w.getch()
181
182     curses.curs_set(0)
183     w.clear()
184     w.refresh()
185     return {"title":"this is what I returned"}
186
187
188 def addForm():
189     book = {"title":"A Book of Tests", "pages":"123"}
190     book = bookForm("Add a book", book, "add")
191     stdscr.getch()
192     bookForm("View the book", book, "done")
193
194
195 def updateMenu():
196     w=curses.newwin(1,50,10,10)
197     w.addstr("I will be used to update or modify book records")
198     w.refresh()
199
200 def deleteMenu():
201     w=curses.newwin(1,50,10,10)
202     w.addstr("I will be used to delete book records")
203     w.refresh()
204
205 def browseMenu():
206     w=curses.newwin(1,50,10,10)
207     w.addstr("I will be used to browse book records")
208     w.refresh()
209
210 m = [("Browse Library", browseMenu),
211      ("Add Book or other item", addForm),
212      ("Modify/Update record", updateMenu),
213      ("Remove book from catalogue", deleteMenu),
214      ("",exit),
215      ("Exit", exit)]
216 curses.wrapper(menutest, m)