-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrandom_text.py
67 lines (53 loc) · 2.19 KB
/
random_text.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
# coding: utf-8
import string
import random
import sublime_plugin
import sublime
class RandomTextCommand(sublime_plugin.TextCommand):
def run(self, edit):
settings = sublime.load_settings("RandomText.sublime-settings")
default_set = settings.get('default_text_set', 'printable')
default_length = settings.get('random_text_length', 32)
name = self.view.settings().get("random_text_charset", default_set)
length = self.view.settings().get("random_text_length", default_length)
if name == 'default':
name = default_set
if length == 0:
length = default_length
for r in self.view.sel():
text = self.charset(name, length)
self.view.replace(edit, r, text)
def charset(self, name, length):
if (name == 'printable'):
# Printable string without whitespace or quotations
chars = string.printable
for char in chars:
if char in string.whitespace + '\'"':
chars = chars.replace(char, '')
elif (name == 'alphanumeric'):
chars = string.ascii_letters + string.digits
elif (name == 'letters'):
chars = string.ascii_letters
elif (name == 'digits'):
chars = string.digits
elif (name == 'hexdigits'):
chars = string.hexdigits.lower()
else:
chars = name
# Generate and concatenate random characters from the charset
charset = ''.join(random.SystemRandom().choice(chars) for i in range(length))
return charset
class SetRandomTextCharsetCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.show_input_panel("Charset:", "", self.on_done, None, None)
pass
def on_done(self, text):
line = str(text)
self.window.active_view().settings().set("random_text_charset", line)
class SetRandomTextLengthCommand(sublime_plugin.WindowCommand):
def run(self):
self.window.show_input_panel("Text Length:", "", self.on_done, None, None)
pass
def on_done(self, text):
line = int(text)
self.window.active_view().settings().set("random_text_length", line)