|
21 | 21 | DummyStyleTransformation, |
22 | 22 | StyleTransformation, |
23 | 23 | ) |
24 | | -from prompt_toolkit.utils import is_windows |
25 | 24 |
|
26 | 25 | if TYPE_CHECKING: |
27 | 26 | from prompt_toolkit.application import Application |
@@ -396,7 +395,8 @@ def reset(self, _scroll: bool = False, leave_alternate_screen: bool = True) -> N |
396 | 395 |
|
397 | 396 | # In case of Windows, also make sure to scroll to the current cursor |
398 | 397 | # position. (Only when rendering the first time.) |
399 | | - if is_windows() and _scroll: |
| 398 | + # It does nothing for vt100 terminals. |
| 399 | + if _scroll: |
400 | 400 | self.output.scroll_buffer_to_prompt() |
401 | 401 |
|
402 | 402 | # Quit alternate screen. |
@@ -432,9 +432,13 @@ def height_is_known(self) -> bool: |
432 | 432 | is known. (It's often nicer to draw bottom toolbars only if the height |
433 | 433 | is known, in order to avoid flickering when the CPR response arrives.) |
434 | 434 | """ |
435 | | - return ( |
436 | | - self.full_screen or self._min_available_height > 0 or is_windows() |
437 | | - ) # On Windows, we don't have to wait for a CPR. |
| 435 | + if self.full_screen or self._min_available_height > 0: |
| 436 | + return True |
| 437 | + try: |
| 438 | + self._min_available_height = self.output.get_rows_below_cursor_position() |
| 439 | + return True |
| 440 | + except NotImplementedError: |
| 441 | + return False |
438 | 442 |
|
439 | 443 | @property |
440 | 444 | def rows_above_layout(self) -> int: |
@@ -468,42 +472,48 @@ def request_absolute_cursor_position(self) -> None: |
468 | 472 | # In full-screen mode, always use the total height as min-available-height. |
469 | 473 | if self.full_screen: |
470 | 474 | self._min_available_height = self.output.get_size().rows |
| 475 | + return |
471 | 476 |
|
472 | 477 | # For Win32, we have an API call to get the number of rows below the |
473 | 478 | # cursor. |
474 | | - elif is_windows(): |
| 479 | + try: |
475 | 480 | self._min_available_height = self.output.get_rows_below_cursor_position() |
| 481 | + return |
| 482 | + except NotImplementedError: |
| 483 | + pass |
476 | 484 |
|
477 | 485 | # Use CPR. |
478 | | - else: |
479 | | - if self.cpr_support == CPR_Support.NOT_SUPPORTED: |
480 | | - return |
| 486 | + if self.cpr_support == CPR_Support.NOT_SUPPORTED: |
| 487 | + return |
| 488 | + |
| 489 | + def do_cpr() -> None: |
| 490 | + # Asks for a cursor position report (CPR). |
| 491 | + self._waiting_for_cpr_futures.append(Future()) |
| 492 | + self.output.ask_for_cpr() |
481 | 493 |
|
482 | | - def do_cpr() -> None: |
483 | | - # Asks for a cursor position report (CPR). |
484 | | - self._waiting_for_cpr_futures.append(Future()) |
485 | | - self.output.ask_for_cpr() |
| 494 | + if self.cpr_support == CPR_Support.SUPPORTED: |
| 495 | + do_cpr() |
| 496 | + return |
486 | 497 |
|
487 | | - if self.cpr_support == CPR_Support.SUPPORTED: |
488 | | - do_cpr() |
| 498 | + # If we don't know whether CPR is supported, only do a request if |
| 499 | + # none is pending, and test it, using a timer. |
| 500 | + if self.waiting_for_cpr: |
| 501 | + return |
489 | 502 |
|
490 | | - # If we don't know whether CPR is supported, only do a request if |
491 | | - # none is pending, and test it, using a timer. |
492 | | - elif self.cpr_support == CPR_Support.UNKNOWN and not self.waiting_for_cpr: |
493 | | - do_cpr() |
| 503 | + do_cpr() |
494 | 504 |
|
495 | | - async def timer() -> None: |
496 | | - await sleep(self.CPR_TIMEOUT) |
| 505 | + async def timer() -> None: |
| 506 | + await sleep(self.CPR_TIMEOUT) |
497 | 507 |
|
498 | | - # Not set in the meantime -> not supported. |
499 | | - if self.cpr_support == CPR_Support.UNKNOWN: |
500 | | - self.cpr_support = CPR_Support.NOT_SUPPORTED |
| 508 | + # Not set in the meantime -> not supported. |
| 509 | + if self.cpr_support == CPR_Support.UNKNOWN: |
| 510 | + self.cpr_support = CPR_Support.NOT_SUPPORTED |
501 | 511 |
|
502 | | - if self.cpr_not_supported_callback: |
503 | | - # Make sure to call this callback in the main thread. |
504 | | - self.cpr_not_supported_callback() |
| 512 | + if self.cpr_not_supported_callback: |
| 513 | + # Make sure to call this callback in the main thread. |
| 514 | + self.cpr_not_supported_callback() |
505 | 515 |
|
506 | | - get_app().create_background_task(timer()) |
| 516 | + get_app().create_background_task(timer()) |
507 | 517 |
|
508 | 518 | def report_absolute_cursor_row(self, row: int) -> None: |
509 | 519 | """ |
|
0 commit comments