3from typing
import TYPE_CHECKING, Callable, Dict, Iterable, List, Union
6 from typing
import Final
10from .segment
import ControlCode, ControlType, Segment
13 from .console
import Console, ConsoleOptions, RenderResult
15STRIP_CONTROL_CODES: Final = [
22_CONTROL_STRIP_TRANSLATE: Final = {
23 _codepoint:
None for _codepoint
in STRIP_CONTROL_CODES
26CONTROL_ESCAPE: Final = {
34CONTROL_CODES_FORMAT: Dict[int, Callable[..., str]] = {
55 """A renderable that inserts a control code (non printable but may move cursor).
58 *codes (str): Positional arguments are either a :class:`~rich.segment.ControlType` enum or a
59 tuple of ControlType and an integer parameter
62 __slots__ = [
"segment"]
64 def __init__(self, *codes: Union[ControlType, ControlCode]) ->
None:
65 control_codes: List[ControlCode] = [
66 (code,)
if isinstance(code, ControlType)
else code
for code
in codes
68 _format_map = CONTROL_CODES_FORMAT
69 rendered_codes =
"".join(
70 _format_map[code](*parameters)
for code, *parameters
in control_codes
75 def bell(cls) -> "Control":
76 """Ring the 'bell'."""
80 def home(cls) -> "Control":
81 """Move cursor to 'home' position."""
85 def move(cls, x: int = 0, y: int = 0) ->
"Control":
86 """Move cursor relative to current position.
93 ~Control: Control object.
115 """Move to the given column, optionally add offset to row.
118 x (int): absolute x (column)
119 y (int): optional y offset (row)
122 ~Control: Control object.
138 def move_to(cls, x: int, y: int) ->
"Control":
139 """Move cursor to absolute position.
142 x (int): x offset (column)
143 y (int): y offset (row)
146 ~Control: Control object.
152 """Clear the screen."""
157 """Show or hide the cursor."""
162 """Enable or disable alt screen."""
169 def title(cls, title: str) ->
"Control":
170 """Set the terminal window title
173 title (str): The new terminal window title
181 self, console:
"Console", options:
"ConsoleOptions"
187def strip_control_codes(
188 text: str, _translate_table: Dict[int,
None] = _CONTROL_STRIP_TRANSLATE
190 """Remove control codes from text.
193 text (str): A string possibly contain control codes.
196 str: String with control codes removed.
201def escape_control_codes(
203 _translate_table: Dict[int, str] = CONTROL_ESCAPE,
205 """Replace control codes with their "escaped" equivalent in the given text.
206 (e.g. "\b" becomes "\\b")
209 text (str): A string possibly containing control codes.
212 str: String with control codes replaced with their escaped version.
217if __name__ ==
"__main__":
"Control" move_to_column(cls, int x, int y=0)
"Control" show_cursor(cls, bool show)
"Control" move_to(cls, int x, int y)
"Control" move(cls, int x=0, int y=0)
None __init__(self, *Union[ControlType, ControlCode] codes)
"RenderResult" __rich_console__(self, "Console" console, "ConsoleOptions" options)
"Control" alt_screen(cls, bool enable)
"Control" title(cls, str title)