Repository navigation
Expand file tree
/
Copy pathformatter.py
More file actions
775 lines (556 loc) · 15.5 KB
/
Copy pathformatter.py
File metadata and controls
775 lines (556 loc) · 15.5 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
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
from __future__ import annotations
from .modules.colors import (BLACK, BLUE, CYAN, GREEN, MAGENTA, RED, WHITE,
YELLOW)
from .modules.constants import END
from .modules.error import (InvalidColorError, InvalidStyleError,
InvalidTagError)
from .modules.styles import (BACKGROUND, BLINK, BOLD, FADED, ITALIC, STRIKE,
UNDERLINE)
class Formatter:
# Available color tags list.
__colors = [BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE]
# Available style tags list.
__styles = [BACKGROUND, BLINK, BOLD, FADED, ITALIC, STRIKE, UNDERLINE]
def __init__(self, string: str = '') -> None:
"""
Instantiate a brand new string formatter.
---
Arguments
---
string (str, '')
Some string to add.
"""
# Set new attributes.
self.clear()
# Write the string, if provided.
self.write(string)
def __str__(self) -> str:
"""
Called when printing this instance.
---
Returns
---
str
The formatted string.
"""
# Return the formatted string.
return self.render()
def background(self, color: str = None, string: str = '') -> Formatter:
"""
Paint the background.
---
Arguments
---
color (str, None)
A color for the background.
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Paint the background and then return the instance itself.
return self.paint(color).style(BACKGROUND, string)
def black(self, string: str = '') -> Formatter:
"""
Paint with a black foreground.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Paint the foreground and then return the instance itself.
return self.paint(BLACK, string)
def blink(self, string: str = '') -> Formatter:
"""
Make the string blink.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Stylize the string and then return the instance itself.
return self.style(BLINK, string)
def blue(self, string: str = '') -> Formatter:
"""
Paint with a blue foreground.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Paint the foreground and then return the instance itself.
return self.paint(BLUE, string)
def bold(self, string: str = '') -> Formatter:
"""
Apply bold style to the string.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Stylize the string and then return the instance itself.
return self.style(BOLD, string)
def check_color(self, color: str) -> None:
"""
Check whether the color tag is among those available.
---
Arguments
---
color (str)
A color tag to check.
---
Raises
---
InvalidColorError
The color tag is not available.
"""
# If the color tag is not in the color list,...
if color not in self.__colors:
# ... raise an error.
raise InvalidColorError(color)
def check_style(self, style: str) -> None:
"""
Check whether the style tag is among those available.
---
Arguments
---
style (str)
A style tag to check.
---
Raises
---
InvalidStyleError
The style tag is not available.
"""
# If the style tag is not in the style list,...
if style not in self.__styles:
# ... raise an error.
raise InvalidStyleError(style)
def check_tag(self, tag: str) -> None:
"""
Check whether the tag is among those available.
---
Arguments
---
tag (str)
A tag to check.
---
Raises
---
InvalidTagError
The tag is not available.
"""
# If the tag is not in the color or style list,...
if tag not in self.__colors + self.__styles:
# ... raise an error.
raise InvalidTagError(tag)
def clear(self) -> Formatter:
"""
Reset the string and clean the format stack.
---
Returns
---
Formatter
The instance itself.
"""
# Reset the formatted string.
self.__string = ''
# Unpack the applied formats.
self.__stack = []
# Return the instance itself.
return self
def cyan(self, string: str = '') -> Formatter:
"""
Paint with a cyan foreground.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Paint the foreground and then return the instance itself.
return self.paint(CYAN, string)
def end(self, string: str = '') -> Formatter:
"""
Close the last format.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Add the END tag, write the string and return the instance itself.
return self.write(END).write(string)
def erase(self, string: str = '') -> Formatter:
"""
Reset just the string.
---
Arguments
---
string (str, '')
Some text to format.
---
Returns
---
Formatter
The instance itself.
"""
# Reset the formatted string.
self.__string = ''
# For each stacked format,...
for tag in self.__stack:
# ... check if it is valid...
self.check_tag(tag)
# ... and write it to the formatted string.
self.write(tag)
# Then, write the string and return the instance itself.
return self.write(string)
def faded(self, string: str = '') -> Formatter:
"""
Apply faded color style to the string.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Stylize the string and then return the instance itself.
return self.style(FADED, string)
def green(self, string: str = '') -> Formatter:
"""
Paint with a green foreground.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Paint the foreground and then return the instance itself.
return self.paint(GREEN, string)
def italic(self, string: str = '') -> Formatter:
"""
Apply italic style to the string.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Stylize the string and then return the instance itself.
return self.style(ITALIC, string)
def magenta(self, string: str = '') -> Formatter:
"""
Paint with a magenta foreground.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Paint the foreground and then return the instance itself.
return self.paint(MAGENTA, string)
def paint(self, color: str = None, string: str = '') -> Formatter:
"""
Paint the string with some color.
---
Arguments
---
color (str, None)
A color to paint.
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# If a color was provided,...
if color is not None:
# ... check if it is valid.
self.check_color(color)
# Push the color tag to the format stack and return the instance itself.
return self.push(color, string)
def pop(self, string: str = '') -> Formatter:
"""
Unstack the last Formatter tag from the stack.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Pop the last tag from the format stack.
last = self.__stack.pop()
# If the `BACKGROUND` tag was the last,...
if last == BACKGROUND:
# ... then it should pop the second last, which is a color tag.
self.__stack.pop()
# Continue the string and return the instance itself.
return self.end(''.join(self.__stack)).write(string)
def print(self) -> None:
"""
Directly print the formatted string.
"""
# Just print the instance itself.
print(self)
def push(self, tag: str = None, string: str = '') -> Formatter:
"""
Stack a Formatter tag to the stack.
---
Arguments
---
tag (str, None)
A tag to push in.
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# If a tag was provided,...
if tag is not None:
# ... check if it is valid.
self.check_tag(tag)
# If it is equal to the last one,...
if len(self.__stack) != 0 and tag == self.__stack[-1]:
# ... then pop it from the stack.
self.pop()
# If no, push the tag to the format stack,...
self.__stack.append(tag)
# ... and write it.
self.write(tag)
# Write the provided string and return the instance itself.
return self.write(string)
def red(self, string: str = '') -> Formatter:
"""
Paint with a red foreground.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Paint the foreground and then return the instance itself.
return self.paint(RED, string)
def render(self) -> str:
"""
Return the formatted string.
---
Returns
---
str
The formatted string.
"""
# Return the formatted string, followed by the END tag.
return self.__string + END
def reset(self, string: str = '') -> Formatter:
"""
Reset both the string and stack attributes.
---
Arguments
---
string (str, '')
Some string to format.
---
Returns
---
Formatter
The instance itself.
"""
# Clean the format stack.
self.__stack.clear()
# Close the format string and return the instance itself.
return self.end(string)
def strike(self, string: str = '') -> Formatter:
"""
Apply a strike to the string.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Stylize the string and then return the instance itself.
return self.style(STRIKE, string)
def string(self) -> str:
"""
Return the formatted string without the END tag.
---
Returns
---
str
Just the string as is.
"""
# Return the string as is.
return self.__string
def style(self, style: str = None, string: str = '') -> Formatter:
"""
Stylize the string with some style.
---
Arguments
---
style (str, None)
A style tag.
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# If a style tag was provided,...
if style is not None:
# ... check if it is valid.
self.check_style(style)
# Push the style tag to the format stack and return the instance itself.
return self.push(style, string)
def underline(self, string: str = '') -> Formatter:
"""
Apply an underline to the string.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Stylize the string and then return the instance itself.
return self.style(UNDERLINE, string)
def white(self, string: str = '') -> Formatter:
"""
Paint with a white foreground.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Paint the foreground and then return the instance itself.
return self.paint(WHITE, string)
def write(self, string: str = '') -> Formatter:
"""
Add some string to the formatted string.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Make a string from the current tags.
tags = ''.join(self.__stack)
# Check whether the string is, actually, another Formatter instance.
if isinstance(string, Formatter):
# Split the string between the END tags.
strings = string.string().split(END)
# Join the strings with all of the pre-existing formats.
return self.write((END + tags).join(strings)).end(tags)
# Don't forget any previous format.
self.__string += (END + tags).join(str(string).split(END))
# Return the instance itself.
return self
def yellow(self, string: str = '') -> Formatter:
"""
Paint with a yellow foreground.
---
Arguments
---
string (str, '')
Some string to add.
---
Returns
---
Formatter
The instance itself.
"""
# Paint the foreground and then return the instance itself.
return self.paint(YELLOW, string)