예제 #1
0
class Text(Widget):
  """ Text canvas. """
  minsize = XY(5, 5)
  stretch = both

  def __init__(self, **kwargs):
    super().__init__(**kwargs)
    self.lines = deque(maxlen=1000)
    self.dirty = 0  # buffer needs to be flushed
    self.timer = Timer(self.draw, None)
    self.timer.start()

  def draw(self, force=True):
    if not force:
      if not self.dirty:
        self.timer.restart(0.3)
      self.dirty += 1
      if self.dirty < 5:
        return
    self.dirty = 0
    self.timer.cancel()

    # wrap long lines
    result = []
    for line in self.lines:
      chunks = splitline(line, self.size.x)
      result.extend(chunks)
    # display only visible lines
    visible = result[-self.size.y:]
    for i, line in enumerate(visible):
      pos = self.pos + XY(0, i)
      # self.canvas.printf(" "*self.size.x, pos)
      self.canvas.printf(line, pos)
      filler = " " * (self.size.x - len(line))
      self.canvas.printf(filler, pos + XY(len(line), 0))

  def println(self, s):
    self.lines.append(str(s))
    self.draw(force=False)

  def write(self, s):
    """ This one is just to emulate file API. """
    self.println(s.strip())

  def clear(self):
    self.lines.clear()
    self.draw()
예제 #2
0
 def __init__(self, **kwargs):
   super().__init__(**kwargs)
   self.lines = deque(maxlen=1000)
   self.dirty = 0  # buffer needs to be flushed
   self.timer = Timer(self.draw, None)
   self.timer.start()