Skip to content

Commit 30b80c2

Browse files
committed
Merge pull request #101 from tony/tmuxcmd
Refactor tmux objects to .cmd, tmuxp_cmd.
2 parents b6d22e1 + da2f0c5 commit 30b80c2

16 files changed

+92
-75
lines changed

CHANGES

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@ CURRENT
99

1010
- [internal]: Fix documentation for :meth:``Session.switch_client()``.
1111
- [cli]: Add ``--log-level`` argument.
12+
- [internal]: Refactor ``{Server,Session,Window,Pane}.tmux`` into:
13+
14+
- :meth:`Server.cmd()`
15+
- :meth:`Session.cmd()`
16+
- :meth:`Window.cmd()`
17+
- :meth:`Pane.cmd()`
18+
19+
(See conversation at https://github.com/bitprophet/dotfiles/issues/5)
20+
- [internal]: Refactor ``util.tmux`` into :meth:`util.tmux_cmd`.
1221

1322
0.1.13
1423
------

doc/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ Internals
5959
.. autoclass:: tmuxp.util.TmuxMappingObject
6060
:members:
6161

62-
.. autoclass:: tmuxp.util.tmux
62+
.. autoclass:: tmuxp.util.tmux_cmd
6363

6464
.. automethod:: tmuxp.util.has_required_tmux_version
6565

doc/quickstart_python.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ As long as you have the object, or are iterating through a list of them, you can
348348
349349
See the other window, notice that :meth:`Pane.send_keys` has " ``echo hey``" written,
350350
*still in the prompt*. Note the leading space character so the command won't be added
351-
to the user's history. Use `pane.tmux('send-keys', text)` to send keys without this
351+
to the user's history. Use `pane.cmd('send-keys', text)` to send keys without this
352352
leading space.
353353

354354
``enter=False`` can be used to send keys without pressing return. In this case,

tmuxp/pane.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class Pane(util.TmuxMappingObject, util.TmuxRelationalObject):
2222
2323
:param window: :class:`Window`
2424
25+
:versionchanged: 0.8
26+
Renamed from ``.tmux`` to ``.cmd``.
27+
2528
"""
2629

2730
def __init__(self, window=None, **kwargs):
@@ -56,21 +59,21 @@ def by(val, *args):
5659

5760
return list(filter(by, self.server._panes))[0]
5861

59-
def tmux(self, cmd, *args, **kwargs):
60-
"""Return :meth:`Server.tmux` defaulting to ``target_pane`` as target.
62+
def cmd(self, cmd, *args, **kwargs):
63+
"""Return :meth:`Server.cmd` defaulting to ``target_pane`` as target.
6164
6265
Send command to tmux with :attr:`pane_id` as ``target-pane``.
6366
6467
Specifying ``('-t', 'custom-target')`` or ``('-tcustom_target')`` in
6568
``args`` will override using the object's ``pane_id`` as target.
6669
67-
:rtype: :class:`Server.tmux`
70+
:rtype: :class:`Server.cmd`
6871
6972
"""
7073
if not any(arg.startswith('-t') for arg in args):
7174
args = ('-t', self.get('pane_id')) + args
7275

73-
return self.server.tmux(cmd, *args, **kwargs)
76+
return self.server.cmd(cmd, *args, **kwargs)
7477

7578
def send_keys(self, cmd, enter=True):
7679
"""``$ tmux send-keys`` to the pane.
@@ -84,7 +87,7 @@ def send_keys(self, cmd, enter=True):
8487
:type enter: bool
8588
8689
"""
87-
self.tmux('send-keys', ' ' + cmd)
90+
self.cmd('send-keys', ' ' + cmd)
8891

8992
if enter:
9093
self.enter()
@@ -96,7 +99,7 @@ def clear(self):
9699
def reset(self):
97100
"""Reset and clear pane history. """
98101

99-
self.tmux('send-keys', '-R \; clear-history')
102+
self.cmd('send-keys', '-R \; clear-history')
100103

101104
def split_window(self, attach=False):
102105
"""Split window at pane and return newly created :class:`Pane`.
@@ -139,11 +142,11 @@ def resize_pane(self, *args, **kwargs):
139142
"""
140143

141144
if 'height' in kwargs:
142-
proc = self.tmux('resize-pane', '-y%s' % int(kwargs['height']))
145+
proc = self.cmd('resize-pane', '-y%s' % int(kwargs['height']))
143146
elif 'width' in kwargs:
144-
proc = self.tmux('resize-pane', '-x%s' % int(kwargs['width']))
147+
proc = self.cmd('resize-pane', '-x%s' % int(kwargs['width']))
145148
else:
146-
proc = self.tmux('resize-pane', args[0])
149+
proc = self.cmd('resize-pane', args[0])
147150

148151
if proc.stderr:
149152
raise exc.TmuxpException(proc.stderr)
@@ -157,7 +160,7 @@ def enter(self):
157160
``$ tmux send-keys`` send Enter to the pane.
158161
159162
"""
160-
self.tmux('send-keys', 'Enter')
163+
self.cmd('send-keys', 'Enter')
161164

162165
def select_pane(self):
163166
"""Select pane. Return ``self``.

tmuxp/server.py

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import os
1212
import logging
1313

14-
from .util import tmux, TmuxRelationalObject
14+
from .util import tmux_cmd, TmuxRelationalObject
1515
from .session import Session
1616
from . import formats, exc
1717

@@ -69,10 +69,13 @@ def __init__(
6969
if colors:
7070
self.colors = colors
7171

72-
def tmux(self, *args, **kwargs):
73-
"""Return :class:`util.tmux` send tmux commands with sockets, colors.
72+
def cmd(self, *args, **kwargs):
73+
"""Return :class:`util.tmux_cmd` send tmux commands with sockets, colors.
7474
75-
:rtype: :class:`util.tmux`
75+
:rtype: :class:`util.tmux_cmd`
76+
77+
:versionchanged: 0.8
78+
Renamed from ``.tmux`` to ``.cmd``.
7679
7780
"""
7881

@@ -91,14 +94,14 @@ def tmux(self, *args, **kwargs):
9194
else:
9295
raise ValueError('Server.colors must equal 88 or 256')
9396

94-
return tmux(*args, **kwargs)
97+
return tmux_cmd(*args, **kwargs)
9598

9699
def _list_sessions(self):
97100
"""Return list of sessions in :py:obj:`dict` form.
98101
99102
Retrieved from ``$ tmux(1) list-sessions`` stdout.
100103
101-
The :py:obj:`list` is derived from ``stdout`` in :class:`util.tmux`
104+
The :py:obj:`list` is derived from ``stdout`` in :class:`util.tmux_cmd`
102105
which wraps :py:class:`subprocess.Popen`.
103106
104107
:rtype: :py:obj:`list` of :py:obj:`dict`
@@ -112,7 +115,7 @@ def _list_sessions(self):
112115
'-F%s' % '\t'.join(tmux_formats), # output
113116
)
114117

115-
proc = self.tmux(
118+
proc = self.cmd(
116119
'list-sessions',
117120
*tmux_args
118121
)
@@ -169,7 +172,7 @@ def _list_windows(self):
169172
170173
Retrieved from ``$ tmux(1) list-windows`` stdout.
171174
172-
The :py:obj:`list` is derived from ``stdout`` in :class:`util.tmux`
175+
The :py:obj:`list` is derived from ``stdout`` in :class:`util.tmux_cmd`
173176
which wraps :py:class:`subprocess.Popen`.
174177
175178
:rtype: list
@@ -179,7 +182,7 @@ def _list_windows(self):
179182
wformats = ['session_name', 'session_id'] + formats.WINDOW_FORMATS
180183
tmux_formats = ['#{%s}' % format for format in wformats]
181184

182-
proc = self.tmux(
185+
proc = self.cmd(
183186
'list-windows', # ``tmux list-windows``
184187
'-a',
185188
'-F%s' % '\t'.join(tmux_formats), # output
@@ -227,7 +230,7 @@ def _list_panes(self):
227230
228231
Retrieved from ``$ tmux(1) list-panes`` stdout.
229232
230-
The :py:obj:`list` is derived from ``stdout`` in :class:`util.tmux`
233+
The :py:obj:`list` is derived from ``stdout`` in :class:`util.tmux_cmd`
231234
which wraps :py:class:`subprocess.Popen`.
232235
233236
:rtype: list
@@ -241,7 +244,7 @@ def _list_panes(self):
241244
] + formats.PANE_FORMATS
242245
tmux_formats = ['#{%s}\t' % f for f in pformats]
243246

244-
proc = self.tmux(
247+
proc = self.cmd(
245248
'list-panes',
246249
'-a',
247250
'-F%s' % ''.join(tmux_formats), # output
@@ -314,7 +317,7 @@ def has_session(self, target_session):
314317
315318
"""
316319

317-
proc = self.tmux('has-session', '-t%s' % target_session)
320+
proc = self.cmd('has-session', '-t%s' % target_session)
318321

319322
if 'failed to connect to server' in proc.stdout:
320323
return False
@@ -327,7 +330,7 @@ def has_session(self, target_session):
327330

328331
def kill_server(self):
329332
"""``$ tmux kill-server``."""
330-
self.tmux('kill-server')
333+
self.cmd('kill-server')
331334

332335
def kill_session(self, target_session=None):
333336
"""Kill the tmux session with ``$ tmux kill-session``, return ``self``.
@@ -338,7 +341,7 @@ def kill_session(self, target_session=None):
338341
:rtype: :class:`Server`
339342
340343
"""
341-
proc = self.tmux('kill-session', '-t%s' % target_session)
344+
proc = self.cmd('kill-session', '-t%s' % target_session)
342345

343346
if proc.stderr:
344347
raise exc.TmuxpException(proc.stderr)
@@ -352,8 +355,7 @@ def switch_client(self, target_session):
352355
353356
"""
354357

355-
# tmux('switch-client', '-t', target_session)
356-
proc = self.tmux('switch-client', '-t%s' % target_session)
358+
proc = self.cmd('switch-client', '-t%s' % target_session)
357359

358360
if proc.stderr:
359361
raise exc.TmuxpException(proc.stderr)
@@ -368,7 +370,7 @@ def attach_session(self, target_session=None):
368370
if target_session:
369371
tmux_args += ('-t%s' % target_session,)
370372

371-
proc = self.tmux('attach-session', *tmux_args)
373+
proc = self.cmd('attach-session', *tmux_args)
372374

373375
if proc.stderr:
374376
raise exc.TmuxpException(proc.stderr)
@@ -412,7 +414,7 @@ def new_session(self,
412414

413415
if self.has_session(session_name):
414416
if kill_session:
415-
self.tmux('kill-session', '-t%s' % session_name)
417+
self.cmd('kill-session', '-t%s' % session_name)
416418
logger.info('session %s exists. killed it.' % session_name)
417419
else:
418420
raise exc.TmuxSessionExists(
@@ -437,7 +439,7 @@ def new_session(self,
437439
if not attach:
438440
tmux_args += ('-d',)
439441

440-
proc = self.tmux(
442+
proc = self.cmd(
441443
'new-session',
442444
*tmux_args
443445
)

tmuxp/session.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,31 +58,34 @@ def by(val, *args):
5858
except IndexError as e:
5959
logger.error(e)
6060

61-
def tmux(self, *args, **kwargs):
62-
"""Return :meth:`Server.tmux`.
61+
def cmd(self, *args, **kwargs):
62+
"""Return :meth:`server.cmd`.
6363
64-
:rtype: :class:`Server.tmux`
64+
:rtype: :class:`server.cmd`
65+
66+
:versionchanged: 0.8
67+
Renamed from ``.tmux`` to ``.cmd``.
6568
6669
"""
6770
if '-t' not in kwargs:
6871
kwargs['-t'] = self.get('session_id')
69-
return self.server.tmux(*args, **kwargs)
72+
return self.server.cmd(*args, **kwargs)
7073

7174
def attach_session(self, target_session=None):
7275
"""Return ``$ tmux attach-session`` aka alias: ``$ tmux attach``.
7376
7477
:param: target_session: str. name of the session. fnmatch(3) works.
7578
7679
"""
77-
proc = self.tmux('attach-session', '-t%s' % self.get('session_id'))
80+
proc = self.cmd('attach-session', '-t%s' % self.get('session_id'))
7881

7982
if proc.stderr:
8083
raise exc.TmuxpException(proc.stderr)
8184

8285
def kill_session(self):
8386
"""``$ tmux kill-session``."""
8487

85-
proc = self.tmux('kill-session', '-t%s' % self.get('session_id'))
88+
proc = self.cmd('kill-session', '-t%s' % self.get('session_id'))
8689

8790
if proc.stderr:
8891
raise exc.TmuxpException(proc.stderr)
@@ -92,7 +95,7 @@ def switch_client(self, target_session=None):
9295
9396
:param: target_session: str. note this accepts fnmatch(3).
9497
"""
95-
proc = self.tmux('switch-client', '-t%s' % self.get('session_id'))
98+
proc = self.cmd('switch-client', '-t%s' % self.get('session_id'))
9699

97100
if proc.stderr:
98101
raise exc.TmuxpException(proc.stderr)
@@ -105,7 +108,7 @@ def rename_session(self, new_name):
105108
:rtype: :class:`Session`
106109
107110
"""
108-
proc = self.tmux(
111+
proc = self.cmd(
109112
'rename-session',
110113
'-t%s' % self.get('session_id'),
111114
new_name
@@ -173,7 +176,7 @@ def new_window(self,
173176
'-t%s:%s' % (self.get('session_id'), window_index),
174177
)
175178

176-
proc = self.tmux('new-window', *window_args)
179+
proc = self.cmd('new-window', *window_args)
177180

178181
if proc.stderr:
179182
raise exc.TmuxpException(proc.stderr)
@@ -209,7 +212,7 @@ def kill_window(self, target_window=None):
209212
else:
210213
target = '-t%s' % target_window
211214

212-
proc = self.tmux('kill-window', target)
215+
proc = self.cmd('kill-window', target)
213216

214217
if proc.stderr:
215218
raise exc.TmuxpException(proc.stderr)
@@ -288,7 +291,7 @@ def select_window(self, target_window):
288291

289292
target = '-t%s' % target_window
290293

291-
proc = self.tmux('select-window', target)
294+
proc = self.cmd('select-window', target)
292295

293296
if proc.stderr:
294297
raise exc.TmuxpException(proc.stderr)
@@ -318,7 +321,7 @@ def set_option(self, option, value):
318321
elif isinstance(value, bool) and not value:
319322
value = 'off'
320323

321-
proc = self.tmux(
324+
proc = self.cmd(
322325
'set-option', option, value
323326
)
324327

@@ -350,7 +353,7 @@ def show_options(self, option=None, g=False):
350353
return self.show_option(option, g=g)
351354
else:
352355
tmux_args += ('show-options',)
353-
session_options = self.tmux(
356+
session_options = self.cmd(
354357
*tmux_args
355358
).stdout
356359

@@ -380,7 +383,7 @@ def show_option(self, option, g=False):
380383
if g:
381384
tmux_args += ('-g',)
382385

383-
window_option = self.tmux(
386+
window_option = self.cmd(
384387
'show-options', option, *tmux_args
385388
).stdout
386389
window_option = [tuple(item.split(' ')) for item in window_option][0]

tmuxp/testsuite/cli.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import kaptan
1919

2020
from .. import config, cli
21-
from ..util import tmux
2221
from .helpers import TestCase
2322

2423
logger = logging.getLogger(__name__)

tmuxp/testsuite/config.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import kaptan
1919

2020
from .. import config, exc
21-
from ..util import tmux
2221
from .helpers import TestCase
2322

2423

0 commit comments

Comments
 (0)