Fixed bugs, changed dictionary to IntEnum.
This commit is contained in:
parent
886cc017ab
commit
c6dd9513b4
1 changed files with 38 additions and 26 deletions
64
pat.py
64
pat.py
|
@ -1,4 +1,5 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
|
from enum import IntEnum
|
||||||
from sys import argv, stdin, stderr
|
from sys import argv, stdin, stderr
|
||||||
from string import whitespace
|
from string import whitespace
|
||||||
|
|
||||||
|
@ -8,47 +9,58 @@ from os import system
|
||||||
|
|
||||||
system("")
|
system("")
|
||||||
|
|
||||||
color_info = {"amount": 299, "max_rgb_value": 255}
|
# If the program is using too much memory, try decreasing this value
|
||||||
color_info["per_stage"] = (color_info["amount"] + 1) // 3
|
bytes_to_read = 1000000
|
||||||
color_info["min_rgb_value"] = color_info["max_rgb_value"] - color_info["per_stage"]
|
|
||||||
|
|
||||||
|
class ColorInfo(IntEnum):
|
||||||
|
AMOUNT = 299
|
||||||
|
MAX_RGB_VALUE = 255
|
||||||
|
PER_STAGE = (AMOUNT + 1) // 3
|
||||||
|
MIN_RGB_VALUE = MAX_RGB_VALUE - PER_STAGE
|
||||||
|
|
||||||
|
|
||||||
if len(argv) == 1:
|
if len(argv) == 1:
|
||||||
argv.append("-")
|
argv.append("-")
|
||||||
|
|
||||||
for argument in argv[1:]:
|
for arg in argv[1:]:
|
||||||
try:
|
try:
|
||||||
read_result = stdin.read() if argument == "-" else open(argument, "r").read()
|
file = stdin if arg == "-" else open(arg, "r")
|
||||||
|
if file.seekable():
|
||||||
|
file_length = file.seek(0, 2)
|
||||||
|
if file_length > bytes_to_read:
|
||||||
|
file.seek(file_length - bytes_to_read)
|
||||||
|
else:
|
||||||
|
file.seek(0)
|
||||||
|
read_result = file.read()
|
||||||
|
else:
|
||||||
|
read_result = file.read(bytes_to_read)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e, file=stderr)
|
print("Error reading %s: %s" % (file.name, e), file=stderr)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
color_info["changes"] = (
|
changes = len(list(filter(lambda c: not c in whitespace, read_result)))
|
||||||
len(list(filter(lambda c: not c in whitespace, read_result))) - 1
|
|
||||||
)
|
|
||||||
color_info["step"] = (
|
|
||||||
0
|
|
||||||
if color_info["changes"] == 0
|
|
||||||
else color_info["amount"] / color_info["changes"]
|
|
||||||
)
|
|
||||||
color_info["index"] = 0
|
|
||||||
|
|
||||||
for character in read_result:
|
step = 0 if changes == 0 else ColorInfo.AMOUNT / changes
|
||||||
if character in whitespace:
|
index = 0
|
||||||
print(character, end="")
|
|
||||||
|
for char in read_result:
|
||||||
|
if char in whitespace:
|
||||||
|
print(char, end="")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
color_value = round(color_info["index"])
|
color_value = round(index)
|
||||||
value_modifier = color_value % color_info["per_stage"]
|
value_modifier = color_value % ColorInfo.PER_STAGE
|
||||||
rgb_values = [
|
rgb_values = [
|
||||||
color_info["min_rgb_value"] + value_modifier,
|
ColorInfo.MIN_RGB_VALUE + value_modifier,
|
||||||
color_info["max_rgb_value"] - value_modifier,
|
ColorInfo.MAX_RGB_VALUE - value_modifier,
|
||||||
color_info["min_rgb_value"],
|
ColorInfo.MIN_RGB_VALUE,
|
||||||
]
|
]
|
||||||
[red, green, blue] = [
|
[red, green, blue] = [
|
||||||
[rgb_values[1], rgb_values[0], rgb_values[2]],
|
[rgb_values[1], rgb_values[0], rgb_values[2]],
|
||||||
rgb_values[::-1],
|
rgb_values[::-1],
|
||||||
[rgb_values[0], rgb_values[2], rgb_values[1]],
|
[rgb_values[0], rgb_values[2], rgb_values[1]],
|
||||||
][color_value // color_info["per_stage"]]
|
][color_value // ColorInfo.PER_STAGE]
|
||||||
|
|
||||||
print("\x1b[38;2;%d;%d;%dm%s" % (red, green, blue, character), end="")
|
print("\x1b[38;2;%d;%d;%dm%s" % (red, green, blue, char), end="")
|
||||||
color_info["index"] += color_info["step"]
|
index += step
|
||||||
|
|
Loading…
Reference in a new issue