forked from qt-creator/qt-creator
macOS: Reduce clutter shown when opening terminal
By writing the needed special commands to a temporary file. What we cannot get rid of is the command for opening a bash that sources that file. Since Terminal usually opens a login shell, but we cannot set a special file for sourcing in that case, the special commands include mimicking the behavior of a login shell by reading the corresponding config files. This is in preparation to setting up the environment for the shell. Since we do not start a new process for the Terminal on macOS, we will need to explicitly export the whole environment after the fact, resulting in potentially dozens of export commands to be executed. Change-Id: Ia24cf1f00e62411734f5d6514d073e11d4cdae6e Reviewed-by: Tobias Hunger <tobias.hunger@qt.io>
This commit is contained in:
@@ -29,20 +29,15 @@ import os
|
||||
import pipes
|
||||
import subprocess
|
||||
import sys
|
||||
from tempfile import NamedTemporaryFile
|
||||
|
||||
def quote_for_applescript(arg):
|
||||
def quote_applescript(arg):
|
||||
return arg.replace('\\', '\\\\').replace('"', '\\"')
|
||||
|
||||
def quote(arg):
|
||||
return quote_for_applescript(pipes.quote(arg))
|
||||
def quote_shell(arg):
|
||||
return pipes.quote(arg)
|
||||
|
||||
def quote_args(args):
|
||||
return [quote(arg) for arg in args]
|
||||
|
||||
def cd_cwd_arguments():
|
||||
return ['cd', os.getcwd()]
|
||||
|
||||
def apple_script(shell_script):
|
||||
def apple_script(shell_command):
|
||||
return '''
|
||||
--Terminal opens a window by default when it is not running, so check
|
||||
on applicationIsRunning(applicationName)
|
||||
@@ -63,13 +58,21 @@ tell application "Terminal"
|
||||
set currentWindow to first window whose tabs contains currentTab
|
||||
activate
|
||||
end tell
|
||||
'''.format(shell_script)
|
||||
'''.format(shell_command)
|
||||
|
||||
def main():
|
||||
args = quote_args(cd_cwd_arguments()) + [';'] + quote_args(sys.argv[1:])
|
||||
shell_script = ' '.join(args)
|
||||
osascript_process = subprocess.Popen(['/usr/bin/osascript'], stdin=subprocess.PIPE)
|
||||
osascript_process.communicate(apple_script(shell_script))
|
||||
# create temporary file to be sourced into bash that deletes itself
|
||||
with NamedTemporaryFile(delete=False) as shell_script:
|
||||
quoted_shell_script = quote_shell(shell_script.name)
|
||||
commands = ('cd ' + quote_shell(os.getcwd()) + '\n' +
|
||||
' '.join([quote_shell(arg) for arg in sys.argv[1:]]) + '\n' +
|
||||
'rm ' + quoted_shell_script + '\n'
|
||||
)
|
||||
shell_script.write(commands)
|
||||
shell_script.flush()
|
||||
shell_command = quote_applescript('source ' + quoted_shell_script)
|
||||
osascript_process = subprocess.Popen(['/usr/bin/osascript'], stdin=subprocess.PIPE)
|
||||
osascript_process.communicate(apple_script(shell_command))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user