textgrid-rust/demo_termstr.py
2026-07-20 12:53:51 +10:00

52 lines
1.4 KiB
Python

import math
import time
import textgrid
import termstr
ROWS = 24
COLS = 80
BLOCKS = " ░▒▓█"
def rainbow(hue):
r = int(255 * (0.5 + 0.5 * math.cos(hue * math.tau)))
g = int(255 * (0.5 + 0.5 * math.cos(hue * math.tau - 2.094)))
b = int(255 * (0.5 + 0.5 * math.cos(hue * math.tau - 4.188)))
return r, g, b
def main():
tg = textgrid.TextGrid(ROWS, COLS, "TermStr Demo")
phase = 0.0
try:
while True:
rows = []
for y in range(ROWS):
text_chars = []
styles = []
for x in range(COLS):
dx = x - COLS / 2
dy = y - ROWS / 2
v = math.sin(
math.sqrt(dx * dx + dy * dy) * 0.25 - phase
)
v = max(-1, min(1, v))
idx = int((v + 1) * 0.5 * (len(BLOCKS) - 1))
text_chars.append(BLOCKS[idx])
hue = (x / COLS + y / ROWS + phase * 0.25) % 1.0
styles.append(termstr.CharStyle(fg_color=rainbow(hue)))
text = "".join(text_chars)
rows.append(termstr.TermStr(text, tuple(styles)))
tg.update(rows)
phase += 0.05
time.sleep(0.03)
except KeyboardInterrupt:
pass
finally:
tg.close()
if __name__ == "__main__":
main()