def __init__(self, scr): self.row = 0 self.col = 0 self._saved = Stack() self.adjust(scr)
class Cursor(object): def __init__(self, scr): self.row = 0 self.col = 0 self._saved = Stack() self.adjust(scr) def __call__(self): return self.row, self.col def __str__(self): return "(%d, %d)"% (self.row, self.col) def adjust(self, scr): self.rows = scr.rows-1 self.cols = scr.cols-1 self.constrain() def constrain(self): self.row = constrain(self.row, 0, self.rows) self.col = constrain(self.col, 0, self.cols) def save(self): self._saved.push((self.row, self.col)) def restore(self): try: self.row, self.col = self._saved.pop() except IndexError: # no prior save self.row, self.col = 0,0 def reset(self, r=0, c=0): self.row = r self.col = c home = reset def set(self, r=None, c=None): if r is not None: self.row = r if c is not None: self.col = c def up(self, count=1): if self.row == 0: return 1 else: self.row = constrain(self.row - count, 0, self.rows) return 0 def down(self, count=1): if self.row == self.rows: return 1 else: self.row = constrain(self.row + count, 0, self.rows) return 0 def back(self, count=1): if self.col == 0: return 1 else: self.col = constrain(self.col - count, 0, self.cols) return 0 def forward(self, count=1): if self.col == self.cols: return 1 else: self.col = constrain(self.col + count, 0, self.cols) return 0