Fix reading past EOF + .nfp viewer

This commit is contained in:
Casey 2023-12-20 19:59:28 +03:00
parent cdaa2c210d
commit cea17fc255
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
2 changed files with 68 additions and 1 deletions

View File

@ -41,7 +41,12 @@ local function load(path)
local line = { s = "", bg = "", fg = "" }
for x = 1, image.w do
line.s = line.s .. fp:read(1)
local color = string.byte(fp:read(1))
local char = fp:read(1)
if char == nil then
printError("Fatal: failed to read color data for y=" .. y .. " x=" .. x)
break
end
local color = string.byte(char)
line.bg = line.bg .. string.format("%x", bit32.band(0xF, color))
line.fg = line.fg .. string.format("%x", bit32.band(0xF, bit32.rshift(color, 4)))
end

62
nfpview.py Normal file
View File

@ -0,0 +1,62 @@
#!/usr/bin/env python3
from argparse import ArgumentParser
colors = {
"0": (240, 240, 240),
"1": (242, 178, 51),
"2": (229, 127, 216),
"3": (153, 178, 242),
"4": (222, 222, 108),
"5": (127, 204, 25),
"6": (242, 178, 204),
"7": ( 76, 76, 76),
"8": (153, 153, 153),
"9": ( 76, 153, 178),
"a": (178, 102, 229),
"b": ( 51, 102, 204),
"c": (127, 102, 76),
"d": ( 87, 166, 78),
"e": (204, 76, 76),
"f": ( 17, 17, 17),
}
color_names = {
"0": "white",
"1": "orange",
"2": "magenta",
"3": "lightBlue",
"4": "yellow",
"5": "lime",
"6": "pink",
"7": "gray",
"8": "lightGray",
"9": "cyan",
"a": "purple",
"b": "blue",
"c": "brown",
"d": "green",
"e": "red",
"f": "black",
}
parser = ArgumentParser(description="CC default image viewer")
parser.add_argument("filename")
def color(v: str):
return tuple(bytes.fromhex(v.lstrip('#')))
for k, (r, g, b) in colors.items():
parser.add_argument(f"-{k}", type=color, default=(r, g, b),
help="Set color %s" % color_names[k])
args = parser.parse_args()
with open(args.filename, "r") as fp:
for line in fp:
for char in line.rstrip():
if char in colors:
print("\x1b[48;2;%d;%d;%dm \x1b[0m" % colors[char], end="")
else:
print(" ", end="")
print()