7
7
An arrangement consists of a list of windows. And a window has a list of panes,
8
8
arranged by ordering them in HSplit/VSplit instances.
9
9
"""
10
- from __future__ import unicode_literals
11
-
12
- from ptterm import Terminal
13
- from prompt_toolkit .application .current import get_app , get_app_or_none , set_app
14
- from prompt_toolkit .buffer import Buffer
15
-
16
10
import math
17
11
import os
18
12
import weakref
19
- import six
13
+ from typing import Optional
14
+
15
+ from prompt_toolkit .application .current import get_app , get_app_or_none , set_app
16
+ from prompt_toolkit .buffer import Buffer
17
+ from ptterm import Terminal
20
18
21
19
__all__ = (
22
- ' LayoutTypes' ,
23
- ' Pane' ,
24
- ' HSplit' ,
25
- ' VSplit' ,
26
- ' Window' ,
27
- ' Arrangement' ,
20
+ " LayoutTypes" ,
21
+ " Pane" ,
22
+ " HSplit" ,
23
+ " VSplit" ,
24
+ " Window" ,
25
+ " Arrangement" ,
28
26
)
29
27
30
28
31
29
class LayoutTypes :
32
30
# The values are in lowercase with dashes, because that is what users can
33
31
# use at the command line.
34
- EVEN_HORIZONTAL = ' even-horizontal'
35
- EVEN_VERTICAL = ' even-vertical'
36
- MAIN_HORIZONTAL = ' main-horizontal'
37
- MAIN_VERTICAL = ' main-vertical'
38
- TILED = ' tiled'
32
+ EVEN_HORIZONTAL = " even-horizontal"
33
+ EVEN_VERTICAL = " even-vertical"
34
+ MAIN_HORIZONTAL = " main-horizontal"
35
+ MAIN_VERTICAL = " main-vertical"
36
+ TILED = " tiled"
39
37
40
38
_ALL = [EVEN_HORIZONTAL , EVEN_VERTICAL , MAIN_HORIZONTAL , MAIN_VERTICAL , TILED ]
41
39
42
40
43
- class Pane ( object ) :
41
+ class Pane :
44
42
"""
45
43
One pane, containing one process and a search buffer for going into copy
46
44
mode or displaying the help.
47
45
"""
48
- _pane_counter = 1000 # Start at 1000, to be sure to not confuse this with pane indexes.
46
+
47
+ _pane_counter = (
48
+ 1000 # Start at 1000, to be sure to not confuse this with pane indexes.
49
+ )
49
50
50
51
def __init__ (self , terminal = None ):
51
52
assert isinstance (terminal , Terminal )
@@ -68,7 +69,7 @@ def __init__(self, terminal=None):
68
69
self .scroll_buffer = Buffer (read_only = True )
69
70
self .copy_get_tokens_for_line = lambda lineno : []
70
71
self .display_scroll_buffer = False
71
- self .scroll_buffer_title = ''
72
+ self .scroll_buffer_title = ""
72
73
73
74
@property
74
75
def process (self ):
@@ -88,7 +89,7 @@ def name(self):
88
89
if name :
89
90
return os .path .basename (name )
90
91
91
- return ''
92
+ return ""
92
93
93
94
def enter_copy_mode (self ):
94
95
"""
@@ -114,6 +115,7 @@ class _WeightsDictionary(weakref.WeakKeyDictionary):
114
115
This dictionary maps the child (another HSplit/VSplit or Pane), to the
115
116
size. (Integer.)
116
117
"""
118
+
117
119
def __getitem__ (self , key ):
118
120
try :
119
121
# (Don't use 'super' here. This is a classobj in Python2.)
@@ -127,6 +129,7 @@ class _Split(list):
127
129
Base class for horizontal and vertical splits. (This is a higher level
128
130
split than prompt_toolkit.layout.HSplit.)
129
131
"""
132
+
130
133
def __init__ (self , * a , ** kw ):
131
134
list .__init__ (self , * a , ** kw )
132
135
@@ -138,7 +141,7 @@ def __hash__(self):
138
141
return id (self )
139
142
140
143
def __repr__ (self ):
141
- return ' %s(%s)' % (self .__class__ .__name__ , list .__repr__ (self ))
144
+ return " %s(%s)" % (self .__class__ .__name__ , list .__repr__ (self ))
142
145
143
146
144
147
class HSplit (_Split ):
@@ -153,6 +156,7 @@ class Window(object):
153
156
"""
154
157
Pymux window.
155
158
"""
159
+
156
160
_window_counter = 1000 # Start here, to avoid confusion with window index.
157
161
158
162
def __init__ (self , index = 0 ):
@@ -178,24 +182,27 @@ def invalidation_hash(self):
178
182
Return a hash (string) that can be used to determine when the layout
179
183
has to be rebuild.
180
184
"""
181
- # if not self.root:
182
- # return '<empty-window>'
185
+ # if not self.root:
186
+ # return '<empty-window>'
183
187
184
188
def _hash_for_split (split ):
185
189
result = []
186
190
for item in split :
187
191
if isinstance (item , (VSplit , HSplit )):
188
192
result .append (_hash_for_split (item ))
189
193
elif isinstance (item , Pane ):
190
- result .append (' p%s' % item .pane_id )
194
+ result .append (" p%s" % item .pane_id )
191
195
192
196
if isinstance (split , HSplit ):
193
- return ' HSplit(%s)' % (',' .join (result ))
197
+ return " HSplit(%s)" % ("," .join (result ))
194
198
else :
195
- return ' VSplit(%s)' % (',' .join (result ))
199
+ return " VSplit(%s)" % ("," .join (result ))
196
200
197
- return '<window_id=%s,zoom=%s,children=%s>' % (
198
- self .window_id , self .zoom , _hash_for_split (self .root ))
201
+ return "<window_id=%s,zoom=%s,children=%s>" % (
202
+ self .window_id ,
203
+ self .zoom ,
204
+ _hash_for_split (self .root ),
205
+ )
199
206
200
207
@property
201
208
def active_pane (self ):
@@ -240,7 +247,7 @@ def name(self):
240
247
if pane :
241
248
return pane .name
242
249
243
- return ''
250
+ return ""
244
251
245
252
def add_pane (self , pane , vsplit = False ):
246
253
"""
@@ -354,7 +361,9 @@ def focus_next(self, count=1):
354
361
" Focus the next pane. "
355
362
panes = self .panes
356
363
if panes :
357
- self .active_pane = panes [(panes .index (self .active_pane ) + count ) % len (panes )]
364
+ self .active_pane = panes [
365
+ (panes .index (self .active_pane ) + count ) % len (panes )
366
+ ]
358
367
else :
359
368
self .active_pane = None # No panes left.
360
369
@@ -381,10 +390,10 @@ def rotate(self, count=1, with_pane_before_only=False, with_pane_after_only=Fals
381
390
382
391
# Only before after? Reduce list of panes.
383
392
if with_pane_before_only :
384
- items = items [current_pane_index - 1 : current_pane_index + 1 ]
393
+ items = items [current_pane_index - 1 : current_pane_index + 1 ]
385
394
386
395
elif with_pane_after_only :
387
- items = items [current_pane_index : current_pane_index + 2 ]
396
+ items = items [current_pane_index : current_pane_index + 2 ]
388
397
389
398
# Rotate positions.
390
399
for i , triple in enumerate (items ):
@@ -417,22 +426,26 @@ def select_layout(self, layout_type):
417
426
418
427
# main-horizontal.
419
428
elif layout_type == LayoutTypes .MAIN_HORIZONTAL :
420
- self .root = HSplit ([
421
- self .active_pane ,
422
- VSplit ([p for p in self .panes if p != self .active_pane ])
423
- ])
429
+ self .root = HSplit (
430
+ [
431
+ self .active_pane ,
432
+ VSplit ([p for p in self .panes if p != self .active_pane ]),
433
+ ]
434
+ )
424
435
425
436
# main-vertical.
426
437
elif layout_type == LayoutTypes .MAIN_VERTICAL :
427
- self .root = VSplit ([
428
- self .active_pane ,
429
- HSplit ([p for p in self .panes if p != self .active_pane ])
430
- ])
438
+ self .root = VSplit (
439
+ [
440
+ self .active_pane ,
441
+ HSplit ([p for p in self .panes if p != self .active_pane ]),
442
+ ]
443
+ )
431
444
432
445
# tiled.
433
446
elif layout_type == LayoutTypes .TILED :
434
447
panes = self .panes
435
- column_count = math .ceil (len (panes ) ** .5 )
448
+ column_count = math .ceil (len (panes ) ** 0 .5 )
436
449
437
450
rows = HSplit ()
438
451
current_row = VSplit ()
@@ -495,9 +508,11 @@ def find_split_and_child(split_cls, is_before):
495
508
split = self ._get_parent (child )
496
509
497
510
def found ():
498
- return isinstance (split , split_cls ) and (
499
- not is_before or split .index (child ) > 0 ) and (
500
- is_before or split .index (child ) < len (split ) - 1 )
511
+ return (
512
+ isinstance (split , split_cls )
513
+ and (not is_before or split .index (child ) > 0 )
514
+ and (is_before or split .index (child ) < len (split ) - 1 )
515
+ )
501
516
502
517
while split and not found ():
503
518
child = split
@@ -532,8 +547,9 @@ def handle_side(split_cls, is_before, amount, trying_other_side=False):
532
547
# case it's logical to move the left border to the right
533
548
# instead.
534
549
if not trying_other_side :
535
- handle_side (split_cls , not is_before , - amount ,
536
- trying_other_side = True )
550
+ handle_side (
551
+ split_cls , not is_before , - amount , trying_other_side = True
552
+ )
537
553
538
554
handle_side (VSplit , True , left )
539
555
handle_side (VSplit , False , right )
@@ -553,6 +569,7 @@ class Arrangement(object):
553
569
window. All the clients share the same Arrangement instance, but they can
554
570
have different windows active.
555
571
"""
572
+
556
573
def __init__ (self ):
557
574
self .windows = []
558
575
self .base_index = 0
@@ -569,7 +586,7 @@ def invalidation_hash(self):
569
586
When this changes, the layout needs to be rebuild.
570
587
"""
571
588
if not self .windows :
572
- return ' <no-windows>'
589
+ return " <no-windows>"
573
590
574
591
w = self .get_active_window ()
575
592
return w .invalidation_hash ()
@@ -583,7 +600,9 @@ def get_active_window(self):
583
600
try :
584
601
return self ._active_window_for_cli [app ]
585
602
except KeyError :
586
- self ._active_window_for_cli [app ] = self ._last_active_window or self .windows [0 ]
603
+ self ._active_window_for_cli [app ] = (
604
+ self ._last_active_window or self .windows [0 ]
605
+ )
587
606
return self .windows [0 ]
588
607
589
608
def set_active_window (self , window ):
@@ -621,17 +640,14 @@ def get_window_by_index(self, index):
621
640
if w .index == index :
622
641
return w
623
642
624
- def create_window (self , pane , name = None , set_active = True ):
643
+ def create_window (self , pane : Pane , name : Optional [ str ] = None , set_active = True ):
625
644
"""
626
645
Create a new window that contains just this pane.
627
646
628
647
:param pane: The :class:`.Pane` instance to put in the new window.
629
648
:param name: If given, name for the new window.
630
649
:param set_active: When True, focus the new window.
631
650
"""
632
- assert isinstance (pane , Pane )
633
- assert name is None or isinstance (name , six .text_type )
634
-
635
651
# Take the first available index.
636
652
taken_indexes = [w .index for w in self .windows ]
637
653
@@ -700,14 +716,16 @@ def remove_pane(self, pane):
700
716
def focus_previous_window (self ):
701
717
w = self .get_active_window ()
702
718
703
- self .set_active_window (self .windows [
704
- (self .windows .index (w ) - 1 ) % len (self .windows )])
719
+ self .set_active_window (
720
+ self .windows [(self .windows .index (w ) - 1 ) % len (self .windows )]
721
+ )
705
722
706
723
def focus_next_window (self ):
707
724
w = self .get_active_window ()
708
725
709
- self .set_active_window (self .windows [
710
- (self .windows .index (w ) + 1 ) % len (self .windows )])
726
+ self .set_active_window (
727
+ self .windows [(self .windows .index (w ) + 1 ) % len (self .windows )]
728
+ )
711
729
712
730
def break_pane (self , set_active = True ):
713
731
"""
0 commit comments