-
Notifications
You must be signed in to change notification settings - Fork 37
/
exec.py
70 lines (60 loc) · 2.47 KB
/
exec.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import os
import sublime_plugin
class TerminalViewExec(sublime_plugin.WindowCommand):
_init_args = {}
def run(self, **kwargs):
# Get the title for the view.
name = kwargs.get("name", "Executable")
# Custom environment variables are ignored for now. LinuxPty should be
# able to handle that in the future.
env = kwargs.get("env", {})
env = os.environ.copy().update(env)
# Get the command that we'll invoke.
cmd = kwargs.get("cmd", [])
if not cmd:
cmd = [kwargs.get("shell_cmd", "")]
# Get the cwd.
working_dir = kwargs.get("working_dir")
if not working_dir:
view = self.window.active_view()
if view and view.file_name():
working_dir = os.path.basedir(view.file_name())
else:
working_dir = env.get("HOME", "")
if not working_dir:
working_dir = "/"
# If there's init args, get those.
args = kwargs.get("args", "")
invocation = " ".join(cmd)
if not args:
# Otherwise, prompt the user for init args.
self.name = name
self.invocation = invocation
self.working_dir = working_dir
# Retrieve the init args for lazy people
cached_args = self.__class__._init_args.get(invocation, "")
title = 'Arguments for "{}" '.format(invocation)
if cached_args:
title += ":"
else:
title += "(press Enter for no args): "
self.window.show_input_panel(title,
cached_args,
self._on_done,
None,
None)
else:
self._run(invocation, working_dir, name)
def _on_done(self, text):
# Cache the init args for lazy people
self.__class__._init_args[self.invocation] = text
self.invocation += " " + text
self._run(self.invocation, self.working_dir, self.name)
def _run(self, cmd, cwd, title):
self.window.run_command("terminal_view_open",
{
"cmd": cmd,
"cwd": cwd,
"title": title,
"keep_open": True
})