コード例 #1
0
    def test_write_styled(
        SetConsoleTextAttribute,
        win32_console_getters,
        win32_handle,
        capsys,
    ):
        style = Style.parse("black on red")
        text = "Hello, world!"
        term = LegacyWindowsTerm(sys.stdout)

        term.write_styled(text, style)

        captured = capsys.readouterr()
        assert captured.out == text

        # Ensure we set the text attributes and then reset them after writing styled text
        call_args = SetConsoleTextAttribute.call_args_list
        assert len(call_args) == 2
        first_args, first_kwargs = call_args[0]
        second_args, second_kwargs = call_args[1]

        assert first_args == (win32_handle,)
        assert first_kwargs["attributes"].value == 64
        assert second_args == (win32_handle,)
        assert second_kwargs["attributes"] == DEFAULT_STYLE_ATTRIBUTE
コード例 #2
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
    def test_move_cursor_forward(_, SetConsoleCursorPosition, win32_handle):
        term = LegacyWindowsTerm(sys.stdout)

        term.move_cursor_forward()

        SetConsoleCursorPosition.assert_called_once_with(
            win32_handle,
            coords=WindowsCoordinates(row=CURSOR_Y, col=CURSOR_X + 1))
コード例 #3
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
    def test_write_text(_, win32_handle, capsys):
        text = "Hello, world!"
        term = LegacyWindowsTerm(sys.stdout)

        term.write_text(text)

        captured = capsys.readouterr()
        assert captured.out == text
コード例 #4
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
    def test_move_cursor_to(_, SetConsoleCursorPosition, win32_handle):
        coords = WindowsCoordinates(row=4, col=5)
        term = LegacyWindowsTerm(sys.stdout)

        term.move_cursor_to(coords)

        SetConsoleCursorPosition.assert_called_once_with(win32_handle,
                                                         coords=coords)
コード例 #5
0
 def test_move_cursor_to_column(
     SetConsoleCursorPosition, win32_console_getters, win32_handle
 ):
     term = LegacyWindowsTerm(sys.stdout)
     term.move_cursor_to_column(5)
     SetConsoleCursorPosition.assert_called_once_with(
         win32_handle, coords=WindowsCoordinates(CURSOR_Y, 5)
     )
コード例 #6
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
    def test_move_cursor_to_out_of_bounds_col(_, SetConsoleCursorPosition,
                                              win32_handle):
        coords = WindowsCoordinates(row=10, col=-4)
        term = LegacyWindowsTerm(sys.stdout)

        term.move_cursor_to(coords)

        assert not SetConsoleCursorPosition.called
コード例 #7
0
    def test_hide_cursor(SetConsoleCursorInfo, win32_console_getters, win32_handle):
        term = LegacyWindowsTerm(sys.stdout)
        term.hide_cursor()

        call_args = SetConsoleCursorInfo.call_args_list

        assert len(call_args) == 1

        args, kwargs = call_args[0]
        assert kwargs["cursor_info"].bVisible == 0
        assert kwargs["cursor_info"].dwSize == CURSOR_SIZE
コード例 #8
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
    def test_show_cursor(_, SetConsoleCursorInfo, win32_handle):
        term = LegacyWindowsTerm(sys.stdout)
        term.show_cursor()

        call_args = SetConsoleCursorInfo.call_args_list

        assert len(call_args) == 1

        args, kwargs = call_args[0]
        assert kwargs["cursor_info"].bVisible == 1
        assert kwargs["cursor_info"].dwSize == 100
コード例 #9
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
    def test_write_styled_bold(_, SetConsoleTextAttribute, win32_handle):
        style = Style.parse("bold black on red")
        text = "Hello, world!"
        term = LegacyWindowsTerm(sys.stdout)

        term.write_styled(text, style)

        call_args = SetConsoleTextAttribute.call_args_list
        first_args, first_kwargs = call_args[0]

        expected_attr = 64 + 8  # 64 for red bg, +8 for bright black
        assert first_args == (win32_handle, )
        assert first_kwargs["attributes"].value == expected_attr
