8
8
arranged by ordering them in HSplit/VSplit instances.
9
9
"""
10
10
from __future__ import unicode_literals
11
- from .process import Process
12
11
12
+ from ptterm import Terminal
13
+ from prompt_toolkit .application .current import get_app , set_app
13
14
from prompt_toolkit .buffer import Buffer
14
- from prompt_toolkit .document import Document
15
- from prompt_toolkit .interface import CommandLineInterface
16
- from prompt_toolkit .search_state import SearchState
17
- from prompt_toolkit .token import Token
18
15
19
16
import math
20
17
import os
@@ -50,10 +47,10 @@ class Pane(object):
50
47
"""
51
48
_pane_counter = 1000 # Start at 1000, to be sure to not confuse this with pane indexes.
52
49
53
- def __init__ (self , process ):
54
- assert isinstance (process , Process )
50
+ def __init__ (self , terminal = None ):
51
+ assert isinstance (terminal , Terminal )
55
52
56
- self .process = process
53
+ self .terminal = terminal
57
54
self .chosen_name = None
58
55
59
56
# Displayed the clock instead of this pane content.
@@ -73,10 +70,9 @@ def __init__(self, process):
73
70
self .display_scroll_buffer = False
74
71
self .scroll_buffer_title = ''
75
72
76
- # Search buffer, for use in copy mode. (Each pane gets its own search buffer.)
77
- self .search_buffer = Buffer ()
78
- self .is_searching = False
79
- self .search_state = SearchState (ignore_case = False )
73
+ @property
74
+ def process (self ):
75
+ return self .terminal .process
80
76
81
77
@property
82
78
def name (self ):
@@ -99,41 +95,13 @@ def enter_copy_mode(self):
99
95
Suspend the process, and copy the screen content to the `scroll_buffer`.
100
96
That way the user can search through the history and copy/paste.
101
97
"""
102
- document , get_tokens_for_line = self .process .create_copy_document ()
103
- self ._enter_scroll_buffer ('Copy' , document , get_tokens_for_line )
104
-
105
- def display_text (self , text , title = '' ):
106
- """
107
- Display the given text in the scroll buffer.
108
- """
109
- document = Document (text , 0 )
110
-
111
- def get_tokens_for_line (lineno ):
112
- return [(Token , document .lines [lineno ])]
113
-
114
- self ._enter_scroll_buffer (
115
- title ,
116
- document = document ,
117
- get_tokens_for_line = get_tokens_for_line )
118
-
119
- def _enter_scroll_buffer (self , title , document , get_tokens_for_line ):
120
- # Suspend child process.
121
- self .process .suspend ()
98
+ self .terminal .enter_copy_mode ()
122
99
123
- self .scroll_buffer .set_document (document , bypass_readonly = True )
124
- self .copy_get_tokens_for_line = get_tokens_for_line
125
- self .display_scroll_buffer = True
126
- self .scroll_buffer_title = title
127
-
128
- # Reset search state.
129
- self .search_state = SearchState (ignore_case = False )
130
-
131
- def exit_scroll_buffer (self ):
100
+ def focus (self ):
132
101
"""
133
- Exit scroll buffer. (Exits help or copy mode.)
102
+ Focus this pane.
134
103
"""
135
- self .process .resume ()
136
- self .display_scroll_buffer = False
104
+ get_app ().layout .focus (self .terminal )
137
105
138
106
139
107
class _WeightsDictionary (weakref .WeakKeyDictionary ):
@@ -210,6 +178,9 @@ def invalidation_hash(self):
210
178
Return a hash (string) that can be used to determine when the layout
211
179
has to be rebuild.
212
180
"""
181
+ # if not self.root:
182
+ # return '<empty-window>'
183
+
213
184
def _hash_for_split (split ):
214
185
result = []
215
186
for item in split :
@@ -382,7 +353,10 @@ def active_process(self):
382
353
def focus_next (self , count = 1 ):
383
354
" Focus the next pane. "
384
355
panes = self .panes
385
- self .active_pane = panes [(panes .index (self .active_pane ) + count ) % len (panes )]
356
+ if panes :
357
+ self .active_pane = panes [(panes .index (self .active_pane ) + count ) % len (panes )]
358
+ else :
359
+ self .active_pane = None # No panes left.
386
360
387
361
def focus_previous (self ):
388
362
" Focus the previous pane. "
@@ -590,65 +564,54 @@ def __init__(self):
590
564
# is attached.
591
565
self ._last_active_window = None
592
566
593
- def pane_has_priority (self , pane ):
594
- """
595
- Return True when this Pane sohuld get priority in the output processing.
596
- This is true for panes that have the focus in any of the visible windows.
597
- """
598
- windows = set (self ._active_window_for_cli .values ())
599
-
600
- for w in windows :
601
- if w .active_pane == pane :
602
- return True
603
-
604
- return False
605
-
606
- def invalidation_hash (self , cli ):
567
+ def invalidation_hash (self ):
607
568
"""
608
569
When this changes, the layout needs to be rebuild.
609
570
"""
610
- w = self .get_active_window (cli )
571
+ if not self .windows :
572
+ return '<no-windows>'
573
+
574
+ w = self .get_active_window ()
611
575
return w .invalidation_hash ()
612
576
613
- def get_active_window (self , cli ):
577
+ def get_active_window (self ):
614
578
"""
615
579
The current active :class:`.Window`.
616
580
"""
617
- assert isinstance ( cli , CommandLineInterface )
581
+ app = get_app ( )
618
582
619
583
try :
620
- return self ._active_window_for_cli [cli ]
584
+ return self ._active_window_for_cli [app ]
621
585
except KeyError :
622
- self ._active_window_for_cli [cli ] = self ._last_active_window or self .windows [0 ]
586
+ self ._active_window_for_cli [app ] = self ._last_active_window or self .windows [0 ]
623
587
return self .windows [0 ]
624
588
625
- def set_active_window (self , cli , window ):
626
- assert isinstance (cli , CommandLineInterface )
589
+ def set_active_window (self , window ):
627
590
assert isinstance (window , Window )
591
+ app = get_app ()
628
592
629
- previous = self .get_active_window (cli )
630
- self ._prev_active_window_for_cli [cli ] = previous
631
- self ._active_window_for_cli [cli ] = window
593
+ previous = self .get_active_window ()
594
+ self ._prev_active_window_for_cli [app ] = previous
595
+ self ._active_window_for_cli [app ] = window
632
596
self ._last_active_window = window
633
597
634
- def set_active_window_from_pane_id (self , cli , pane_id ):
598
+ def set_active_window_from_pane_id (self , pane_id ):
635
599
"""
636
600
Make the window with this pane ID the active Window.
637
601
"""
638
- assert isinstance (cli , CommandLineInterface )
639
602
assert isinstance (pane_id , int )
640
603
641
604
for w in self .windows :
642
605
for p in w .panes :
643
606
if p .pane_id == pane_id :
644
- self .set_active_window (cli , w )
607
+ self .set_active_window (w )
645
608
646
- def get_previous_active_window (self , cli ):
609
+ def get_previous_active_window (self ):
647
610
" The previous active Window or None if unknown. "
648
- assert isinstance ( cli , CommandLineInterface )
611
+ app = get_app ( )
649
612
650
613
try :
651
- return self ._prev_active_window_for_cli [cli ]
614
+ return self ._prev_active_window_for_cli [app ]
652
615
except KeyError :
653
616
return None
654
617
@@ -658,17 +621,15 @@ def get_window_by_index(self, index):
658
621
if w .index == index :
659
622
return w
660
623
661
- def create_window (self , cli , pane , name = None , set_active = True ):
624
+ def create_window (self , pane , name = None , set_active = True ):
662
625
"""
663
626
Create a new window that contains just this pane.
664
627
665
- :param cli: If been given, this window will be focussed for that client.
666
628
:param pane: The :class:`.Pane` instance to put in the new window.
667
629
:param name: If given, name for the new window.
668
630
:param set_active: When True, focus the new window.
669
631
"""
670
632
assert isinstance (pane , Pane )
671
- assert cli is None or isinstance (cli , CommandLineInterface )
672
633
assert name is None or isinstance (name , six .text_type )
673
634
674
635
# Take the first available index.
@@ -686,8 +647,10 @@ def create_window(self, cli, pane, name=None, set_active=True):
686
647
# Sort windows by index.
687
648
self .windows = sorted (self .windows , key = lambda w : w .index )
688
649
689
- if cli is not None and set_active :
690
- self .set_active_window (cli , w )
650
+ app = get_app (return_none = True )
651
+
652
+ if app is not None and set_active :
653
+ self .set_active_window (w )
691
654
692
655
if name is not None :
693
656
w .chosen_name = name
@@ -707,13 +670,11 @@ def move_window(self, window, new_index):
707
670
# Sort windows by index.
708
671
self .windows = sorted (self .windows , key = lambda w : w .index )
709
672
710
- def get_active_pane (self , cli ):
673
+ def get_active_pane (self ):
711
674
"""
712
675
The current :class:`.Pane` from the current window.
713
676
"""
714
- assert isinstance (cli , CommandLineInterface )
715
-
716
- w = self .get_active_window (cli )
677
+ w = self .get_active_window ()
717
678
if w is not None :
718
679
return w .active_pane
719
680
@@ -729,49 +690,42 @@ def remove_pane(self, pane):
729
690
# No panes left in this window?
730
691
if not w .has_panes :
731
692
# Focus next.
732
- for cli , active_w in self ._active_window_for_cli .items ():
693
+ for app , active_w in self ._active_window_for_cli .items ():
733
694
if w == active_w :
734
- self .focus_next_window (cli )
695
+ with set_app (app ):
696
+ self .focus_next_window ()
735
697
736
698
self .windows .remove (w )
737
699
738
- def focus_previous_window (self , cli ):
739
- assert isinstance (cli , CommandLineInterface )
740
-
741
- w = self .get_active_window (cli )
700
+ def focus_previous_window (self ):
701
+ w = self .get_active_window ()
742
702
743
- self .set_active_window (cli , self .windows [
703
+ self .set_active_window (self .windows [
744
704
(self .windows .index (w ) - 1 ) % len (self .windows )])
745
705
746
- def focus_next_window (self , cli ):
747
- assert isinstance ( cli , CommandLineInterface )
706
+ def focus_next_window (self ):
707
+ w = self . get_active_window ( )
748
708
749
- w = self .get_active_window (cli )
750
-
751
- self .set_active_window (cli , self .windows [
709
+ self .set_active_window (self .windows [
752
710
(self .windows .index (w ) + 1 ) % len (self .windows )])
753
711
754
- def break_pane (self , cli , set_active = True ):
712
+ def break_pane (self , set_active = True ):
755
713
"""
756
714
When the current window has multiple panes, remove the pane from this
757
715
window and put it in a new window.
758
716
759
717
:param set_active: When True, focus the new window.
760
718
"""
761
- assert isinstance (cli , CommandLineInterface )
762
-
763
- w = self .get_active_window (cli )
719
+ w = self .get_active_window ()
764
720
765
721
if len (w .panes ) > 1 :
766
722
pane = w .active_pane
767
- self .get_active_window (cli ).remove_pane (pane )
768
- self .create_window (cli , pane , set_active = set_active )
723
+ self .get_active_window ().remove_pane (pane )
724
+ self .create_window (pane , set_active = set_active )
769
725
770
- def rotate_window (self , cli , count = 1 ):
726
+ def rotate_window (self , count = 1 ):
771
727
" Rotate the panes in the active window. "
772
- assert isinstance (cli , CommandLineInterface )
773
-
774
- w = self .get_active_window (cli )
728
+ w = self .get_active_window ()
775
729
w .rotate (count = count )
776
730
777
731
@property
0 commit comments