10
10
11
11
if sys .platform == "win32" :
12
12
13
- from rich ._win32_console import COORD , LegacyWindowsTerm , WindowsCoordinates
14
13
from rich import _win32_console
14
+ from rich ._win32_console import COORD , LegacyWindowsTerm , WindowsCoordinates
15
15
16
16
CURSOR_X = 1
17
17
CURSOR_Y = 2
20
20
SCREEN_HEIGHT = 30
21
21
DEFAULT_STYLE_ATTRIBUTE = 16
22
22
23
-
24
23
@dataclasses .dataclass
25
24
class StubScreenBufferInfo :
26
25
dwCursorPosition : COORD = COORD (CURSOR_X , CURSOR_Y )
27
26
dwSize : COORD = COORD (SCREEN_WIDTH , SCREEN_HEIGHT )
28
27
wAttributes : int = DEFAULT_STYLE_ATTRIBUTE
29
28
30
-
31
29
pytestmark = pytest .mark .skipif (sys .platform != "win32" , reason = "windows only" )
32
30
33
-
34
31
def test_windows_coordinates_to_ctype ():
35
32
coord = WindowsCoordinates .from_param (WindowsCoordinates (row = 1 , col = 2 ))
36
33
assert coord .X == 2
37
34
assert coord .Y == 1
38
35
39
-
40
36
@pytest .fixture
41
37
def win32_handle ():
42
38
handle = mock .sentinel
43
39
with mock .patch .object (_win32_console , "GetStdHandle" , return_value = handle ):
44
40
yield handle
45
41
46
-
47
42
@patch .object (
48
43
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
49
44
)
50
45
def test_cursor_position (_ ):
51
46
term = LegacyWindowsTerm ()
52
47
assert term .cursor_position == WindowsCoordinates (row = CURSOR_Y , col = CURSOR_X )
53
48
54
-
55
49
@patch .object (
56
50
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
57
51
)
58
52
def test_screen_size (_ ):
59
53
term = LegacyWindowsTerm ()
60
- assert term .screen_size == WindowsCoordinates (row = SCREEN_HEIGHT , col = SCREEN_WIDTH )
61
-
54
+ assert term .screen_size == WindowsCoordinates (
55
+ row = SCREEN_HEIGHT , col = SCREEN_WIDTH
56
+ )
62
57
63
58
@patch .object (
64
59
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
@@ -72,7 +67,6 @@ def test_write_text(_):
72
67
73
68
assert f .getvalue () == text
74
69
75
-
76
70
@patch .object (_win32_console , "SetConsoleTextAttribute" )
77
71
@patch .object (
78
72
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
@@ -93,14 +87,13 @@ def test_write_styled(_, SetConsoleTextAttribute, win32_handle):
93
87
assert call_args [0 ].kwargs ["attributes" ].value == 64
94
88
assert call_args [1 ] == call (win32_handle , attributes = DEFAULT_STYLE_ATTRIBUTE )
95
89
96
-
97
90
@patch .object (_win32_console , "FillConsoleOutputCharacter" , return_value = None )
98
91
@patch .object (_win32_console , "FillConsoleOutputAttribute" , return_value = None )
99
92
@patch .object (
100
93
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
101
94
)
102
95
def test_erase_line (
103
- _ , FillConsoleOutputAttribute , FillConsoleOutputCharacter , win32_handle
96
+ _ , FillConsoleOutputAttribute , FillConsoleOutputCharacter , win32_handle
104
97
):
105
98
term = LegacyWindowsTerm ()
106
99
term .erase_line ()
@@ -112,14 +105,13 @@ def test_erase_line(
112
105
win32_handle , DEFAULT_STYLE_ATTRIBUTE , length = SCREEN_WIDTH , start = start
113
106
)
114
107
115
-
116
108
@patch .object (_win32_console , "FillConsoleOutputCharacter" , return_value = None )
117
109
@patch .object (_win32_console , "FillConsoleOutputAttribute" , return_value = None )
118
110
@patch .object (
119
111
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
120
112
)
121
113
def test_erase_end_of_line (
122
- _ , FillConsoleOutputAttribute , FillConsoleOutputCharacter , win32_handle
114
+ _ , FillConsoleOutputAttribute , FillConsoleOutputCharacter , win32_handle
123
115
):
124
116
term = LegacyWindowsTerm ()
125
117
term .erase_end_of_line ()
@@ -134,14 +126,13 @@ def test_erase_end_of_line(
134
126
start = CURSOR_POSITION ,
135
127
)
136
128
137
-
138
129
@patch .object (_win32_console , "FillConsoleOutputCharacter" , return_value = None )
139
130
@patch .object (_win32_console , "FillConsoleOutputAttribute" , return_value = None )
140
131
@patch .object (
141
132
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
142
133
)
143
134
def test_erase_start_of_line (
144
- _ , FillConsoleOutputAttribute , FillConsoleOutputCharacter , win32_handle
135
+ _ , FillConsoleOutputAttribute , FillConsoleOutputCharacter , win32_handle
145
136
):
146
137
term = LegacyWindowsTerm ()
147
138
term .erase_start_of_line ()
@@ -155,7 +146,6 @@ def test_erase_start_of_line(
155
146
win32_handle , DEFAULT_STYLE_ATTRIBUTE , length = CURSOR_X , start = start
156
147
)
157
148
158
-
159
149
@patch .object (_win32_console , "SetConsoleCursorPosition" , return_value = None )
160
150
@patch .object (
161
151
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
@@ -168,33 +158,34 @@ def test_move_cursor_to(_, SetConsoleCursorPosition, win32_handle):
168
158
169
159
SetConsoleCursorPosition .assert_called_once_with (win32_handle , coords = coords )
170
160
171
-
172
161
@patch .object (_win32_console , "SetConsoleCursorPosition" , return_value = None )
173
162
@patch .object (
174
163
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
175
164
)
176
- def test_move_cursor_to_out_of_bounds_row (_ , SetConsoleCursorPosition , win32_handle ):
165
+ def test_move_cursor_to_out_of_bounds_row (
166
+ _ , SetConsoleCursorPosition , win32_handle
167
+ ):
177
168
coords = WindowsCoordinates (row = - 1 , col = 4 )
178
169
term = LegacyWindowsTerm ()
179
170
180
171
term .move_cursor_to (coords )
181
172
182
173
assert not SetConsoleCursorPosition .called
183
174
184
-
185
175
@patch .object (_win32_console , "SetConsoleCursorPosition" , return_value = None )
186
176
@patch .object (
187
177
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
188
178
)
189
- def test_move_cursor_to_out_of_bounds_col (_ , SetConsoleCursorPosition , win32_handle ):
179
+ def test_move_cursor_to_out_of_bounds_col (
180
+ _ , SetConsoleCursorPosition , win32_handle
181
+ ):
190
182
coords = WindowsCoordinates (row = 10 , col = - 4 )
191
183
term = LegacyWindowsTerm ()
192
184
193
185
term .move_cursor_to (coords )
194
186
195
187
assert not SetConsoleCursorPosition .called
196
188
197
-
198
189
@patch .object (_win32_console , "SetConsoleCursorPosition" , return_value = None )
199
190
@patch .object (
200
191
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
@@ -208,7 +199,6 @@ def test_move_cursor_up(_, SetConsoleCursorPosition, win32_handle):
208
199
win32_handle , coords = WindowsCoordinates (row = CURSOR_Y - 1 , col = CURSOR_X )
209
200
)
210
201
211
-
212
202
@patch .object (_win32_console , "SetConsoleCursorPosition" , return_value = None )
213
203
@patch .object (
214
204
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
@@ -222,7 +212,6 @@ def test_move_cursor_down(_, SetConsoleCursorPosition, win32_handle):
222
212
win32_handle , coords = WindowsCoordinates (row = CURSOR_Y + 1 , col = CURSOR_X )
223
213
)
224
214
225
-
226
215
@patch .object (_win32_console , "SetConsoleCursorPosition" , return_value = None )
227
216
@patch .object (
228
217
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
@@ -236,16 +225,15 @@ def test_move_cursor_forward(_, SetConsoleCursorPosition, win32_handle):
236
225
win32_handle , coords = WindowsCoordinates (row = CURSOR_Y , col = CURSOR_X + 1 )
237
226
)
238
227
239
-
240
228
@patch .object (_win32_console , "SetConsoleCursorPosition" , return_value = None )
241
229
def test_move_cursor_forward_newline_wrap (SetConsoleCursorPosition , win32_handle ):
242
230
cursor_at_end_of_line = StubScreenBufferInfo (
243
231
dwCursorPosition = COORD (SCREEN_WIDTH - 1 , CURSOR_Y )
244
232
)
245
233
with patch .object (
246
- _win32_console ,
247
- "GetConsoleScreenBufferInfo" ,
248
- return_value = cursor_at_end_of_line ,
234
+ _win32_console ,
235
+ "GetConsoleScreenBufferInfo" ,
236
+ return_value = cursor_at_end_of_line ,
249
237
):
250
238
term = LegacyWindowsTerm ()
251
239
term .move_cursor_forward ()
@@ -254,7 +242,6 @@ def test_move_cursor_forward_newline_wrap(SetConsoleCursorPosition, win32_handle
254
242
win32_handle , coords = WindowsCoordinates (row = CURSOR_Y + 1 , col = 0 )
255
243
)
256
244
257
-
258
245
@patch .object (_win32_console , "SetConsoleCursorPosition" , return_value = None )
259
246
@patch .object (
260
247
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
@@ -266,7 +253,6 @@ def test_move_cursor_to_column(_, SetConsoleCursorPosition, win32_handle):
266
253
win32_handle , coords = WindowsCoordinates (CURSOR_Y , 5 )
267
254
)
268
255
269
-
270
256
@patch .object (_win32_console , "SetConsoleCursorPosition" , return_value = None )
271
257
@patch .object (
272
258
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
@@ -278,22 +264,25 @@ def test_move_cursor_backward(_, SetConsoleCursorPosition, win32_handle):
278
264
win32_handle , coords = WindowsCoordinates (row = CURSOR_Y , col = CURSOR_X - 1 )
279
265
)
280
266
281
-
282
267
@patch .object (_win32_console , "SetConsoleCursorPosition" , return_value = None )
283
- def test_move_cursor_backward_prev_line_wrap (SetConsoleCursorPosition , win32_handle ):
284
- cursor_at_start_of_line = StubScreenBufferInfo (dwCursorPosition = COORD (0 , CURSOR_Y ))
268
+ def test_move_cursor_backward_prev_line_wrap (
269
+ SetConsoleCursorPosition , win32_handle
270
+ ):
271
+ cursor_at_start_of_line = StubScreenBufferInfo (
272
+ dwCursorPosition = COORD (0 , CURSOR_Y )
273
+ )
285
274
with patch .object (
286
- _win32_console ,
287
- "GetConsoleScreenBufferInfo" ,
288
- return_value = cursor_at_start_of_line ,
275
+ _win32_console ,
276
+ "GetConsoleScreenBufferInfo" ,
277
+ return_value = cursor_at_start_of_line ,
289
278
):
290
279
term = LegacyWindowsTerm ()
291
280
term .move_cursor_backward ()
292
281
SetConsoleCursorPosition .assert_called_once_with (
293
- win32_handle , coords = WindowsCoordinates (row = CURSOR_Y - 1 , col = SCREEN_WIDTH - 1 )
282
+ win32_handle ,
283
+ coords = WindowsCoordinates (row = CURSOR_Y - 1 , col = SCREEN_WIDTH - 1 ),
294
284
)
295
285
296
-
297
286
@patch .object (_win32_console , "SetConsoleCursorInfo" , return_value = None )
298
287
@patch .object (
299
288
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
@@ -308,7 +297,6 @@ def test_hide_cursor(_, SetConsoleCursorInfo, win32_handle):
308
297
assert call_args [0 ].kwargs ["cursor_info" ].bVisible == 0
309
298
assert call_args [0 ].kwargs ["cursor_info" ].dwSize == 100
310
299
311
-
312
300
@patch .object (_win32_console , "SetConsoleCursorInfo" , return_value = None )
313
301
@patch .object (
314
302
_win32_console , "GetConsoleScreenBufferInfo" , return_value = StubScreenBufferInfo
@@ -323,15 +311,13 @@ def test_show_cursor(_, SetConsoleCursorInfo, win32_handle):
323
311
assert call_args [0 ].kwargs ["cursor_info" ].bVisible == 1
324
312
assert call_args [0 ].kwargs ["cursor_info" ].dwSize == 100
325
313
326
-
327
314
@patch .object (_win32_console , "SetConsoleTitle" , return_value = None )
328
315
def test_set_title (SetConsoleTitle ):
329
316
term = LegacyWindowsTerm ()
330
317
term .set_title ("title" )
331
318
332
319
SetConsoleTitle .assert_called_once_with ("title" )
333
320
334
-
335
321
@patch .object (_win32_console , "SetConsoleTitle" , return_value = None )
336
322
def test_set_title_too_long (_ ):
337
323
term = LegacyWindowsTerm ()
0 commit comments