From 68c43ee179d24c38f490a752f61a771c0f7ce945 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 12 Mar 2022 16:42:41 -0600 Subject: [PATCH 1/4] feat: Allow passing sleep to commands --- tmuxp/workspacebuilder.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tmuxp/workspacebuilder.py b/tmuxp/workspacebuilder.py index 3bb8f499702..1d032c88eff 100644 --- a/tmuxp/workspacebuilder.py +++ b/tmuxp/workspacebuilder.py @@ -5,6 +5,7 @@ """ import logging +import time from libtmux.exc import TmuxSessionExists from libtmux.pane import Pane @@ -363,8 +364,14 @@ def get_pane_shell(): suppress = True enter = pconf.get("enter", True) + sleep = pconf.get("sleep", None) + for cmd in pconf["shell_command"]: enter = cmd.get("enter", enter) + sleep = cmd.get("sleep", sleep) + + if sleep is not None: + time.sleep(sleep) p.send_keys(cmd["cmd"], suppress_history=suppress, enter=enter) From 592746df095905536909daa66c419c2dcabc201a Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sat, 12 Mar 2022 16:46:25 -0600 Subject: [PATCH 2/4] test(workspacebuilder): Sleep in command --- tests/test_workspacebuilder.py | 68 ++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/tests/test_workspacebuilder.py b/tests/test_workspacebuilder.py index 158f9f68751..4f1edcfc0ac 100644 --- a/tests/test_workspacebuilder.py +++ b/tests/test_workspacebuilder.py @@ -1021,3 +1021,71 @@ def test_load_workspace_enter( assert output in captured_pane else: assert output not in captured_pane + + +@pytest.mark.parametrize( + "yaml,output", + [ + [ + textwrap.dedent( + """ +session_name: Should not execute +windows: +- panes: + - shell_command: echo "___$((1 + 3))___" + sleep: 2 + """ + ), + "___4___", + ], + [ + textwrap.dedent( + """ +session_name: Should not execute +windows: +- panes: + - shell_command: + - echo "___$((1 + 3))___" + sleep: 2 + """ + ), + "___4___", + ], + ], + ids=[ + "pane_sleep_3_shortform", + "pane_sleep_3_longform", + ], +) +def test_load_workspace_sleep( + tmp_path: pathlib.Path, + server: libtmux.Server, + monkeypatch: pytest.MonkeyPatch, + yaml, + output, +): + yaml_config = tmp_path / "simple.yaml" + yaml_config.write_text( + yaml, + encoding="utf-8", + ) + sconfig = kaptan.Kaptan(handler="yaml") + sconfig = sconfig.import_config(str(yaml_config)).get() + sconfig = config.expand(sconfig) + sconfig = config.trickle(sconfig) + builder = WorkspaceBuilder(sconf=sconfig, server=server) + builder.build() + + t = time.process_time() + + time.sleep(1) + session = builder.session + pane = session.attached_pane + + while (time.process_time() - t) * 1000 < 2: + captured_pane = "\n".join(pane.capture_pane()) + + assert output not in captured_pane + time.sleep(0.1) + captured_pane = "\n".join(pane.capture_pane()) + assert output in captured_pane From aee1e7b305cf787ae10fa9513daf71aced0146a4 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 07:36:07 -0500 Subject: [PATCH 3/4] Add changelog note --- CHANGES | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/CHANGES b/CHANGES index 159640a0491..f346d41067c 100644 --- a/CHANGES +++ b/CHANGES @@ -6,6 +6,18 @@ #### What's new +- #749: Sleep before command execution, via `sleep: false` (per-pane only) + + ```yaml + session_name: Should not execute + windows: + - panes: + - shell_command: echo "___$((1 + 3))___" + sleep: 2 + ``` + + This will pause 2 seconds before execution + - #747: Skip execution via `enter: false` ```yaml From 93b4ff11f5e34da8bfe7171debd4b21e97296895 Mon Sep 17 00:00:00 2001 From: Tony Narlock Date: Sun, 13 Mar 2022 07:40:00 -0500 Subject: [PATCH 4/4] docs(sleep): Sleep command --- docs/examples.md | 27 +++++++++++++++++++++++++++ examples/sleep.json | 13 +++++++++++++ examples/sleep.yaml | 5 +++++ 3 files changed, 45 insertions(+) create mode 100644 examples/sleep.json create mode 100644 examples/sleep.yaml diff --git a/docs/examples.md b/docs/examples.md index d47970321dd..713e4dce32f 100644 --- a/docs/examples.md +++ b/docs/examples.md @@ -356,6 +356,33 @@ Omit sending {kbd}`enter` to key commands. Equivalent to ```` +## Pause / delay command execution + +```{versionadded} 1.10.0bX +`sleep: [seconds]` option +``` + +Omit sending {kbd}`enter` to key commands. Equivalent to +[`time.sleep()`](time.sleep) before [`send_keys(enter=False)`](libtmux.Pane.send_keys). + +````{tab} YAML + +```{literalinclude} ../examples/sleep.yaml +:language: yaml + +``` + +```` + +````{tab} JSON + +```{literalinclude} ../examples/sleep.json +:language: json + +``` + +```` + ## Window Index You can specify a window's index using the `window_index` property. Windows diff --git a/examples/sleep.json b/examples/sleep.json new file mode 100644 index 00000000000..ce95ffd24f3 --- /dev/null +++ b/examples/sleep.json @@ -0,0 +1,13 @@ +{ + "session_name": "Should sleep 3 seconds before sending pane commands", + "windows": [ + { + "panes": [ + { + "shell_command": "echo \"___$((1 + 3))___\"", + "sleep": 3 + } + ] + } + ] +} \ No newline at end of file diff --git a/examples/sleep.yaml b/examples/sleep.yaml new file mode 100644 index 00000000000..d114f8ad902 --- /dev/null +++ b/examples/sleep.yaml @@ -0,0 +1,5 @@ +session_name: Should sleep 3 seconds before sending pane commands +windows: +- panes: + - shell_command: echo "___$((1 + 3))___" + sleep: 3