-
Notifications
You must be signed in to change notification settings - Fork 1
/
printer.py
798 lines (703 loc) · 26.7 KB
/
printer.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
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
#######################################################
# Printer Module #
# Created by Jacob Humston #
# #
# Util script for creating neat text bubbles, #
# along with other things you might find helpful! #
# #
# GITHUB: https://github.com/jacobhumston/printer.py #
# #
# Credits to https://pastebin.com/0CMbB7EK for #
# the custom get_key implementation. #
#######################################################
# NOTES: #
# - Reset and italic are considered colors in terms #
# of classes and such. #
# - Naming that starts with an underscore are #
# considered to be private. #
# - To prevent conflicts, all public variables are #
# capitalized. #
#######################################################
import os as _os
import sys as _sys
from typing import Literal as _Literal
if _sys.version_info[0] > 2:
from msvcrt import getwch as _getch
else:
from msvcrt import _getch
def get_keypress() -> str:
"Get a keypress."
c1 = _getch()
if c1 in ("\x00", "\xe0"):
arrows = {"H": "up", "P": "down", "M": "right", "K": "left"}
c2 = _getch()
return arrows.get(c2, c1 + c2)
elif c1 == "\r":
return "return"
elif c1 == "\t":
return "tab"
elif c1 == "\b":
return "backspace"
return c1
class _OptionsCore:
"Options used by other methods of this module."
no_colors: bool = False
"If true, no colors will be used. (Even when providing a color to a method.)"
text_bubble_shape: _Literal["Round", "Square"] = "Round"
"The shape of text bubbles."
def set(
self,
name: _Literal["no_colors", "text_bubble_shape"],
value: _Literal["Round", "Square"] | bool,
) -> None:
"""Optional function to modify options.
\n`name` - Name of the option to change.
\n`value` - New value of this option.
"""
if type(getattr(self, name)) != type(value):
raise Exception(
f"Type mismatch! Expected '{type(getattr(self, name)).__name__}' but got '{type(value).__name__}'."
)
return setattr(self, name, value)
Options = _OptionsCore()
"Options used by other methods of this module."
class ColorCodes:
"Terminal color codes."
black: str = "\u001b[30m"
"Black color code."
red: str = "\u001b[31m"
"Red color code."
green: str = "\u001b[32m"
"Green color code."
yellow: str = "\u001b[33m"
"Yellow color code."
blue: str = "\u001b[34m"
"Black color code."
magenta: str = "\u001b[35m"
"Magenta color code."
cyan: str = "\u001b[36m"
"Cyan color code."
white: str = "\u001b[37m"
"White color code."
reset: str = "\u001b[0m"
"Reset color code."
italic: str = "\u001b[3m"
"Italic color code."
class Colors:
"Terminal colors."
class _ColorCore:
"Core functionality."
def __init__(self, color: str):
self.color = color
def replace(self, string: str, replacer: str) -> str:
"""Replace all instances of `replacer` with a colored variant.
\n`string` - String to modify.
\n`replacer` - Part(s) of `string` to replace with a colored variant.
"""
if Options.no_colors == True:
return string
return string.replace(replacer, f"{self.color}{replacer}{ColorCodes.reset}")
def new(self, string: str) -> str:
"""Returns a colored version of `string`.
\n`string` - String to color.
"""
if Options.no_colors == True:
return string
string = string.replace("\n", "{}\n{}".format(ColorCodes.reset, self.color))
return f"{self.color}{string}{ColorCodes.reset}"
def remove(self, string: str) -> str:
"""Removes all of this color from `string`.
\n`string` - String to remove this color from.
"""
return string.replace(self.color, "")
black = _ColorCore(ColorCodes.black)
"Black color method."
red = _ColorCore(ColorCodes.red)
"Red color method."
green = _ColorCore(ColorCodes.green)
"Green color method."
yellow = _ColorCore(ColorCodes.yellow)
"Yellow color method."
blue = _ColorCore(ColorCodes.blue)
"Blue color method."
magenta = _ColorCore(ColorCodes.magenta)
"Magenta color method."
cyan = _ColorCore(ColorCodes.cyan)
"Cyan color method."
white = _ColorCore(ColorCodes.white)
"White color method."
reset = _ColorCore(ColorCodes.reset)
"Reset color method."
italic = _ColorCore(ColorCodes.italic)
"Italic color method."
AllColors = [
Colors.black,
Colors.red,
Colors.green,
Colors.yellow,
Colors.blue,
Colors.magenta,
Colors.cyan,
Colors.white,
Colors.reset,
Colors.italic,
]
"List of all colors, including reset."
def add_custom_rgb_color(red: int, green: int, blue: int) -> Colors._ColorCore:
"""Create a custom color using rgb. It's important to note that not all consoles support colors, let alone custom ones.
\n`red` `green` `blue` - The amount of r/g/b in the color respectfully.
"""
color = Colors._ColorCore(f"\x1b[38;2;{red};{green};{blue}m")
AllColors.append(color)
return color
def get_all_colors() -> list[Colors._ColorCore]:
"""Returns a list of all currently available colors. Including custom ones."""
return AllColors
def clear_console() -> None:
"Clear the console."
_os.system("cls" if _os.name == "nt" else "clear")
return None
def duplicate_string(string: str, amount: int) -> str:
"""Replace all instances of `replacer` with a colored variant.
\n`string` - String to duplicate.
\n`amount` - Amount of times to duplicate the string.
"""
new_string = ""
for _ in range(amount):
new_string = f"{new_string}{string}"
return new_string
def remove_all_colors(string: str) -> str:
"""Removes all the colors from a string.
\n`string` - String to remove the colors from.
"""
for color in AllColors:
string = color.remove(string)
return string
def get_string_length(string: str) -> int:
"""Get the length of a string. (Supports colors and multiple lines.)
\n`string` - The string to get the length of.
"""
lines = remove_all_colors(string).split("\n")
return len(max(lines, key=len))
def create_text_bubble(
text: str,
label: str | None = None,
color: Colors._ColorCore = Colors.green,
input_decorator: bool = False,
shape: _Literal["Round", "Square"] | None = None,
) -> str:
"""Create a text bubble.
\n`text` - The text to put inside the bubble.
\n`label` - The label of this text bubble.
\n`color` - The color of this text bubble.
\n`input_decorator` - If true, an input decorator will be place at the bottom of the bubble.
\n`shape` - Shape of the text bubble. Overrides `<options>.text_bubble_shape`.
"""
line = color.new(duplicate_string("─", get_string_length(text) + 2))
side = color.new("│")
corner1 = color.new("╭")
corner2 = color.new("╮")
corner3 = color.new("╯")
corner4 = color.new("╰")
bubble = ""
shape = shape or Options.text_bubble_shape
if shape == "Square":
corner1 = color.new("┌")
corner2 = color.new("┐")
corner3 = color.new("┘")
corner4 = color.new("└")
label_is_bigger = False
if label != None:
if get_string_length(line) < get_string_length(label) + 3:
line = color.new(duplicate_string("─", get_string_length(label) + 3))
label_is_bigger = True
lines = text.split("\n")
for index, part in enumerate(lines):
lines[
index
] = f"{side} {part}{duplicate_string(' ', get_string_length(text) + 1 - get_string_length(part))}{side}"
if label != None:
if label_is_bigger == True:
lines[
index
] = f"{side} {part}{duplicate_string(' ', get_string_length(label) + 2 - get_string_length(part))}{side}"
joined_lines = "\n".join(lines)
if label != None:
label = color.new(label)
line2 = color.new(
duplicate_string(
"─", (get_string_length(text) - get_string_length(label) - 3) + 2
)
)
line2Part = color.replace("─", "─")
bubble = f"{corner1}{line2Part} {label} {line2}{corner2}\n{joined_lines}\n{corner4}{line}{corner3}"
else:
bubble = f"{corner1}{line}{corner2}\n{joined_lines}\n{corner4}{line}{corner3}"
if input_decorator == True:
return f"{bubble.replace(corner4, color.new('├'))}\n{color.new('│>')} "
else:
return bubble
def print_text_bubble(
text: str,
label: str | None = None,
color: Colors._ColorCore = Colors.green,
input_decorator: bool = False,
shape: _Literal["Round", "Square"] | None = None,
) -> str:
"""Same function as `create_text_bubble` but also prints it.
\n`text` - The text to put inside the bubble.
\n`label` - The label of this text bubble.
\n`color` - The color of this text bubble.
\n`input_decorator` - If true, an input decorator will be place at the bottom of the bubble.
\n`shape` - Shape of the text bubble. Overrides `<options>.text_bubble_shape`.
"""
bubble = create_text_bubble(
text=text,
label=label,
color=color,
input_decorator=input_decorator,
shape=shape,
)
print(bubble)
return bubble
def create_warning_text_bubble(message: str):
"""Create a warning text bubble.
\n`message` - The warning message.
"""
return create_text_bubble(message, "Warning", Colors.yellow)
class _InputCore:
"Input methods."
class _HiddenCore:
"Hidden input methods."
def string(
self,
text: str,
label: str | None = None,
color: Colors._ColorCore = Colors.green,
shape: _Literal["Round", "Square"] | None = None,
max: int = 99999999999999,
min: int = 1,
warning: str | None = None,
) -> str:
"""Hidden input that always returns a string.
\n`text` - The text to put inside the bubble.
\n`label` - The label of this text bubble.
\n`color` - The color of this text bubble.
\n`shape` - Shape of the text bubble. Overrides `<options>.text_bubble_shape`.
\n`max` - Maximum length of the string allowed.
\n`min` - Minimum length of the string allowed.
\n`warning` - Warning message to display.
"""
current_input: str = ""
return_pressed: int = False
while return_pressed == False:
clear_console()
if warning != None:
print(create_warning_text_bubble(warning))
bubble = create_text_bubble(
text=text,
label=label,
color=color,
shape=shape,
input_decorator=True,
)
print(f"{bubble}{duplicate_string('*', len(current_input))}\n")
key = get_keypress()
if key == "return":
return_pressed = True
elif key == "backspace":
current_input = current_input[:-1]
elif len(key) == 1:
current_input = f"{current_input}{key}"
clear_console()
if len(current_input) < min or len(current_input) > max:
return self.string(
text=text,
label=label,
color=color,
shape=shape,
max=max,
min=min,
warning=f"Input must be between {Colors.blue.new(str(min))} and {Colors.blue.new(str(max))} characters long.",
)
return current_input
def integer(
self,
text: str,
label: str | None = None,
color: Colors._ColorCore = Colors.green,
shape: _Literal["Round", "Square"] | None = None,
max: int | float = 99999999999999,
min: int | float = -999999999999,
warning: str | None = None,
) -> int:
"""Hidden input that always returns an int.
\n`text` - The text to put inside the bubble.
\n`label` - The label of this text bubble.
\n`color` - The color of this text bubble.
\n`shape` - Shape of the text bubble. Overrides `<options>.text_bubble_shape`.
\n`max` - Maximum number allowed.
\n`min` - Minimum number allowed.
\n`warning` - Warning message to display.
"""
result = self.string(
text=text,
label=label,
color=color,
shape=shape,
max=15,
min=1,
warning=warning,
)
if result.isnumeric() == True:
number = int(result)
if not number >= min or not number <= max:
return self.integer(
text=text,
label=label,
color=color,
shape=shape,
max=max,
min=min,
warning=f"Input must be a number (integer) {Colors.blue.new(str(min))} and {Colors.blue.new(str(max))}.",
)
else:
return number
else:
return self.integer(
text=text,
label=label,
color=color,
shape=shape,
max=max,
min=min,
warning="Input must be a number (integer).",
)
def float(
self,
text: str,
label: str | None = None,
color: Colors._ColorCore = Colors.green,
shape: _Literal["Round", "Square"] | None = None,
max: int | float = 99999999999999,
min: int | float = -999999999999,
warning: str | None = None,
) -> float:
"""Hidden input that always returns a float.
\n`text` - The text to put inside the bubble.
\n`label` - The label of this text bubble.
\n`color` - The color of this text bubble.
\n`shape` - Shape of the text bubble. Overrides `<options>.text_bubble_shape`.
\n`max` - Maximum number allowed.
\n`min` - Minimum number allowed.
\n`warning` - Warning message to display.
"""
result = self.string(
text=text,
label=label,
color=color,
shape=shape,
max=30,
min=1,
warning=warning,
)
is_float: bool = True
try:
float(result)
except:
is_float = False
if is_float == True:
number = float(result)
if not number >= min or not number <= max:
return self.float(
text=text,
label=label,
color=color,
shape=shape,
max=max,
min=min,
warning=f"Input must be a number (float) between {Colors.blue.new(str(min))} and {Colors.blue.new(str(max))}.",
)
else:
return number
else:
return self.float(
text=text,
label=label,
color=color,
shape=shape,
max=max,
min=min,
warning="Input must be a number (float).",
)
hidden = _HiddenCore()
"Hidden input methods."
def string(
self,
text: str,
label: str | None = None,
color: Colors._ColorCore = Colors.green,
shape: _Literal["Round", "Square"] | None = None,
max: int = 99999999999999,
min: int = 1,
warning: str | None = None,
) -> str:
"""Input that always returns a string.
\n`text` - The text to put inside the bubble.
\n`label` - The label of this text bubble.
\n`color` - The color of this text bubble.
\n`shape` - Shape of the text bubble. Overrides `<options>.text_bubble_shape`.
\n`max` - Maximum length of the string allowed.
\n`min` - Minimum length of the string allowed.
\n`warning` - Warning message to display.
"""
clear_console()
if warning != None:
print(create_warning_text_bubble(warning))
bubble = create_text_bubble(
text=text,
label=label,
color=color,
shape=shape,
input_decorator=True,
)
current_input = input(bubble)
clear_console()
if len(current_input) < min or len(current_input) > max:
return self.string(
text=text,
label=label,
color=color,
shape=shape,
max=max,
min=min,
warning=f"Input must be between {Colors.blue.new(str(min))} and {Colors.blue.new(str(max))} characters long.",
)
return current_input
def integer(
self,
text: str,
label: str | None = None,
color: Colors._ColorCore = Colors.green,
shape: _Literal["Round", "Square"] | None = None,
max: int | float = 99999999999999,
min: int | float = -999999999999,
warning: str | None = None,
) -> int:
"""Input that always returns an int.
\n`text` - The text to put inside the bubble.
\n`label` - The label of this text bubble.
\n`color` - The color of this text bubble.
\n`shape` - Shape of the text bubble. Overrides `<options>.text_bubble_shape`.
\n`max` - Maximum number allowed.
\n`min` - Minimum number allowed.
\n`warning` - Warning message to display.
"""
result = self.string(
text=text,
label=label,
color=color,
shape=shape,
max=15,
min=1,
warning=warning,
)
if result.isnumeric() == True:
number = int(result)
if not number >= min or not number <= max:
return self.integer(
text=text,
label=label,
color=color,
shape=shape,
max=max,
min=min,
warning=f"Input must be a number (integer) {Colors.blue.new(str(min))} and {Colors.blue.new(str(max))}.",
)
else:
return number
else:
return self.integer(
text=text,
label=label,
color=color,
shape=shape,
max=max,
min=min,
warning="Input must be a number (integer).",
)
def float(
self,
text: str,
label: str | None = None,
color: Colors._ColorCore = Colors.green,
shape: _Literal["Round", "Square"] | None = None,
max: int | float = 99999999999999,
min: int | float = -999999999999,
warning: str | None = None,
) -> float:
"""Input that always returns a float.
\n`text` - The text to put inside the bubble.
\n`label` - The label of this text bubble.
\n`color` - The color of this text bubble.
\n`shape` - Shape of the text bubble. Overrides `<options>.text_bubble_shape`.
\n`max` - Maximum number allowed.
\n`min` - Minimum number allowed.
\n`warning` - Warning message to display.
"""
result = self.string(
text=text,
label=label,
color=color,
shape=shape,
max=30,
min=1,
warning=warning,
)
is_float: bool = True
try:
float(result)
except:
is_float = False
if is_float == True:
number = float(result)
if not number >= min or not number <= max:
return self.float(
text=text,
label=label,
color=color,
shape=shape,
max=max,
min=min,
warning=f"Input must be a number (float) between {Colors.blue.new(str(min))} and {Colors.blue.new(str(max))}.",
)
else:
return number
else:
return self.float(
text=text,
label=label,
color=color,
shape=shape,
max=max,
min=min,
warning="Input must be a number (float).",
)
def select(
self,
text: str,
options: list[str],
label: str | None = None,
color: Colors._ColorCore = Colors.green,
shape: _Literal["Round", "Square"] | None = None,
selected_option: int | None = None,
hide_instructions: bool = False,
) -> int:
"""Input that always returns an index (int) of the selected option.
\n`text` - The text to put inside the bubble.
\n`options` - List of string options to select from.
\n`label` - The label of this text bubble.
\n`color` - The color of this text bubble.
\n`shape` - Shape of the text bubble. Overrides `<options>.text_bubble_shape`.
\n`selected_option` - Currently selected option. Can be used to modify the default selected with a custom one.
\n`hide_instructions` - If true, the instructions explaining on how change the current selection will be removed.
"""
selected_option = selected_option or 1
options_string = ""
for index, option in enumerate(options):
if index > 0:
options_string += "\n"
if selected_option == index + 1:
options_string += Colors.yellow.new(f"➜ {option}")
else:
options_string += Colors.magenta.new(f"• {option}")
instructions = Colors.italic.new(
Colors.magenta.new(
"Use the up and down arrow keys to change\nthe currently selected option."
)
)
options_bubble = create_text_bubble(
text=f"{instructions}\n\n{options_string}",
label="Options",
input_decorator=False,
shape=shape,
color=Colors.magenta,
)
if hide_instructions == True:
options_bubble = create_text_bubble(
text=f"{options_string}",
label="Options",
input_decorator=False,
shape=shape,
color=Colors.magenta,
)
clear_console()
bubble = create_text_bubble(
text=f"{text}\n{options_bubble}",
label=label,
color=color,
input_decorator=False,
shape=shape,
)
print(f"{bubble}\n")
result = get_keypress()
if result == "return":
clear_console()
return selected_option
if result == "down" or result == "right":
selected_option += 1
if selected_option < 1 or selected_option > len(options):
selected_option -= 1
return self.select(
text=text,
label=label,
color=color,
shape=shape,
options=options,
selected_option=selected_option,
hide_instructions=hide_instructions,
)
else:
return self.select(
text=text,
label=label,
color=color,
shape=shape,
options=options,
selected_option=selected_option,
hide_instructions=hide_instructions,
)
elif result == "up" or result == "left":
selected_option -= 1
if selected_option < 1 or selected_option > len(options):
selected_option += 1
return self.select(
text=text,
label=label,
color=color,
shape=shape,
options=options,
selected_option=selected_option,
hide_instructions=hide_instructions,
)
else:
return self.select(
text=text,
label=label,
color=color,
shape=shape,
options=options,
selected_option=selected_option,
hide_instructions=hide_instructions,
)
else:
return self.select(
text=text,
label=label,
color=color,
shape=shape,
options=options,
selected_option=selected_option,
hide_instructions=hide_instructions,
)
Input = _InputCore()