Example #1
0
def main():
    app = App(framerate=float("inf"))
    app.t0 = perf_counter()
    app.frames = 0
    app.pos = 0

    @app.on("frame")
    def on_frame():
        app.pos = (app.pos + 1) % (app.screen.w * app.screen.h)
        x = app.pos % app.screen.w
        y = app.pos // app.screen.w
        app.screen.clear()
        app.screen.print(" " * 5, x, y, bg=AQUA)
        app.screen.update()
        app.frames += 1
        app.t1 = perf_counter()

    @app.on("after_stop")
    def on_after_stop():
        tpf = (app.t1 - app.t0) / app.frames
        print("Terminal: {} ({}x{})\n".format(app.backend.terminal_name,
                                              app.screen.w, app.screen.h))
        print("Avg time per frame: {:.4f}\n".format(tpf))
        print("Avg framerate: {:.2f}\n".format(1 / tpf))

    app.start()
    app.await_stop()
Example #2
0
def main():
    app = App(mouse=True)

    @app.on("start")
    def on_start():
        app.mouse = None
        app.key = None
        app.dirty = False
        redraw(app)

    @app.on("resize")
    def on_resize():
        redraw(app)

    @app.on("key")
    def on_key(k):
        app.key = k
        app.dirty = True

    @app.on("mouse")
    def on_mouse(m):
        app.mouse = m
        app.dirty = True

    @app.on("frame")
    def on_frame():
        if app.dirty:
            redraw(app)
            app.dirty = False

    app.start()
    app.await_stop()
Example #3
0
def main():
    app = App(mouse=True)
    frame_num = 0
    size_text = ""

    @app.on("start")
    def start():
        app.screen.show_cursor = True
        app.screen.print("App start, waiting 1 second.", 0, 0)
        app.screen.update()
        sleep(1)
    
    @app.on("frame")
    def frame():
        nonlocal frame_num
        app.screen.clear()
        app.screen.print(f"Frame: {frame_num}", 0, 0)
        app.screen.print("Press 'q' to quit.", 0, 1)
        app.screen.print(size_text, 0, 2)
        app.screen.update()
        frame_num += 1

    @app.on("resize")
    def resize():
        nonlocal size_text
        size_text = f"Resized to {app.screen.w}x{app.screen.h}"

    @app.on("key")
    def key(k):
        if k == "q":
            app.stop()
    
    @app.on("mouse")
    def mouse(m):
        app.screen.cursor_pos = (m.x, m.y)
    
    @app.on("before_stop")
    def before_stop():
        # Terminal is not reset; we can use the screen.
        app.screen.clear()
        app.screen.print("Before stop", 0, 0)
        app.screen.update()
        sleep(0.5)
    
    @app.on("after_stop")
    def after_stop():
        # Terminal is reset; we can print normally.
        print("After stop")
    
    # the following two method calls are equivalent to calling app.run()
    app.start() # this does not block
    # you could run code concurrently with the App here, but you should not
    # interact with its attributes outside of event listeners due to potential
    # concurrency issues.
    app.await_stop() # this blocks
    print("Exited")
Example #4
0
def main():
    app = App()

    @app.on("key")
    def on_key(k):
        app.screen.clear()
        app.screen.print(repr(k), 1, 1)
        app.screen.update()

    app.start()
    app.await_stop()
Example #5
0
def main():
    app = App()

    @app.on("frame")
    def on_frame():
        app.screen.print("Goodbye", 0,
                         0)  # this should be overwritten completely
        app.screen.print("こんにちは世界", 0, 0)  # double-width characters
        app.screen.print(
            " (Hello World)")  # this should follow the previous printout
        app.screen.update()

    app.start()
    app.await_stop()
Example #6
0
def main():
    app = App()

    @app.on("frame")
    def on_frame():
        clock_time = time.strftime("%H:%M:%S", time.gmtime())
        app.screen.print("Clock: " + clock_time, 1, 1)
        try:
            app.backend.window_title = clock_time
        except:
            app.screen.print("Terminal does not support setting window title", 1, 2, fg=Color.rgb(1,0,0))
        app.screen.update()
    
    app.start()
    app.await_stop()
