vamp 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bcb9e8749153c14dfd71c3ebc974bef2404f03e7
4
- data.tar.gz: 7ef8f88e0c884e12be551b293e8eb60824f81683
3
+ metadata.gz: 3ed0c28b4a5fa22a09ccafc6674d51076fb75816
4
+ data.tar.gz: 931968ca87c4c1cf6c4c5a4b8865625bece04ec0
5
5
  SHA512:
6
- metadata.gz: f9eee26d68f6a3d7fcce7bb1d1d0bb25086f92bc5dec549ba931ea9c1599c83570bd112e9067947354edc8476241b443ba2a8d753be442259cb18d049ad6f839
7
- data.tar.gz: ec9ca46d0488386fda25cf936ba58380886e118672ab1f4fcc208ba239c301d35dd82968f12e7da8179af232a72aebece2d616852bbb161b537814da6f2cab4f
6
+ metadata.gz: b196797bc06e8c60be048e59a4a9a11bcbf8fdece181c07d3e0832afd61616f79aee6caa725380f58b4be0bc61a6f04c72dde40dd5a8cfd33debf0c5fb1db91f
7
+ data.tar.gz: e122c443482653ceaa831f2dad9a6d84a010dd2040e710c89b159b6a6bbc5803d4ffb0738ed2fec214c9fb8da6b1cba24a307f03b3cd2bb998f0415310d6605b
data/lib/vamp/animator.rb CHANGED
@@ -3,32 +3,37 @@ require_relative "colorize"
3
3
  module Vamp
4
4
  # play animation on console
5
5
  class Animator
6
- attr_accessor :data # complete animation lines
7
- attr_accessor :number # animation source height
8
- attr_accessor :width # animation width
9
- attr_accessor :height # animation height
10
- attr_accessor :scroll # running text
6
+ attr_accessor :data # complete animation lines
7
+ attr_accessor :number # animation source height
8
+ attr_accessor :width # animation width
9
+ attr_accessor :height # animation height ( +1 if scroll_text)
10
+ attr_accessor :scroll_text # running text data
11
+ attr_accessor :scroll_counter # for calculating running text offset
12
+ attr_accessor :ani_color # animation color (if any: "red", "blue", ..)
13
+ attr_accessor :text_color # text color (if any: "blue", "green", ...)
11
14
 
12
- def initialize(file, number = 31, height = number, scroll)
15
+
16
+ def initialize(file, number = 31, start = 0, height = number, scroll_text = nil, cycles = 1)
13
17
  @data = []
14
18
  @number = number
15
- @width = 80
16
- @height = height
17
- @scroll = scroll
18
- @offset = 0
19
+ @height = height - start
20
+ @scroll_text = scroll_text
21
+ @cycles = cycles
22
+ @width = detect_terminal_width || 80
23
+ if scroll_text
24
+ @height += 1
25
+ @scroll_counter = 0
26
+ @scroll_text = (" " * @width) + scroll_text + (" " * @width)
27
+ end
28
+ @ani_color = "red"
29
+ @text_color = "blue"
19
30
  lines = IO.readlines(file)
20
31
  lines.each_slice(number) do |block|
21
32
  d = []
22
33
  block.each_with_index do |line, index|
23
- # puts line.class
24
- # puts printf("%-40s", line)
25
- # d << line
26
- d << (line.rstrip + (" " * 80))[0..80]
27
- # d << sprintf("%80s", line)
28
- # puts block.length
34
+ d << (line.rstrip + (" " * 80))[0..(width - 1)] if index >= start
29
35
  break if index >= height
30
36
  end
31
- # puts lines
32
37
  @data << d
33
38
  end
34
39
  end
@@ -37,16 +42,12 @@ module Vamp
37
42
  print "\e[H\e[2J"
38
43
  end
39
44
 
40
- def home
41
- print "\e[H\e[#{height}F"
42
- end
43
-
44
- def home
45
- print "\e[H\e[#{height}F"
45
+ def home(lines = height)
46
+ print "\e[H\e[#{lines}F"
46
47
  end
47
48
 
48
- def down(lines = @height)
49
- # number.times { puts }
49
+ def down(lines = height)
50
+ # lines.times { puts }
50
51
  print "\e[H\e[#{lines}E"
51
52
  end
52
53
 
@@ -54,14 +55,33 @@ module Vamp
54
55
  $stdout.flush
55
56
  end
56
57
 
58
+ def scroll_offset
59
+ @scroll_counter / 3
60
+ end
61
+
57
62
  def animate(msg)
58
63
  home
