Skip to content

Commit e69eb49

Browse files
committed
#56: work in progress, documentation for using python API to control live tmux sessions via python.
1 parent 87293f6 commit e69eb49

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

CHANGES

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ Changelog
44

55
Here you can find the recent changes to tmuxp.
66

7+
CURRENT
8+
-------
9+
10+
- [docs]: :ref:`python_api_quickstart` per `Issue #56`_.
11+
12+
.. _Issue #56: https://github.com/tony/tmuxp/issues/56
13+
714
0.1.7
815
-----
916

doc/api.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
API Reference
55
=============
66

7+
.. seealso::
8+
:ref:`python_api_quickstart` to see how you can control tmux via
9+
python API calls.
10+
711
.. module:: tmuxp
812

913
Server Object

doc/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Explore:
2020

2121
about
2222
quickstart
23+
quickstart_python
2324
examples
2425
cli
2526
internals

doc/quickstart_python.rst

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
.. _python_api_quickstart:
2+
3+
=====================
4+
Python API Quickstart
5+
=====================
6+
7+
tmuxp allows for developers and system administrators to control live tmux
8+
sessions using python code.
9+
10+
In this example, we will launch a tmux session and control the windows
11+
from inside a live tmux session.
12+
13+
14+
Setting up tab-completion
15+
-------------------------
16+
17+
To begin, it's preferable to install a python CLI with tab-completion.
18+
19+
You can install a custom python shell like `bpython`_ or `iPython`_, which
20+
has some awesome CLI features, or setup vanilla :py:mod:`readline` support.
21+
22+
``readline`` tab-completion
23+
"""""""""""""""""""""""""""
24+
25+
.. seealso::
26+
Source: `How do I add tab-completion to the python shell`_ on
27+
`StackOverflow`_.
28+
29+
Create ``~.pythonrc`` in ``$HOME`` folder:
30+
31+
.. code-block:: python
32+
33+
# ~/.pythonrc
34+
# enable syntax completion
35+
try:
36+
import readline
37+
except ImportError:
38+
print "Module readline not available."
39+
else:
40+
import rlcompleter
41+
readline.parse_and_bind("tab: complete")
42+
43+
Then to your ``.bashrc`` or ``.zshrc`` file, add:
44+
45+
.. code-block:: bash
46+
47+
export PYTHONSTARTUP=~/.pythonrc
48+
49+
.. _How do I add tab-completion to the python shell: http://stackoverflow.com/a/246779
50+
.. _StackOverflow: http://www.stackoverflow.com
51+
52+
bpython or ipython cli
53+
""""""""""""""""""""""
54+
55+
`bpython`_ can be installed with ``$ [sudo] pip install bpython`` and
56+
`ipython`_ can be installed with ``$ [sudo] pip install ipython``.
57+
58+
bpython allows using ``<F2>`` to see the source of CLI methods in colors.
59+
60+
.. todo::
61+
If you know any extra benefits of ipython or bpython for CLI and could
62+
list them here please edit this page.
63+
64+
65+
.. _bpython: https://bitbucket.org/bobf/bpython
66+
.. _ipython: http://ipython.org
67+
68+
Control tmux via python
69+
-----------------------
70+
71+
.. seealso:: :ref:`api`
72+
73+
.. todo:: Do a version of this with `sliderepl`_
74+
75+
To begin, ensure the ``tmux`` program is installed.
76+
77+
Next, ensure ``tmuxp`` (note the p!) is installed:
78+
79+
.. code-block:: bash
80+
81+
$ [sudo] pip install tmuxp
82+
83+
Now, let's open a tmux session.
84+
85+
.. code-block:: bash
86+
87+
$ tmux
88+
89+
We are inside of a tmux session, let's launch our python interpretter
90+
(``$ python``, ``$ bpython`` or ``$ ipython``) and begin issuing commands
91+
to tmuxp CLI style. For this I'll use ``python``.
92+
93+
.. code-block:: bash
94+
95+
$ python
96+
97+
.. module:: tmuxp
98+
99+
First, we can grab a :class:`Server`.
100+
101+
102+
.. code-block:: python
103+
104+
server = tmuxp.Server()
105+
106+
.. note::
107+
108+
You can specify a ``socket_name``, ``socket_path`` and ``config_file``
109+
in your server object. ``tmuxp.Server(socket_name='mysocket')`` is
110+
equivalent to ``$ tmux -L mysocket``.
111+
112+
``server`` is now a living object bound to the tmux server's Sessions,
113+
Windows and Panes.
114+
115+
116+
.. _sliderepl: http://discorporate.us/projects/sliderepl/

0 commit comments

Comments
 (0)