From 94d12c5b3501911c896efb440c8fe2705baed3bc Mon Sep 17 00:00:00 2001 From: Stratos Moros Date: Thu, 5 Dec 2013 16:03:30 +0200 Subject: [PATCH 1/2] add window_index option includes docs and tests --- doc/examples.rst | 20 ++++++++++++- examples/window-index.json | 24 ++++++++++++++++ examples/window-index.yaml | 12 ++++++++ tmuxp/session.py | 6 ++-- tmuxp/testsuite/test_workspacebuilder.py | 36 ++++++++++++++++++++++++ tmuxp/workspacebuilder.py | 3 +- 6 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 examples/window-index.json create mode 100644 examples/window-index.yaml 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..368d35d2bab 100644 --- a/tmuxp/testsuite/test_workspacebuilder.py +++ b/tmuxp/testsuite/test_workspacebuilder.py @@ -462,3 +462,39 @@ 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): + name_index_map = { + 'zero': '0', + 'one': '1', + '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(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 From 2ee6007470dd4e791f85cdf12432115999678d0c Mon Sep 17 00:00:00 2001 From: Stratos Moros Date: Thu, 5 Dec 2013 20:41:20 +0200 Subject: [PATCH 2/2] take base-index into account in WindowIndexTest --- tmuxp/testsuite/test_workspacebuilder.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tmuxp/testsuite/test_workspacebuilder.py b/tmuxp/testsuite/test_workspacebuilder.py index 368d35d2bab..0ff8ba0c910 100644 --- a/tmuxp/testsuite/test_workspacebuilder.py +++ b/tmuxp/testsuite/test_workspacebuilder.py @@ -481,10 +481,12 @@ class WindowIndexTest(TmuxTestCase): """ 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', - 'one': '1', - 'five': '5', + 'zero': 0 + base_index, + 'one': 1 + base_index, + 'five': 5, } sconfig = kaptan.Kaptan(handler='yaml') @@ -494,7 +496,6 @@ def test_window_index(self): builder = WorkspaceBuilder(sconf=sconfig) - for window, wconf in builder.iter_create_windows(self.session): expected_index = name_index_map[window['window_name']] - self.assertEqual(window['window_index'], expected_index) + self.assertEqual(int(window['window_index']), expected_index)