| 1 |
from rich.control import Control, escape_control_codes, strip_control_codes |
| 2 |
from rich.segment import ControlType, Segment |
| 3 |
|
| 4 |
|
| 5 |
def test_control(): |
| 6 |
control = Control(ControlType.BELL) |
| 7 |
assert str(control) == "\x07" |
| 8 |
|
| 9 |
|
| 10 |
def test_strip_control_codes(): |
| 11 |
assert strip_control_codes("") == "" |
| 12 |
assert strip_control_codes("foo\rbar") == "foobar" |
| 13 |
assert strip_control_codes("Fear is the mind killer") == "Fear is the mind killer" |
| 14 |
|
| 15 |
|
| 16 |
def test_escape_control_codes(): |
| 17 |
assert escape_control_codes("") == "" |
| 18 |
assert escape_control_codes("foo\rbar") == "foo\\rbar" |
| 19 |
assert escape_control_codes("Fear is the mind killer") == "Fear is the mind killer" |
| 20 |
|
| 21 |
|
| 22 |
def test_control_move_to(): |
| 23 |
control = Control.move_to(5, 10) |
| 24 |
print(control.segment) |
| 25 |
assert control.segment == Segment( |
| 26 |
"\x1b[11;6H", None, [(ControlType.CURSOR_MOVE_TO, 5, 10)] |
| 27 |
) |
| 28 |
|
| 29 |
|
| 30 |
def test_control_move(): |
| 31 |
assert Control.move(0, 0).segment == Segment("", None, []) |
| 32 |
control = Control.move(3, 4) |
| 33 |
print(repr(control.segment)) |
| 34 |
assert control.segment == Segment( |
| 35 |
"\x1b[3C\x1b[4B", |
| 36 |
None, |
| 37 |
[(ControlType.CURSOR_FORWARD, 3), (ControlType.CURSOR_DOWN, 4)], |
| 38 |
) |
| 39 |
|
| 40 |
|
| 41 |
def test_move_to_column(): |
| 42 |
print(repr(Control.move_to_column(10, 20).segment)) |
| 43 |
assert Control.move_to_column(10, 20).segment == Segment( |
| 44 |
"\x1b[11G\x1b[20B", |
| 45 |
None, |
| 46 |
[(ControlType.CURSOR_MOVE_TO_COLUMN, 10), (ControlType.CURSOR_DOWN, 20)], |
| 47 |
) |
| 48 |
|
| 49 |
assert Control.move_to_column(10, -20).segment == Segment( |
| 50 |
"\x1b[11G\x1b[20A", |
| 51 |
None, |
| 52 |
[(ControlType.CURSOR_MOVE_TO_COLUMN, 10), (ControlType.CURSOR_UP, 20)], |
| 53 |
) |
| 54 |
|
| 55 |
|
| 56 |
def test_title(): |
| 57 |
control_segment = Control.title("hello").segment |
| 58 |
assert control_segment == Segment( |
| 59 |
"\x1b]0;hello\x07", |
| 60 |
None, |
| 61 |
[(ControlType.SET_WINDOW_TITLE, "hello")], |
| 62 |
) |