forked from SublimeText/InsertNums
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInsertNums.py
More file actions
68 lines (52 loc) · 1.98 KB
/
InsertNums.py
File metadata and controls
68 lines (52 loc) · 1.98 KB
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
import sublime, sublime_plugin
class PromptInsertNumsCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.show_input_panel('Enter a starting number/character, step and padding.', '1 1 0', self.insertNums, self.insertNums, None)
pass
def insertNums(self, text):
try:
(current, step, padding) = map(str, text.split(" "))
if self.window.active_view():
self.window.active_view().run_command("insert_nums", {"current": current, "step": step, "padding": padding } )
except ValueError:
pass
class InsertNumsCommand(sublime_plugin.TextCommand):
digits = '0123456789'
alpha = 'abcdefghijklmnopqrstuvwxyz'
def run(self, edit, current, step, padding):
if current in self.digits:
def tick(counter):
return str('%0*d' % (int(padding), int(current) + counter))
elif current[0] == 'x':
current = int(current[1:])
def tick(counter):
return str('%0*x' % (int(padding), int(current) + counter))
elif current in self.alpha:
current = self.decode(current)
def tick(counter):
return self.encode(current + counter)
elif current in self.alpha.upper():
current = self.decode(current.lower())
def tick(counter):
return self.encode(current + counter).upper()
else:
return
# Do the stuff
counter = 0
for region in self.view.sel():
self.view.replace(edit, region, tick(counter))
counter += int(step)
def encode(self, num):
res = ''
while num > 0:
num -= 1
if(num >= 0):
res = self.alpha[int(num) % 26] + res
num /= 26
return res
def decode(self, str):
res = 0
for i in str:
res *= 26
res += self.alpha.index(i) + 1
return res