lolcat 42.0.21 → 42.0.42

Sign up to get free protection for your applications and to get access to all the features.
data/lib/lolcat/cat.rb CHANGED
@@ -19,43 +19,113 @@
19
19
  require "lolcat/version"
20
20
  require "lolcat/lol"
21
21
 
22
- require 'optparse'
22
+ require 'stringio'
23
+ require 'trollop'
23
24
 
24
25
  module Lol
26
+ def self.halp!(text, opts={})
27
+ opts = {
28
+ :animate => false,
29
+ :duration => 12,
30
+ :os => 0,
31
+ :speed => 20,
32
+ :spread => 8.0,
33
+ :freq => 0.3
34
+ }.merge opts
35
+
36
+ begin
37
+ i = 20
38
+ o = rand(256)
39
+ text.split("\n").each do |line|
40
+ i -= 1
41
+ opts[:os] = o+i
42
+ Lol.println line, opts
43
+ end
44
+ puts "\n"
45
+ rescue Interrupt
46
+ end
47
+ exit 1
48
+ end
49
+
25
50
  def self.cat!
26
- if ['-h','--help','--halp','--version'].include? ARGV[0]
51
+ p = Trollop::Parser.new do
52
+ banner <<HEADER
53
+
54
+ Usage: lolcat [OPTION]... [FILE]...
55
+
56
+ Concatenate FILE(s), or standard input, to standard output.
57
+ With no FILE, or when FILE is -, read standard input.
58
+
59
+ HEADER
60
+ banner ''
61
+ opt :spread, "Rainbow spread", :short => 'p', :default => 8.0
62
+ opt :seed, "Rainbow seed, 0 = random", :short => 'S', :default => 0
63
+ opt :animate, "Enable psychedelics", :short => 'a', :default => false
64
+ opt :duration, "Animation duration", :short => 'd', :default => 12
65
+ opt :speed, "Animation speed", :short => 's', :default => 20.0
66
+ opt :force, "Force color even when stdout is not a tty", :short => 'f', :default => false
67
+ opt :version, "Print version and exit", :short => 'v'
68
+ opt :help, "Show this message", :short => 'h'
69
+ banner <<FOOTER
70
+
71
+ Examples:
72
+ lolcat f - g Output f's contents, then standard input, then g's contents.
73
+ lolcat Copy standard input to standard output.
74
+ fortune | lolcat Display a rainbow cookie.
75
+
76
+ Report lolcat bugs to <http://www.github.org/busyloop/lolcat/issues>
77
+ lolcat home page: <http://www.github.org/busyloop/lolcat/>
78
+ Report lolcat translation bugs to <http://speaklolcat.com/>
79
+ For complete documentation, read the source!
80
+
81
+ FOOTER
82
+ end
83
+
84
+ opts = Trollop::with_standard_exception_handling p do
27
85
  begin
28
- puts
29
- Lol.whut "Usage: lolcat [[file] [[file] [[file] [[file] [file]]]] [...]]", 20
30
- puts
31
- Lol.whut "Concatenate FILE(s), or standard input, to standard output.", 19
32
- Lol.whut "With no FILE, or when FILE is -, read standard input.", 18
33
- puts
34
- Lol.whut " -h, --help, --halp, --version display this help and exit", 17
35
-
36
- puts
37
- Lol.whut "Examples:"
38
- Lol.whut " lolcat f - g Output f's contents, then standard input, then g's contents.", 16
39
- Lol.whut " lolcat Copy standard input to standard output.", 15
40
- Lol.whut " fortune | lolcat Display a rainbow cookie.", 14
41
-
42
- puts
43
- Lol.whut "Report lolcat bugs to <http://www.github.org/busyloop/lolcat/issues>", 13
44
- Lol.whut "lolcat home page: <http://www.github.org/busyloop/lolcat/>", 12
45
- Lol.whut "Report lolcat translation bugs to <http://speaklolcat.com/>", 11
46
- Lol.whut "For complete documentation, read the source!", 10
47
- puts
48
- rescue Interrupt
86
+ o = p.parse ARGV
87
+ rescue Trollop::HelpNeeded
88
+ buf = StringIO.new
89
+ p.educate buf
90
+ buf.rewind
91
+ halp! buf.read, {}
92
+ buf.close
49
93
  end
50
- exit 1
94
+ o
51
95
  end
52
-
96
+
97
+ p.die :spread, "must be > 0" if opts[:spread] < 0.1
98
+ p.die :duration, "must be > 0" if opts[:duration] < 0.1
99
+ p.die :speed, "must be > 0.1" if opts[:duration] < 0.1
100
+
101
+ opts = { :freq => 0.3 }.merge(opts)
102
+ opts[:os] = opts[:seed]
103
+ opts[:os] = rand(256) if opts[:os] == 0
104
+
53
105
  begin
