Class: Vamp::Animator
- Inherits:
-
Object
- Object
- Vamp::Animator
- Defined in:
- lib/vamp/animator.rb
Overview
play animation on console
Instance Attribute Summary collapse
-
#ani_color ⇒ Object
animation color (if any: “red”, “blue”, ..).
-
#data ⇒ Object
complete animation lines.
-
#height ⇒ Object
animation height ( +1 if scroll_text).
-
#number ⇒ Object
animation source height.
-
#scroll_counter ⇒ Object
for calculating running text offset.
-
#scroll_text ⇒ Object
running text data.
-
#text_color ⇒ Object
text color (if any: “blue”, “green”, …).
-
#width ⇒ Object
animation width.
Instance Method Summary collapse
- #animate(msg) ⇒ Object
- #clear ⇒ Object
-
#command_exists?(command) ⇒ Boolean
Determines if a shell command exists by searching for it in ENV.
- #cursor_off ⇒ Object
- #cursor_on ⇒ Object
-
#detect_terminal_width ⇒ Object
stolen from <github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb> Returns width of terminal when detected, nil if not detected.
- #down(lines = height) ⇒ Object
- #finished? ⇒ Boolean
- #flush ⇒ Object
- #home(lines = height) ⇒ Object
-
#initialize(file, number = 31, start = 0, height = number, scroll_text = nil, cycles = 1) ⇒ Animator
constructor
A new instance of Animator.
- #play ⇒ Object
- #running_line ⇒ Object
- #scroll_offset ⇒ Object
Constructor Details
#initialize(file, number = 31, start = 0, height = number, scroll_text = nil, cycles = 1) ⇒ Animator
Returns a new instance of Animator.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/vamp/animator.rb', line 16 def initialize(file, number = 31, start = 0, height = number, scroll_text = nil, cycles = 1) @data = [] @number = number @height = height - start @scroll_text = scroll_text @cycles = cycles @width = detect_terminal_width || 80 if scroll_text @height += 1 @scroll_counter = 0 @scroll_text = (" " * @width) + scroll_text + (" " * @width) end @ani_color = "red" @text_color = "blue" lines = IO.readlines(file) lines.each_slice(number) do |block| d = [] block.each_with_index do |line, index| d << (line.rstrip + (" " * 80))[0..(width - 1)] if index >= start break if index >= height end @data << d end end |
Instance Attribute Details
#ani_color ⇒ Object
animation color (if any: “red”, “blue”, ..)
12 13 14 |
# File 'lib/vamp/animator.rb', line 12 def ani_color @ani_color end |
#data ⇒ Object
complete animation lines
6 7 8 |
# File 'lib/vamp/animator.rb', line 6 def data @data end |
#height ⇒ Object
animation height ( +1 if scroll_text)
9 10 11 |
# File 'lib/vamp/animator.rb', line 9 def height @height end |
#number ⇒ Object
animation source height
7 8 9 |
# File 'lib/vamp/animator.rb', line 7 def number @number end |
#scroll_counter ⇒ Object
for calculating running text offset
11 12 13 |
# File 'lib/vamp/animator.rb', line 11 def scroll_counter @scroll_counter end |
#scroll_text ⇒ Object
running text data
10 11 12 |
# File 'lib/vamp/animator.rb', line 10 def scroll_text @scroll_text end |
#text_color ⇒ Object
text color (if any: “blue”, “green”, …)
13 14 15 |
# File 'lib/vamp/animator.rb', line 13 def text_color @text_color end |
#width ⇒ Object
animation width
8 9 10 |
# File 'lib/vamp/animator.rb', line 8 def width @width end |
Instance Method Details
#animate(msg) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/vamp/animator.rb', line 62 def animate(msg) home if ani_color puts Vamp::Colorize.colorize(ani_color, msg) else puts msg end if scroll_text if text_color puts Vamp::Colorize.colorize(text_color, running_line) else puts running_line end end flush sleep(1.0/48.0) end |
#clear ⇒ Object
41 42 43 |
# File 'lib/vamp/animator.rb', line 41 def clear print "\e[H\e[2J" end |
#command_exists?(command) ⇒ Boolean
Determines if a shell command exists by searching for it in ENV.
135 136 137 |
# File 'lib/vamp/animator.rb', line 135 def command_exists?(command) ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) } end |
#cursor_off ⇒ Object
85 86 87 |
# File 'lib/vamp/animator.rb', line 85 def cursor_off print "\e[H\e[?25l" end |
#cursor_on ⇒ Object
89 90 91 |
# File 'lib/vamp/animator.rb', line 89 def cursor_on print "\e[H\e[?25h" end |
#detect_terminal_width ⇒ Object
stolen from <github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb> Returns width of terminal when detected, nil if not detected.
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/vamp/animator.rb', line 120 def detect_terminal_width if ENV['COLUMNS'] =~ /^\d+$/ ENV['COLUMNS'].to_i elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput') `tput cols`.to_i elsif STDIN.tty? && command_exists?('stty') `stty size`.scan(/\d+/)[1].to_i else nil end rescue nil end |
#down(lines = height) ⇒ Object
49 50 51 52 |
# File 'lib/vamp/animator.rb', line 49 def down(lines = height) # lines.times { puts } print "\e[H\e[#{lines}E" end |
#finished? ⇒ Boolean
108 109 110 111 112 113 114 115 116 |
# File 'lib/vamp/animator.rb', line 108 def finished? if scroll_text scroll_offset + width >= scroll_text.size else @cycle ||= 0 @cycle += 1 @cycle > @cycles end end |
#flush ⇒ Object
54 55 56 |
# File 'lib/vamp/animator.rb', line 54 def flush $stdout.flush end |
#home(lines = height) ⇒ Object
45 46 47 |
# File 'lib/vamp/animator.rb', line 45 def home(lines = height) print "\e[H\e[#{lines}F" end |
#play ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/vamp/animator.rb', line 93 def play if $stdout.isatty begin cursor_off clear data.each { |lines| animate(lines.join("\n")) } until finished? ensure cursor_on down(height + 1) end else puts "sorry, I must have a tty device to play an animation" end end |
#running_line ⇒ Object
80 81 82 83 |
# File 'lib/vamp/animator.rb', line 80 def running_line @scroll_counter += 1 "#{scroll_text[scroll_offset..(scroll_offset + width - 1)]}" end |
#scroll_offset ⇒ Object
58 59 60 |
# File 'lib/vamp/animator.rb', line 58 def scroll_offset @scroll_counter / 3 end |