browser has scrolling now
[public/library.git] / browser.py
1 import curses
2 import dbLayer as db
3
4 class browserWindow:
5     hl=0
6     books = []
7     topline = 0
8     columns = [('ID',3),
9                ('Title',30),
10                ('Authors',20),
11                ('ISBN',13)]
12     mx = my = 0
13
14     def __init__(self,window):
15         self.w = window
16         self.updateGeometry()
17         self.refreshBooks()
18         self.w.keypad(1)
19
20     def refreshBooks(self):
21         self.books = db.getBooks()
22
23     def sortByColumn(self, col):
24         self.books.sort() # key=dict.get(col))
25
26     def updateGeometry(self):
27         (self.my,self.mx)=self.w.getmaxyx()
28         self.pageSize = self.my-3
29         # maybe recalculate column widths here.
30
31     def refresh(self):
32         self.displayHeader()
33         for r in range(0,self.pageSize):
34             self.displayRow(r)
35         self.w.refresh()
36         self.highlight()
37
38     def displayHeader(self):
39         cursor = 0
40         for header,width in self.columns:
41             self.w.addnstr(0,cursor,header,width)
42             self.w.addstr(1,cursor,"-"*width)
43             cursor += width+1
44
45     def displayRow(self,row):
46         if self.topline+row < len(self.books):
47             book = self.books[self.topline+row]
48             cursor = 0
49             for k,width in self.columns:
50                 if k.lower() in book:
51                     self.w.addnstr(row+2,cursor,str(book[k.lower()])+" "*width,width)
52                 cursor += width+1
53         else:
54             self.w.addstr(row+2,0," "*self.mx)
55
56     def highlight(self):
57         row = self.hl-self.topline+2
58         if row > 1 and row < self.my:
59             self.w.chgat(row,0,self.mx,curses.A_REVERSE)
60
61     def unHighlight(self):
62         row = self.hl-self.topline+2
63         if row > 1 and row < self.my:
64             self.w.chgat(row,0,self.mx,curses.A_NORMAL)
65
66     def mvHighlight(self,delta):
67         new = self.hl+delta
68         new = max(new,0)
69         new = min(new,len(self.books)-1)
70         self.unHighlight()
71         self.hl = new
72         self.highlight()
73     
74     def scroll(self,delta):
75         self.unHighlight()
76         self.topline += delta
77         self.topline = max(self.topline,0)
78         self.topline = min(self.topline,len(self.books)-1)
79         self.refresh()
80
81     def startBrowser(self):
82         self.refresh()
83
84         ch = self.w.getch()
85         while ch != 27 and ch != 113:
86             if ch == curses.KEY_UP:
87                 if self.hl == self.topline:
88                     self.scroll(-self.pageSize/2)
89                 self.mvHighlight(-1)
90             elif ch == curses.KEY_DOWN:
91                 if self.hl == self.topline+self.pageSize-1:
92                     self.scroll(+self.pageSize/2)
93                 self.mvHighlight(+1)
94             elif ch == curses.KEY_PPAGE:
95                 self.scroll(-self.pageSize)
96                 self.mvHighlight(-self.pageSize)
97             elif ch == curses.KEY_NPAGE:
98                 self.scroll(+self.pageSize)
99                 self.mvHighlight(+self.pageSize)
100
101             self.w.refresh()
102             ch = self.w.getch()
103
104
105
106
107
108