Skip to content

Freeze config for attached session #660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,19 @@ my_window
## Freeze sessions

```
tmuxp freeze
tmuxp freeze <session_name>
tmuxp freeze --force <session_name>
```

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
Expand Down
27 changes: 16 additions & 11 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -868,31 +868,31 @@ 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
['freeze'],
['\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()
Expand All @@ -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",
Expand Down
22 changes: 21 additions & 1 deletion tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion tmuxp/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down
11 changes: 9 additions & 2 deletions tmuxp/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion tmuxp/workspacebuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down