1010import math
1111import os
1212import weakref
13- from typing import Optional
13+ from enum import Enum
14+ from typing import Dict , List , Optional , Union
1415
15- from prompt_toolkit .application . current import get_app , get_app_or_none , set_app
16+ from prompt_toolkit .application import Application , get_app , get_app_or_none , set_app
1617from prompt_toolkit .buffer import Buffer
1718from ptterm import Terminal
1819
19- __all__ = (
20+ __all__ = [
2021 "LayoutTypes" ,
2122 "Pane" ,
2223 "HSplit" ,
2324 "VSplit" ,
2425 "Window" ,
2526 "Arrangement" ,
26- )
27+ ]
2728
2829
29- class LayoutTypes :
30+ class LayoutTypes ( Enum ) :
3031 # The values are in lowercase with dashes, because that is what users can
3132 # use at the command line.
32- EVEN_HORIZONTAL = "even-horizontal"
33- EVEN_VERTICAL = "even-vertical"
34- MAIN_HORIZONTAL = "main-horizontal"
35- MAIN_VERTICAL = "main-vertical"
36- TILED = "tiled"
37-
38- _ALL = [EVEN_HORIZONTAL , EVEN_VERTICAL , MAIN_HORIZONTAL , MAIN_VERTICAL , TILED ]
33+ EVEN_HORIZONTAL = "EVEN-HORIZONTAL"
34+ EVEN_VERTICAL = "EVEN-VERTICAL"
35+ MAIN_HORIZONTAL = "MAIN-HORIZONTAL"
36+ MAIN_VERTICAL = "MAIN-VERTICAL"
37+ TILED = "TILED"
3938
4039
4140class Pane :
@@ -50,7 +49,7 @@ class Pane:
5049
5150 def __init__ (self , terminal : Terminal ) -> None :
5251 self .terminal = terminal
53- self .chosen_name = None
52+ self .chosen_name : Optional [ str ] = None
5453
5554 # Displayed the clock instead of this pane content.
5655 self .clock_mode = False
@@ -156,13 +155,13 @@ class Window:
156155
157156 _window_counter = 1000 # Start here, to avoid confusion with window index.
158157
159- def __init__ (self , index = 0 ):
158+ def __init__ (self , index = 0 ) -> None :
160159 self .index = index
161- self .root = HSplit ()
162- self ._active_pane = None
163- self ._prev_active_pane = None
164- self .chosen_name = None
165- self .previous_selected_layout = None
160+ self .root : Union [ VSplit , HSplit ] = HSplit ()
161+ self ._active_pane : Optional [ Pane ] = None
162+ self ._prev_active_pane : Optional [ Pane ] = None
163+ self .chosen_name : Optional [ str ] = None
164+ self .previous_selected_layout : Optional [ LayoutTypes ] = None
166165
167166 #: When true, the current pane is zoomed in.
168167 self .zoom = False
@@ -209,7 +208,7 @@ def active_pane(self):
209208 return self ._active_pane
210209
211210 @active_pane .setter
212- def active_pane (self , value : Pane ):
211+ def active_pane (self , value : Pane ) -> None :
213212 # Remember previous active pane.
214213 if self ._active_pane :
215214 self ._prev_active_pane = weakref .ref (self ._active_pane )
@@ -347,7 +346,7 @@ def active_process(self):
347346 if p is not None :
348347 return p .process
349348
350- def focus_next (self , count = 1 ):
349+ def focus_next (self , count = 1 ) -> None :
351350 " Focus the next pane. "
352351 panes = self .panes
353352 if panes :
@@ -357,7 +356,7 @@ def focus_next(self, count=1):
357356 else :
358357 self .active_pane = None # No panes left.
359358
360- def focus_previous (self ):
359+ def focus_previous (self ) -> None :
361360 " Focus the previous pane. "
362361 self .focus_next (count = - 1 )
363362
@@ -394,12 +393,10 @@ def rotate(self, count=1, with_pane_before_only=False, with_pane_after_only=Fals
394393 split [index ] = new_item
395394 split .weights [new_item ] = weight
396395
397- def select_layout (self , layout_type ):
396+ def select_layout (self , layout_type : LayoutTypes ):
398397 """
399398 Select one of the predefined layouts.
400399 """
401- assert layout_type in LayoutTypes ._ALL
402-
403400 # When there is only one pane, always choose EVEN_HORIZONTAL,
404401 # Otherwise, we create VSplit/HSplit instances with an empty list of
405402 # children.
@@ -453,7 +450,7 @@ def select_layout(self, layout_type):
453450
454451 self .previous_selected_layout = layout_type
455452
456- def select_next_layout (self , count = 1 ) :
453+ def select_next_layout (self , count : int = 1 ) -> None :
457454 """
458455 Select next layout. (Cycle through predefined layouts.)
459456 """
@@ -462,10 +459,10 @@ def select_next_layout(self, count=1):
462459 if len (self .panes ) == 2 :
463460 all_layouts = [LayoutTypes .EVEN_HORIZONTAL , LayoutTypes .EVEN_VERTICAL ]
464461 else :
465- all_layouts = LayoutTypes . _ALL
462+ all_layouts = list ( LayoutTypes )
466463
467464 # Get index of current layout.
468- layout = self .previous_selected_layout or LayoutTypes . _ALL [- 1 ]
465+ layout = self .previous_selected_layout or list ( LayoutTypes ) [- 1 ]
469466 try :
470467 index = all_layouts .index (layout )
471468 except ValueError :
@@ -475,7 +472,7 @@ def select_next_layout(self, count=1):
475472 new_layout = all_layouts [(index + count ) % len (all_layouts )]
476473 self .select_layout (new_layout )
477474
478- def select_previous_layout (self ):
475+ def select_previous_layout (self ) -> None :
479476 self .select_next_layout (count = - 1 )
480477
481478 def change_size_for_active_pane (self , up = 0 , right = 0 , down = 0 , left = 0 ):
@@ -558,18 +555,22 @@ class Arrangement:
558555 have different windows active.
559556 """
560557
561- def __init__ (self ):
562- self .windows = []
558+ def __init__ (self ) -> None :
559+ self .windows : List [ Window ] = []
563560 self .base_index = 0
564561
565- self ._active_window_for_cli = weakref .WeakKeyDictionary ()
566- self ._prev_active_window_for_cli = weakref .WeakKeyDictionary ()
562+ self ._active_window_for_cli : Dict [
563+ Application , Window
564+ ] = weakref .WeakKeyDictionary ()
565+ self ._prev_active_window_for_cli : Dict [
566+ Application , Window
567+ ] = weakref .WeakKeyDictionary ()
567568
568569 # The active window of the last CLI. Used as default when a new session
569570 # is attached.
570- self ._last_active_window = None
571+ self ._last_active_window : Optional [ Window ] = None
571572
572- def invalidation_hash (self ):
573+ def invalidation_hash (self ) -> str :
573574 """
574575 When this changes, the layout needs to be rebuild.
575576 """
@@ -579,7 +580,7 @@ def invalidation_hash(self):
579580 w = self .get_active_window ()
580581 return w .invalidation_hash ()
581582
582- def get_active_window (self ):
583+ def get_active_window (self ) -> Window :
583584 """
584585 The current active :class:`.Window`.
585586 """
@@ -593,15 +594,15 @@ def get_active_window(self):
593594 )
594595 return self .windows [0 ]
595596
596- def set_active_window (self , window : Window ):
597+ def set_active_window (self , window : Window ) -> None :
597598 app = get_app ()
598599
599600 previous = self .get_active_window ()
600601 self ._prev_active_window_for_cli [app ] = previous
601602 self ._active_window_for_cli [app ] = window
602603 self ._last_active_window = window
603604
604- def set_active_window_from_pane_id (self , pane_id : int ):
605+ def set_active_window_from_pane_id (self , pane_id : int ) -> None :
605606 """
606607 Make the window with this pane ID the active Window.
607608 """
@@ -610,7 +611,7 @@ def set_active_window_from_pane_id(self, pane_id: int):
610611 if p .pane_id == pane_id :
611612 self .set_active_window (w )
612613
613- def get_previous_active_window (self ):
614+ def get_previous_active_window (self ) -> Optional [ Window ] :
614615 " The previous active Window or None if unknown. "
615616 app = get_app ()
616617
@@ -625,7 +626,9 @@ def get_window_by_index(self, index):
625626 if w .index == index :
626627 return w
627628
628- def create_window (self , pane : Pane , name : Optional [str ] = None , set_active = True ):
629+ def create_window (
630+ self , pane : Pane , name : Optional [str ] = None , set_active : bool = True
631+ ) -> None :
629632 """
630633 Create a new window that contains just this pane.
631634
@@ -659,7 +662,7 @@ def create_window(self, pane: Pane, name: Optional[str] = None, set_active=True)
659662 assert w .active_pane == pane
660663 assert w ._get_parent (pane )
661664
662- def move_window (self , window : Window , new_index : int ):
665+ def move_window (self , window : Window , new_index : int ) -> None :
663666 """
664667 Move window to a new index.
665668 """
@@ -668,15 +671,16 @@ def move_window(self, window: Window, new_index: int):
668671 # Sort windows by index.
669672 self .windows = sorted (self .windows , key = lambda w : w .index )
670673
671- def get_active_pane (self ):
674+ def get_active_pane (self ) -> Optional [ Pane ] :
672675 """
673676 The current :class:`.Pane` from the current window.
674677 """
675678 w = self .get_active_window ()
676679 if w is not None :
677680 return w .active_pane
681+ return None
678682
679- def remove_pane (self , pane : Pane ):
683+ def remove_pane (self , pane : Pane ) -> None :
680684 """
681685 Remove a :class:`.Pane`. (Look in all windows.)
682686 """
@@ -693,21 +697,21 @@ def remove_pane(self, pane: Pane):
693697
694698 self .windows .remove (w )
695699
696- def focus_previous_window (self ):
700+ def focus_previous_window (self ) -> None :
697701 w = self .get_active_window ()
698702
699703 self .set_active_window (
700704 self .windows [(self .windows .index (w ) - 1 ) % len (self .windows )]
701705 )
702706
703- def focus_next_window (self ):
707+ def focus_next_window (self ) -> None :
704708 w = self .get_active_window ()
705709
706710 self .set_active_window (
707711 self .windows [(self .windows .index (w ) + 1 ) % len (self .windows )]
708712 )
709713
710- def break_pane (self , set_active = True ):
714+ def break_pane (self , set_active : bool = True ) -> None :
711715 """
712716 When the current window has multiple panes, remove the pane from this
713717 window and put it in a new window.
@@ -721,13 +725,13 @@ def break_pane(self, set_active=True):
721725 self .get_active_window ().remove_pane (pane )
722726 self .create_window (pane , set_active = set_active )
723727
724- def rotate_window (self , count = 1 ) :
728+ def rotate_window (self , count : int = 1 ) -> None :
725729 " Rotate the panes in the active window. "
726730 w = self .get_active_window ()
727731 w .rotate (count = count )
728732
729733 @property
730- def has_panes (self ):
734+ def has_panes (self ) -> bool :
731735 " True when any of the windows has a :class:`.Pane`. "
732736 for w in self .windows :
733737 if w .has_panes :
0 commit comments