10
10
import math
11
11
import os
12
12
import weakref
13
- from typing import Optional
13
+ from enum import Enum
14
+ from typing import Dict , List , Optional , Union
14
15
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
16
17
from prompt_toolkit .buffer import Buffer
17
18
from ptterm import Terminal
18
19
19
- __all__ = (
20
+ __all__ = [
20
21
"LayoutTypes" ,
21
22
"Pane" ,
22
23
"HSplit" ,
23
24
"VSplit" ,
24
25
"Window" ,
25
26
"Arrangement" ,
26
- )
27
+ ]
27
28
28
29
29
- class LayoutTypes :
30
+ class LayoutTypes ( Enum ) :
30
31
# The values are in lowercase with dashes, because that is what users can
31
32
# 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"
39
38
40
39
41
40
class Pane :
@@ -50,7 +49,7 @@ class Pane:
50
49
51
50
def __init__ (self , terminal : Terminal ) -> None :
52
51
self .terminal = terminal
53
- self .chosen_name = None
52
+ self .chosen_name : Optional [ str ] = None
54
53
55
54
# Displayed the clock instead of this pane content.
56
55
self .clock_mode = False
@@ -156,13 +155,13 @@ class Window:
156
155
157
156
_window_counter = 1000 # Start here, to avoid confusion with window index.
158
157
159
- def __init__ (self , index = 0 ):
158
+ def __init__ (self , index = 0 ) -> None :
160
159
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
166
165
167
166
#: When true, the current pane is zoomed in.
168
167
self .zoom = False
@@ -209,7 +208,7 @@ def active_pane(self):
209
208
return self ._active_pane
210
209
211
210
@active_pane .setter
212
- def active_pane (self , value : Pane ):
211
+ def active_pane (self , value : Pane ) -> None :
213
212
# Remember previous active pane.
214
213
if self ._active_pane :
215
214
self ._prev_active_pane = weakref .ref (self ._active_pane )
@@ -347,7 +346,7 @@ def active_process(self):
347
346
if p is not None :
348
347
return p .process
349
348
350
- def focus_next (self , count = 1 ):
349
+ def focus_next (self , count = 1 ) -> None :
351
350
" Focus the next pane. "
352
351
panes = self .panes
353
352
if panes :
@@ -357,7 +356,7 @@ def focus_next(self, count=1):
357
356
else :
358
357
self .active_pane = None # No panes left.
359
358
360
- def focus_previous (self ):
359
+ def focus_previous (self ) -> None :
361
360
" Focus the previous pane. "
362
361
self .focus_next (count = - 1 )
363
362
@@ -394,12 +393,10 @@ def rotate(self, count=1, with_pane_before_only=False, with_pane_after_only=Fals
394
393
split [index ] = new_item
395
394
split .weights [new_item ] = weight
396
395
397
- def select_layout (self , layout_type ):
396
+ def select_layout (self , layout_type : LayoutTypes ):
398
397
"""
399
398
Select one of the predefined layouts.
400
399
"""
401
- assert layout_type in LayoutTypes ._ALL
402
-
403
400
# When there is only one pane, always choose EVEN_HORIZONTAL,
404
401
# Otherwise, we create VSplit/HSplit instances with an empty list of
405
402
# children.
@@ -453,7 +450,7 @@ def select_layout(self, layout_type):
453
450
454
451
self .previous_selected_layout = layout_type
455
452
456
- def select_next_layout (self , count = 1 ) :
453
+ def select_next_layout (self , count : int = 1 ) -> None :
457
454
"""
458
455
Select next layout. (Cycle through predefined layouts.)
459
456
"""
@@ -462,10 +459,10 @@ def select_next_layout(self, count=1):
462
459
if len (self .panes ) == 2 :
463
460
all_layouts = [LayoutTypes .EVEN_HORIZONTAL , LayoutTypes .EVEN_VERTICAL ]
464
461
else :
465
- all_layouts = LayoutTypes . _ALL
462
+ all_layouts = list ( LayoutTypes )
466
463
467
464
# 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 ]
469
466
try :
470
467
index = all_layouts .index (layout )
471
468
except ValueError :
@@ -475,7 +472,7 @@ def select_next_layout(self, count=1):
475
472
new_layout = all_layouts [(index + count ) % len (all_layouts )]
476
473
self .select_layout (new_layout )
477
474
478
- def select_previous_layout (self ):
475
+ def select_previous_layout (self ) -> None :
479
476
self .select_next_layout (count = - 1 )
480
477
481
478
def change_size_for_active_pane (self , up = 0 , right = 0 , down = 0 , left = 0 ):
@@ -558,18 +555,22 @@ class Arrangement:
558
555
have different windows active.
559
556
"""
560
557
561
- def __init__ (self ):
562
- self .windows = []
558
+ def __init__ (self ) -> None :
559
+ self .windows : List [ Window ] = []
563
560
self .base_index = 0
564
561
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 ()
567
568
568
569
# The active window of the last CLI. Used as default when a new session
569
570
# is attached.
570
- self ._last_active_window = None
571
+ self ._last_active_window : Optional [ Window ] = None
571
572
572
- def invalidation_hash (self ):
573
+ def invalidation_hash (self ) -> str :
573
574
"""
574
575
When this changes, the layout needs to be rebuild.
575
576
"""
@@ -579,7 +580,7 @@ def invalidation_hash(self):
579
580
w = self .get_active_window ()
580
581
return w .invalidation_hash ()
581
582
582
- def get_active_window (self ):
583
+ def get_active_window (self ) -> Window :
583
584
"""
584
585
The current active :class:`.Window`.
585
586
"""
@@ -593,15 +594,15 @@ def get_active_window(self):
593
594
)
594
595
return self .windows [0 ]
595
596
596
- def set_active_window (self , window : Window ):
597
+ def set_active_window (self , window : Window ) -> None :
597
598
app = get_app ()
598
599
599
600
previous = self .get_active_window ()
600
601
self ._prev_active_window_for_cli [app ] = previous
601
602
self ._active_window_for_cli [app ] = window
602
603
self ._last_active_window = window
603
604
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 :
605
606
"""
606
607
Make the window with this pane ID the active Window.
607
608
"""
@@ -610,7 +611,7 @@ def set_active_window_from_pane_id(self, pane_id: int):
610
611
if p .pane_id == pane_id :
611
612
self .set_active_window (w )
612
613
613
- def get_previous_active_window (self ):
614
+ def get_previous_active_window (self ) -> Optional [ Window ] :
614
615
" The previous active Window or None if unknown. "
615
616
app = get_app ()
616
617
@@ -625,7 +626,9 @@ def get_window_by_index(self, index):
625
626
if w .index == index :
626
627
return w
627
628
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 :
629
632
"""
630
633
Create a new window that contains just this pane.
631
634
@@ -659,7 +662,7 @@ def create_window(self, pane: Pane, name: Optional[str] = None, set_active=True)
659
662
assert w .active_pane == pane
660
663
assert w ._get_parent (pane )
661
664
662
- def move_window (self , window : Window , new_index : int ):
665
+ def move_window (self , window : Window , new_index : int ) -> None :
663
666
"""
664
667
Move window to a new index.
665
668
"""
@@ -668,15 +671,16 @@ def move_window(self, window: Window, new_index: int):
668
671
# Sort windows by index.
669
672
self .windows = sorted (self .windows , key = lambda w : w .index )
670
673
671
- def get_active_pane (self ):
674
+ def get_active_pane (self ) -> Optional [ Pane ] :
672
675
"""
673
676
The current :class:`.Pane` from the current window.
674
677
"""
675
678
w = self .get_active_window ()
676
679
if w is not None :
677
680
return w .active_pane
681
+ return None
678
682
679
- def remove_pane (self , pane : Pane ):
683
+ def remove_pane (self , pane : Pane ) -> None :
680
684
"""
681
685
Remove a :class:`.Pane`. (Look in all windows.)
682
686
"""
@@ -693,21 +697,21 @@ def remove_pane(self, pane: Pane):
693
697
694
698
self .windows .remove (w )
695
699
696
- def focus_previous_window (self ):
700
+ def focus_previous_window (self ) -> None :
697
701
w = self .get_active_window ()
698
702
699
703
self .set_active_window (
700
704
self .windows [(self .windows .index (w ) - 1 ) % len (self .windows )]
701
705
)
702
706
703
- def focus_next_window (self ):
707
+ def focus_next_window (self ) -> None :
704
708
w = self .get_active_window ()
705
709
706
710
self .set_active_window (
707
711
self .windows [(self .windows .index (w ) + 1 ) % len (self .windows )]
708
712
)
709
713
710
- def break_pane (self , set_active = True ):
714
+ def break_pane (self , set_active : bool = True ) -> None :
711
715
"""
712
716
When the current window has multiple panes, remove the pane from this
713
717
window and put it in a new window.
@@ -721,13 +725,13 @@ def break_pane(self, set_active=True):
721
725
self .get_active_window ().remove_pane (pane )
722
726
self .create_window (pane , set_active = set_active )
723
727
724
- def rotate_window (self , count = 1 ) :
728
+ def rotate_window (self , count : int = 1 ) -> None :
725
729
" Rotate the panes in the active window. "
726
730
w = self .get_active_window ()
727
731
w .rotate (count = count )
728
732
729
733
@property
730
- def has_panes (self ):
734
+ def has_panes (self ) -> bool :
731
735
" True when any of the windows has a :class:`.Pane`. "
732
736
for w in self .windows :
733
737
if w .has_panes :
0 commit comments