Skip to content

Instantly share code, notes, and snippets.

@ivarref
Created February 25, 2025 12:19
Show Gist options
  • Save ivarref/353b8b0293c6dce155e0393f42c3d222 to your computer and use it in GitHub Desktop.
Save ivarref/353b8b0293c6dce155e0393f42c3d222 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import random
import sys
def hex_strs_to_ansi(hex_strs):
assert isinstance(hex_strs, list)
res = []
for col in hex_strs:
assert 6 == len(col)
ansi_col = []
for idx in range(0, len(col), 2):
col_str = col[idx] + col[idx+1]
col_int = int(col_str, base=16)
ansi_col.append(str(col_int))
ansi_item = f"\x1b[38;2;{ansi_col[0]};{ansi_col[1]};{ansi_col[2]}mβ‰ˆ\x1b[0m"
res.append(ansi_item)
return res
_desert_life = ("🌡", "🏜", "️πŸͺ", "🐫", "⚱️", "πŸ¦‚", "🦑", "β˜€οΈ", "🐍")
# https://www.xp-pen.com/blog/nature-color-palette.html
_desert_colors = ['DD692C',
'D48662',
'D9C3A9',
'D9B6A3',
'BF754B',
'BF9B7A',
'C16630',
'A6583C',
'A67458',
'734434',
'723715',
'734B34',
'732F17',
'783215',
'8B322C']
_marine_life = ("🐳", "🐠", "🦈", "πŸ™", "🐑", "🐬", "🐟", "πŸ¦€", "πŸ‹")
_marine_colors = ["\x1b[38;2;0;100;200mβ‰ˆ\x1b[0m", # 1
"\x1b[38;2;10;110;210mβ‰ˆ\x1b[0m", # 2
"\x1b[38;2;20;120;220mβ‰ˆ\x1b[0m", # 3
"\x1b[38;2;30;130;230mβ‰ˆ\x1b[0m", # 4
"\x1b[38;2;40;140;240mβ‰ˆ\x1b[0m", # 5
"\x1b[38;2;50;150;250mβ‰ˆ\x1b[0m", # 6
"\x1b[38;2;60;160;255mβ‰ˆ\x1b[0m", # 7
"\x1b[38;2;70;170;255mβ‰ˆ\x1b[0m", # 8
"\x1b[38;2;80;180;255mβ‰ˆ\x1b[0m", # 9
"\x1b[38;2;90;190;255mβ‰ˆ\x1b[0m", # 10
"\x1b[38;2;100;200;255mβ‰ˆ\x1b[0m", # 11
"\x1b[38;2;110;210;255mβ‰ˆ\x1b[0m", # 12
"\x1b[38;2;120;220;255mβ‰ˆ\x1b[0m", # 13
"\x1b[38;2;130;230;255mβ‰ˆ\x1b[0m", # 14
"\x1b[38;2;140;240;255mβ‰ˆ\x1b[0m"]
themes = {'desert': (hex_strs_to_ansi(_desert_colors), _desert_life),
'marine': (_marine_colors, _marine_life)}
def gen_marine(theme, max_length, life_chance, seed):
assert seed is not None
buffer = []
life = theme[1]
for i in range(0, max_length+1):
def gen(ii):
random.seed(seed + ii + life_chance)
if random.random() < life_chance:
choice = random.choice(life)
idx = life.index(choice)
assert idx <= 9
return [str(idx), str(idx)]
return ['.', '.']
c = gen(i)
buffer.append(c[0])
buffer.append(c[1])
buffer.append('.')
return buffer
def marine_line_len_seed(theme, max_length, fish_chance, seed=None) -> str:
assert seed is not None
seed = abs(seed)
background = theme[0]
life = theme[1]
add = seed % 3
seed_org = seed
seed = int((seed/3.))
strmarine = gen_marine(theme, max_length+1, fish_chance, seed)
buf = []
for i in range(0, max_length, 2):
c = strmarine[i+add]
c1 = strmarine[i+1+add]
buf.append(c)
buf.append(c1)
lin = ''.join(buf)
for idx, fish in enumerate(life):
idxstr = str(idx) + str(idx)
lin = lin.replace(idxstr, '##')
lin = lin.replace(str(idx), '.')
lin = lin.replace('##', idxstr)
buf = []
skip_next = False
char_count = 0
for idx, c in enumerate(lin):
random.seed(seed_org + idx + fish_chance)
if skip_next:
skip_next = False
continue
if idx == len(lin) - 1:
char_count += 1
buf.append(random.choice(background))
elif c == '.':
char_count += 1
buf.append(random.choice(background))
elif char_count+2 > max_length:
char_count += 1
buf.append(random.choice(background))
else:
fish = life[int(c)]
buf.append(fish)
char_count += 2
skip_next = True
if char_count == max_length:
break
assert char_count == max_length
return ''.join(buf)
if __name__ == "__main__":
max_len = 80
if '--len' in sys.argv:
max_len = int(sys.argv[sys.argv.index('--len') + 1])
if '--seed' in sys.argv:
seed = int(sys.argv[sys.argv.index('--seed') + 1])
else:
seed = random.randint(0, 1_000_000)
if '--theme' in sys.argv:
theme = themes[sys.argv[sys.argv.index('--theme') + 1]]
else:
theme = themes['marine']
print(marine_line_len_seed(theme, max_len, 0.35, seed), end='', flush=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment