Skip to content

Commit 95d6df9

Browse files
committed
Merge pull request #31 from stratoukos/master
add window_index option
2 parents 630bb09 + 2ee6007 commit 95d6df9

File tree

6 files changed

+98
-4
lines changed

6 files changed

+98
-4
lines changed

doc/examples.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,25 @@ JSON
179179

180180
.. literalinclude:: ../examples/focus-window-and-panes.json
181181
:language: json
182-
182+
183+
Window Index
184+
------------
185+
186+
You can specify a window's index using the ``window_index`` property. Windows
187+
without ``window_index`` will use the lowest available window index.
188+
189+
YAML
190+
""""
191+
192+
.. literalinclude:: ../examples/window-index.yaml
193+
:language: yaml
194+
195+
JSON
196+
""""
197+
198+
.. literalinclude:: ../examples/window-index.json
199+
:language: json
200+
183201
Automatic Rename
184202
----------------
185203

examples/window-index.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"windows": [
3+
{
4+
"panes": [
5+
"echo \"this window's index will be zero\""
6+
],
7+
"window_name": "zero"
8+
},
9+
{
10+
"panes": [
11+
"echo \"this window's index will be five\""
12+
],
13+
"window_index": 5,
14+
"window_name": "five"
15+
},
16+
{
17+
"panes": [
18+
"echo \"this window's index will be one\""
19+
],
20+
"window_name": "one"
21+
}
22+
],
23+
"session_name": "Window index example"
24+
}

examples/window-index.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
session_name: Window index example
2+
windows:
3+
- window_name: zero
4+
panes:
5+
- echo "this window's index will be zero"
6+
- window_name: five
7+
panes:
8+
- echo "this window's index will be five"
9+
window_index: 5
10+
- window_name: one
11+
panes:
12+
- echo "this window's index will be one"

tmuxp/session.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ def rename_session(self, new_name):
125125
def new_window(self,
126126
window_name=None,
127127
start_directory=None,
128-
attach=True):
128+
attach=True,
129+
window_index=''):
129130
"""Return :class:`Window` from ``$ tmux new-window``.
130131
131132
.. note::
@@ -175,7 +176,8 @@ def new_window(self,
175176
window_args += ('-n%s' % window_name,)
176177

177178
window_args += (
178-
'-t%s' % self.get('session_id'),
179+
# empty string for window_index will use the first one available
180+
'-t%s:%s' % (self.get('session_id'), window_index),
179181
)
180182

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

tmuxp/testsuite/test_workspacebuilder.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,3 +462,40 @@ def test_pane_order(self):
462462
time.sleep(.2)
463463

464464
self.assertEqual(p.get('pane_current_path'), pane_path)
465+
466+
467+
class WindowIndexTest(TmuxTestCase):
468+
yaml_config = """
469+
session_name: sampleconfig
470+
windows:
471+
- window_name: zero
472+
panes:
473+
- echo 'zero'
474+
- window_name: five
475+
panes:
476+
- echo 'five'
477+
window_index: 5
478+
- window_name: one
479+
panes:
480+
- echo 'one'
481+
"""
482+
483+
def test_window_index(self):
484+
proc = self.session.tmux('show-option', '-gv', 'base-index')
485+
base_index = int(proc.stdout[0])
486+
name_index_map = {
487+
'zero': 0 + base_index,
488+
'one': 1 + base_index,
489+
'five': 5,
490+
}
491+
492+
sconfig = kaptan.Kaptan(handler='yaml')
493+
sconfig = sconfig.import_config(self.yaml_config).get()
494+
sconfig = config.expand(sconfig)
495+
sconfig = config.trickle(sconfig)
496+
497+
builder = WorkspaceBuilder(sconf=sconfig)
498+
499+
for window, wconf in builder.iter_create_windows(self.session):
500+
expected_index = name_index_map[window['window_name']]
501+
self.assertEqual(int(window['window_index']), expected_index)

tmuxp/workspacebuilder.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,8 @@ def iter_create_windows(self, s):
184184
window_name=window_name,
185185
start_directory=wconf[
186186
'start_directory'] if 'start_directory' in wconf else None,
187-
attach=False # do not move to the new window
187+
attach=False, # do not move to the new window
188+
window_index=wconf.get('window_index', ''),
188189
)
189190

190191
if i == int(1) and w1: # if first window, use window 1

0 commit comments

Comments
 (0)