コード例 #10
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
    def test_write_styled_reverse(_, SetConsoleTextAttribute, win32_handle):
        style = Style.parse("dim bright_red on blue")
        text = "Hello, world!"
        term = LegacyWindowsTerm(sys.stdout)

        term.write_styled(text, style)

        call_args = SetConsoleTextAttribute.call_args_list
        first_args, first_kwargs = call_args[0]

        expected_attr = 4 + 16  # 4 for red text (after dim), +16 for blue bg
        assert first_args == (win32_handle, )
        assert first_kwargs["attributes"].value == expected_attr
コード例 #11
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
    def test_move_cursor_forward_newline_wrap(SetConsoleCursorPosition,
                                              win32_handle):
        cursor_at_end_of_line = StubScreenBufferInfo(
            dwCursorPosition=COORD(SCREEN_WIDTH - 1, CURSOR_Y))
        with patch.object(
                _win32_console,
                "GetConsoleScreenBufferInfo",
                return_value=cursor_at_end_of_line,
        ):
            term = LegacyWindowsTerm(sys.stdout)
            term.move_cursor_forward()

        SetConsoleCursorPosition.assert_called_once_with(
            win32_handle, coords=WindowsCoordinates(row=CURSOR_Y + 1, col=0))
コード例 #12
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
 def test_erase_line(_, FillConsoleOutputAttribute,
                     FillConsoleOutputCharacter, win32_handle):
     term = LegacyWindowsTerm(sys.stdout)
     term.erase_line()
     start = WindowsCoordinates(row=CURSOR_Y, col=0)
     FillConsoleOutputCharacter.assert_called_once_with(win32_handle,
                                                        " ",
                                                        length=SCREEN_WIDTH,
                                                        start=start)
     FillConsoleOutputAttribute.assert_called_once_with(
         win32_handle,
         DEFAULT_STYLE_ATTRIBUTE,
         length=SCREEN_WIDTH,
         start=start)
コード例 #13
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
    def test_write_styled_no_foreground_color(_, SetConsoleTextAttribute,
                                              win32_handle):
        style = Style.parse("on blue")
        text = "Hello, world!"
        term = LegacyWindowsTerm(sys.stdout)

        term.write_styled(text, style)

        call_args = SetConsoleTextAttribute.call_args_list
        first_args, first_kwargs = call_args[0]

        expected_attr = 16 | term._default_fore  # 16 for blue bg, plus default fg color
        assert first_args == (win32_handle, )
        assert first_kwargs["attributes"].value == expected_attr
コード例 #14
0
 def test_move_cursor_backward_prev_line_wrap(
     SetConsoleCursorPosition, win32_console_getters, win32_handle
 ):
     cursor_at_start_of_line = StubScreenBufferInfo(
         dwCursorPosition=COORD(0, CURSOR_Y)
     )
     win32_console_getters[
         "GetConsoleScreenBufferInfo"
     ].return_value = cursor_at_start_of_line
     term = LegacyWindowsTerm(sys.stdout)
     term.move_cursor_backward()
     SetConsoleCursorPosition.assert_called_once_with(
         win32_handle,
         coords=WindowsCoordinates(row=CURSOR_Y - 1, col=SCREEN_WIDTH - 1),
     )
コード例 #15
0
    def test_write_styled_reverse(
        SetConsoleTextAttribute, win32_console_getters, win32_handle
    ):
        style = Style.parse("reverse red on blue")
        text = "Hello, world!"
        term = LegacyWindowsTerm(sys.stdout)

        term.write_styled(text, style)

        call_args = SetConsoleTextAttribute.call_args_list
        first_args, first_kwargs = call_args[0]

        expected_attr = 64 + 1  # 64 for red bg (after reverse), +1 for blue fg
        assert first_args == (win32_handle,)
        assert first_kwargs["attributes"].value == expected_attr
コード例 #16
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
    def test_erase_end_of_line(_, FillConsoleOutputAttribute,
                               FillConsoleOutputCharacter, win32_handle):
        term = LegacyWindowsTerm(sys.stdout)
        term.erase_end_of_line()

        FillConsoleOutputCharacter.assert_called_once_with(
            win32_handle,
            " ",
            length=SCREEN_WIDTH - CURSOR_X,
            start=CURSOR_POSITION)
        FillConsoleOutputAttribute.assert_called_once_with(
            win32_handle,
            DEFAULT_STYLE_ATTRIBUTE,
            length=SCREEN_WIDTH - CURSOR_X,
            start=CURSOR_POSITION,
        )
