forked from markbirbeck/sublime-text-shell-command
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShellCommand.py
182 lines (134 loc) · 6.57 KB
/
ShellCommand.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import sublime
from . import SublimeHelper as SH
from . import OsShell
class ShellCommandCommand(SH.TextCommand):
def __init__(self, plugin, default_prompt=None, **kwargs):
SH.TextCommand.__init__(self, plugin, **kwargs)
if default_prompt is None:
self.default_prompt = 'Shell Command'
else:
self.default_prompt = default_prompt
self.data_key = 'ShellCommand'
self.output_written = False
def run(self, edit, command=None, command_prefix=None, prompt=None, region=None, arg_required=None, panel=None, title=None, syntax=None, refresh=None, wait_for_completion=None):
if region is None:
region is False
if arg_required is None:
arg_required = False
if panel is None:
panel = False
if refresh is None:
refresh = False
arg = None
# If regions should be used then work them out, and append
# them to the command:
#
if region is True:
arg = self.get_region().strip()
if arg == '':
if arg_required is True:
sublime.message_dialog('This command requires a parameter.')
return
# Setup a closure to run the command:
#
def _C(command):
if command_prefix is not None:
command = command_prefix + ' ' + command
if arg is not None:
command = command + ' ' + arg
self.run_shell_command(command, panel=panel, title=title, syntax=syntax, refresh=refresh, wait_for_completion=wait_for_completion)
# If no command is specified then we prompt for one, otherwise
# we can just execute the command:
#
if command is None:
if prompt is None:
prompt = self.default_prompt
self.view.window().show_input_panel(prompt, '', _C, None, None)
else:
_C(command)
def run_shell_command(self, command=None, panel=False, title=None, syntax=None, refresh=False, console=None, working_dir=None, wait_for_completion=None):
view = self.view
window = view.window()
settings = sublime.load_settings('ShellCommand.sublime-settings')
if command is None:
sublime.message_dialog('No command provided.')
return
if working_dir is None:
working_dir = self.get_working_dir()
# Run the command and write any output to the buffer:
#
message = self.default_prompt + ': (' + ''.join(command)[:20] + ')'
self.finished = False
self.output_target = None
self.output_written = False
# Start our progress bar in the initiating window. If a new window
# gets opened then the progress bar will get moved to that:
#
self.progress = SH.ProgressDisplay(view, message, message,
settings.get('progress_display_heartbeat'))
self.progress.start()
def _C(output):
# If output is None then the command has finished:
#
if output is None:
self.finished = True
# If there has been no output:
#
if self.output_written is False:
show_message = settings.get('show_success_but_no_output_message')
if show_message:
output = settings.get('success_but_no_output_message')
# Check whether the initiating view needs refreshing:
#
if refresh is True:
view.run_command('shell_command_refresh')
# Stop the progress bar:
#
self.progress.stop()
# If there is something to output...
#
if output is not None:
# ...only allow blank lines if something else has already been
# written:
#
if self.output_written is True or len(output.strip()) > 0:
# If no output window has been created yet then create one now:
#
if self.output_target is None:
self.output_target = SH.OutputTarget(window,
self.data_key,
command,
working_dir,
title=title,
syntax=syntax,
panel=panel,
console=console)
# Switch our progress bar to the new window:
#
if self.finished is False:
self.progress.stop()
self.progress = SH.ProgressDisplay(self.output_target, message, message,
settings.get('progress_display_heartbeat'))
self.progress.start()
# Append our output to whatever buffer is being used, and
# track that some output has now been written:
#
self.output_target.append_text(output)
self.output_written = True
OsShell.process(command, _C, settings=settings, working_dir=working_dir, wait_for_completion=wait_for_completion)
class ShellCommandOnRegionCommand(ShellCommandCommand):
def run(self, edit, command=None, command_prefix=None, prompt=None, arg_required=None, panel=None, title=None, syntax=None, refresh=None):
ShellCommandCommand.run(self, edit, command=command, command_prefix=command_prefix, prompt=prompt, region=True, arg_required=True, panel=panel, title=title, syntax=syntax, refresh=refresh)
# Refreshing a shell command simply involves re-running the original command:
#
class ShellCommandRefreshCommand(ShellCommandCommand):
def run(self, edit, callback=None):
console = self.view
settings = console.settings()
if settings.has(self.data_key):
data = settings.get(self.data_key + '_data', None)
if data is not None:
console.set_read_only(False)
console.run_command('sublime_helper_clear_buffer')
console.set_read_only(True)
self.run_shell_command(command=data['command'], console=console, working_dir=data['working_dir'])