diff --git a/doc/examples.rst b/doc/examples.rst index 84abcad193f..838e1d46052 100644 --- a/doc/examples.rst +++ b/doc/examples.rst @@ -179,7 +179,25 @@ JSON .. literalinclude:: ../examples/focus-window-and-panes.json :language: json - + +Window Index +------------ + +You can specify a window's index using the ``window_index`` property. Windows +without ``window_index`` will use the lowest available window index. + +YAML +"""" + +.. literalinclude:: ../examples/window-index.yaml + :language: yaml + +JSON +"""" + +.. literalinclude:: ../examples/window-index.json + :language: json + Automatic Rename ---------------- diff --git a/examples/window-index.json b/examples/window-index.json new file mode 100644 index 00000000000..63df0377f72 --- /dev/null +++ b/examples/window-index.json @@ -0,0 +1,24 @@ +{ + "windows": [ + { + "panes": [ + "echo \"this window's index will be zero\"" + ], + "window_name": "zero" + }, + { + "panes": [ + "echo \"this window's index will be five\"" + ], + "window_index": 5, + "window_name": "five" + }, + { + "panes": [ + "echo \"this window's index will be one\"" + ], + "window_name": "one" + } + ], + "session_name": "Window index example" +} diff --git a/examples/window-index.yaml b/examples/window-index.yaml new file mode 100644 index 00000000000..36da4f07266 --- /dev/null +++ b/examples/window-index.yaml @@ -0,0 +1,12 @@ +session_name: Window index example +windows: + - window_name: zero + panes: + - echo "this window's index will be zero" + - window_name: five + panes: + - echo "this window's index will be five" + window_index: 5 + - window_name: one + panes: + - echo "this window's index will be one" diff --git a/tmuxp/session.py b/tmuxp/session.py index 9d6ae3de37c..c9a49ebb07b 100644 --- a/tmuxp/session.py +++ b/tmuxp/session.py @@ -125,7 +125,8 @@ def rename_session(self, new_name): def new_window(self, window_name=None, start_directory=None, - attach=True): + attach=True, + window_index=''): """Return :class:`Window` from ``$ tmux new-window``. .. note:: @@ -175,7 +176,8 @@ def new_window(self, window_args += ('-n%s' % window_name,) window_args += ( - '-t%s' % self.get('session_id'), + # empty string for window_index will use the first one available + '-t%s:%s' % (self.get('session_id'), window_index), ) proc = self.tmux('new-window', *window_args) diff --git a/tmuxp/testsuite/test_workspacebuilder.py b/tmuxp/testsuite/test_workspacebuilder.py index 0b95bfa2471..0ff8ba0c910 100644 --- a/tmuxp/testsuite/test_workspacebuilder.py +++ b/tmuxp/testsuite/test_workspacebuilder.py @@ -462,3 +462,40 @@ def test_pane_order(self): time.sleep(.2) self.assertEqual(p.get('pane_current_path'), pane_path) + + +class WindowIndexTest(TmuxTestCase): + yaml_config = """ + session_name: sampleconfig + windows: + - window_name: zero + panes: + - echo 'zero' + - window_name: five + panes: + - echo 'five' + window_index: 5 + - window_name: one + panes: + - echo 'one' + """ + + def test_window_index(self): + proc = self.session.tmux('show-option', '-gv', 'base-index') + base_index = int(proc.stdout[0]) + name_index_map = { + 'zero': 0 + base_index, + 'one': 1 + base_index, + 'five': 5, + } + + sconfig = kaptan.Kaptan(handler='yaml') + sconfig = sconfig.import_config(self.yaml_config).get() + sconfig = config.expand(sconfig) + sconfig = config.trickle(sconfig) + + builder = WorkspaceBuilder(sconf=sconfig) + + for window, wconf in builder.iter_create_windows(self.session): + expected_index = name_index_map[window['window_name']] + self.assertEqual(int(window['window_index']), expected_index) diff --git a/tmuxp/workspacebuilder.py b/tmuxp/workspacebuilder.py index d8523b47f10..a7c9184076a 100644 --- a/tmuxp/workspacebuilder.py +++ b/tmuxp/workspacebuilder.py @@ -184,7 +184,8 @@ def iter_create_windows(self, s): window_name=window_name, start_directory=wconf[ 'start_directory'] if 'start_directory' in wconf else None, - attach=False # do not move to the new window + attach=False, # do not move to the new window + window_index=wconf.get('window_index', ''), ) if i == int(1) and w1: # if first window, use window 1