Exemplo n.º 1
0
def main():
    t = Terminal()

    def update(s):
        t.style_reset()
        t.clear_line()
        fmt = ""
        try:
            col, fmt = parse_color(s)
            t.style(Color.rgb(col[0], col[1], col[2]).bg())
            t.write(" " * SWATCH_WIDTH)
            t.style_reset()
            t.write(" ")
        except ColorParseError:
            t.write(">" * SWATCH_WIDTH, " ")
        finally:
            t.cursor_save()
            t.style(Color.hex(0x909090))
            t.cursor_move(0, 1).clear_to_end().write(fmt)
            t.cursor_restore().style_reset()
            t.write(s)

    with t.managed():
        t.set_cbreak(True)

        # make space
        t.writeln()
        t.cursor_move(0, -1).flush()

        try:
            # read and parse color
            col_str = read_line(t, update)
            col, fmt = parse_color(col_str)

            t.writeln().writeln()

            # write converted colors
            formats = (("RGBA", format_rgba(col)), ("RGBA (hex)",
                                                    format_hex_rgba(col)),
                       ("RGB (hex)", format_hex_rgb(col)))
            format_name_width = max((len(f[0]) for f in formats)) + 2
            for name, value in formats:
                padding = format_name_width - (len(name) + 1)
                t.style(Color.hex(0x909090)).write(name, ":", " " * padding)
                t.style_reset().writeln(value)
            t.flush()
        except ColorParseError:
            t.writeln().writeln()
            t.writeln("Not a color.")
Exemplo n.º 2
0
from pytermfx import Terminal
import pytermfx.md as md
import sys

t = Terminal()

md.render(t, sys.stdin.readlines())
t.flush()
Exemplo n.º 3
0
def main():
    t = Terminal()
    t.cursor_set_visible(False)

    # the beautiful art
    art = [
        "DDDDDDDDDVVVVVV        VVVVDDDDDDDDDD   ",
        "DDDDDDDDDDDVVVVV      VVVVVDDDDDDDDDDDD ",
        "        DDDD VVVV    VVVV           DDDD",
        "DDD      DDD  VVVV  VVVV    DDD      DDD",
        "DDD     DDDD   VVVVVVVV     DDD     DDDD",
        "DDDDDDDDDDD     VVVVVV      DDDDDDDDDDD ",
        "DDDDDDDDD        VVVV       DDDDDDDDD   ",
        "                  VV                    "
    ]
    aw = len(art[0])
    ah = len(art)
    pos = [t.w / 2, t.h / 2]
    vel = [1.12 / 2, 0.67 / 2]

    t.style(Color.hex(0x0000FF))
    t.style(Color.hex(0x000000).bg())
    t.style(Style("bold"))
    t.flush()

    def changecol():
        colors = (Color.hex(0x0000FF), Color.hex(0xFFFF00),
                  Color.hex(0x7700FF), Color.hex(0xFF0077),
                  Color.hex(0xFF7700))
        t.style(random.choice(colors))

    def update():
        t.clear_box(pos[0] - aw / 2, pos[1] - ah / 2, aw, ah)

        # integrate velocity
        pos[0] += vel[0]
        pos[1] += vel[1]

        # ugly bounce physics
        if pos[0] < aw / 2:
            pos[0] = aw / 2
            vel[0] = -vel[0]
            changecol()
        if pos[1] < ah / 2 + 1:
            pos[1] = ah / 2 + 1
            vel[1] = -vel[1]
            changecol()
        if pos[0] > t.w - 1 - aw / 2:
            pos[0] = t.w - 1 - aw / 2
            vel[0] = -vel[0]
            changecol()
        if pos[1] > t.h - 1 - ah / 2:
            pos[1] = t.h - 1 - ah / 2
            vel[1] = -vel[1]
            changecol()

        # update console
        draw_art(t, art, pos[0], pos[1])
        t.flush()

    def on_input(c):
        if c == "q":
            app.stop()

    app = TerminalApp(t, 60, update=update, on_input=on_input)
    app.start()