This repository has been archived by the owner on Mar 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Commands.py
101 lines (85 loc) · 3.46 KB
/
Commands.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
# -*- coding: utf-8 -*-
import sublime
import sublime_plugin
import subprocess
from os import environ
import re
class Settings:
def get(setting):
return sublime.load_settings('Z.sublime-settings').get(setting)
class ZCommand:
def __init__(self, directory_regex):
self.directory_regex = directory_regex
self.shell = Settings.get('shell') or environ.get('SHELL')
self.command = [ self.shell, '-l', '-c' ]
def run(self, flag):
try:
zcmd = [ environ.get('_Z_CMD') or 'z', flag, '"{}"'.format(self.directory_regex) ]
output = subprocess.check_output(self.command + [' '.join(zcmd)], shell=False)
return output.decode("utf-8")
except subprocess.CalledProcessError as e:
e.returncode
if e.returncode == 1:
return ''
elif e.returncode == 127:
raise Exception("z not found")
raise
except Exception as e:
raise
def execute(self):
self.run('')
def getBestMatch(self):
match = self.run('--echo')
return match.strip() if match else None
def getList(self):
matches = self.run('--list')
return re.findall('^[0-9]+\.?[0-9]*\s+(.*)', matches, re.MULTILINE) if matches else None
class ZJumpToDirectoryCommand(sublime_plugin.WindowCommand):
def run(self, menu=None, action=None):
self.mode = Settings.get('mode')
if self.mode == 'list_all':
self.mode_list(ZCommand(''))
else:
self.window.show_input_panel("Z: Directory RegEx", "", self.on_show_input_panel_done, None, None)
def on_show_input_panel_done(self, directory_regex):
z = ZCommand(directory_regex)
if self.mode == 'best_match':
self.mode_best_match(z)
elif self.mode == 'list':
self.mode_list(z)
else:
raise Exception('Unknown z mode')
def mode_best_match(self, z):
try:
directory = z.getBestMatch()
if directory:
self.open_directory(directory)
else:
sublime.message_dialog('No match for {}'.format(z.directory_regex))
except Exception as e:
sublime.error_message(str(e))
def mode_list(self, z):
try:
directory_list = z.getList()
if directory_list:
if len(directory_list) == 1:
self.open_directory(directory_list[0])
else:
self.show_directories(directory_list)
else:
sublime.message_dialog('No matches for {}'.format(z.directory_regex))
except Exception as e:
sublime.error_message(str(e))
def on_directory_selected(self, directory):
if directory:
self.open_directory(directory)
def open_directory(self, directory):
if self.window.project_data():
sublime.run_command('new_window')
sublime.active_window().set_project_data({'folders': [{'path': directory}]})
# Since everything has been successful let's actually z jump to the directory
# as if we were on the command line
ZCommand(directory).execute()
def show_directories(self, directory_list):
selectedCallback = lambda index: self.on_directory_selected(directory_list[index] if index > -1 else None)
self.window.show_quick_panel(directory_list, selectedCallback, sublime.KEEP_OPEN_ON_FOCUS_LOST, 0, None)