Skip to content

Commit

Permalink
Refactor Tmux objects .tmux method to .cmd, change util.tmux to util.…
Browse files Browse the repository at this point in the history
…tmux_cmd.
  • Loading branch information
tony committed May 7, 2015
1 parent b6d22e1 commit da2f0c5
Show file tree
Hide file tree
Showing 16 changed files with 92 additions and 75 deletions.
9 changes: 9 additions & 0 deletions CHANGES
Expand Up @@ -9,6 +9,15 @@ CURRENT

- [internal]: Fix documentation for :meth:``Session.switch_client()``.
- [cli]: Add ``--log-level`` argument.
- [internal]: Refactor ``{Server,Session,Window,Pane}.tmux`` into:

- :meth:`Server.cmd()`
- :meth:`Session.cmd()`
- :meth:`Window.cmd()`
- :meth:`Pane.cmd()`

(See conversation at https://github.com/bitprophet/dotfiles/issues/5)
- [internal]: Refactor ``util.tmux`` into :meth:`util.tmux_cmd`.

0.1.13
------
Expand Down
2 changes: 1 addition & 1 deletion doc/api.rst
Expand Up @@ -59,7 +59,7 @@ Internals
.. autoclass:: tmuxp.util.TmuxMappingObject
:members:

.. autoclass:: tmuxp.util.tmux
.. autoclass:: tmuxp.util.tmux_cmd

.. automethod:: tmuxp.util.has_required_tmux_version

Expand Down
2 changes: 1 addition & 1 deletion doc/quickstart_python.rst
Expand Up @@ -348,7 +348,7 @@ As long as you have the object, or are iterating through a list of them, you can
See the other window, notice that :meth:`Pane.send_keys` has " ``echo hey``" written,
*still in the prompt*. Note the leading space character so the command won't be added
to the user's history. Use `pane.tmux('send-keys', text)` to send keys without this
to the user's history. Use `pane.cmd('send-keys', text)` to send keys without this
leading space.

``enter=False`` can be used to send keys without pressing return. In this case,
Expand Down
23 changes: 13 additions & 10 deletions tmuxp/pane.py
Expand Up @@ -22,6 +22,9 @@ class Pane(util.TmuxMappingObject, util.TmuxRelationalObject):
:param window: :class:`Window`
:versionchanged: 0.8
Renamed from ``.tmux`` to ``.cmd``.
"""

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

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

def tmux(self, cmd, *args, **kwargs):
"""Return :meth:`Server.tmux` defaulting to ``target_pane`` as target.
def cmd(self, cmd, *args, **kwargs):
"""Return :meth:`Server.cmd` defaulting to ``target_pane`` as target.
Send command to tmux with :attr:`pane_id` as ``target-pane``.
Specifying ``('-t', 'custom-target')`` or ``('-tcustom_target')`` in
``args`` will override using the object's ``pane_id`` as target.
:rtype: :class:`Server.tmux`
:rtype: :class:`Server.cmd`
"""
if not any(arg.startswith('-t') for arg in args):
args = ('-t', self.get('pane_id')) + args

return self.server.tmux(cmd, *args, **kwargs)
return self.server.cmd(cmd, *args, **kwargs)

def send_keys(self, cmd, enter=True):
"""``$ tmux send-keys`` to the pane.
Expand All @@ -84,7 +87,7 @@ def send_keys(self, cmd, enter=True):
:type enter: bool
"""
self.tmux('send-keys', ' ' + cmd)
self.cmd('send-keys', ' ' + cmd)

if enter:
self.enter()
Expand All @@ -96,7 +99,7 @@ def clear(self):
def reset(self):
"""Reset and clear pane history. """

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

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

if 'height' in kwargs:
proc = self.tmux('resize-pane', '-y%s' % int(kwargs['height']))
proc = self.cmd('resize-pane', '-y%s' % int(kwargs['height']))
elif 'width' in kwargs:
proc = self.tmux('resize-pane', '-x%s' % int(kwargs['width']))
proc = self.cmd('resize-pane', '-x%s' % int(kwargs['width']))
else:
proc = self.tmux('resize-pane', args[0])
proc = self.cmd('resize-pane', args[0])

if proc.stderr:
raise exc.TmuxpException(proc.stderr)
Expand All @@ -157,7 +160,7 @@ def enter(self):
``$ tmux send-keys`` send Enter to the pane.
"""
self.tmux('send-keys', 'Enter')
self.cmd('send-keys', 'Enter')

def select_pane(self):
"""Select pane. Return ``self``.
Expand Down
40 changes: 21 additions & 19 deletions tmuxp/server.py
Expand Up @@ -11,7 +11,7 @@
import os
import logging

from .util import tmux, TmuxRelationalObject
from .util import tmux_cmd, TmuxRelationalObject
from .session import Session
from . import formats, exc

Expand Down Expand Up @@ -69,10 +69,13 @@ def __init__(
if colors:
self.colors = colors

def tmux(self, *args, **kwargs):
"""Return :class:`util.tmux` send tmux commands with sockets, colors.
def cmd(self, *args, **kwargs):
"""Return :class:`util.tmux_cmd` send tmux commands with sockets, colors.
:rtype: :class:`util.tmux`
:rtype: :class:`util.tmux_cmd`
:versionchanged: 0.8
Renamed from ``.tmux`` to ``.cmd``.
"""

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

return tmux(*args, **kwargs)
return tmux_cmd(*args, **kwargs)

def _list_sessions(self):
"""Return list of sessions in :py:obj:`dict` form.
Retrieved from ``$ tmux(1) list-sessions`` stdout.
The :py:obj:`list` is derived from ``stdout`` in :class:`util.tmux`
The :py:obj:`list` is derived from ``stdout`` in :class:`util.tmux_cmd`
which wraps :py:class:`subprocess.Popen`.
:rtype: :py:obj:`list` of :py:obj:`dict`
Expand All @@ -112,7 +115,7 @@ def _list_sessions(self):
'-F%s' % '\t'.join(tmux_formats), # output
)

proc = self.tmux(
proc = self.cmd(
'list-sessions',
*tmux_args
)
Expand Down Expand Up @@ -169,7 +172,7 @@ def _list_windows(self):
Retrieved from ``$ tmux(1) list-windows`` stdout.
The :py:obj:`list` is derived from ``stdout`` in :class:`util.tmux`
The :py:obj:`list` is derived from ``stdout`` in :class:`util.tmux_cmd`
which wraps :py:class:`subprocess.Popen`.
:rtype: list
Expand All @@ -179,7 +182,7 @@ def _list_windows(self):
wformats = ['session_name', 'session_id'] + formats.WINDOW_FORMATS
tmux_formats = ['#{%s}' % format for format in wformats]

proc = self.tmux(
proc = self.cmd(
'list-windows', # ``tmux list-windows``
'-a',
'-F%s' % '\t'.join(tmux_formats), # output
Expand Down Expand Up @@ -227,7 +230,7 @@ def _list_panes(self):
Retrieved from ``$ tmux(1) list-panes`` stdout.
The :py:obj:`list` is derived from ``stdout`` in :class:`util.tmux`
The :py:obj:`list` is derived from ``stdout`` in :class:`util.tmux_cmd`
which wraps :py:class:`subprocess.Popen`.
:rtype: list
Expand All @@ -241,7 +244,7 @@ def _list_panes(self):
] + formats.PANE_FORMATS
tmux_formats = ['#{%s}\t' % f for f in pformats]

proc = self.tmux(
proc = self.cmd(
'list-panes',
'-a',
'-F%s' % ''.join(tmux_formats), # output
Expand Down Expand Up @@ -314,7 +317,7 @@ def has_session(self, target_session):
"""

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

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

def kill_server(self):
"""``$ tmux kill-server``."""
self.tmux('kill-server')
self.cmd('kill-server')

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

if proc.stderr:
raise exc.TmuxpException(proc.stderr)
Expand All @@ -352,8 +355,7 @@ def switch_client(self, target_session):
"""

# tmux('switch-client', '-t', target_session)
proc = self.tmux('switch-client', '-t%s' % target_session)
proc = self.cmd('switch-client', '-t%s' % target_session)

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

proc = self.tmux('attach-session', *tmux_args)
proc = self.cmd('attach-session', *tmux_args)

if proc.stderr:
raise exc.TmuxpException(proc.stderr)
Expand Down Expand Up @@ -412,7 +414,7 @@ def new_session(self,

if self.has_session(session_name):
if kill_session:
self.tmux('kill-session', '-t%s' % session_name)
self.cmd('kill-session', '-t%s' % session_name)
logger.info('session %s exists. killed it.' % session_name)
else:
raise exc.TmuxSessionExists(
Expand All @@ -437,7 +439,7 @@ def new_session(self,
if not attach:
tmux_args += ('-d',)

proc = self.tmux(
proc = self.cmd(
'new-session',
*tmux_args
)
Expand Down
31 changes: 17 additions & 14 deletions tmuxp/session.py
Expand Up @@ -58,31 +58,34 @@ def by(val, *args):
except IndexError as e:
logger.error(e)

def tmux(self, *args, **kwargs):
"""Return :meth:`Server.tmux`.
def cmd(self, *args, **kwargs):
"""Return :meth:`server.cmd`.
:rtype: :class:`Server.tmux`
:rtype: :class:`server.cmd`
:versionchanged: 0.8
Renamed from ``.tmux`` to ``.cmd``.
"""
if '-t' not in kwargs:
kwargs['-t'] = self.get('session_id')
return self.server.tmux(*args, **kwargs)
return self.server.cmd(*args, **kwargs)

def attach_session(self, target_session=None):
"""Return ``$ tmux attach-session`` aka alias: ``$ tmux attach``.
:param: target_session: str. name of the session. fnmatch(3) works.
"""
proc = self.tmux('attach-session', '-t%s' % self.get('session_id'))
proc = self.cmd('attach-session', '-t%s' % self.get('session_id'))

if proc.stderr:
raise exc.TmuxpException(proc.stderr)

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

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

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

if proc.stderr:
raise exc.TmuxpException(proc.stderr)
Expand All @@ -105,7 +108,7 @@ def rename_session(self, new_name):
:rtype: :class:`Session`
"""
proc = self.tmux(
proc = self.cmd(
'rename-session',
'-t%s' % self.get('session_id'),
new_name
Expand Down Expand Up @@ -173,7 +176,7 @@ def new_window(self,
'-t%s:%s' % (self.get('session_id'), window_index),
)

proc = self.tmux('new-window', *window_args)
proc = self.cmd('new-window', *window_args)

if proc.stderr:
raise exc.TmuxpException(proc.stderr)
Expand Down Expand Up @@ -209,7 +212,7 @@ def kill_window(self, target_window=None):
else:
target = '-t%s' % target_window

proc = self.tmux('kill-window', target)
proc = self.cmd('kill-window', target)

if proc.stderr:
raise exc.TmuxpException(proc.stderr)
Expand Down Expand Up @@ -288,7 +291,7 @@ def select_window(self, target_window):

target = '-t%s' % target_window

proc = self.tmux('select-window', target)
proc = self.cmd('select-window', target)

if proc.stderr:
raise exc.TmuxpException(proc.stderr)
Expand Down Expand Up @@ -318,7 +321,7 @@ def set_option(self, option, value):
elif isinstance(value, bool) and not value:
value = 'off'

proc = self.tmux(
proc = self.cmd(
'set-option', option, value
)

Expand Down Expand Up @@ -350,7 +353,7 @@ def show_options(self, option=None, g=False):
return self.show_option(option, g=g)
else:
tmux_args += ('show-options',)
session_options = self.tmux(
session_options = self.cmd(
*tmux_args
).stdout

Expand Down Expand Up @@ -380,7 +383,7 @@ def show_option(self, option, g=False):
if g:
tmux_args += ('-g',)

window_option = self.tmux(
window_option = self.cmd(
'show-options', option, *tmux_args
).stdout
window_option = [tuple(item.split(' ')) for item in window_option][0]
Expand Down
1 change: 0 additions & 1 deletion tmuxp/testsuite/cli.py
Expand Up @@ -18,7 +18,6 @@
import kaptan

from .. import config, cli
from ..util import tmux
from .helpers import TestCase

logger = logging.getLogger(__name__)
Expand Down
1 change: 0 additions & 1 deletion tmuxp/testsuite/config.py
Expand Up @@ -18,7 +18,6 @@
import kaptan

from .. import config, exc
from ..util import tmux
from .helpers import TestCase


Expand Down

0 comments on commit da2f0c5

Please sign in to comment.