mirror of
https://codeberg.org/dnkl/foot.git
synced 2026-03-12 05:34:01 -04:00
generate-alt-random-writes: use python random, allow setting a seed
Using Python's own PRNG should make the code cleaner and allow for
reproducible stimulus files if that is desired via setting --seed (at
least for the same versions of the script, changing the kind and/or
order of the random calls will of course impact the output in the
future).
I did the following substitutions:
* rand.read(1)[0] % n and struct.unpack('@H', rand.read(2))[0] % n →
random.randrange(n)
* rand.read(1)[0] → random.randrange(256)
* rand.read(n) → [random.randrange(256) for _ in range(n)]
(better alternative would have been random.randbytes(n), but is only
available for Python >= 3.9, switching to this in the future will
impact output)
* list[rand.read(1) % len(list)] → random.choice(list)
This commit is contained in:
parent
339acc57cf
commit
050da302b8
1 changed files with 101 additions and 99 deletions
|
|
@ -3,6 +3,7 @@ import argparse
|
||||||
import enum
|
import enum
|
||||||
import fcntl
|
import fcntl
|
||||||
import struct
|
import struct
|
||||||
|
import random
|
||||||
import sys
|
import sys
|
||||||
import termios
|
import termios
|
||||||
|
|
||||||
|
|
@ -31,6 +32,7 @@ def main():
|
||||||
parser.add_argument('--attr-italic', action='store_true')
|
parser.add_argument('--attr-italic', action='store_true')
|
||||||
parser.add_argument('--attr-underline', action='store_true')
|
parser.add_argument('--attr-underline', action='store_true')
|
||||||
parser.add_argument('--sixel', action='store_true')
|
parser.add_argument('--sixel', action='store_true')
|
||||||
|
parser.add_argument('--seed', type=int)
|
||||||
|
|
||||||
opts = parser.parse_args()
|
opts = parser.parse_args()
|
||||||
out = opts.out if opts.out is not None else sys.stdout
|
out = opts.out if opts.out is not None else sys.stdout
|
||||||
|
|
@ -61,133 +63,133 @@ def main():
|
||||||
# Enter alt screen
|
# Enter alt screen
|
||||||
out.write('\033[?1049h')
|
out.write('\033[?1049h')
|
||||||
|
|
||||||
with open('/dev/urandom', 'rb') as rand:
|
# uses system time or /dev/urandom if available if opt.seed == None
|
||||||
for _ in range(count):
|
# pin seeding method to make seeding stable across future versions
|
||||||
if opts.scroll and rand.read(1)[0] == 0:
|
random.seed(a=opts.seed, version=2)
|
||||||
out.write('\033[m')
|
|
||||||
|
|
||||||
if opts.scroll_region and rand.read(1)[0] == 0:
|
for _ in range(count):
|
||||||
top = rand.read(1)[0] % 3
|
if opts.scroll and random.randrange(256) == 0:
|
||||||
bottom = rand.read(1)[0] % 3
|
out.write('\033[m')
|
||||||
out.write(f'\033[{top};{lines - bottom}r')
|
|
||||||
|
|
||||||
lines_to_scroll = rand.read(1)[0] % (lines - 1)
|
if opts.scroll_region and random.randrange(256) == 0:
|
||||||
rev = rand.read(1)[0] % 2
|
top = random.randrange(3)
|
||||||
if not rev and rand.read(1)[0] % 2:
|
bottom = random.randrange(3)
|
||||||
out.write(f'\033[{lines};{cols}H')
|
out.write(f'\033[{top};{lines - bottom}r')
|
||||||
out.write('\n' * lines_to_scroll)
|
|
||||||
else:
|
|
||||||
out.write(f'\033[{lines_to_scroll + 1}{"T" if rev == 1 else "S"}')
|
|
||||||
continue
|
|
||||||
|
|
||||||
# Generate a random location and a random character
|
lines_to_scroll = random.randrange(lines - 1)
|
||||||
row = rand.read(1)[0] % lines
|
rev = random.randrange(2)
|
||||||
col = rand.read(1)[0] % cols
|
if not rev and random.randrange(2):
|
||||||
c = alphabet[rand.read(1)[0] % len(alphabet)]
|
out.write(f'\033[{lines};{cols}H')
|
||||||
|
out.write('\n' * lines_to_scroll)
|
||||||
|
else:
|
||||||
|
out.write(f'\033[{lines_to_scroll + 1}{"T" if rev == 1 else "S"}')
|
||||||
|
continue
|
||||||
|
|
||||||
repeat = rand.read(1)[0] % (cols - col) + 1
|
# Generate a random location and a random character
|
||||||
assert col + repeat <= cols
|
row = random.randrange(lines)
|
||||||
|
col = random.randrange(cols)
|
||||||
|
c = random.choice(alphabet)
|
||||||
|
|
||||||
color_variant = color_variants[rand.read(1)[0] % len(color_variants)]
|
repeat = random.randrange((cols - col) + 1)
|
||||||
|
assert col + repeat <= cols
|
||||||
|
|
||||||
# Position cursor
|
color_variant = random.choice(color_variants)
|
||||||
out.write(f'\033[{row + 1};{col + 1}H')
|
|
||||||
|
|
||||||
if color_variant in [ColorVariant.REGULAR, ColorVariant.BRIGHT]:
|
# Position cursor
|
||||||
do_bg = rand.read(1)[0] % 2
|
out.write(f'\033[{row + 1};{col + 1}H')
|
||||||
base = 40 if do_bg else 30
|
|
||||||
base += 60 if color_variant == ColorVariant.BRIGHT else 0
|
|
||||||
|
|
||||||
idx = rand.read(1)[0] % 8
|
if color_variant in [ColorVariant.REGULAR, ColorVariant.BRIGHT]:
|
||||||
out.write(f'\033[{base + idx}m')
|
do_bg = random.randrange(2)
|
||||||
|
base = 40 if do_bg else 30
|
||||||
|
base += 60 if color_variant == ColorVariant.BRIGHT else 0
|
||||||
|
|
||||||
elif color_variant == ColorVariant.CUBE:
|
idx = random.randrange(8)
|
||||||
do_bg = rand.read(1)[0] % 2
|
out.write(f'\033[{base + idx}m')
|
||||||
base = 48 if do_bg else 38
|
|
||||||
|
|
||||||
idx = rand.read(1)[0] % 256
|
elif color_variant == ColorVariant.CUBE:
|
||||||
if rand.read(1)[0] % 2:
|
do_bg = random.randrange(2)
|
||||||
# Old-style
|
base = 48 if do_bg else 38
|
||||||
out.write(f'\033[{base};5;{idx}m')
|
|
||||||
else:
|
|
||||||
# New-style (sub-parameter based)
|
|
||||||
out.write(f'\033[{base}:2:5:{idx}m')
|
|
||||||
|
|
||||||
elif color_variant == ColorVariant.RGB:
|
idx = random.randrange(256)
|
||||||
do_bg = rand.read(1)[0] % 2
|
if random.randrange(2):
|
||||||
base = 48 if do_bg else 38
|
# Old-style
|
||||||
rgb = rand.read(3)
|
out.write(f'\033[{base};5;{idx}m')
|
||||||
|
else:
|
||||||
|
# New-style (sub-parameter based)
|
||||||
|
out.write(f'\033[{base}:2:5:{idx}m')
|
||||||
|
|
||||||
if rand.read(1)[0] % 2:
|
elif color_variant == ColorVariant.RGB:
|
||||||
# Old-style
|
do_bg = random.randrange(2)
|
||||||
out.write(f'\033[{base};2;{rgb[0]};{rgb[1]};{rgb[2]}m')
|
base = 48 if do_bg else 38
|
||||||
else:
|
|
||||||
# New-style (sub-parameter based)
|
|
||||||
out.write(f'\033[{base}:2::{rgb[0]}:{rgb[1]}:{rgb[2]}m')
|
|
||||||
|
|
||||||
if opts.attr_bold and rand.read(1)[0] % 5 == 0:
|
# use list comprehension in favor of randbytes(n)
|
||||||
out.write('\033[1m')
|
# which is only available for Python >= 3.9
|
||||||
if opts.attr_italic and rand.read(1)[0] % 5 == 0:
|
rgb = [random.randrange(256) for _ in range(3)]
|
||||||
out.write('\033[3m')
|
|
||||||
if opts.attr_underline and rand.read(1)[0] % 5 == 0:
|
|
||||||
out.write('\033[4m')
|
|
||||||
|
|
||||||
out.write(c * repeat)
|
if random.randrange(2):
|
||||||
|
# Old-style
|
||||||
|
out.write(f'\033[{base};2;{rgb[0]};{rgb[1]};{rgb[2]}m')
|
||||||
|
else:
|
||||||
|
# New-style (sub-parameter based)
|
||||||
|
out.write(f'\033[{base}:2::{rgb[0]}:{rgb[1]}:{rgb[2]}m')
|
||||||
|
|
||||||
do_sgr_reset = rand.read(1)[0] % 2
|
if opts.attr_bold and random.randrange(5) == 0:
|
||||||
if do_sgr_reset:
|
out.write('\033[1m')
|
||||||
reset_actions = ['\033[m', '\033[39m', '\033[49m']
|
if opts.attr_italic and random.randrange(5) == 0:
|
||||||
idx = rand.read(1)[0] % len(reset_actions)
|
out.write('\033[3m')
|
||||||
out.write(reset_actions[idx])
|
if opts.attr_underline and random.randrange(5) == 0:
|
||||||
|
out.write('\033[4m')
|
||||||
|
|
||||||
|
out.write(c * repeat)
|
||||||
|
|
||||||
|
do_sgr_reset = random.randrange(2)
|
||||||
|
if do_sgr_reset:
|
||||||
|
reset_actions = ['\033[m', '\033[39m', '\033[49m']
|
||||||
|
out.write(random.choice(reset_actions))
|
||||||
|
|
||||||
# Leave alt screen
|
# Leave alt screen
|
||||||
out.write('\033[m\033[r\033[?1049l')
|
out.write('\033[m\033[r\033[?1049l')
|
||||||
|
|
||||||
with open('/dev/urandom', 'rb') as rand:
|
if opts.sixel:
|
||||||
if opts.sixel:
|
# The sixel 'alphabet'
|
||||||
# The sixel 'alphabet'
|
sixels = '?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
|
||||||
sixels = '?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~'
|
|
||||||
|
|
||||||
for _ in range(200):
|
for _ in range(200):
|
||||||
# Offset image
|
# Offset image
|
||||||
out.write(' ' * (rand.read(1)[0] % (cols // 2)))
|
out.write(' ' * (random.randrange(cols // 2)))
|
||||||
|
|
||||||
# Begin sixel
|
# Begin sixel
|
||||||
out.write('\033Pq')
|
out.write('\033Pq')
|
||||||
|
|
||||||
# Set up 256 random colors
|
# Set up 256 random colors
|
||||||
for idx in range(256):
|
for idx in range(256):
|
||||||
# param 2: 1=HLS, 2=RGB.
|
# param 2: 1=HLS, 2=RGB.
|
||||||
# param 3/4/5: HLS/RGB values in range 0-100
|
# param 3/4/5: HLS/RGB values in range 0-100
|
||||||
# (except 'hue' which is 0..360)
|
# (except 'hue' which is 0..360)
|
||||||
out.write(f'#{idx};2;{rand.read(1)[0] % 101};{rand.read(1)[0] % 101};{rand.read(1)[0] % 101}')
|
out.write(f'#{idx};2;{random.randrange(101)};{random.randrange(101)};{random.randrange(101)}')
|
||||||
|
|
||||||
# Randomize image width/height
|
# Randomize image width/height
|
||||||
six_height = struct.unpack('@H', rand.read(2))[0] % (height // 2)
|
six_height = random.randrange(height // 2)
|
||||||
six_width = struct.unpack('@H', rand.read(2))[0] % (width // 2)
|
six_width = random.randrange(width // 2)
|
||||||
|
|
||||||
# Sixel size. Without this, sixels will be
|
# Sixel size. Without this, sixels will be
|
||||||
# auto-resized on cell-boundaries. We expect programs
|
# auto-resized on cell-boundaries. We expect programs
|
||||||
# to emit this sequence since otherwise you cannot get
|
# to emit this sequence since otherwise you cannot get
|
||||||
# correctly sized images.
|
# correctly sized images.
|
||||||
out.write(f'"0;0;{six_width};{six_height}')
|
out.write(f'"0;0;{six_width};{six_height}')
|
||||||
|
|
||||||
for row in range(six_height // 6): # Each sixel is 6 pixels
|
for row in range(six_height // 6): # Each sixel is 6 pixels
|
||||||
# Choose a random color
|
# Choose a random color
|
||||||
out.write(f'#{rand.read(1)[0] % 256}')
|
out.write(f'#{random.randrange(256)}')
|
||||||
|
|
||||||
if rand.read(1)[0] == 999999999999:
|
for col in range(six_width):
|
||||||
assert False
|
out.write(f'{random.choice(sixels)}')
|
||||||
out.write(f'!{six_width}{sixels[rand.read(1)[0] % len(sixels)]}')
|
|
||||||
else:
|
|
||||||
for col in range(six_width):
|
|
||||||
out.write(f'{sixels[rand.read(1)[0] % len(sixels)]}')
|
|
||||||
|
|
||||||
# Next line
|
# Next line
|
||||||
out.write('-')
|
out.write('-')
|
||||||
|
|
||||||
# End sixel
|
# End sixel
|
||||||
out.write('\033\\')
|
out.write('\033\\')
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue