How to use the asciimatics.screen.Canvas function in asciimatics

To help you get started, we’ve selected a few asciimatics examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github peterbrittain / asciimatics / tests / test_effects.py View on Github external
def test_sprite_overlap(self):
        """
        Check that Sprites detect overlap.
        """
        screen = MagicMock(spec=Screen, colours=8, unicode_aware=False)
        canvas = Canvas(screen, 10, 40, 0, 0)
        path = Path()
        path.jump_to(10, 5)
        effect1 = Sam(canvas, path)
        effect2 = Sam(canvas, path)

        # These 2 sprites should overlap!
        effect1.reset()
        effect1.update(0)
        effect2.reset()
        effect2.update(0)
        self.assertTrue(effect1.overlaps(effect2))

        # Now create a sprite that shouldn't quite overlap
        width = effect1.last_position()[2]
        path = Path()
        path.jump_to(10 + width, 5)
github peterbrittain / asciimatics / tests / test_widgets.py View on Github external
def test_cjk_forms(self):
        """
        Check form widgets work with CJK characters.
        """
        # Create a dummy screen.
        screen = MagicMock(spec=Screen, colours=8, unicode_aware=True)
        scene = MagicMock(spec=Scene)
        canvas = Canvas(screen, 10, 40, 0, 0)

        # Create the form we want to test.
        form = Frame(canvas, canvas.height, canvas.width, has_border=False)
        layout = Layout([100], fill_frame=True)
        mc_list = MultiColumnListBox(
            4,
            [3, 5, 0],
            [
                (["1", "2", "3"], 1),
                ([u"你", u"確", u"定"], 2),
            ],
            titles=[u"你確定嗎?", u"你確定嗎?", u"你確定嗎?"])
        text = Text()
        text_box = TextBox(3)
        form.add_layout(layout)
        layout.add_widget(mc_list)
github peterbrittain / asciimatics / tests / test_widgets.py View on Github external
def test_time_picker(self):
        """
        Check TimePicker widget works as expected.
        """
        # Now set up the Frame ready for testing
        screen = MagicMock(spec=Screen, colours=8, unicode_aware=False)
        scene = Scene([], duration=-1)
        canvas = Canvas(screen, 10, 40, 0, 0)
        form = TestFrame5(canvas)
        scene.add_effect(form)
        scene.reset()

        # Check that the Frame is rendered correctly.
        for effect in scene.effects:
            effect.update(0)
        self.assertFalse(form.changed)
        self.assert_canvas_equals(
            canvas,
            "+--------------------------------------+\n" +
            "|Date: 02/Jan/2017                     |\n" +
            "|Time: 12:00:59                        O\n" +
            "|                                      |\n" +
            "|                                      |\n" +
            "|                                      |\n" +
github peterbrittain / asciimatics / tests / test_widgets.py View on Github external
def test_radiobutton_input(self):
        """
        Check RadioButton input works as expected.
        """
        screen = MagicMock(spec=Screen, colours=8, unicode_aware=False)
        canvas = Canvas(screen, 10, 40, 0, 0)
        form = TestFrame(canvas)
        form.reset()

        # Check basic selection keys - including limit checking
        self.process_keys(
            form,
            [Screen.KEY_TAB, Screen.KEY_TAB, Screen.KEY_TAB, Screen.KEY_TAB])
        form.save()
        self.assertEqual(form.data["Things"], 1)
        self.process_keys(form, [Screen.KEY_UP])
        form.save()
        self.assertEqual(form.data["Things"], 1)
        self.process_keys(form, [Screen.KEY_DOWN])
        form.save()
        self.assertEqual(form.data["Things"], 2)
        self.process_keys(form, [Screen.KEY_DOWN])
github peterbrittain / asciimatics / tests / test_widgets.py View on Github external
def test_frame_rate(self):
        """
        Check Frame rate limiting works as expected.
        """
        screen = MagicMock(spec=Screen, colours=8, unicode_aware=False)
        canvas = Canvas(screen, 10, 40, 0, 0)
        form = TestFrame(canvas)
        form.reset()

        # With no special CPU consideration, and a cursor to animate, there
        # should be a 5 frame pause.
        self.assertEqual(form.reduce_cpu, False)
        self.assertEqual(form.frame_update_count, 5)

        # Shift focus away from a text input (to get no cursor animation).
        self.process_keys(form, [Screen.KEY_BACK_TAB])

        # With no special CPU consideration, and no cursors to animate, there
        # should be a (very!) long pause.
        self.assertEqual(form.reduce_cpu, False)
        self.assertEqual(form.frame_update_count, 1000000)
github peterbrittain / asciimatics / tests / test_effects.py View on Github external
def test_clock(self, mock_datetime):
        """
        Check that Clock works.
        """
        # Check that Clock updates every second.
        screen = MagicMock(spec=Screen, colours=8, unicode_aware=False)
        mock_datetime.now.return_value = datetime
        mock_datetime.side_effect = lambda *args, **kw: datetime(*args, **kw)
        canvas = Canvas(screen, 10, 40, 0, 0)
        effect = Clock(canvas, 10, 5, 5)
        effect.reset()
        self.assert_blank(canvas)
        my_buffer = [[(32, 7, 0, 0) for _ in range(40)] for _ in range(10)]

        # Set a time for the next update and check it is drawn.
        mock_datetime.now.return_value = \
            datetime(1900, 1, 2, 3, 59, 40)
        effect.update(0)
        mock_datetime.now.assert_called()
        self.assertEqual(self.check_canvas(
            canvas,
            my_buffer,
            lambda value: self.assertLess(value[0], 129)),
            True)
github peterbrittain / asciimatics / tests / test_particles.py View on Github external
def test_explosion(self):
        """
        Test that Explosion works as expected.
        """
        screen = MagicMock(spec=Screen, colours=8)
        canvas = Canvas(screen, 10, 40, 0, 0)
        effect = Explosion(canvas, 4, 4, 25)
        self.check_effect(canvas,
                          effect,
                          lambda value: self.assertIn(chr(value[0]), ' #'),
                          iterations=25)
github peterbrittain / asciimatics / tests / test_widgets.py View on Github external
def test_list_box(self):
        """
        Check ListBox widget works as expected.
        """
        screen = MagicMock(spec=Screen, colours=8, unicode_aware=False)
        scene = MagicMock(spec=Scene)
        canvas = Canvas(screen, 10, 40, 0, 0)
        form = TestFrame2(
            canvas, [("One", 1), ("Two is now quite a bit longer than before", 2)])
        form.register_scene(scene)
        form.reset()

        # Check we have a default value for our list.
        form.save()
        self.assertEqual(form.data, {"selected": "None", "contacts": 1})

        # Check that UP/DOWN change selection.
        self.process_keys(form, [Screen.KEY_DOWN])
        form.save()
        self.assertEqual(form.data, {"selected": "None", "contacts": 2})
        self.process_keys(form, [Screen.KEY_UP])
        form.save()
        self.assertEqual(form.data, {"selected": "None", "contacts": 1})
github peterbrittain / asciimatics / tests / test_effects.py View on Github external
def test_sprite(self):
        """
        Check that Sprites work.
        """
        # Check that we can move a Sprite around the screen.
        screen = MagicMock(spec=Screen, colours=8, unicode_aware=False)
        canvas = Canvas(screen, 10, 40, 0, 0)
        path = Path()
        path.jump_to(10, 5)
        path.move_straight_to(20, 10, 5)
        path.move_straight_to(30, 5, 5)
        path.move_straight_to(20, 0, 5)
        path.move_straight_to(10, 5, 5)
        effect = Sam(canvas, path)
        effect.reset()
        self.assert_blank(canvas)
        my_buffer = [[(32, 7, 0, 0) for _ in range(40)] for _ in range(10)]
        for i in range(30):
            effect.update(i)
            self.assertEqual(self.check_canvas(
                canvas,
                my_buffer,
                lambda value: self.assertLess(value[0], 129)),
github peterbrittain / asciimatics / tests / test_screen.py View on Github external
def demo(screen=None):
            canvas = Canvas(screen, 10, 40, 0, 0)

            # Check underflow and overflow work as expected
            canvas.print_at("ab", -1, 0)
            canvas.print_at("cd", canvas.width - 1, 0)
            self.assert_line_equals(canvas, "b                                      c")