rewrite coloring

This commit is contained in:
q
2025-11-26 10:18:50 +02:00
parent 39f789e7ca
commit 83cd76ecdf

View File

@@ -10,7 +10,7 @@ import sys
import termios import termios
import tty import tty
__version__ = "20251013.a" __version__ = "20251126.a"
class bc: class bc:
@@ -50,7 +50,15 @@ class QAsk:
self.c.disable() self.c.disable()
self.user_input = "" self.user_input = ""
self.eq = "▁▂▃▄▅▆▇█▇▆▅▄▃▂" self.eq = "▁▂▃▄▅▆▇█▇▆▅▄▃▂"
self.limits = ((self.c.r, 2), (self.c.y, 4), (self.c.B, 7), (self.c.c, 9), (self.c.g, 12), (self.c.G, 15)) self.limits = (
(self.c.r, 2, "r"),
(self.c.y, 4, "y"),
(self.c.B, 7, "B"),
(self.c.c, 9, "c"),
(self.c.g, 12, "g"),
(self.c.G, 15, "G"),
)
self.color_hash = ((self.c.r, "r"), (self.c.g, "g"), (self.c.B, "B"), (self.c.c, "c"), (self.c.y, "y"))
self.no_pw = "•" self.no_pw = "•"
self.is_correct = "♥" self.is_correct = "♥"
self.rand_chr = "■" # "╩╦═╬╧╤╪" self.rand_chr = "■" # "╩╦═╬╧╤╪"
@@ -74,6 +82,12 @@ class QAsk:
parser.add_argument("--no-color", action="store_true", default=False, help="Disable colors") parser.add_argument("--no-color", action="store_true", default=False, help="Disable colors")
parser.add_argument("--measure", action="store_true", default=False, help="Measure password goodness") parser.add_argument("--measure", action="store_true", default=False, help="Measure password goodness")
parser.add_argument("--eq", action="store_true", default=False, help="Visual aid EQ mode.") parser.add_argument("--eq", action="store_true", default=False, help="Visual aid EQ mode.")
parser.add_argument(
"--export",
action="store_true",
default=False,
help="output the --title to be used with given password, instead of the password",
)
parser.add_argument( parser.add_argument(
"--expect-sha256", "--expect-sha256",
action="store", action="store",
@@ -102,6 +116,9 @@ class QAsk:
score *= 2 score *= 2
return score return score
def get_color_hash(self, i):
return self.color_hash[int(self.sha256[(1 + i * 2) % len(self.sha256)], 16) % len(self.color_hash)]
def animchar(self, pos): def animchar(self, pos):
"""Returns one character, based on pwscore or pw hash""" """Returns one character, based on pwscore or pw hash"""
i = len(self.user_input) - pos i = len(self.user_input) - pos
@@ -119,26 +136,37 @@ class QAsk:
return clr + self.eq[int(rheight + (i / self.opts.w)) % 14] return clr + self.eq[int(rheight + (i / self.opts.w)) % 14]
else: # no measure else: # no measure
rheight = random.randint(-1, 1)
height = int(self.sha256[i * 2 % len(self.sha256)], 16) / 2
clr = self.get_color_hash(i)[0]
if self.opts.eq: if self.opts.eq:
if len(self.user_input) == 0: if len(self.user_input) == 0:
rheight = random.randint(0, 1) rheight = random.randint(0, 1)
height = 0 height = 0
clr = self.limits[0][0] clr = self.color_hash[0][0]
else: else:
rheight = random.randint(-1, 1) pass
height = int(self.sha256[i * 2 % len(self.sha256)], 16) / 2 return clr + self.eq[int(rheight + height) % len(self.eq)]
clr = self.limits[int(self.sha256[(1 + i * 2) % len(self.sha256)], 16) % len(self.limits)][0]
return clr + self.eq[int(rheight + height) % 14]
if len(self.user_input) == 0: if len(self.user_input) == 0:
clr = self.limits[0][0] clr = self.color_hash[0][0]
return clr + self.no_pw return clr + self.no_pw
else: else:
rheight = random.randint(-1, 1)
height = int(self.sha256[i * 2 % len(self.sha256)], 16) / 2
clr = self.limits[int(self.sha256[(1 + i * 2) % len(self.sha256)], 16) % len(self.limits)][0]
return clr + self.rand_chr[int(rheight + height) % len(self.rand_chr)] return clr + self.rand_chr[int(rheight + height) % len(self.rand_chr)]
def get_export(self):
title = ["-t 'Password hint: "]
for pos in range(self.opts.w):
i = len(self.user_input) - pos
animchar = self.animchar(i)
if self.opts.eq:
pass
else:
a = self.get_color_hash(i)[1]
title.append("{" + a + "}" + animchar[-1])
title.append("'\n")
return "".join(title)
def pquit(self, s="", e=0): def pquit(self, s="", e=0):
print(self.c.blink, file=sys.stderr, end="") print(self.c.blink, file=sys.stderr, end="")
print(self.c.z, file=sys.stderr) print(self.c.z, file=sys.stderr)
@@ -194,7 +222,11 @@ class QAsk:
if ord(key) == 3: # ctrl-c if ord(key) == 3: # ctrl-c
self.pquit(e=1) self.pquit(e=1)
elif ord(key) == 13: # enter elif ord(key) == 13: # enter
self.pquit(self.user_input, e=self.enter_exitcode) if self.opts.export:
msg = self.get_export()
else:
msg = self.user_input
self.pquit(msg, e=self.enter_exitcode)
elif ord(key) == 27: # esc (also starts control characters elif ord(key) == 27: # esc (also starts control characters
key = self.get_chr() key = self.get_chr()
if ord(key) == 27: if ord(key) == 27: