File: animation.rb

package info (click to toggle)
ruby-unicode-plot 0.0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,492 kB
  • sloc: ruby: 2,871; makefile: 4
file content (37 lines) | stat: -rw-r--r-- 606 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
require "unicode_plot"
require "stringio"

N = 1000
M = 50

out = StringIO.new
def out.tty?; true; end

shift = 0
continue = true
Signal.trap(:INT) { continue = false }

while continue
  out.truncate(0)

  xs = 0...N
  ys = xs.map {|x| Math.sin(2*Math::PI*(x + shift) / N) }
  UnicodePlot.lineplot(xs, ys, width: 60, height: 15).render(out)

  lines = out.string.lines
  lines.each do |line|
    $stdout.print "\r#{line}"
  end
  $stdout.print "\e[0J"
  $stdout.flush

  sleep 0.2

  if continue
    n = lines.count
    $stdout.print "\e[#{n}F"
    shift = (shift + M) % N
  end
end

$stdout.print "\e[0J"