4949 an fnmatch isn't working at the API level, please file an issue.
5050
5151 Limitations
52- The client that is picked is most recently attached. For normal usage,
53- this isn't an issue, but more much more advanced used cases, <0.5% of
54- people may not be covered.
52+ You may only have one client attached to use certain functions
53+
54+ Defaults to current client for ``target-client``
55+ :meth:`Server.attached_session()` can return anything that's attached
5556
5657"""
5758import kaptan
@@ -68,7 +69,11 @@ def tmux(*args, **kwargs):
6869 try :
6970 return tmx (* args , ** kwargs )
7071 except ErrorReturnCode_1 as e :
71- pprint (e )
72+ print (
73+ "\t cmd:\t %s\n "
74+ "\t error:\t %s"
75+ % (e .full_cmd , e .stderr )
76+ )
7277 pass
7378
7479
@@ -98,7 +103,7 @@ def live_tmux(f):
98103
99104 :meth:`Session.create_session` aka ``tmux create-session``
100105
101- :meth:`Session .list_sessions` aka ``tmux list-sessions``
106+ :meth:`Server .list_sessions` aka ``tmux list-sessions``
102107 :meth:`Session.new_window` aka ``tmux new-window``
103108 :meth:`Window.split_window` aka ``tmux split-window``
104109 returns a :class:`Pane` with pane metadata
@@ -224,40 +229,11 @@ def from_tmux(cls, **kwargs):
224229
225230 return session
226231
227- @classmethod
228- def list_sessions (cls ):
229- '''
230- Return a list of :class:`.Session`. from tmux server.
231-
232- Uses ``tmux list-sessions``.
233- '''
234- formats = SESSION_FORMATS
235- tmux_formats = ['#{%s}' % format for format in formats ]
236-
237- sessions = tmux (
238- 'list-sessions' , # ``tmux list-windows``
239- '-F%s' % '\t ' .join (tmux_formats ), # output
240- _iter = True # iterate line by line
241- )
242-
243- # combine format keys with values returned from ``tmux list-windows``
244- sessions = [dict (zip (formats , session .split ('\t ' ))) for session in sessions ]
245-
246- # clear up empty dict
247- sessions = [
248- dict ((k , v ) for k , v in session .iteritems () if v ) for session in sessions
249- ]
250-
251- sessions = [cls .from_tmux (** session ) for session in sessions ]
252-
253- for session in sessions :
254- yield session
255-
256232 def list_windows (self ):
257233 '''
258234 Return a list of :class:`.Window` inside the tmux session
259235 '''
260- formats = WINDOW_FORMATS
236+ formats = [ 'session_name' ] + WINDOW_FORMATS
261237 tmux_formats = ['#{%s}' % format for format in formats ]
262238
263239 windows = tmux (
@@ -275,23 +251,21 @@ def list_windows(self):
275251 dict ((k , v ) for k , v in window .iteritems () if v ) for window in windows
276252 ]
277253
278- pprint ('%s, windows for %s' % (
279- len (windows ),
280- self .session_name
281- ))
282- # pprint(windows)
254+ #pprint('%s, windows for %s' % (
255+ # len(windows),
256+ # self.session_name
257+ #))
283258
284259 windows = [Window .from_tmux (session = self , ** window ) for window in windows ]
285260 return windows
286261
287- def active_window (self ):
262+ def attached_window (self ):
288263 '''
289264 Returns active :class:`.Window` object
290265 '''
291266 windows = self .list_windows ()
292267
293268 for window in windows :
294- pprint (window ._TMUX )
295269 if 'window_active' in window ._TMUX :
296270 # for now window_active is a unicode
297271 if window ._TMUX ['window_active' ] == '1' :
@@ -311,16 +285,15 @@ def select_window(self, window):
311285 '''
312286 tmux ('select-window' , '-t' , window )
313287
314- def active_pane (self ):
288+ def attached_pane (self ):
315289 '''
316290 Returns active :class:`.Pane` object
317291 '''
318- return self .active_window ().active_pane ()
292+ return self .attached_window ().attached_pane ()
319293
320294 @property
321295 def windows (self ):
322- # check if this object is based off an active session, use _TMUX for
323- # now.
296+ # check if session is based off an active tmux(1) session.
324297 if self ._TMUX :
325298 return self .list_windows ()
326299 else :
@@ -420,11 +393,28 @@ def select_pane(self, pane):
420393 '''
421394 tmux ('select-pane' , '-t' , pane )
422395
423- def active_pane (self ):
396+ @classmethod
397+ def split_window (cls , session = None , window = None , ** kwargs ):
398+ '''
399+ Create a new pane by splitting the window. Returns :class:`.Pane`.
400+
401+ Used for splitting window and holding in a python object.
402+
403+ Iterates ``tmux split-window``, ``-P`` to return data and
404+ ``-F`` for return formatting.
405+
406+ session
407+ :class:`.Session` object
408+ window
409+ :class:`.Window` object
410+ '''
411+ raise NotImplemented
412+
413+ def attached_pane (self ):
424414 panes = self .list_panes ()
425415
426416 for pane in panes :
427- pprint (pane ._TMUX )
417+ # pprint(pane._TMUX)
428418 if 'pane_active' in pane ._TMUX :
429419 # for now pane_active is a unicode
430420 if pane ._TMUX ['pane_active' ] == '1' :
@@ -463,12 +453,12 @@ def from_tmux(cls, session=None, **kwargs):
463453
464454 window ._panes = window .list_panes ()
465455
466- pprint (type (window ._panes ))
467- pprint ('%s, panes for %s' % (
468- len (window ._panes ),
469- kwargs ['window_index' ]
470- ))
471- pprint (window ._panes )
456+ # pprint(type(window._panes))
457+ # pprint('%s, panes for %s' % (
458+ # len(window._panes),
459+ # kwargs['window_index']
460+ # ))
461+ # pprint(window._panes)
472462
473463 return window
474464
@@ -477,7 +467,7 @@ def list_panes(self):
477467 '''
478468 Returns a list of :class:`.Pane` for the window.
479469 '''
480- formats = PANE_FORMATS + WINDOW_FORMATS
470+ formats = [ 'session_name' , 'window_index' ] + PANE_FORMATS
481471 tmux_formats = ['#{%s}\t ' % format for format in formats ]
482472
483473 panes = tmux (
@@ -515,22 +505,9 @@ class Pane(object):
515505 ``tmux(1)`` pane
516506 '''
517507
518- @classmethod
519- def split_window (cls , session = None , window = None , ** kwargs ):
520- '''
521- Create a new pane by splitting the window. Returns :class:`.Pane`.
522-
523- Used for splitting window and holding in a python object.
524-
525- Iterates ``tmux split-window``, ``-P`` to return data and
526- ``-F`` for return formatting.
527-
528- session
529- :class:`.Session` object
530- window
531- :class:`.Window` object
532- '''
533- raise NotImplemented
508+ @live_tmux
509+ def get_session (self ):
510+ return next ((session for session in Server .list_sessions () if session ._TMUX ['session_name' ] == self ._TMUX ['session_name' ]), None )
534511
535512 @classmethod
536513 def from_tmux (cls , session = None , window = None , ** kwargs ):
@@ -673,65 +650,134 @@ class Server(object):
673650 # note at this level fnmatch may have to be done via python
674651 # and list-sessions to retrieve object correctly
675652 session.la()
676- with session.active_window () as window:
653+ with session.attached_window () as window:
677654 # access to current window
678655 pass
679656 with session.find_window(fnmatch) as window:
680657 # access to tmux matches window
681- with window.active_path () as pane:
658+ with window.attached_path () as pane:
682659 # access to pane
683660 pass
684661
685662 '''
686- def list_sessions (self ):
687- pass
663+
664+ @staticmethod
665+ def list_sessions ():
666+ '''
667+ Return a list of :class:`.Session`. from tmux server.
668+
669+ Uses ``tmux list-sessions``.
670+ '''
671+ formats = SESSION_FORMATS
672+ tmux_formats = ['#{%s}' % format for format in formats ]
673+
674+ sessions = tmux (
675+ 'list-sessions' , # ``tmux list-windows``
676+ '-F%s' % '\t ' .join (tmux_formats ), # output
677+ _iter = True # iterate line by line
678+ )
679+
680+ # combine format keys with values returned from ``tmux list-windows``
681+ sessions = [dict (zip (formats , session .split ('\t ' ))) for session in sessions ]
682+
683+ # clear up empty dict
684+ sessions = [
685+ dict ((k , v ) for k , v in session .iteritems () if v ) for session in sessions
686+ ]
687+
688+ sessions = [Session .from_tmux (** session ) for session in sessions ]
689+
690+ return sessions
691+
692+ def attached_sessions (self ):
693+ '''
694+ Returns active :class:`.session` object
695+
696+ This will not work where multiple tmux sessions are attached.
697+ '''
698+ sessions = self .list_sessions ()
699+ pprint (sessions )
700+ pprint ([s ._TMUX ['session_attached' ] for s in sessions ])
701+ attached_sessions = list ()
702+
703+ for session in sessions :
704+ if 'session_attached' in session ._TMUX :
705+ # for now session_active is a unicode
706+ if session ._TMUX ['session_attached' ] == '1' :
707+ pprint ('session attached' )
708+ attached_sessions .append (session )
709+ else :
710+ continue
711+
712+ return attached_sessions or None
688713
689714 @property
690715 def sessions (self ):
691- raise NotImplemented
716+ return self . list_sessions ()
692717
693718 def list_clients (self ):
694719 raise NotImplemented
695720
721+ def switch_client (self , target_session ):
722+ '''
723+ ``tmux(1) ``switch-client``
724+
725+ target_session
726+ string. name of the session. fnmatch(3) works
727+ '''
728+ tmux ('switch-client' , '-t' , target_session )
696729
697730t = Server ()
698731
699732
700- for session in Session .list_sessions ():
733+ for session in Server .list_sessions ():
701734 for window in session .windows :
702735 for pane in window .panes :
703736 pass
704737
705738
706- tmux ( 'switch-client' , '-t0' )
707- tmux ( 'switch-client' , '-ttony ' )
708- tmux ( 'switch-client' , '-ttonsy ' )
739+ t . switch_client ( 0 )
740+ t . switch_client ( 'tony ' )
741+ t . switch_client ( 'tonsy ' )
709742
710743TEST_SESSION_NAME = 'tmuxwrapper_dev'
711744
712745session = Session .new_session (
713746 session_name = TEST_SESSION_NAME ,
714747 kill_session = True
715748)
716- tmux ('switch-client' , '-t' , TEST_SESSION_NAME )
749+
750+ t .switch_client (TEST_SESSION_NAME )
751+ #tmux('switch-client', '-t', TEST_SESSION_NAME)
717752
718753# bash completion
719754# allow tmuxwrapper to export split-pane, key bindings
720755
721756tmux ('split-window' )
722- session .active_window ().select_layout ('even-horizontal' )
757+ session .attached_window ().select_layout ('even-horizontal' )
723758tmux ('split-window' )
724759
725760tmux ('new-window' )
726761session .select_window (1 )
727762
728- #session.active_window().select_layout('even-vertical')
729- #pprint(session.active_window()._panes[0]._TMUX['pane_index'])
730- #pprint(session.active_window().active_pane())
763+ #session.attached_window().select_layout('even-vertical')
764+ #pprint(session.attached_window()._panes[0]._TMUX['pane_index'])
765+ #pprint(session.attached_window().attached_pane())
766+
767+ session .attached_window ().select_pane (1 )
768+ session .attached_pane ().send_keys ('cd /srv/www/flaskr' )
769+ session .attached_window ().select_pane (0 )
770+ session .attached_pane ().send_keys ('source .env/bin/activate' )
771+
772+ pprint (session .attached_pane ().get_session ())
773+ pprint (t .sessions [0 ])
774+
775+ #tmux('switch-client', '-ttony')
776+ #t.switch_client('tony')
731777
732- session .active_window ().select_pane ( 1 )
733- session .active_pane ().send_keys ( 'cd /srv/www/flaskr' )
734- session . active_window (). select_pane ( 0 )
778+ pprint ( session .attached_pane ().get_session () )
779+ pprint ( session .attached_pane ().get_session (). __dict__ )
780+ pprint ( t . attached_sessions () )
735781
736782#pprint(dir(tmux))
737783#session.send_keys() send to all? or active pane?
0 commit comments