def main(): global sc s = Subscription(EventTypes.Tick, draw) Subscription(EventTypes.KeyPress, keys) sc = TM.Screen() setup(sc) asyncio.run(terminedia_main(screen=sc))
def test_square_fullcolor_sets_pixel_lower(): sc = TM.Screen(size=(3, 3)) sc.data.square.set_at((0,1)) sc.update((0, 0)) yield None assert sc.data.square.get_at((0,1)) is TM.DEFAULT_FG assert sc.data.square.get_at((0,0)) is TM.DEFAULT_BG assert sc.data[0,0].value == TM.subpixels.HalfChars.LOWER_HALF_BLOCK
def test_square_fullcolor_sets_pixel(): sc = TM.Screen(size=(3, 3)) sc.data.square.set_at((0,0)) sc.update((0, 0)) yield None # Uncomment along with decorators to enable 'visual display' of test-output assert sc.data.square.get_at((0,0)) is TM.DEFAULT_FG assert sc.data.square.get_at((0,1)) is TM.DEFAULT_BG assert sc.data[0,0].value == TM.subpixels.HalfChars.UPPER_HALF_BLOCK
def test_square_fullcolor_sets_both_pixels_yelds_full_block(): sc = TM.Screen(size=(3, 3)) sc.data.square.set_at((0,0)) sc.data.square.set_at((0,1)) sc.update((0, 0)) yield None assert sc.data.square.get_at((0,1)) is TM.DEFAULT_FG assert sc.data.square.get_at((0,0)) is TM.DEFAULT_FG assert sc.data[0,0].value == TM.subpixels.HalfChars.FULL_BLOCK
def test_render_blocks_default_color(): FB = TM.values.FULL_BLOCK sc = TM.Screen(size=(3, 3)) sc.data[0, 0] = FB sc.data[2, 2] = FB sc.update((0, 0)) data = strip_ansi_seqs((yield None)) assert data == FB + TM.values.EMPTY * 7 + FB
def setup(): sc = TM.Screen() sc.data.sprites.append([(6, 3)] * 5) colors = "white green yellow red purple".split() for shape, color in zip(sc.data.sprites[0].shapes, colors): shape.context.color = color shape.draw.fill() sc.data.sprites[0].pos = (10, 10) sc.data.sprites[0].active = True return sc
def test_render_transparent_characters_dont_overwrite_terminal_contents(): sc = TM.Screen(size=(3, 3)) sc.data.clear(transparent=True) sc.data[1, 1] = "*" sc.update((0, 0)) data = ansi_movement_to_markup((yield None)) assert data.count("*", 1) # Actual render optimizations won't place a 'move' for each non displayed pixel. # assert data.count("[MOVE") == 8 assert re.sub(r"\[.+?\]", "", data).count(EMPTY) == 0
def main(): """Terminedia snake-game!""" snake = Snake((2, 2), direction=D.RIGHT) with terminedia.Screen() as scr, terminedia.keyboard(): try: game = Game(scr, snake) game.run() except GameOver: pass print("Você bateu!\n\n")
def main(phrases=(), clear=True): if not phrases: phrases = ["Hello World! 1234"] with TM.Screen(clear_screen=clear) as sc: for line, (phrase, effect) in enumerate(zip(cycle(phrases), TM.Effects)): sc.context.effects = TM.Effects.none sc.print_at((0, line), f"{effect.name}: ") sc.context.effects = effect sc.print(phrase) TM.pause()
def main(resolution): """Terminedia snake-game!""" snake = Snake((2, 2), direction=D.RIGHT) with terminedia.Screen() as scr, terminedia.keyboard(): try: game = Game(scr, snake, resolution=resolution) game.run() except GameOver: pass print("You died!\n\n")
def test_square_fullcolor_set_half_pixel_to_other_color_than_default_preserves_other_half(): color = TM.Color("blue") sc = TM.Screen(size=(3, 3)) sc.data.square.set_at((0,0)) sc.data.context.color = color sc.data.square.set_at((0,1)) sc.update((0, 0)) yield None assert sc.data.square.get_at((0,0)) is TM.DEFAULT_FG assert sc.data.square.get_at((0,1)) == color assert sc.data[0,0].background == color assert sc.data[0,0].value == TM.subpixels.HalfChars.UPPER_HALF_BLOCK
def test_text_entry_widget_sequence_write(typed, expected, extra_kw): stdin = io.StringIO() with patch("sys.stdin", stdin): sc = TM.Screen() with sc, TM.keyboard: w = TM.widgets.Entry(sc, pos=(0,0), width=5, **(extra_kw or {})) sc.update() stdin.write(typed) stdin.seek(0) sc.update() yield None sc.update() assert w.value == expected
def main(): sc = TM.Screen() sp1 = sc.sprites.add((40, 10)) done = False def ready(*args): nonlocal done done = True with sc, TM.keyboard, TM.mouse: picker = TM.widgets.FileSelector(sp1, callback=ready) while not done: sc.update() print(picker.value)
def test_render_blocks_foreground_color(): sc = TM.Screen(size=(3, 3)) sc.data.context.color = (255, 0, 0) sc.data[0, 0] = "*" sc.data.context.color = (0, 1.0, 0) sc.data[1, 0] = "*" sc.data.context.color = TM.DEFAULT_FG sc.data[2, 0] = "*" sc.update((0, 0)) data = ansi_colors_to_markup(strip_ansi_movement((yield None))) assert ( data == "[foreground: (255, 0, 0)][background: DEFAULT]*[foreground: (0, 255, 0)]*[foreground: DEFAULT]*" + TM.values.EMPTY * 6)
def test_square_fullcolor_set_half_pixel_to_different_color_preserves_other_half(): color = TM.Color("blue") color2 = TM.Color("red") sc = TM.Screen(size=(3, 3)) sc.data.context.color = color sc.data.square.set_at((0,0)) sc.data.context.color = color2 sc.data.square.set_at((0,1)) sc.update((0, 0)) yield None assert sc.data.square.get_at((0,0)) == color assert sc.data.square.get_at((0,1)) == color2 assert sc.data[0,0].background == color assert sc.data[0,0].foreground == color2 assert sc.data[0,0].value == TM.subpixels.HalfChars.LOWER_HALF_BLOCK
def main(phrases=(), effects=(), clear=True): if not phrases: phrases = ["Hello World! 1234"] if not effects: effects = TM.Effects else: effects = [getattr(TM.Effects, effect, TM.Effects.none) for effect in effects] with TM.Screen(clear_screen=clear) as sc: for line, (phrase, effect) in enumerate(zip(cycle(phrases), effects)): sc.context.effects = TM.Effects.none sc.text[1][0, line] = f"{(effect.name + ':') if len(effects) > 1 else ''}[effect: {effect.name}]{phrase}" # sc.context.effects = effect # sc.text[1].print(phrase) TM.pause()
def test_render_effects_work(): sc = TM.Screen(size=(3, 3)) sc.context.effects = TM.Effects.blink sc.data[0, 0] = "a" sc.context.effects += TM.Effects.encircled sc.data[1, 0] = "a" sc.context.effects -= TM.Effects.blink sc.data[2, 0] = "a" sc.update((0, 0)) data = ansi_colors_to_markup(strip_ansi_movement((yield None))) assert ( data == "[foreground: DEFAULT][background: DEFAULT][effects: BLINK]a\u24d0[effects: NOBLINK]\u24d0" + TM.values.EMPTY * 6 or data == "[foreground: DEFAULT][background: DEFAULT][effects: BLINK]a[effects: BLINK]\u24d0[effects: NOBLINK]\u24d0[effects: NOBLINK] " )
def test_valueshape_concat(direction, quantity, exp_width, exp_height, exp_data, DISPLAY, DELAY): a = IMG.ValueShape.new((3, 3), color=(0,0,0)) b = IMG.ValueShape.new((3, 3), color=(0,0,0)) a[0, 0] = (255,0,0) b[2, 2] = (128, 128, 255) c = a.concat(*((b,) * quantity), direction=direction) compare_data = [v[0] for v in c.data] if DISPLAY: with TM.Screen(clear_screen=True) as sc: sc.draw.blit((0,0), c) sc.context.color = (128, 128, 255) sc.print_at((0, 11), f"quantity={quantity}, width={c.width}, heigth={c.height}") sc.print_at((0, 10), f"{compare_data!r}") TM.pause(DELAY) assert c.width == exp_width assert c.height == exp_height assert compare_data == exp_data
def styled_text(): sc = TM.Screen((20, 10)) sh = TM.shape((20, 10)) sp1 = sc.data.sprites.add(sh) text_plane = sh.text[1] return sc, sh, text_plane
import terminedia as TM from terminedia.transformers import GradientTransformer sc = TM.Screen() a = TM.shape((40, 4)) a.text[4].at((0, 0), "terminedia") b = TM.Sprite(a) b.active = True sc.data.sprites.append(b) b.pos = (5, 5) a.context.transformers.append( GradientTransformer( TM.Gradient([(0, "blue"), (0.5, "white"), (1, "blue")]), TM.Directions.LEFT)) sc.update()
colors = itertools.cycle([ "red", "green", "yellow", "blue", "white", (255, 0, 255), "lime", (255, 0, 0), (128, 128, 128) ]) characters = itertools.cycle("█*#O+.") def clicked(e): global lastclick if lastclick is None: lastclick = e.pos sc[e.pos] = "*" return rect = TM.Rect(lastclick, e.pos) rect.c2 += (1, 1) sc.draw.rect(rect, char=next(characters), color=next(colors)) lastclick = None TM.events.Subscription(TM.events.EventTypes.MouseClick, clicked) lastclick = None with TM.input.mouse, TM.Screen() as sc: while True: key = TM.input.inkey() #_dispatch=True) sc.update() time.sleep(0.1) if key == "\x1b": break
def screen_shape_sprite(): sc = TM.Screen((26, 10)) sh = TM.shape((26, 10)) sp1 = sc.data.sprites.add(sh) gr = TM.ColorGradient([(0, (0, 0, 0)), (1, (1, 1, 1))]) return sc, sh, sp1, gr
def init(): global sc sc = TM.Screen()
def test_render_spaces_default_color(): sc = TM.Screen(size=(3, 3)) sc.update((0, 0)) data = strip_ansi_seqs((yield None)) assert data == TM.values.EMPTY * 9