diff --git a/docs/cli.md b/docs/cli.md index 5c4ccf7fe5c..be9f1adc302 100644 --- a/docs/cli.md +++ b/docs/cli.md @@ -130,13 +130,19 @@ my_window ## Freeze sessions ``` +tmuxp freeze tmuxp freeze +tmuxp freeze --force ``` You can save the state of your tmux session by freezing it. Tmuxp will offer to save your session state to `.json` or `.yaml`. +If no session is specified, it will default to the attached session. + +If the `--force` argument is passed, it will overwrite any existing config file with the same name. + (cli-load)= ## Load session diff --git a/tests/test_cli.py b/tests/test_cli.py index cf0ec046b4c..feb5a1f9710 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -868,9 +868,9 @@ def test_import_tmuxinator(cli_args, inputs, tmpdir, monkeypatch): @pytest.mark.parametrize( "cli_args,inputs", [ - (['freeze', 'mysession'], ['\n', 'y\n', './la.yaml\n', 'y\n']), + (['freeze', 'myfrozensession'], ['\n', 'y\n', './la.yaml\n', 'y\n']), ( # Exists - ['freeze', 'mysession'], + ['freeze', 'myfrozensession'], ['\n', 'y\n', './exists.yaml\n', './la.yaml\n', 'y\n'], ), ( # Imply current session if not entered @@ -878,21 +878,21 @@ def test_import_tmuxinator(cli_args, inputs, tmpdir, monkeypatch): ['\n', 'y\n', './la.yaml\n', 'y\n'], ), (['freeze'], ['\n', 'y\n', './exists.yaml\n', './la.yaml\n', 'y\n']), # Exists - ( # Create a new one - ['freeze', 'mysession', '--force'], - ['\n', 'y\n', './la.yaml\n', 'y\n'], - ), - ( # Imply current session if not entered - ['freeze', '--force'], - ['\n', 'y\n', './la.yaml\n', 'y\n'], - ), ], ) def test_freeze(server, cli_args, inputs, tmpdir, monkeypatch): monkeypatch.setenv('HOME', str(tmpdir)) tmpdir.join('exists.yaml').ensure() - server.new_session(session_name='mysession') + server.new_session(session_name='myfirstsession') + server.new_session(session_name='myfrozensession') + + # Assign an active pane to the session + second_session = server.list_sessions()[1] + first_pane_on_second_session_id = second_session.list_windows()[0].list_panes()[0][ + "pane_id" + ] + monkeypatch.setenv("TMUX_PANE", first_pane_on_second_session_id) with tmpdir.as_cwd(): runner = CliRunner() @@ -902,6 +902,11 @@ def test_freeze(server, cli_args, inputs, tmpdir, monkeypatch): print(out.output) assert tmpdir.join('la.yaml').check() + yaml_config = tmpdir.join('la.yaml').open().read() + frozen_config = kaptan.Kaptan(handler='yaml').import_config(yaml_config).get() + + assert frozen_config['session_name'] == 'myfrozensession' + @pytest.mark.parametrize( "cli_args,inputs", diff --git a/tests/test_util.py b/tests/test_util.py index 17568ce7c13..420e1abfd1f 100644 --- a/tests/test_util.py +++ b/tests/test_util.py @@ -5,7 +5,7 @@ from tmuxp import exc from tmuxp.exc import BeforeLoadScriptError, BeforeLoadScriptNotExists -from tmuxp.util import run_before_script +from tmuxp.util import get_session, run_before_script from . import fixtures_dir @@ -49,3 +49,23 @@ def test_beforeload_returns_stderr_messages(): with pytest.raises(exc.BeforeLoadScriptError) as excinfo: run_before_script(script_file) assert excinfo.match(r'failed with returncode') + + +def test_get_session_should_default_to_local_attached_session(server, monkeypatch): + server.new_session(session_name='myfirstsession') + second_session = server.new_session(session_name='mysecondsession') + + # Assign an active pane to the session + first_pane_on_second_session_id = second_session.list_windows()[0].list_panes()[0][ + 'pane_id' + ] + monkeypatch.setenv('TMUX_PANE', first_pane_on_second_session_id) + + assert get_session(server) == second_session + + +def test_get_session_should_return_first_session_if_no_active_session(server): + first_session = server.new_session(session_name='myfirstsession') + server.new_session(session_name='mysecondsession') + + assert get_session(server) == first_session diff --git a/tmuxp/cli.py b/tmuxp/cli.py index 201d72282e0..9605e5d0eca 100644 --- a/tmuxp/cli.py +++ b/tmuxp/cli.py @@ -924,7 +924,7 @@ def command_freeze(session_name, socket_name, socket_path, force): if session_name: session = t.find_where({'session_name': session_name}) else: - session = t.list_sessions()[0] + session = util.get_session(t) if not session: raise exc.TmuxpException('Session not found.') diff --git a/tmuxp/util.py b/tmuxp/util.py index 7fc32f713da..725dcde8bdc 100644 --- a/tmuxp/util.py +++ b/tmuxp/util.py @@ -111,10 +111,17 @@ def get_session(server, session_name=None, current_pane=None): elif current_pane is not None: session = server.find_where({'session_id': current_pane['session_id']}) else: - session = server.list_sessions()[0] + current_pane = get_current_pane(server) + if current_pane: + session = server.find_where({'session_id': current_pane['session_id']}) + else: + session = server.list_sessions()[0] if not session: - raise exc.TmuxpException('Session not found: %s' % session_name) + if session_name: + raise exc.TmuxpException('Session not found: %s' % session_name) + else: + raise exc.TmuxpException('Session not found') return session diff --git a/tmuxp/workspacebuilder.py b/tmuxp/workspacebuilder.py index 16ab6e90a1a..c38b1c5b01b 100644 --- a/tmuxp/workspacebuilder.py +++ b/tmuxp/workspacebuilder.py @@ -348,7 +348,7 @@ def get_pane_shell(): attach=True, start_directory=get_pane_start_directory(), shell=get_pane_shell(), - target=p.id + target=p.id, ) assert isinstance(p, Pane)