Example #7
0
def main():
    app = App(mouse=True, framerate=60)
    app.mouse_x = 0
    app.mouse_y = 0
    app.mouse_px = 0
    app.mouse_py = 0
    app.particles = []

    @app.on("mouse")
    def on_mouse(m):
        app.mouse_x = m.x
        app.mouse_y = m.y

    @app.on("frame")
    def on_frame():
        dx = app.mouse_x - app.mouse_px
        dy = app.mouse_y - app.mouse_py
        l = math.sqrt(dx**2 + dy**2)
        d = math.atan2(dy, dx)

        j = min(45, int(l)) * 2
        for i in range(j):
            f = i / j
            ll = l * random.uniform(0.25, 0.5)
            dd = d + random.uniform(-0.2, 0.2)
            vx = ll * math.cos(dd)
            vy = ll * math.sin(dd)
            app.particles.append(
                Particle(app.mouse_px + dx * f, app.mouse_py + dy * f, vx, vy))

        app.screen.clear()
        for i, p in enumerate(app.particles):
            col = Color.hsl(i / len(app.particles), 1.0, 0.5)
            app.screen.print(" ", int(p.x), int(p.y), bg=col)
            p.update()
            if p.x < 0 or p.y < 0 or p.x >= app.screen.w or p.y >= app.screen.h:
                app.particles.remove(p)
        app.screen.update()

        app.mouse_px = app.mouse_x
        app.mouse_py = app.mouse_y

    app.start()
    app.await_stop()
Example #8
0
def main():
    app = App(mouse=True)

    in_buffer = []

    @app.input.on("raw_input")
    def on_raw(data):
        in_buffer.append(data)

    def print_escape(line, x, y):
        app.screen.print_pos = (x, y)
        for ch in line:
            fg = COL_FG
            bg = None

            cat = category(ch)
            if "C" in cat:
                fg = COL_CTRL
            elif "N" in cat:
                fg = COL_NUM
            elif "P" in cat:
                fg = COL_PUNC
            elif "Z" in cat:
                bg = COL_SEP
            app.screen.print(repr(ch).lstrip("'").rstrip("'"), fg=fg, bg=bg)

    @app.on("frame")
    @app.on("resize")
    def update():
        max_lines = app.screen.h
        n_lines = min(max_lines, len(in_buffer))
        lines = in_buffer[-n_lines:]

        app.screen.clear()
        for i, line in enumerate(lines):
            print_escape(line, 0, i)

        app.screen.update()

    app.start()
    app.await_stop()
Example #9
0
def main():
    app = App()

    @app.on("start")
    def on_start():
        app.screen.print("Resize the terminal.", 1, 1, fg=Color.rgb(0, 1, 0))
        app.screen.update()

    @app.on("resize")
    def on_resize():
        app.screen.clear()
        app.screen.fill(0, 0, app.screen.w, app.screen.h, bg=Color(0, 255, 0))
        app.screen.fill(1,
                        1,
                        app.screen.w - 2,
                        app.screen.h - 2,
                        bg=Color(0, 0, 0))
        app.screen.print("{} x {}".format(app.screen.w, app.screen.h), 2, 2)
        app.screen.update()

    app.start()
    app.await_stop()
