forked from viisual/ASCII-Decorator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ASCII Decorator.py
638 lines (524 loc) · 22.6 KB
/
ASCII Decorator.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
import sublime
import sublime_plugin
import os
import re
import sys
import traceback
import tempfile
ST3 = int(sublime.version()) >= 3000
if not ST3:
from subfiglet import SublimeFiglet, figlet_paths
import subcomments
else:
from .subfiglet import SublimeFiglet, figlet_paths
from . import subcomments
PACKAGE_LOCATION = os.path.abspath(os.path.dirname(__file__))
def calculate_indent(view, sel):
# Determine the indent of the CSS rule
(row, col) = view.rowcol(sel.begin())
indent_region = view.find('^\s+', view.text_point(row, 0))
if indent_region and view.rowcol(indent_region.begin())[0] == row:
indent = view.substr(indent_region)
else:
indent = ''
return indent
def remove_trailing_ws(string):
return re.sub(r'(?m) *$', '', string)
class FontPreviewGeneratorCommand(sublime_plugin.WindowCommand):
def run(self, text = "Lorem Ipsum", use_selected_text = False):
# Find directory locations
font_locations = figlet_paths()
# Verify selected text
if use_selected_text == True:
view = self.window.active_view()
selections = view.sel()
regionCount = len( selections )
if regionCount == 0 or regionCount > 1:
return
selection = selections[0]
line_A = view.line( selection.a )
line_B = view.line( selection.b )
if line_A != line_B:
return
if selection.a == selection.b:
sourceRegion = line_A
else:
sourceRegion = selection
text = view.substr( sourceRegion )
text = text.strip()
if text == "":
return
# Find available fonts
self.options = []
for fl in font_locations:
for f in os.listdir(fl):
pth = os.path.join(fl, f)
if os.path.isfile(pth):
if f.endswith((".flf", ".tlf")):
self.options.append((f[:-4], fl))
self.options.sort()
with tempfile.NamedTemporaryFile(mode = 'wb', delete=False, suffix='.txt') as p:
for font in self.options:
f = SublimeFiglet(
font=font[0], directory=font[1], width=80,
justify="auto", direction="auto"
)
p.write(("Font: %s Directory: %s\n" % (font[0], font[1])).encode("utf-8"))
p.write(
remove_trailing_ws(f.renderText(text).replace('\r\n', '\n').replace('\r', '\n')).encode('utf-8')
)
p.write("\n\n".encode("utf-8"))
self.window.open_file(p.name)
class UpdateFigletPreviewCommand(sublime_plugin.TextCommand):
"""
A reasonable edit command that works in ST2 and ST3
"""
preview = None
def run(
self, edit, font, directory=None, width=None,
justify=None, direction=None, use_additional_indent=False,
flip=None, reverse=None
):
preview = UpdateFigletPreviewCommand.get_buffer()
if not ST3:
preview = preview.encode('UTF-8')
if preview is not None:
self.view.replace(edit, sublime.Region(0, self.view.size()), preview)
sel = self.view.sel()
sel.clear()
sel.add(sublime.Region(0, self.view.size()))
self.view.run_command(
"figlet",
{
"font": font,
"directory": directory,
"use_additional_indent": use_additional_indent,
"insert_as_comment": False,
"width": width,
"justify": justify,
"direction": direction,
"flip": flip,
"reverse": reverse
}
)
UpdateFigletPreviewCommand.clear_buffer()
sel.clear()
@classmethod
def set_buffer(cls, text):
cls.preview = text
@classmethod
def get_buffer(cls):
return cls.preview
@classmethod
def clear_buffer(cls):
cls.preview = None
class FigletFavoritesCommand( sublime_plugin.TextCommand ):
def run( self, edit ):
self.undo = False
settings = sublime.load_settings('ASCII Decorator.sublime-settings')
favorites = settings.get("favorite_fonts", [])
if len(favorites) == 0:
return
self.fonts = []
for f in favorites:
if "font" not in f or "name" not in f:
continue
self.fonts.append(
{
"name": f.get("name"),
"font": f.get("font"),
"comment": f.get("comment", True),
"comment_style": f.get("comment_style", "block"),
"width": f.get("width", 80),
"direction": f.get("direction", "auto"),
"justify": f.get("justify", "auto"),
"indent": f.get("indent", True),
"flip": f.get("flip", False),
"reverse": f.get("reverse", False)
}
)
# Prepare and show quick panel
if len(self.fonts):
if not ST3:
self.view.window().show_quick_panel(
[f["name"] for f in self.fonts],
self.apply_figlet
)
else:
self.view.window().show_quick_panel(
[f["name"] for f in self.fonts],
self.apply_figlet,
on_highlight=self.preview if bool(settings.get("show_preview", False)) else None
)
def preview(self, value):
"""
Preview the figlet output (ST3 only)
"""
if value != -1:
# Find the first good selection to preview
example = None
for selection in self.view.sel():
line_A = self.view.line( selection.a )
line_B = self.view.line( selection.b )
if line_A == line_B \
and selection.a == selection.b \
and self.view.substr( line_A ).strip() != "": # use caret line
indent = calculate_indent(self.view, line_A)
example = self.view.substr( sublime.Region(line_A.begin() + len(indent), line_A.end()) )
break
elif line_A == line_B \
and selection.a != selection.b \
and self.view.substr( selection ).strip() != "": # use selection
example = self.view.substr( selection )
break
else: # multi line selection
for line in self.view.lines( selection ):
if self.view.substr( line ).strip() != "":
indent = calculate_indent(self.view, line)
example = self.view.substr( sublime.Region(line.begin() + len(indent), line.end()) )
break
if example is None:
return
# Create output panel and set to current syntax
view = self.view.window().get_output_panel('figlet_preview')
view.settings().set("draw_white_space", "none")
self.view.window().run_command("show_panel", {"panel": "output.figlet_preview"})
font = self.fonts[value]
# Preview
UpdateFigletPreviewCommand.set_buffer(example)
view.run_command(
"update_figlet_preview",
{
"font": font.get("font"),
"use_additional_indent": font.get("indent"),
"width": font.get("width"),
"justify": font.get("justify"),
"direction": font.get("direction"),
"flip": font.get("flip"),
"reverse": font.get("reverse")
}
)
def apply_figlet(self, value):
"""
Run and apply pyfiglet on the selections in the view
"""
# Hide the preview panel if shown
self.view.window().run_command("hide_panel", {"panel": "output.figlet_preview"})
# Apply figlet
if value != -1:
font = self.fonts[value]
self.view.run_command(
"figlet",
{
"font": font.get("font"),
"insert_as_comment": font.get("comment"),
"comment_style": font.get("comment_style"),
"use_additional_indent": font.get("indent"),
"width": font.get("width"),
"justify": font.get("justify"),
"direction": font.get("direction"),
"flip": font.get("flip"),
"reverse": font.get("reverse")
}
)
class FigletMenuCommand( sublime_plugin.TextCommand ):
def run( self, edit ):
self.undo = False
settings = sublime.load_settings('ASCII Decorator.sublime-settings')
# Find directory locations
font_locations = figlet_paths()
# Find available fonts
self.options = []
for fl in font_locations:
for f in os.listdir(fl):
pth = os.path.join(fl, f)
if os.path.isfile(pth):
if f.endswith((".flf", ".tlf")):
self.options.append(f)
self.options.sort()
# Prepare and show quick panel
if len(self.options):
if not ST3:
self.view.window().show_quick_panel(
[o[:-4] for o in self.options],
self.apply_figlet
)
else:
self.view.window().show_quick_panel(
[o[:-4] for o in self.options],
self.apply_figlet,
on_highlight=self.preview if bool(settings.get("show_preview", False)) else None
)
def preview(self, value):
"""
Preview the figlet output (ST3 only)
"""
if value != -1:
# Find the first good selection to preview
sel = self.view.sel()
example = None
for s in sel:
if s.size():
example = self.view.substr(s)
break
if example is None:
return
# Create output panel and set to current syntax
view = self.view.window().get_output_panel('figlet_preview')
view.settings().set("draw_white_space", "none")
self.view.window().run_command("show_panel", {"panel": "output.figlet_preview"})
# Preview
UpdateFigletPreviewCommand.set_buffer(example)
view.run_command(
"update_figlet_preview",
{
"font": self.options[value][:-4]
}
)
def apply_figlet(self, value):
"""
Run and apply pyfiglet on the selections in the view
"""
# Hide the preview panel if shown
self.view.window().run_command("hide_panel", {"panel": "output.figlet_preview"})
# Apply figlet
if value != -1:
self.view.run_command(
"figlet",
{
"font": self.options[value][:-4]
}
)
class FigletDefaultCommand( sublime_plugin.TextCommand ):
def run(self, edit):
settings = sublime.load_settings('ASCII Decorator.sublime-settings')
font = settings.get('ascii_decorator_font', "slant")
self.view.run_command("figlet", {"font": font})
class FigletCommand( sublime_plugin.TextCommand ):
"""
@todo Load Settings...
Iterate over selections
convert selection to ascii art
preserve OS line endings and spaces/tabs
update selections
"""
def run(
self, edit, font, directory=None,
insert_as_comment=None, use_additional_indent=None, comment_style=None,
width=80, justify=None, direction=None, flip=None, reverse=None
):
self.edit = edit
newSelections = []
self.init(
font, directory, insert_as_comment, use_additional_indent,
comment_style, width, justify, direction, flip, reverse
)
# Loop through user selections & decorate the selections to ASCII Art.
for selection in self.view.sel():
line_A = self.view.line( selection.a )
line_B = self.view.line( selection.b )
if line_A == line_B \
and selection.a == selection.b \
and self.view.substr( line_A ).strip() != "": # use caret line
indent = calculate_indent(self.view, line_A)
line_A = sublime.Region(line_A.begin() + len(indent), line_A.end())
newSelections.append( self.decorate( self.edit, line_A ) )
elif line_A == line_B \
and selection.a != selection.b \
and self.view.substr( selection ).strip() != "": # use selection
newSelections.append( self.decorate( self.edit, selection ) )
else: # multi line selection
# Calculate the indentation that must be removed from the first line.
# Convert the indent into a value representing the size of the white space:
# space = 1, tab = (current sublime setting)
indent = calculate_indent(self.view, selection)
settings = sublime.load_settings('ASCII Decorator.sublime-settings')
tab_size = int(settings.get('tab_size', 8))
char_count = 0
for c in indent:
if c is '\t':
char_count += tab_size
else:
char_count += 1
lines = []
for line in self.view.lines( selection ):
# Remove the equivalent indentation of each line in the block.
# Helps with files that use inconsistent leading indentation (mix of tabs and spaces).
text = self.view.substr( line )
if text.strip() != "":
line_ws = char_count
char_removal = 0
for c in text:
if c == '\t':
line_ws -= tab_size
char_removal += 1
elif c == ' ':
char_removal += 1
line_ws -= 1
else:
break
if line_ws <= 0:
break
lines.append(sublime.Region(line.begin() + char_removal, line.end()))
newSelections.append( self.decorate_multi( self.edit, lines ) )
# Clear selections since they've been modified.
self.view.sel().clear()
for newSelection in newSelections:
self.view.sel().add( newSelection )
def init(
self, font, directory, insert_as_comment, use_additional_indent,
comment_style, width, justify, direction, flip, reverse
):
"""
Read plugin settings
"""
settings = sublime.load_settings('ASCII Decorator.sublime-settings')
if use_additional_indent is not None:
self.insert_as_comment = insert_as_comment
else:
self.insert_as_comment = settings.get("default_insert_as_comment", False)
if use_additional_indent is not None:
self.use_additional_indent = use_additional_indent
else:
self.use_additional_indent = settings.get("default_insert_as_comment", False)
self.comment_style = settings.get("default_comment_style_preference", "block") if comment_style is None else comment_style
if self.comment_style is None or self.comment_style not in ["line", "block"]:
self.comment_style = "line"
self.width = settings.get('default_width', 80) if width is None else int(width)
self.justify = settings.get('default_justify', "auto") if width is None else justify
if self.justify not in ["auto", "center", "left", "right"]:
self.justify = "auto"
self.direction = settings.get('default_direction', "auto") if width is None else direction
if self.direction not in ["auto", "left-to-right", "right-to-left"]:
self.direction = "auto"
self.flip = flip if flip is not None else False
self.reverse = reverse if reverse is not None else False
self.font = font
self.directory = directory
def decorate_multi( self, edit, currentSelections ):
"""
Take input and use FIGlet to convert it to ASCII art.
Normalize converted ASCII strings to use proper line endings and spaces/tabs.
"""
# Convert the input range to a string, this represents the original selection.
font_locations = figlet_paths() if self.directory is None else [self.directory]
# Find where the font resides
directory = None
found = False
for fl in font_locations:
pth = os.path.join(fl, self.font)
for ext in (".flf", ".tlf"):
directory = fl
if os.path.exists(pth + ext):
found = True
break
if found is True:
break
assert found is True
output = []
for line in currentSelections:
original = self.view.substr(line)
# Convert the input string to ASCII Art.
f = SublimeFiglet(
directory=directory, font=self.font, width=self.width,
justify=self.justify, direction=self.direction
)
line_output = f.renderText( original )
if self.reverse is True:
line_output = line_output.reverse()
if self.flip is True:
line_output = line_output.flip()
if not ST3:
line_output = line_output.decode("utf-8", "replace")
output.append(line_output)
# Normalize line endings based on settings.
output = self.normalize_line_endings( '\n'.join(output) )
# Normalize whitespace based on settings.
totalselection = sublime.Region(currentSelections[0].begin(), currentSelections[-1].end())
output = self.fix_whitespace( original, output, totalselection )
self.view.replace( edit, totalselection, output )
return sublime.Region( totalselection.begin(), totalselection.begin() + len(output) )
def decorate( self, edit, currentSelection ):
"""
Take input and use FIGlet to convert it to ASCII art.
Normalize converted ASCII strings to use proper line endings and spaces/tabs.
"""
# Convert the input range to a string, this represents the original selection.
original = self.view.substr( currentSelection )
font_locations = figlet_paths() if self.directory is None else [self.directory]
# Find where the font resides
directory = None
found = False
for fl in font_locations:
pth = os.path.join(fl, self.font)
for ext in (".flf", ".tlf"):
directory = fl
if os.path.exists(pth + ext):
found = True
break
if found is True:
break
# Convert the input string to ASCII Art.
assert found is True
f = SublimeFiglet(
directory=directory, font=self.font, width=self.width,
justify=self.justify, direction=self.direction
)
output = f.renderText( original )
if self.reverse is True:
output = output.reverse()
if self.flip is True:
output = output.flip()
if not ST3:
output = output.decode("utf-8", "replace")
# Normalize line endings based on settings.
output = self.normalize_line_endings( output )
# Normalize whitespace based on settings.
output = self.fix_whitespace( original, output, currentSelection )
self.view.replace( edit, currentSelection, output )
return sublime.Region( currentSelection.begin(), currentSelection.begin() + len(output) )
def normalize_line_endings(self, string):
# Sublime buffers only use '\n', but then normalize all line-endings to
# the appropriate ending on save.
string = string.replace('\r\n', '\n').replace('\r', '\n')
return string
def fix_whitespace(self, original, prefixed, sel):
"""
Determine leading whitespace and comments if desired.
"""
# Determine the indent of the CSS rule
indent = calculate_indent(self.view, sel)
# Strip whitespace from the prefixed version so we get it right
#prefixed = prefixed.strip()
#prefixed = re.sub(re.compile('^\s+', re.M), '', prefixed)
# Get comments for current syntax if desired
comment = ('',)
if self.insert_as_comment:
comments = subcomments.get_comment(self.view, sel.begin())
if len(comments[0]):
comment = comments[0][0]
if (self.comment_style == "block" or len(comments[0]) == 0) and len(comments[1]):
comment = comments[1][0]
# Indent the prefixed version to the right level
settings = self.view.settings()
use_spaces = settings.get('translate_tabs_to_spaces')
tab_size = int(settings.get('tab_size', 8))
# Determine if additional indentation is desired
if self.use_additional_indent:
indent_characters = '\t'
if use_spaces:
indent_characters = ' ' * tab_size
else:
indent_characters = ''
# Prefix the text with desired indentation level, and comments if desired
if len(comment) > 1:
prefixed = prefixed.replace('\n', '\n' + indent + indent_characters)
prefixed = comment[0] + '\n' + indent + indent_characters + prefixed + '\n' + indent + comment[1] + '\n'
else:
prefixed = prefixed.replace('\n', '\n' + indent + comment[0] + indent_characters)
prefixed = comment[0] + indent_characters + prefixed # add needed indent for first line
match = re.search('^(\s*)', original)
prefix = match.groups()[0]
match = re.search('(\s*)\Z', original)
suffix = match.groups()[0]
return remove_trailing_ws(prefixed)