-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathOpenTerminal.py
55 lines (43 loc) · 1.32 KB
/
OpenTerminal.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
import sublime
import sublime_plugin
import os
from subprocess import Popen
class OpenTerminal(sublime_plugin.WindowCommand):
def _get_paths(self, paths):
# empty list
result = []
# loop paths and add to valid directories
for path in paths:
if path == "?filename":
if not self.window.active_view().file_name() is None:
result.append(os.path.dirname(os.path.realpath(self.window.active_view().file_name())))
elif os.path.isfile(path):
result.append(os.path.dirname(os.path.realpath(path)))
elif os.path.isdir(path):
result.append(os.path.realpath(path))
else:
pass
# remove duplicates
if len(result) > 0:
result = list(set(result))
# finally...
return result
def run(self, paths):
# get command from settings
settings = sublime.load_settings("OpenTerminal.sublime-settings")
command = settings.get("command", "gnome-terminal --working-directory=\"{0}\"")
paths = self._get_paths(paths)
# add home directory if list is empty
if len(paths) == 0:
paths.append(os.path.expanduser("~"))
# open each path in terminal
for path in paths:
Popen(command.format(path), shell=True)
def description(self, paths):
# default caption
return None
def is_visible(self, paths):
# always visible
return True
def is_enabled(self, paths):
return len(self._get_paths(paths)) > 0