[Nym3-commit] r142 - trunk/nym3/Client
jr at komite.net
jr at komite.net
Tue Dec 28 02:43:06 CET 2004
Author: jr
Date: 2004-12-28 02:43:04 +0100 (Tue, 28 Dec 2004)
New Revision: 142
Modified:
trunk/nym3/Client/testcurses.py
Log:
- rewrite the testcurses to give it a more class oriented structure
and make more easy the addition of functionnality
- supports window resize, up, down and quit
Modified: trunk/nym3/Client/testcurses.py
===================================================================
--- trunk/nym3/Client/testcurses.py 2004-12-26 15:17:10 UTC (rev 141)
+++ trunk/nym3/Client/testcurses.py 2004-12-28 01:43:04 UTC (rev 142)
@@ -20,74 +20,153 @@
"Abus de notation."
]
-def draw_line(stdscr, idx, line, maxcol, color):
- stdscr.move(line, 0)
- src = "%s %s" % (month[idx], subj[idx])
- stdscr.addstr(src[0: maxcol], curses.color_pair(color))
- stdscr.clrtoeol()
-def draw_emptyline(stdscr, line):
- stdscr.move(line, 0)
- stdscr.clrtoeol()
+
+class Tab:
+ """the table used to get lines displayed in a scrollingf window"""
+ def size(self):
+ """return the size of the table"""
+ return 0
+
+ def line(self, i):
+ """return the content of the line i"""
+ return ""
+
+class Ex_tab(Tab):
+ """exemple of table"""
+ def size(self):
+ return len(subj)
+
+ def line(self, i):
+ if i < 0 or i >= len(subj):
+ return ""
+ else:
+ return subj[i]
+
+
+class Screen_ctx:
+ """ this class holds all the information relative to the content and the
+ way a screen is drawn"""
+
+ def __init__(self, screen, tab):
+ """initialize the structure from a screen and a displayable list"""
+ self.NORMAL = curses.color_pair(0)
+ self.HIGHLIGHT = curses.color_pair(1)
+ self.STATUS = curses.color_pair(2)
+ # the screen itself
+ self.screen = screen
+ #the table, in fact a class with a method size() and line(i), and
+ #other action based on the lines
+ self.tab = tab
+ (self.maxline, self.maxcol) = screen.getmaxyx()
+ self.index = 0 #index of the active line
+ self.fstindex = 0 #index of the lowest line displayed
+ self.fstpos = 1 #index of the line on the screen where the first
+ #line of the table is displayed
+ self.listrange = max (0, self.maxline - 3)
+ self.action = {
+ ord('q') : self.quit,
+ curses.KEY_DOWN : self.on_K_down,
+ curses.KEY_UP : self.on_K_up,
+ curses.KEY_RESIZE : self.on_resize
+ }
+ self.draw_picture()
+
+ def message(self):
+ return "q:Quitter"
+
+ def status(self):
+ return "NymClient"
+
+ def buffer(self):
+ return "%d %d %d %d %d" % (self.listrange, self.index, self.maxline,
+ self.tab.size(), self.fstindex)
-def draw_picture(stdscr, index, fstindex, listrange, listsize, maxcol):
- stdscr.move(0,0)
- for i in range(0, listrange):
- if fstindex + i < listsize:
- if (fstindex + i == index):
- j = 1
- else:
- j = 0
- draw_line(stdscr, fstindex + i, i, maxcol, j)
- else:
- draw_emptyline(stdscr, i)
- #if maxline >= 12:
- # for i in range(12, maxline):
- # stdscr.addstr("\n");
-
- #print "%i %i\n" % (i, j)
- stdscr.refresh()
+ def draw_picture(self):
+ """Displays the screen"""
+ if self.maxline > 2:
+ self.draw_string(self.message(), 0, self.STATUS)
+ if self.maxline == 1:
+ self.draw_string(self.status(), 0, self.STATUS)
+ else:
+ self.draw_string(self.buffer(), self.maxline - 1, self.NORMAL)
+ self.draw_string(self.status(), self.maxline - 2, self.STATUS)
+ for i in range(0, self.listrange):
+ if self.fstindex + i < self.tab.size():
+ if (self.fstindex + i == self.index):
+ j = self.HIGHLIGHT
+ else:
+ j = self.NORMAL
+ self.draw_line(self.fstindex + i, self.fstpos + i, j)
+ else:
+ self.draw_empty_line(self.fstpos + i)
+ self.screen.refresh()
+ def draw_empty_line(self, i):
+ """draws an empty line at line i of the screen"""
+ self.screen.move(i,0)
+ self.screen.clrtoeol()
+
+ def draw_line(self, idx, pos, color):
+ """draws line idx of tab at the line at the position pos on the screen
+ with color color"""
+ self.screen.move(pos,0)
+ src = self.tab.line(idx)
+ self.screen.addstr(src[0: self.maxcol], color)
+ self.screen.bkgdset(ord(' '), color)
+ self.screen.clrtoeol()
+ self.screen.bkgdset(ord(' '), self.NORMAL)
+
+ def draw_string(self, s, pos, color):
+ """draws string s at the position pos with color color"""
+ self.screen.move(pos,0)
+ self.screen.addstr(s[0: self.maxcol], color)
+ self.screen.bkgdset(ord(' '),color)
+ self.screen.clrtoeol()
+ self.screen.bkgdset(ord(' '),self.NORMAL)
+
+
+ def execute(self):
+ self.active = True
+ while(self.active):
+ c = self.screen.getch()
+ if self.action.has_key(c):
+ self.action[c]()
+ self.draw_picture()
+
+ def quit(self):
+ self.active = False
+
+ def on_K_down(self):
+ if self.index < self.tab.size() - 1:
+ self.index = self.index + 1
+ if (self.index == self.fstindex + self.listrange):
+ self.fstindex = self.index
+ else:
+ curses.beep()
+
+ def on_K_up(self):
+ if self.index > 0:
+ self.index = self.index - 1
+ if (self.index == self.fstindex - 1):
+ self.fstindex = max(0, self.fstindex - self.listrange)
+ else:
+ curses.beep()
+
+ def on_resize(self):
+ (self.maxline, self.maxcol) = self.screen.getmaxyx()
+ self.listrange = max (0, self.maxline - 3)
+ if self.listrange != 0:
+ self.fstindex = (self.index / self.listrange) * self.listrange
+
def func(stdscr):
curses.curs_set(0)
- curses.raw()
#define here our colors pair
#cursor
curses.init_pair(1, curses.COLOR_BLACK, curses.COLOR_CYAN)
#status bar
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLUE)
-
- # init the variables
- (maxline, maxcol) = stdscr.getmaxyx()
- index = 0
- fstindex = 0
- listrange = maxline
- listsize = 12
-
- draw_picture(stdscr, index, fstindex, listrange, listsize, maxcol)
- while 1:
- c = stdscr.getch()
- if c == curses.KEY_DOWN:
- if index < listsize - 1:
- index = index + 1
- if (index == fstindex + listrange):
- fstindex = index
- else:
- curses.beep()
- elif c == curses.KEY_UP:
- if index > 0:
- index = index - 1
- if (index == fstindex - 1):
- fstindex = max(0, fstindex - listrange)
- else:
- curses.beep()
- elif c == ord('q'):
- break
- elif c == curses.KEY_RESIZE:
- (maxline, maxcol) = stdscr.getmaxyx()
- listrange = maxline
- if maxline != 0:
- fstindex = index - index % maxline
- draw_picture(stdscr, index, fstindex, listrange, listsize, maxcol)
+ ctx = Screen_ctx(stdscr, Ex_tab())
+ ctx.execute()
curses.wrapper(func)
More information about the Nym3-commit
mailing list