Esempio n. 1
0
 def _log_str(self, *a, **kw):
     sep = kw.get('sep', ' ')
     width = kw.get('width', None)
     string = sep.join(map(str, a))
     if width:
         return '\n'.join(word_wrap(string, width)) + '\n'
     else:
         return string + '\n'
Esempio n. 2
0
 def getmsg(self):
     msg = self._msg
     if self.settings['wrap']:
         msg =  utils.word_wrap(msg)
     return msg
Esempio n. 3
0
async def ux_show_story(msg,
                        title=None,
                        escape=None,
                        sensitive=False,
                        strict_escape=False):
    # show a big long string, and wait for XY to continue
    # - returns character used to get out (X or Y)
    # - can accept other chars to 'escape' as well.
    # - accepts a stream or string
    from glob import dis, numpad
    from display import FontLarge

    lines = []
    if title:
        # kinda weak rendering but it works.
        lines.append('\x01' + title)

    if hasattr(msg, 'readline'):
        msg.seek(0)
        for ln in msg:
            if ln[-1] == '\n':
                ln = ln[:-1]

            if len(ln) > CH_PER_W:
                lines.extend(word_wrap(ln, CH_PER_W))
            else:
                # ok if empty string, just a blank line
                lines.append(ln)

        # no longer needed & rude to our caller, but let's save the memory
        msg.close()
        del msg
        gc.collect()
    else:
        for ln in msg.split('\n'):
            if len(ln) > CH_PER_W:
                lines.extend(word_wrap(ln, CH_PER_W))
            else:
                # ok if empty string, just a blank line
                lines.append(ln)

    # trim blank lines at end, add our own marker
    while not lines[-1]:
        lines = lines[:-1]

    lines.append('EOT')

    #print("story:\n\n\"" + '"\n"'.join(lines))
    #lines[0] = '111111111121234567893'

    top = 0
    H = 5
    ch = None
    pr = PressRelease()
    while 1:
        # redraw
        dis.clear()

        y = 0
        for ln in lines[top:top + H]:
            if ln == 'EOT':
                dis.hline(y + 3)
            elif ln and ln[0] == '\x01':
                dis.text(0, y, ln[1:], FontLarge)
                y += 21
            else:
                dis.text(0, y, ln)

                if sensitive and len(ln) > 3 and ln[2] == ':':
                    dis.mark_sensitive(y, y + 13)

                y += 13

        dis.scroll_bar(top / len(lines))
        dis.show()

        # wait to do something
        ch = await pr.wait()
        if escape and (ch == escape or ch in escape):
            # allow another way out for some usages
            return ch
        elif ch in 'xy':
            if not strict_escape:
                return ch
        elif ch == '0':
            top = 0
        elif ch == '7':  # page up
            top = max(0, top - H)
        elif ch == '9':  # page dn
            top = min(len(lines) - 2, top + H)
        elif ch == '5':  # scroll up
            top = max(0, top - 1)
        elif ch == '8':  # scroll dn
            top = min(len(lines) - 2, top + 1)