コード例 #17
0
    def test_erase_start_of_line(
        FillConsoleOutputAttribute,
        FillConsoleOutputCharacter,
        win32_console_getters,
        win32_handle,
    ):
        term = LegacyWindowsTerm(sys.stdout)
        term.erase_start_of_line()

        start = WindowsCoordinates(CURSOR_Y, 0)

        FillConsoleOutputCharacter.assert_called_once_with(
            win32_handle, " ", length=CURSOR_X, start=start
        )
        FillConsoleOutputAttribute.assert_called_once_with(
            win32_handle, DEFAULT_STYLE_ATTRIBUTE, length=CURSOR_X, start=start
        )
コード例 #18
0
def legacy_windows_render(buffer: Iterable[Segment], term: LegacyWindowsTerm) -> None:
    """Makes appropriate Windows Console API calls based on the segments in the buffer.

    Args:
        buffer (Iterable[Segment]): Iterable of Segments to convert to Win32 API calls.
        term (LegacyWindowsTerm): Used to call the Windows Console API.
    """
    for text, style, control in buffer:
        if not control:
            if style:
                term.write_styled(text, style)
            else:
                term.write_text(text)
        else:
            control_codes: Sequence[ControlCode] = control
            for control_code in control_codes:
                control_type = control_code[0]
                if control_type == ControlType.CURSOR_MOVE_TO:
                    _, x, y = cast(Tuple[ControlType, int, int], control_code)
                    term.move_cursor_to(WindowsCoordinates(row=y - 1, col=x - 1))
                elif control_type == ControlType.CARRIAGE_RETURN:
                    term.write_text("\r")
                elif control_type == ControlType.HOME:
                    term.move_cursor_to(WindowsCoordinates(0, 0))
                elif control_type == ControlType.CURSOR_UP:
                    term.move_cursor_up()
                elif control_type == ControlType.CURSOR_DOWN:
                    term.move_cursor_down()
                elif control_type == ControlType.CURSOR_FORWARD:
                    term.move_cursor_forward()
                elif control_type == ControlType.CURSOR_BACKWARD:
                    term.move_cursor_backward()
                elif control_type == ControlType.CURSOR_MOVE_TO_COLUMN:
                    _, column = cast(Tuple[ControlType, int], control_code)
                    term.move_cursor_to_column(column - 1)
                elif control_type == ControlType.HIDE_CURSOR:
                    term.hide_cursor()
                elif control_type == ControlType.SHOW_CURSOR:
                    term.show_cursor()
                elif control_type == ControlType.ERASE_IN_LINE:
                    _, mode = cast(Tuple[ControlType, int], control_code)
                    if mode == 0:
                        term.erase_end_of_line()
                    elif mode == 1:
                        term.erase_start_of_line()
                    elif mode == 2:
                        term.erase_line()
                elif control_type == ControlType.SET_WINDOW_TITLE:
                    _, title = cast(Tuple[ControlType, str], control_code)
                    term.set_title(title)
コード例 #19
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
 def test_screen_size(_):
     term = LegacyWindowsTerm(sys.stdout)
     assert term.screen_size == WindowsCoordinates(row=SCREEN_HEIGHT,
                                                   col=SCREEN_WIDTH)
コード例 #20
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
    def test_set_title_too_long(_):
        term = LegacyWindowsTerm(sys.stdout)

        with pytest.raises(AssertionError):
            term.set_title("a" * 255)
コード例 #21
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
 def test_cursor_position(_):
     term = LegacyWindowsTerm(sys.stdout)
     assert term.cursor_position == WindowsCoordinates(row=CURSOR_Y,
                                                       col=CURSOR_X)
コード例 #22
0
ファイル: test_win32_console.py プロジェクト: sejust/rich
    def test_set_title(SetConsoleTitle):
        term = LegacyWindowsTerm(sys.stdout)
        term.set_title("title")

        SetConsoleTitle.assert_called_once_with("title")