@@ -90,20 +90,18 @@ def strip_style(text: str) -> str:
90
90
return ANSI_STYLE_RE .sub ('' , text )
91
91
92
92
93
- def ansi_safe_wcswidth (text : str ) -> int :
93
+ def style_aware_wcswidth (text : str ) -> int :
94
94
"""
95
95
Wrap wcswidth to make it compatible with strings that contains ANSI style sequences
96
-
97
96
:param text: the string being measured
98
97
"""
99
98
# Strip ANSI style sequences since they cause wcswidth to return -1
100
99
return wcswidth (strip_style (text ))
101
100
102
101
103
- def ansi_aware_write (fileobj : IO , msg : str ) -> None :
102
+ def style_aware_write (fileobj : IO , msg : str ) -> None :
104
103
"""
105
104
Write a string to a fileobject and strip its ANSI style sequences if required by allow_style setting
106
-
107
105
:param fileobj: the file object being written to
108
106
:param msg: the string being written
109
107
"""
@@ -114,8 +112,8 @@ def ansi_aware_write(fileobj: IO, msg: str) -> None:
114
112
115
113
116
114
def fg_lookup (fg_name : str ) -> str :
117
- """Look up ANSI escape codes based on foreground color name.
118
-
115
+ """
116
+ Look up ANSI escape codes based on foreground color name.
119
117
:param fg_name: foreground color name to look up ANSI escape code(s) for
120
118
:return: ANSI escape code(s) associated with this color
121
119
:raises ValueError if the color cannot be found
@@ -128,8 +126,8 @@ def fg_lookup(fg_name: str) -> str:
128
126
129
127
130
128
def bg_lookup (bg_name : str ) -> str :
131
- """Look up ANSI escape codes based on background color name.
132
-
129
+ """
130
+ Look up ANSI escape codes based on background color name.
133
131
:param bg_name: background color name to look up ANSI escape code(s) for
134
132
:return: ANSI escape code(s) associated with this color
135
133
:raises ValueError if the color cannot be found
@@ -142,8 +140,8 @@ def bg_lookup(bg_name: str) -> str:
142
140
143
141
144
142
def style (text : Any , * , fg : str = '' , bg : str = '' , bold : bool = False , underline : bool = False ) -> str :
145
- """Styles a string with ANSI colors and/or styles and returns the new string.
146
-
143
+ """
144
+ Apply ANSI colors and/or styles to a string and return it.
147
145
The styling is self contained which means that at the end of the string reset code(s) are issued
148
146
to undo whatever styling was done at the beginning.
149
147
@@ -210,14 +208,14 @@ def async_alert_str(*, terminal_columns: int, prompt: str, line: str, cursor_off
210
208
# That will be included in the input lines calculations since that is where the cursor is.
211
209
num_prompt_terminal_lines = 0
212
210
for line in prompt_lines [:- 1 ]:
213
- line_width = ansi_safe_wcswidth (line )
211
+ line_width = style_aware_wcswidth (line )
214
212
num_prompt_terminal_lines += int (line_width / terminal_columns ) + 1
215
213
216
214
# Now calculate how many terminal lines are take up by the input
217
215
last_prompt_line = prompt_lines [- 1 ]
218
- last_prompt_line_width = ansi_safe_wcswidth (last_prompt_line )
216
+ last_prompt_line_width = style_aware_wcswidth (last_prompt_line )
219
217
220
- input_width = last_prompt_line_width + ansi_safe_wcswidth (line )
218
+ input_width = last_prompt_line_width + style_aware_wcswidth (line )
221
219
222
220
num_input_terminal_lines = int (input_width / terminal_columns ) + 1
223
221
0 commit comments