Example #10
0
def main():
    app = App(framerate=60)

    @app.on("start")
    def on_start():
        app.buffer = Buffer(16, 4)
        app.buffer.clear(bg=Color.rgb(0.2, 0, 0), fg=Color.rgb(1, 0.5, 0.5))
        app.buffer.print("Hello world")

    @app.on("frame")
    def on_frame():
        t = time()
        app.screen.clear()
        app.screen.blit(
            app.buffer,
            round(app.screen.w / 2 + sin(t * 3) * 16) - app.buffer.w // 2,
            round(app.screen.h / 2 + cos(t * 1) * 4) - app.buffer.h // 2)
        app.screen.print(
            "Update time: {:.2f}ms".format(app.screen._update_duration * 1000),
            0, 0)
        app.screen.update()

    app.start()
    app.await_stop()
Example #11
0
def main():
    app = App(mouse=True, framerate=60)
    app.screen.show_cursor = True

    w = 0
    h = 0
    buf = None
    c_x = 0
    c_y = 0
    show_help = True

    @app.on("start")
    @app.on("resize")
    def make_buffers():
        nonlocal w, h, buf
        w = app.screen.w
        h = app.screen.h * 2
        buf = [P(**props["air"]) for y in range(w) for x in range(h)]

    @app.on("frame")
    def on_frame():
        app.screen.clear()
        app.screen.cursor_pos = (c_x, c_y)
        P.update(buf, w, h)
        P.draw(app.screen, buf, w, h)
        if show_help:
            app.screen.print(helptext, 1, 1, fg=Color.rgb(1, 1, 1))
        app.screen.update()

    def interact(place):
        nonlocal show_help
        show_help = False

        x = c_x
        y = c_y * 2
        if x >= 0 and x < w and y >= 0 and y < h:
            if place:
                buf[y * w + x] = P(color=Color.hsl(perf_counter() * 0.25, 1,
                                                   0.5),
                                   **props["sand"])
            else:
                buf[y * w + x] = P(**props["air"])

    @app.on("mouse")
    def on_mouse(m):
        nonlocal c_x, c_y

        c_x = m.x
        c_y = m.y

        if m.left:
            interact(True)
        if m.right:
            interact(False)

    @app.on("key")
    def on_key(k):
        nonlocal c_x, c_y

        if k == "left":
            c_x = max(0, c_x - 1)
        if k == "right":
            c_x = min(w - 1, c_x + 1)
        if k == "up":
            c_y = max(0, c_y - 1)
        if k == "down":
            c_y = min(w - 1, c_y + 1)
        if k == "z":
            interact(True)
        if k == "x":
            interact(False)

    app.start()
    app.await_stop()
Example #12
0
def main():
    app = App()
    inner_buffer = Buffer(0, 0)

    outer_pad = 1
    inner_w = 0
    inner_h = 0

    @app.on("start")
    @app.on("resize")
    @app.on("frame")
    def update():
        nonlocal inner_w, inner_h
        app.screen.clear()
        inner_buffer.clear()

        # compute inner dimensions of box
        inner_w = app.screen.w - outer_pad * 2 - 2
        inner_h = app.screen.h - outer_pad * 2 - 2
        inner_buffer.resize(inner_w, inner_h)

        # draw a box with a title
        draw_box(app.screen, outer_pad, outer_pad, inner_w + 2, inner_h + 2, chars=BOX_CHARS_LIGHT_DOUBLE_TOP)
        title = "Hello drawing"
        app.screen.print(title, app.screen.w // 2 - len(title) // 2, outer_pad)

        # draw spinners
        inner_buffer.print("Inner header ", 0, 0)
        draw_spinner(inner_buffer, *inner_buffer.print_pos, freq=2, fg=GREEN)
        inner_buffer.print(" ")
        draw_spinner(inner_buffer, *inner_buffer.print_pos, freq=2, fg=GREY, frames=SPINNER_PIPE)
        inner_buffer.print(" ")
        draw_spinner(inner_buffer, *inner_buffer.print_pos, freq=2, fg=GREY, frames=SPINNER_CLOCK)
        inner_buffer.print(" ")
        draw_spinner(inner_buffer, *inner_buffer.print_pos, freq=2, t=math.sin(time.perf_counter()), fg=YELLOW, frames=SPINNER_MOON)
        inner_buffer.print(" ")
        draw_spinner(inner_buffer, *inner_buffer.print_pos, freq=1, fg=WHITE, frames=SPINNER_DOTS)

        # draw a horizontal line along x=2 within the box
        draw_hline(inner_buffer, 2, fg=GREY)

        # print some word-wrapped text inside the box
        inner_buffer.print(wrap_text(LIPSUM, inner_w), 0, 3, fg=YELLOW)

        # progress bar
        draw_progress(inner_buffer, 0, inner_buffer.print_pos[1] + 2,
                      w=inner_buffer.w // 3,
                      progress=math.sin(time.perf_counter()) * 0.5 + 0.5,
                      fg=BRIGHT_YELLOW,
                      bg=GREY,
                      **PROGRESS_SMOOTH)

        # draw a color bitmap at 2x vertical resolution
        draw_colormap_2x(inner_buffer, SMILEY, 2, inner_buffer.h - 3 - 2, w=7, h=7)

        # copy box contents to screen
        app.screen.blit(inner_buffer, outer_pad + 1, outer_pad + 1)
        app.screen.update()
    
    app.start()
    app.await_stop()