54
- fds = ARGV.empty? ? [ARGF] : ARGV[0..-1]
55
- fds.each do |file|
56
- file = ARGF if file == '-'
57
- file = File.open file unless file == ARGF
58
- Lol.cat file,8
106
+ files = ARGV.empty? ? [:stdin] : ARGV[0..-1]
107
+ files.each do |file|
108
+ fd = ARGF if file == '-' or file == :stdin
109
+ begin
110
+ fd = File.open file unless fd == ARGF
111
+
112
+ if $stdout.tty? or opts[:force]
113
+ Lol.cat fd, opts
114
+ else
115
+ until fd.eof? do
116
+ $stdout.write(fd.read(8192))
117
+ end
118
+ end
119
+ rescue Errno::ENOENT
120
+ puts "lolcat: #{file}: No such file or directory"
121
+ exit 1
122
+ rescue Errno::EACCES
123
+ puts "lolcat: #{file}: Permission denied"
124
+ exit 1
125
+ rescue Errno::EISDIR
126
+ puts "lolcat: #{file}: Is a directory"
127
+ exit 1
128
+ end
59
129
  end
60
130
  rescue Interrupt
61
131
  end
data/lib/lolcat/lol.rb CHANGED
@@ -17,10 +17,11 @@
17
17
  # 0. You just DO WHAT THE FUCK YOU WANT TO.
18
18
 
19
19
  require "lolcat/version"
20
-
21
20
  require 'paint'
22
21
 
23
22
  module Lol
23
+ STRIP_ANSI = Regexp.compile '\e\[(\d+)(;\d+)?(;\d+)?[m|K]', nil
24
+
24
25
  def self.rainbow(freq, i)
25
26
  red = Math.sin(freq*i + 0) * 127 + 128
26
27
  green = Math.sin(freq*i + 2*Math::PI/3) * 127 + 128
@@ -28,28 +29,37 @@ module Lol
28
29
  "#%02X%02X%02X" % [ red, green, blue ]
29
30
  end
30
31
 
31
- def self.whut(text, duration=12, delay=0.05, spread=8)
32
- (1..duration).each do |i|
33
- print "\e[#{text.length}D"
34
- text.chars.each_with_index do |c,j|
35
- print Paint[c, rainbow(0.3, i+j/spread)]
36
- end
37
- sleep delay
32
+ def self.cat(fd, opts={})
33
+ fd.each do |line|
34
+ opts[:os] += 1
35
+ println(line, opts)
38
36
  end
37
+ end
38
+
39
+ def self.println(str, defaults={}, opts={})
40
+ opts.merge!(defaults)
41
+ str.chomp!
42
+ str.gsub! STRIP_ANSI, '' if !str.nil? and ($stdout.tty? or opts[:force])
43
+ opts[:animate] ? println_ani(str, opts) : println_plain(str, opts)
39
44
  puts
40
45
  end
41
46
 
42
- def self.lput(str, offset, spread)
43
- print Paint[str, rainbow(0.3, offset/spread)]
47
+ private
48
+
49
+ def self.println_plain(str, defaults={}, opts={})
50
+ opts.merge!(defaults)
51
+ str.chomp.chars.each_with_index do |c,i|
52
+ print Paint[c, rainbow(opts[:freq], opts[:os]+i/opts[:spread])]
53
+ end
44
54
  end
45
55
 
46
- def self.cat(fd, spread)
47
- i=0
48
- fd.each do |line|
49
- line.chars.each_with_index do |c,j|
50
- lput c, i+j, spread
51
- end
52
- i+=1
56
+ def self.println_ani(str, opts={})
57
+ return if str.empty?
58
+ (1..opts[:duration]).each do |i|
59
+ print "\e[#{str.length}D"
60
+ opts[:os] += opts[:spread]/Math::PI
61
+ println_plain(str, opts)
62
+ sleep 1.0/opts[:speed]
53
63
  end
54
64
  end
55
65
  end
@@ -1,3 +1,3 @@
1
1
  module Lolcat
2
- VERSION = "42.0.21"
2
+ VERSION = "42.0.42"
3
3
  end
data/lolcat.gemspec CHANGED
@@ -13,6 +13,7 @@ Gem::Specification.new do |s|
13
13
 
14
14
  #s.rubyforge_project = "lolcat"
15
15
  s.add_dependency "paint", "~> 0.8.3"
16
+ s.add_dependency "trollop", "~> 1.16.2"
16
17
 
17
18
  s.files = `git ls-files`.split("\n")
18
19
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 42
7
7
  - 0
8
- - 21
9
- version: 42.0.21
8
+ - 42
9
+ version: 42.0.42
10
10
  platform: ruby
11
11
  authors:
12
12
  - Moe
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2011-08-10 00:00:00 +02:00
17
+ date: 2011-08-11 00:00:00 +02:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -32,6 +32,21 @@ dependencies:
32
32
  version: 0.8.3
33
33
  type: :runtime
34
34
  version_requirements: *id001
35
+ - !ruby/object:Gem::Dependency
36
+ name: trollop
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ segments:
44
+ - 1
45
+ - 16
46
+ - 2
47
+ version: 1.16.2
48
+ type: :runtime
49
+ version_requirements: *id002
35
50
  description: Rainbows and unicorns!
36
51
  email:
37
52
  - moe@busyloop.net