Skip to content

Instantly share code, notes, and snippets.

@Garmelon
Created May 17, 2019 18:52
Show Gist options
  • Save Garmelon/81e376a47b241d243b8e8cd960bbabf4 to your computer and use it in GitHub Desktop.
Save Garmelon/81e376a47b241d243b8e8cd960bbabf4 to your computer and use it in GitHub Desktop.
# Info:
#
# To run this script, install urwid (http://urwid.org/) via your favourite
# package manager.
import math
import urwid
class TestWidget(urwid.WidgetWrap):
DELTA = 0.05
CHARS = " ░▒▓██"
#CHARS = " ░▒▓█"
#CHARS = " ▁▂▃▄▅▆▇█"
def __init__(self):
self.text = urwid.Text("<placeholder text>", align=urwid.CENTER)
self.filler = urwid.Filler(self.text)
super().__init__(self.filler)
self.t = 0
def render(self, size, focus):
self.width, self.height = size
return super().render(size, focus)
def func(self, x, y, t):
# FULL of magic numbers!
x -= self.width/2
y -= self.height/2
x /= 2
x /= 7
y /= 7
t *= 2
d = math.sqrt(x**2 + y**2)
th = math.atan2(y, x) * 5
f = max(-1, min(1, (d - 2)))
g = 1 if f > 0 else -1
h = 1 if f > 0 else 2
n = math.sin(5*d - t*h + th*g)
n *= f
return n
def value_to_char(self, val):
val = max(-1, min(1, val))
val = (val + 1) / 2
l = len(self.CHARS)
d = 1 / l
for n in range(l):
if val <= (n + 1) * d:
return self.CHARS[n]
def text_from_func(self, width, height, t):
lines = []
for y in range(height):
line = []
for x in range(width):
value = self.func(x, y, t)
line.append(self.value_to_char(value))
lines.append("".join(line))
return "\n".join(lines)
def update_text(self, loop, user_data):
self.t += self.DELTA
new_text = self.text_from_func(self.width, self.height, self.t)
self.text.set_text(new_text)
loop.set_alarm_in(self.DELTA, self.update_text)
widget = TestWidget()
loop = urwid.MainLoop(
widget,
)
loop.set_alarm_in(widget.DELTA, widget.update_text)
loop.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment