-
Notifications
You must be signed in to change notification settings - Fork 6
/
bin_to_dec.py
28 lines (25 loc) · 903 Bytes
/
bin_to_dec.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
import sublime
import sublime_plugin
class BinToDecCommand(sublime_plugin.TextCommand):
MAX_STR_LEN = 20
def run(self, edit):
v = self.view
reglist = list(v.sel())
for j in range(0, len(reglist)):
bin = v.substr(v.sel()[j])
bin = bin.strip()
l = True
if bin == '':
l = False
for i in bin:
if not((i == '1') or (i == '0')):
l = False
if l:
v.replace(edit, v.sel()[j], str(int(bin, 2)))
else:
if len(bin) > self.MAX_STR_LEN:
logMsg = bin[0:self.MAX_STR_LEN] + "..."
else:
logMsg = bin
sublime.status_message("\"%s\" isn't a binary number!" % logMsg)
sublime.error_message("\"%s\" isn't a binary number!" % logMsg)