Skip to content

Commit 5bfd582

Browse files
authored
feat(freeze): tmuxp freeze: Infer the attached session if none specified (#660)
Entering `tmuxp freeze` without any argument will now detect the current attached session.
2 parents 97b7e8d + 6c5ffe0 commit 5bfd582

File tree

6 files changed

+54
-16
lines changed

6 files changed

+54
-16
lines changed

docs/cli.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,19 @@ my_window
130130
## Freeze sessions
131131

132132
```
133+
tmuxp freeze
133134
tmuxp freeze <session_name>
135+
tmuxp freeze --force <session_name>
134136
```
135137

136138
You can save the state of your tmux session by freezing it.
137139

138140
Tmuxp will offer to save your session state to `.json` or `.yaml`.
139141

142+
If no session is specified, it will default to the attached session.
143+
144+
If the `--force` argument is passed, it will overwrite any existing config file with the same name.
145+
140146
(cli-load)=
141147

142148
## Load session

tests/test_cli.py

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -868,31 +868,31 @@ def test_import_tmuxinator(cli_args, inputs, tmpdir, monkeypatch):
868868
@pytest.mark.parametrize(
869869
"cli_args,inputs",
870870
[
871-
(['freeze', 'mysession'], ['\n', 'y\n', './la.yaml\n', 'y\n']),
871+
(['freeze', 'myfrozensession'], ['\n', 'y\n', './la.yaml\n', 'y\n']),
872872
( # Exists
873-
['freeze', 'mysession'],
873+
['freeze', 'myfrozensession'],
874874
['\n', 'y\n', './exists.yaml\n', './la.yaml\n', 'y\n'],
875875
),
876876
( # Imply current session if not entered
877877
['freeze'],
878878
['\n', 'y\n', './la.yaml\n', 'y\n'],
879879
),
880880
(['freeze'], ['\n', 'y\n', './exists.yaml\n', './la.yaml\n', 'y\n']), # Exists
881-
( # Create a new one
882-
['freeze', 'mysession', '--force'],
883-
['\n', 'y\n', './la.yaml\n', 'y\n'],
884-
),
885-
( # Imply current session if not entered
886-
['freeze', '--force'],
887-
['\n', 'y\n', './la.yaml\n', 'y\n'],
888-
),
889881
],
890882
)
891883
def test_freeze(server, cli_args, inputs, tmpdir, monkeypatch):
892884
monkeypatch.setenv('HOME', str(tmpdir))
893885
tmpdir.join('exists.yaml').ensure()
894886

895-
server.new_session(session_name='mysession')
887+
server.new_session(session_name='myfirstsession')
888+
server.new_session(session_name='myfrozensession')
889+
890+
# Assign an active pane to the session
891+
second_session = server.list_sessions()[1]
892+
first_pane_on_second_session_id = second_session.list_windows()[0].list_panes()[0][
893+
"pane_id"
894+
]
895+
monkeypatch.setenv("TMUX_PANE", first_pane_on_second_session_id)
896896

897897
with tmpdir.as_cwd():
898898
runner = CliRunner()
@@ -902,6 +902,11 @@ def test_freeze(server, cli_args, inputs, tmpdir, monkeypatch):
902902
print(out.output)
903903
assert tmpdir.join('la.yaml').check()
904904

905+
yaml_config = tmpdir.join('la.yaml').open().read()
906+
frozen_config = kaptan.Kaptan(handler='yaml').import_config(yaml_config).get()
907+
908+
assert frozen_config['session_name'] == 'myfrozensession'
909+
905910

906911
@pytest.mark.parametrize(
907912
"cli_args,inputs",

tests/test_util.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from tmuxp import exc
77
from tmuxp.exc import BeforeLoadScriptError, BeforeLoadScriptNotExists
8-
from tmuxp.util import run_before_script
8+
from tmuxp.util import get_session, run_before_script
99

1010
from . import fixtures_dir
1111

@@ -49,3 +49,23 @@ def test_beforeload_returns_stderr_messages():
4949
with pytest.raises(exc.BeforeLoadScriptError) as excinfo:
5050
run_before_script(script_file)
5151
assert excinfo.match(r'failed with returncode')
52+
53+
54+
def test_get_session_should_default_to_local_attached_session(server, monkeypatch):
55+
server.new_session(session_name='myfirstsession')
56+
second_session = server.new_session(session_name='mysecondsession')
57+
58+
# Assign an active pane to the session
59+
first_pane_on_second_session_id = second_session.list_windows()[0].list_panes()[0][
60+
'pane_id'
61+
]
62+
monkeypatch.setenv('TMUX_PANE', first_pane_on_second_session_id)
63+
64+
assert get_session(server) == second_session
65+
66+
67+
def test_get_session_should_return_first_session_if_no_active_session(server):
68+
first_session = server.new_session(session_name='myfirstsession')
69+
server.new_session(session_name='mysecondsession')
70+
71+
assert get_session(server) == first_session

tmuxp/cli.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ def command_freeze(session_name, socket_name, socket_path, force):
924924
if session_name:
925925
session = t.find_where({'session_name': session_name})
926926
else:
927-
session = t.list_sessions()[0]
927+
session = util.get_session(t)
928928

929929
if not session:
930930
raise exc.TmuxpException('Session not found.')

tmuxp/util.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,17 @@ def get_session(server, session_name=None, current_pane=None):
111111
elif current_pane is not None:
112112
session = server.find_where({'session_id': current_pane['session_id']})
113113
else:
114-
session = server.list_sessions()[0]
114+
current_pane = get_current_pane(server)
115+
if current_pane:
116+
session = server.find_where({'session_id': current_pane['session_id']})
117+
else:
118+
session = server.list_sessions()[0]
115119

116120
if not session:
117-
raise exc.TmuxpException('Session not found: %s' % session_name)
121+
if session_name:
122+
raise exc.TmuxpException('Session not found: %s' % session_name)
123+
else:
124+
raise exc.TmuxpException('Session not found')
118125

119126
return session
120127

tmuxp/workspacebuilder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def get_pane_shell():
348348
attach=True,
349349
start_directory=get_pane_start_directory(),
350350
shell=get_pane_shell(),
351-
target=p.id
351+
target=p.id,
352352
)
353353

354354
assert isinstance(p, Pane)

0 commit comments

Comments
 (0)