def main(): """Example and test for multi-threaded terminal output """ with keyboard(), Screen() as scr: scr.context.color = 1, 1, 1 scr.draw.line((0, 0), (100, 0)) threads = [] with scr.context(color=(1, 0, 0)) as ctx: for i, color in enumerate( [(0.1, 0, 0), (0.1, 0.05, 0), (0, 0.1, 0), (0, 0.1, 0.1), (0.1, 0, 0.1)] ): threads.append( threading.Thread(target=worker, args=(scr, i * 20, color, 5)) ) threads[-1].start() time.sleep(1 / 10) for t in threads: t.join() scr.draw.line((0, 13), (100, 13)) while True: key = inkey() if key == "\x1b": break time.sleep(1 / 30)
def main(): """Example for drawing straight lines with the API, using the 1/4 block resolution. """ with keyboard(), Screen() as scr: test_lines(scr.high) while True: if inkey() == "\x1b": break
def main(): """Drawing API example for plotting a Bezier-curve """ with Screen() as scr: scr.high.draw.bezier((0, 0), (0, 40), (100, 40), (100, 10)) #scr.high.draw.bezier( #(100,10), (100, 0), (150, 0), (150, 40) #) scr.high.draw.bezier((100, 10), (125, 0), (125, 50), (150, 40)) pause()
def main(text, position, size, color, clear, font): """Terminedia example for rendering large text characters """ position = literal_eval(f"({position})") color = literal_eval(f"({color})") if color.count(",") >= 2 else getattr( terminedia.values, color) with Screen(clear_screen=clear) as sc: sc.context.color = color sc.context.font = font sc.text[size].at(position, text) pause()
def main(image_paths, output=""): """Displays an image, given in a path, on the terminal. """ # TODO add more options to control the output, # including disabling auto-scaling. if not image_paths: image_paths = (default_image, ) scr = Screen() if output: output_file = open(output, "wt", encoding="utf-8") else: scr.clear() for img_path in image_paths: img = shape(img_path, screen=scr) if output: img.render(output=output_file) output_file.write("\n") else: with scr.commands: scr.draw.blit((0, 0), img) pause()
def main(image_paths): """Displays an image, given in a path, on the terminal. """ # TODO add more options to control the output, # including disabling auto-scaling. if not image_paths: image_paths = (default_image, ) with Screen() as scr: for img_path in image_paths: img = shape(img_path) with scr.commands: scr.draw.blit((0, 0), img) pause()
def main(shape, high=False, braille=False): """Quick example to navigate an string-defined shape through the terminal using the arrow keys! Press <ESC> to exit. Usage example: $ terminedia-shapes --custom=" \\n *** \\n * * \\n *** " """ # Work around apparent bug in click: if shape is None: shape = shape1 if "\\n" in shape: shape = shape.replace("\\n", "\n") shape = shape.rstrip("\n") size_ = V2((shape.find("\n") if "\n" in shape else len(shape)), shape.count("\n") + 1) factor = 1 with realtime_keyb(), Screen(clear_screen=False) as scr: parent_scr = scr if high: scr = scr.high factor = 2 elif braille: scr = scr.braille factor = 1 x = scr.get_size()[0] // 2 - 6 y = 0 pos = V2(x, y) old_pos = pos while True: key = inkey() if key in (K.ESC, "q"): break with parent_scr.commands: scr.draw.rect(pos, rel=size_ + (1, 1), erase=True) pos += (factor * ((key == K.RIGHT) - (key == K.LEFT)), factor * ((key == K.DOWN) - (key == K.UP))) scr.draw.blit( pos, shape, **({ "color_map": c_map } if shape == shape2 else {})) time.sleep(1 / 30)
def main(shape, high=False): """Quick example to navigate an string-defined shape through the terminal using the arrow keys! Press <ESC> to exit. Usage example: $ terminedia-shapes --custom=" \\n *** \\n * * \\n *** " """ # Work around apparent bug in click: if shape is None: shape = shape1 if "\\n" in shape: shape = shape.replace("\\n", "\n") size_ = (shape.find("\n") if "\n" in shape else 1), shape.count("\n") #size_ = 13, 7 factor = 1 #if shape == shape2: # size_ = 21, 12 with realtime_keyb(), Screen(clear_screen=False) as scr: parent_scr = scr if high: scr = scr.high factor = 2 x = scr.get_size()[0] // 2 - 6 y = 0 while True: key = inkey() if key in (K.ESC, "q"): break with parent_scr.commands: scr.draw.rect((x, y), rel=size_, erase=True) x += factor * ((key == K.RIGHT) - (key == K.LEFT)) y += factor * ((key == K.DOWN) - (key == K.UP)) scr.draw.blit((x, y), shape, **({ "color_map": c_map } if shape == shape2 else {})) time.sleep(1 / 30)
def main(text, position, size, color, clear, font): """Terminedia example for rendering large text characters """ position = literal_eval(f"({position})") if color == "default": color = DEFAULT_FG else: color = Color( literal_eval(f"({color})") if color.count(",") >= 2 else color) if size.isdigit(): size = int(size) elif size == "square": size = (8, 4) with Screen(clear_screen=clear) as sc: sc.context.color = color sc.context.font = font sc.text[size].at(position, text) pause()
def main(image_paths, size=None, output="", backend="", resolution=""): """Displays an image, given in a path, on the terminal. """ # TODO add more options to control the output, # including disabling auto-scaling. if not image_paths: image_paths = (default_image, ) context = scr = Screen(backend=backend) if not size: size = size_in_pixels(scr.size, resolution=resolution) else: size = V2(int(comp) for comp in size.lower().split("x")) if output: output_file = open(output, "wt", encoding="utf-8") context = DummyCtx() with context: for img_path in image_paths: if not resolution: img = shape(img_path, size=size) elif resolution == "square": img = shape(img_path, size=size, promote=True, resolution=resolution) else: # For finer than half-block, threshold image prior to rendering preliminar_img = shape(img_path, size=size, promote=True) img = shape(size_in_blocks(size, resolution)) preliminar_img.context.transformers.append( ThresholdTransformer(invert=False)) getattr(img, resolution).draw.blit((0, 0), preliminar_img) if output: img.render(output=output_file, backend=backend) output_file.write("\n") else: scr.clear() with scr.commands: scr.draw.blit((0, 0), img) pause()
def main(shape, resolution=None, clear=False, cycle=False): """Quick example to navigate an string-defined shape through the terminal using the arrow keys! Press <ESC> to exit. Usage example: $ terminedia-shapes --custom=" \\n *** \\n * * \\n *** " """ # Work around apparent bug in click: if shape is None: shape = shape1 if "\\n" in shape: shape = shape.replace("\\n", "\n") original_shape = shape shape = TM.shape( original_shape, **({ "color_map": c_map } if original_shape == shape2 else {})) shape = TM.FullShape.promote(shape, resolution=resolution) last_frame = time.time() time_acumulator = 0 counter = 0 def cycle_color(foreground, tick): if foreground != TM.DEFAULT_FG: return foreground return TM.Color(["red", "blue", "yellow", "lime"][tick % 4]) try: with keyboard(), Screen(clear_screen=clear) as scr: # with Screen(clear_screen=True) as scr: x = scr.get_size()[0] // 2 - 6 y = 0 pos = V2(x, y) sprite = scr.data.sprites.add(shape, pos, active=True) if cycle: sprite.transformers.append( TM.Transformer(foreground=cycle_color)) while True: key = inkey() if key in (K.ESC, "q"): break if not clear and key in (K.RIGHT, K.LEFT, K.UP, K.DOWN): scr.data.draw.rect(sprite.rect, erase=True) sprite.pos += ( ((key == K.RIGHT) - (key == K.LEFT)), ((key == K.DOWN) - (key == K.UP)), ) scr.update() current = time.time() ellapsed = current - last_frame time_acumulator += ellapsed counter += 1 pause_time = max(FRAME_DELAY - ellapsed, 0) time.sleep(pause_time) last_frame = time.time() finally: print( f"\nTotal frames: {counter}\nAverage time per frame: {time_acumulator / (counter or 1):.04f}" )
angle_inc = 2 * math.pi / rate for r in range(10, 55, 3): for i in range(rate): angle = angle_inc * i x, y = polar_coordinate(center_x, center_y, r + randrange(-2, 2), angle) context.color = choice([ (1, 0, 1), (1, 1, 0), (0, 1, 1), ]) line((x, y), (x, y)) with Screen() as scr: term = os.get_terminal_size() max_x = term.columns * 2 max_y = term.lines * 2 center_x, center_y = max_x // 2, max_y // 2 shape(scr, center_x, center_y) center_x, center_y = 3 * max_x // 4, max_y // 2 shape(scr, center_x, center_y) center_x, center_y = max_x // 4, max_y // 2 shape(scr, center_x, center_y) pause()
def main(sleep=0.2): """Example and benchmark tests for the drawing API using ellipses """ with realtime_keyb(), Screen() as scr: test_ellipses(scr, sleep) pause()
def main(func=None, domain=(-2, 2)): if func is None: func = "-2 * x**3 - 3 * x**2 + x - 1" with Screen() as sc: plot(sc, func, domain) pause()