forked from tianjianchn/Sublime_ConEmuOpen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CmderOpen.py
61 lines (53 loc) · 2.44 KB
/
CmderOpen.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
import os, sublime, sublime_plugin
class CmderOpenCommand():
def get_project(self):
has_project = 0
if self.window.project_file_name(): # try project file name first
project_dir, project_name = os.path.split(self.window.project_file_name())
project_name, ext = os.path.splitext('st:' + project_name)
project_dir = self.window.folders()[0]
has_project = 1
elif self.window.folders(): # if no project, use the top folder
project_dir = self.window.folders()[0]
project_name = project_dir
elif self.window.active_view().file_name(): # if no folder, use current file's folder
project_dir, _ = os.path.split(self.window.active_view().file_name())
project_name = project_dir
else: #then exit
return (None, None, 0)
return (project_name, project_dir, has_project)
def open_cmder(self, dirname, title):
command= "start cmder.exe /Single /Dir \""+dirname+"\" /cmdlist powershell -new_console:t:\""+title+"\""
os.system(command)
# open project folder
class OpenCmderProjectCommand(sublime_plugin.WindowCommand, CmderCommand):
def run(self):
project_name, project_dir, has_project = self.get_project()
if not project_name:
return
self.open_cmder(project_dir, project_name)
# open "here" folder
class OpenCmderHereCommand(sublime_plugin.WindowCommand, CmderOpenCommand):
def run(self, paths=[]):
project_name, project_dir, has_project = self.get_project()
if not project_name:
return
if paths:
heredir = paths[0]
if os.path.isfile(heredir):
heredir = os.path.dirname(heredir)
elif self.window.active_view().file_name():
heredir = os.path.dirname(self.window.active_view().file_name())
else: # if no active file open, then try to open project folder
heredir = project_dir
# get the tab title
title = heredir
if has_project:
rel_path = os.path.relpath(heredir, project_dir)
if rel_path == '.':
title = project_name
elif rel_path[:2] == '..': # not in project, show the whole path
title = heredir
else:
title = project_name + ': ' + rel_path
self.open_cmder(heredir, title)