59
- puts Vamp::Colorize.colorize("red", msg)
60
- puts Vamp::Colorize.colorize("blue", animate_line) if scroll
64
+ if ani_color
65
+ puts Vamp::Colorize.colorize(ani_color, msg)
66
+ else
67
+ puts msg
68
+ end
69
+ if scroll_text
70
+ if text_color
71
+ puts Vamp::Colorize.colorize(text_color, running_line)
72
+ else
73
+ puts running_line
74
+ end
75
+ end
61
76
  flush
62
77
  sleep(1.0/48.0)
63
78
  end
64
79
 
80
+ def running_line
81
+ @scroll_counter += 1
82
+ "#{scroll_text[scroll_offset..(scroll_offset + width - 1)]}"
83
+ end
84
+
65
85
  def cursor_off
66
86
  print "\e[H\e[?25l"
67
87
  end
@@ -70,24 +90,50 @@ module Vamp
70
90
  print "\e[H\e[?25h"
71
91
  end
72
92
 
73
- def animate_line
74
- @offset += 1
75
- "#{@scroll[(@offset / 2)..(@offset / 2 + @width - 1)]}"
76
- end
77
-
78
93
  def play
79
94
  if $stdout.isatty
80
95
  begin
81
96
  cursor_off
82
97
  clear
83
- data.each do
84
- |lines| animate(lines.join("\n"))
85
- end
98
+ data.each { |lines| animate(lines.join("\n")) } until finished?
86
99
  ensure
87
100
  cursor_on
88
- down(@height + 2)
101
+ down(height + 1)
89
102
  end
103
+ else
104
+ puts "sorry, I must have a tty device to play an animation"
105
+ end
106
+ end
107
+
108
+ def finished?
109
+ if scroll_text
110
+ scroll_offset + width >= scroll_text.size
111
+ else
112
+ @cycle ||= 0
113
+ @cycle += 1
114
+ @cycle > @cycles
90
115
  end
91
116
  end
117
+
118
+ # stolen from <https://github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb>
119
+ # Returns width of terminal when detected, nil if not detected.
120
+ def detect_terminal_width
121
+ if ENV['COLUMNS'] =~ /^\d+$/
122
+ ENV['COLUMNS'].to_i
123
+ elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exists?('tput')
124
+ `tput cols`.to_i
125
+ elsif STDIN.tty? && command_exists?('stty')
126
+ `stty size`.scan(/\d+/)[1].to_i
127
+ else
128
+ nil
129
+ end
130
+ rescue
131
+ nil
132
+ end
133
+
134
+ # Determines if a shell command exists by searching for it in ENV['PATH'].
135
+ def command_exists?(command)
136
+ ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) }
137
+ end
92
138
  end
93
139
  end
data/lib/vamp/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Vamp
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/sample/pyramid.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "vamp"
2
+
3
+ animator = Vamp::Animator.new(File.join(Gem.loaded_specs["vamp"].gem_dir, "files", "pyramid.txt"), 31, 7, 27, nil, 2)
4
+ animator.play
data/sample/vampire.rb CHANGED
@@ -1,10 +1,8 @@
1
1
  require "vamp"
2
2
 
3
- animator = Vamp::Animator.new(File.join(Gem.loaded_specs["vamp"].gem_dir, "files", "vampire.txt"), 31, 24,
4
- " " * 80 +
5
- "No man knows till he has suffered from the night how sweet and how dear to his heart and eye the morning can be." \
3
+ animator = Vamp::Animator.new(File.join(Gem.loaded_specs["vamp"].gem_dir, "files", "vampire.txt"), 31, 0, 24,
4
+ "No man knows till he has suffered from the night how sweet and how dear to his heart and eye the morning can be." \
6
5
  " When the sun grew so high this morning that it struck the top of the great gateway opposite my window," \
7
6
  " the high spot which it touched seemed to me as if the dove from the ark had lighted there. My fear fell from me" \
8
- " as if it had been a vaporous garment which dissolved in the warmth." +
9
- " " * 80)
10
- 16.times { animator.play }
7
+ " as if it had been a vaporous garment which dissolved in the warmth.")
8
+ animator.play
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vamp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Meyling
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-31 00:00:00.000000000 Z
11
+ date: 2015-11-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -62,6 +62,7 @@ files:
62
62
  - lib/vamp/ply/vampire.rb
63
63
  - lib/vamp/version.rb
64
64
  - sample/cli.rb
65
+ - sample/pyramid.rb
65
66
  - sample/vampire.rb
66
67
  - spec/spec_helper.rb
67
68
  - spec/vamp_spec.rb