-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
profiles.py
177 lines (130 loc) · 5.35 KB
/
profiles.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
import os, re, shutil, sublime, sublime_plugin
package_name = 'sublime-profiles'
def plugin_loaded():
global base_path, current_profile_path, previous_profile_path, preferences_path
base_path = os.path.join(sublime.packages_path(), package_name, 'Data')
current_profile_path = os.path.join(base_path, 'current')
previous_profile_path = os.path.join(base_path, 'previous')
preferences_path = os.path.join(sublime.packages_path(), 'User', 'Preferences.sublime-settings')
def current_profile():
if os.path.exists(current_profile_path):
with open(current_profile_path) as file:
return file.read().strip()
else:
return 'Default'
def previous_profile():
if os.path.exists(previous_profile_path):
with open(previous_profile_path) as file:
return file.read().strip()
else:
return None
def profile_path(name):
return os.path.join(base_path, name + '.profile')
def profiles():
items = {current_profile()}
if os.path.exists(base_path):
for path in os.listdir(base_path):
if path.endswith('.profile'):
items.add(path[:-len('.profile')])
return sorted(items)
# Create
class ProfileNameInputHandler(sublime_plugin.TextInputHandler):
def placeholder(self):
return "Profile name"
def preview(self, text):
text = text.strip()
if text.lower() in [p.lower() for p in profiles()]:
return "'" + text + "' already exists"
def validate(self, text):
text = text.strip().lower()
return bool(text and text not in [p.lower() for p in profiles()])
class CreateProfileCommand(sublime_plugin.ApplicationCommand):
def run(self, profile_name):
os.makedirs(base_path, exist_ok=True)
# Save current
shutil.copyfile(preferences_path, profile_path(current_profile()))
# Save old current to previous
with open(previous_profile_path, "w") as file:
file.write(current_profile())
# Save new current name
with open(current_profile_path, "w") as file:
file.write(profile_name)
def input(self, args):
return ProfileNameInputHandler()
# Switch
class SwitchProfileInputHandler(sublime_plugin.ListInputHandler):
def __init__(self):
self.profiles = profiles()
self.current_profile = current_profile()
def placeholder(self):
return 'Select profile'
def list_items(self):
return [("→ " + x if x == self.current_profile else x) for x in self.profiles]
def validate(self, value):
return not value.startswith("→ ")
class SwitchProfileCommand(sublime_plugin.ApplicationCommand):
def run(self, switch_profile):
CreateProfileCommand().run(switch_profile)
# Load target profile
switch_profile = switch_profile.strip("→ ")
switch_profile_path = profile_path(switch_profile)
shutil.copyfile(switch_profile_path, preferences_path)
# Remove old copy of new current
os.remove(switch_profile_path)
def input(self, args):
return SwitchProfileInputHandler()
def is_enabled(self):
return len(profiles()) > 1
# Rename
class RenameProfileInputHandler(sublime_plugin.ListInputHandler):
def __init__(self):
self.profiles = profiles()
self.current_profile = current_profile()
def placeholder(self):
return 'Select profile'
def list_items(self):
return [("→ " + x if x == self.current_profile else x) for x in self.profiles if x != "Default"]
def next_input(self, args):
return ProfileNameInputHandler()
class RenameProfileCommand(sublime_plugin.ApplicationCommand):
def run(self, rename_profile, profile_name):
rename_profile = rename_profile.strip("→ ")
if rename_profile == current_profile():
with open(current_profile_path, "w") as file:
file.write(profile_name)
else:
os.rename(profile_path(rename_profile), profile_path(profile_name))
if previous_profile() == rename_profile:
with open(previous_profile_path, "w") as file:
file.write(profile_name)
def input(self, args):
return RenameProfileInputHandler()
def is_enabled(self):
return len(profiles()) > 1
# Delete
class DeleteProfileInputHandler(sublime_plugin.ListInputHandler):
def __init__(self):
self.profiles = profiles()
self.current_profile = current_profile()
def placeholder(self):
return 'Select profile'
def list_items(self):
return ["× " + x for x in self.profiles if x != "Default" and x != self.current_profile]
class DeleteProfileCommand(sublime_plugin.ApplicationCommand):
def run(self, delete_profile):
delete_profile = delete_profile[len("× "):]
os.remove(profile_path(delete_profile))
if (previous_profile() == delete_profile):
os.remove(previous_profile_path)
def input(self, args):
return DeleteProfileInputHandler()
def is_enabled(self):
return len(profiles()) > (1 if current_profile() == "Default" else 2)
# Toggle
class ToggleProfileCommand(sublime_plugin.ApplicationCommand):
def run(self):
p = previous_profile()
if p:
SwitchProfileCommand().run(p)
def is_enabled(self):
return os.path.exists(previous_profile_path)