Skip to content

Commit 793f5e9

Browse files
committed
Add animation example
1 parent 5d2f78c commit 793f5e9

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

example/animation.rb

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require "unicode_plot"
2+
require "stringio"
3+
4+
N = 1000
5+
M = 50
6+
7+
out = StringIO.new
8+
def out.tty?; true; end
9+
10+
shift = 0
11+
continue = true
12+
Signal.trap(:INT) { continue = false }
13+
14+
while continue
15+
out.rewind
16+
17+
xs = 0...N
18+
ys = xs.map {|x| Math.sin(2*Math::PI*(x + shift) / N) }
19+
UnicodePlot.lineplot(xs, ys, width: 60, height: 15).render(out)
20+
21+
lines = out.string.lines
22+
lines.each do |line|
23+
$stdout.print "\r#{line}"
24+
end
25+
$stdout.flush
26+
27+
if continue
28+
n = lines.count
29+
$stdout.print "\e[#{n}F"
30+
shift = (shift + M) % N
31+
end
32+
33+
sleep 0.2
34+
end

0 commit comments

Comments
 (0)