A renderable that inserts a control code (non printable but may move cursor). Args: *codes (str): Positional arguments are either a :class:`~rich.segment.ControlType` enum or a tuple of ControlType and an integer parameter
| 46 | |
| 47 | |
| 48 | class Control: |
| 49 | """A renderable that inserts a control code (non printable but may move cursor). |
| 50 | |
| 51 | Args: |
| 52 | *codes (str): Positional arguments are either a :class:`~rich.segment.ControlType` enum or a |
| 53 | tuple of ControlType and an integer parameter |
| 54 | """ |
| 55 | |
| 56 | __slots__ = ["segment"] |
| 57 | |
| 58 | def __init__(self, *codes: Union[ControlType, ControlCode]) -> None: |
| 59 | control_codes: List[ControlCode] = [ |
| 60 | (code,) if isinstance(code, ControlType) else code for code in codes |
| 61 | ] |
| 62 | _format_map = CONTROL_CODES_FORMAT |
| 63 | rendered_codes = "".join( |
| 64 | _format_map[code](*parameters) for code, *parameters in control_codes |
| 65 | ) |
| 66 | self.segment = Segment(rendered_codes, None, control_codes) |
| 67 | |
| 68 | @classmethod |
| 69 | def bell(cls) -> "Control": |
| 70 | """Ring the 'bell'.""" |
| 71 | return cls(ControlType.BELL) |
| 72 | |
| 73 | @classmethod |
| 74 | def home(cls) -> "Control": |
| 75 | """Move cursor to 'home' position.""" |
| 76 | return cls(ControlType.HOME) |
| 77 | |
| 78 | @classmethod |
| 79 | def move(cls, x: int = 0, y: int = 0) -> "Control": |
| 80 | """Move cursor relative to current position. |
| 81 | |
| 82 | Args: |
| 83 | x (int): X offset. |
| 84 | y (int): Y offset. |
| 85 | |
| 86 | Returns: |
| 87 | ~Control: Control object. |
| 88 | |
| 89 | """ |
| 90 | |
| 91 | def get_codes() -> Iterable[ControlCode]: |
| 92 | control = ControlType |
| 93 | if x: |
| 94 | yield ( |
| 95 | control.CURSOR_FORWARD if x > 0 else control.CURSOR_BACKWARD, |
| 96 | abs(x), |
| 97 | ) |
| 98 | if y: |
| 99 | yield ( |
| 100 | control.CURSOR_DOWN if y > 0 else control.CURSOR_UP, |
| 101 | abs(y), |
| 102 | ) |
| 103 | |
| 104 | control = cls(*get_codes()) |
| 105 | return control |
no outgoing calls