def update(): global t, mouse_x, mouse_y, mouse_px, mouse_py dx = mouse_x - mouse_px dy = mouse_y - mouse_py l = math.sqrt(dx**2 + dy**2) d = math.atan2(dy, dx) j = min(15, int(l)) 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) particles.append(Particle(mouse_px + dx * f, mouse_py + dy * f, vx, vy)) for i, p in enumerate(particles): t.cursor_to(int(p.x), int(p.y)) t.style(Style.none).write(" ") p.update() if p.x < 0 or p.y < 0 or p.x >= t.w or p.y >= t.h: particles.remove(p) else: t.cursor_to(int(p.x), int(p.y)) t.style(Color.hsl(i / len(particles), 1.0, 0.5)).write("@") t.flush() mouse_px = mouse_x mouse_py = mouse_y
def make_pipes(n): p = [] for i in range(n): x = randint(0, t.w) y = randint(0, t.h) color = Color.hsl(random(), 0.5, 0.5) p.append(Pipe(x, y, color)) return p
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)
def draw_buffer(): global buffer, damage, t for y in range(t.h): for x in range(t.w): i = clip(y, 0, t.h-1) * t.w + clip(x, 0, t.w-1) if damage[i]: t.cursor_to(x, y) v = min(1.0, buffer[i]) chars = ".'`^\",:;Il!i><~+_-?][}{1)(|\\/tfjrxnuvczXYUJCLQ0OZmwqpdbkhao*#MW&8%B@$" ch = chars[int(v * (len(chars) - 1))] # ch = "#" t.style(Color.hsl(0.1, 1.0, v*0.6)) t.write(ch) damage[i] = False
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.")
def update(): for x in range(t.w): for y in range(t.h): t.cursor_to(x, y) dx = (x / t.w - 0.5) / 0.5 dy = (y / t.h - 0.5) / 0.5 a = math.atan2(dy, dx) d = math.sqrt(dx**2 + dy**2) t.style(Color.hsl(a / (math.pi * 2), 1.0, 1 - d).bg()) t.write(" ") t.cursor_to(1, t.h - 2) t.write("Press R to toggle color mode.") t.cursor_to(1, t.h - 3) t.write("Press Q to quit.") t.flush()
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()
def changecol(): colors = (Color.hex(0x0000FF), Color.hex(0xFFFF00), Color.hex(0x7700FF), Color.hex(0xFF0077), Color.hex(0xFF7700)) t.style(random.choice(colors))
t.flush() c = t.getch() if isinstance(c, MouseEvent): if c.down: size = BASE_SIZE * 3 elif c.left or c.right: size = BASE_SIZE * 2 else: size = BASE_SIZE t.style(Style.none) t.clear_box(old_x - MAX_SIZE // 2, old_y - MAX_SIZE // 4, MAX_SIZE, MAX_SIZE // 2) if c.left: t.style(Color.hex(0xFF0000).bg()) elif c.right: t.style(Color.hex(0x00FF00).bg()) else: t.style(Style("reverse")) t.clear_box(c.x - size // 2, c.y - size // 4, size, size // 2) old_x = c.x old_y = c.y t.flush() elif c == "q": break except KeyboardInterrupt: pass finally: t.reset()
from pytermfx import Terminal, Color t = Terminal() t.clear() adaptor_name = type(t.adaptor).__name__ t.print("Terminal (", adaptor_name, ") ", t.w, "x", t.h, sep="") t.style(Color.hex(0xFF51C0)) t.print("Hello world in red!") t.reset()
def resize(): global buffer, damage, t buffer = [0 for i in range(t.w * t.h)] damage = [True for i in range(t.w * t.h)] t.style(Color.hex(0).bg()).clear_box(0, 0, t.w, t.h) t.flush()
from pytermfx import Terminal, Color, Style from pytermfx.tools import TerminalApp from pytermfx.keys import MouseEvent import random import math mouse_x = 0 mouse_y = 0 mouse_px = 0 mouse_py = 0 particles = [] t = Terminal() t.cursor_set_visible(False) t.set_cbreak(True) t.mouse_enable("move") t.style(Color.hex(0)).clear() class Particle: def __init__(self, x, y, vx=0, vy=0): self.x = x self.y = y self.vx = vx self.vy = vy def update(self): self.x += self.vx self.y += self.vy self.vy += 0.1
from pytermfx.keys import MouseEvent from time import clock t = Terminal() t.set_cbreak(True) t.mouse_enable("move") t.cursor_set_visible(False) t.writeln("Left+drag to draw, Right+drag to erase.") t.writeln("Press C to clear, Q to quit.") t.flush() try: while True: c = t.getch() if isinstance(c, MouseEvent): if c.left: t.cursor_to(c.x, c.y) t.style(Color.hsl(clock(), 1, 0.7).bg()) t.write(" ").flush() elif c.right: t.style_reset() t.clear_box(c.x - 3, c.y - 1, 6, 3).flush() elif c == "c": t.style_reset().clear().flush() elif c == "q": break except KeyboardInterrupt: pass finally: t.reset()
from pytermfx import Terminal, Color import sys t = Terminal() i = 0 PERIOD = 500 for line in sys.stdin: for char in line: i += 1 t.style(Color.hsl(i / PERIOD, 1.0, 0.6)) t.write(char) t.flush()
def color(f): return Color.hsl(f / MAX, 1.0, 0.5).bg()