forked from adamchainz/SublimeFiglet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfiglet.py
188 lines (150 loc) · 6.19 KB
/
figlet.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
178
179
180
181
182
183
184
185
186
187
188
# coding=utf-8
import os
import sublime
import sublime_plugin
import sys
BASE_PATH = os.path.abspath(os.path.dirname(__file__))
sys.path.append(BASE_PATH)
def add_comment(text, comment_start, comment_end):
settings = sublime.load_settings("Preferences.sublime-settings")
padding = settings.get('figlet_comment_padding', 4)
max_width = get_max_line_length(text)
comment_line_char = comment_start[-1]
# Exception HTML
if comment_start == '<!--':
comment_line_char = '!'
# Exception MATLAB
if comment_start == '%':
comment_line_char = '#'
# Exception Pascal
if comment_start == '{':
comment_line_char = '#'
# First row
horizontal_border = comment_start + \
((max_width + 2 * padding) * comment_line_char) + \
comment_end
result = horizontal_border + '\n'
# Content
for line in text.split('\n'):
result += comment_start + padding * ' ' + line + \
(max_width - len(line) + padding) * ' ' + comment_end + '\n'
# Last row
result += horizontal_border
return result
def figlet_text(text):
import pyfiglet
settings = sublime.load_settings("Preferences.sublime-settings")
font = settings.get('figlet_font', 'standard')
width = get_width()
result = pyfiglet.Figlet(font=font, width=width).renderText(text=text)
# Strip trailing whitespace, because why not?
if settings.get('figlet_no_trailing_spaces', True):
result = '\n'.join((line.rstrip() for line in result.split('\n')))
return result[:len(result) - 1]
def get_width():
# Return width to wrap at (or large number if wrapping not enabled)
width = 1000000
view_settings = sublime.active_window().active_view().settings()
if view_settings.get('word_wrap'):
output_width = view_settings.get('wrap_width')
if output_width not in (None, 0):
width = output_width
return width
def get_max_line_length(text):
return max(len(line) for line in text.split('\n'))
class FigletSelectFontCommand(sublime_plugin.WindowCommand):
def run(self):
import pyfiglet
self.fonts = pyfiglet.FigletFont.getFonts()
self.window.show_quick_panel(self.fonts, self.on_done)
def on_done(self, index):
settings = sublime.load_settings("Preferences.sublime-settings")
settings.set("figlet_font", self.fonts[index])
sublime.save_settings("Preferences.sublime-settings")
class FigletTextCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.active_view()
sel = view.sel()
if len(sel) == 1 and sel[0].size() > 0:
view.run_command('figlet_insert_text', {'text': None})
else:
self.window.show_input_panel("Text to Figletize:", "",
self.on_done, None, None)
def on_done(self, text):
view = self.window.active_view()
view.run_command('figlet_insert_text', {'text': text})
class FigletCommentCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.active_view()
sel = view.sel()
if len(sel) == 1 and sel[0].size() > 0:
view.run_command('figlet_insert_text', {'text': None,
'comment': True})
else:
self.window.show_input_panel("Text to Figletize in comment:", "",
self.on_done, None, None)
def on_done(self, text):
view = self.window.active_view()
view.run_command('figlet_insert_text', {'text': text, 'comment': True})
class FigletInsertTextCommand(sublime_plugin.TextCommand):
def run(self, edit, text=None, comment=False):
view = self.view
sel = view.sel()
text_length = 0
if text is None: # ... then grab selection
if len(sel) != 1 or sel[0].size() == 0:
return
text = view.substr(sel[0])
text_length = len(text)
cursor = min(sel[0].a, sel[0].b)
text = figlet_text(text)
if comment:
comment_start, comment_end = self.getCommentPrefix()
text = add_comment(text, comment_start, comment_end)
# Add tabulation?
tab = (len(view.line(cursor)) - text_length) * ' '
text = '\n'.join((tab + line for line in text.split('\n')))
text = text[len(tab):]
view.erase(edit, sel[0])
view.insert(edit, cursor, text)
sel.clear()
sel.add(sublime.Region(cursor, cursor + len(text)))
def getCommentPrefix(self):
view = self.view
sel = view.sel()
cursor = min(sel[0].a, sel[0].b)
# Get comment characters
meta_infos = view.meta_info('shellVariables', cursor)
comment_start = None
comment_end = None
comment_start_2 = None
comment_end_2 = None
comment_start_3 = None
comment_end_3 = None
for meta_info in meta_infos:
if meta_info['name'] == 'TM_COMMENT_START':
comment_start = meta_info['value']
if meta_info['name'] == 'TM_COMMENT_END':
comment_end = meta_info['value']
if meta_info['name'] == 'TM_COMMENT_START_2':
comment_start_2 = meta_info['value']
if meta_info['name'] == 'TM_COMMENT_END_2':
comment_end_2 = meta_info['value']
if meta_info['name'] == 'TM_COMMENT_START_3':
comment_start_2 = meta_info['value']
if meta_info['name'] == 'TM_COMMENT_END_3':
comment_end_2 = meta_info['value']
if comment_end_2 is not None and comment_start_2 is not None:
comment_start = comment_start_2
comment_end = comment_end_2
if comment_end_3 is not None and comment_start_3 is not None:
comment_start = comment_start_3
comment_end = comment_end_3
if comment_start is None:
# When nothing is set (e.g. Plain text)
comment_start = ';'
if comment_end is None:
comment_end = comment_start[::-1]
comment_start = comment_start.replace(" ", "")
comment_end = comment_end.replace(" ", "")
return comment_start, comment_end