Skip to content

Commit 6657975

Browse files
committed
Remove is_windows() calls from the renderer
1 parent ffc3a0c commit 6657975

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

prompt_toolkit/renderer.py

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
DummyStyleTransformation,
2222
StyleTransformation,
2323
)
24-
from prompt_toolkit.utils import is_windows
2524

2625
if TYPE_CHECKING:
2726
from prompt_toolkit.application import Application
@@ -396,7 +395,8 @@ def reset(self, _scroll: bool = False, leave_alternate_screen: bool = True) -> N
396395

397396
# In case of Windows, also make sure to scroll to the current cursor
398397
# position. (Only when rendering the first time.)
399-
if is_windows() and _scroll:
398+
# It does nothing for vt100 terminals.
399+
if _scroll:
400400
self.output.scroll_buffer_to_prompt()
401401

402402
# Quit alternate screen.
@@ -432,9 +432,13 @@ def height_is_known(self) -> bool:
432432
is known. (It's often nicer to draw bottom toolbars only if the height
433433
is known, in order to avoid flickering when the CPR response arrives.)
434434
"""
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
438442

439443
@property
440444
def rows_above_layout(self) -> int:
@@ -468,42 +472,48 @@ def request_absolute_cursor_position(self) -> None:
468472
# In full-screen mode, always use the total height as min-available-height.
469473
if self.full_screen:
470474
self._min_available_height = self.output.get_size().rows
475+
return
471476

472477
# For Win32, we have an API call to get the number of rows below the
473478
# cursor.
474-
elif is_windows():
479+
try:
475480
self._min_available_height = self.output.get_rows_below_cursor_position()
481+
return
482+
except NotImplementedError:
483+
pass
476484

477485
# 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()
481493

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
486497

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
489502

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()
494504

495-
async def timer() -> None:
496-
await sleep(self.CPR_TIMEOUT)
505+
async def timer() -> None:
506+
await sleep(self.CPR_TIMEOUT)
497507

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
501511

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()
505515

506-
get_app().create_background_task(timer())
516+
get_app().create_background_task(timer())
507517

508518
def report_absolute_cursor_row(self, row: int) -> None:
509519
"""

0 commit comments

Comments
 (0)