From 7a4436681be98c0d32b6d6942226ea0e4013cecd Mon Sep 17 00:00:00 2001 From: Jonathan Haylett Date: Sat, 2 Nov 2019 19:09:39 +0000 Subject: [PATCH] Launch programs with i3 exec command Switched to using i3's exec command instead of Python's subprocess module for restoring programs because otherwise if you restore a workspace that is not currently focused and i3-resurrect switches workspace, programs may be restored on the previously focused workspace instead of the correct one. --- i3_resurrect/main.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/i3_resurrect/main.py b/i3_resurrect/main.py index 3dc6e6c..1bc363c 100644 --- a/i3_resurrect/main.py +++ b/i3_resurrect/main.py @@ -175,7 +175,7 @@ def restore_programs(workspace, directory): commands_file = Path(directory) / f'workspace_{workspace}_programs.json' commands = json.loads(commands_file.read_text()) for entry in commands: - command = entry['command'] + cmdline = entry['command'] working_directory = entry['working_directory'] # If the working directory does not exist, set working directory to @@ -183,20 +183,19 @@ def restore_programs(workspace, directory): if not Path(working_directory).exists(): working_directory = Path.home() - # If command has multiple arguments, split them into an array. - if isinstance(command, list): - cmdline = command + # If cmdline is array, join it into one string for use with i3's exec + # command. + if isinstance(cmdline, list): + # Quote each argument of the command in case some of them contain + # spaces. + for i in range(0, len(cmdline)): + cmdline[i] = f'"{cmdline[i]}"' + command = ' '.join(cmdline) else: - cmdline = shlex.split(command) + command = cmdline # Execute command as subprocess. - subprocess.Popen( - cmdline, - cwd=working_directory, - env={**os.environ, **{'PWD': working_directory}}, - stdout=subprocess.DEVNULL, - stderr=subprocess.STDOUT, - ) + i3.command(f'exec cd "{working_directory}" && {command}') def restore_layout(workspace, directory):