Opal 0.2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. data/lib/opal.rb +527 -0
  2. data/lib/opcolor.rb +94 -0
  3. metadata +47 -0
@@ -0,0 +1,527 @@
1
+ #!/usr/bin/env ruby
2
+ # This is the main file for Opal.
3
+
4
+ class Object
5
+ def about
6
+ if defined?(Colored)
7
+ return "-- " + self.class.name.cyan.bold + " methods".magenta.bold + " --\n" + self.methods(false).join("\n").green.bold.underline + "\n" + self.private_methods(false).join("\n").red.bold.underline
8
+ else
9
+ return "-- #{self.class.name} methods --\n" + self.methods(false).join("\n") + "\n" + self.private_methods(false).join("\n")
10
+ end
11
+ end
12
+ def htdoc
13
+ return <<EOF
14
+ <div style="background-color: black; color: white; font-family: Courier New; padding: 7px;">
15
+ -- <strong style="color: cyan;">#{self.class.name}</strong> <strong style="color: magenta;">methods</strong> --
16
+ <div style="color: lime; text-decoration: underline;">
17
+ #{self.methods(false).join("<br/>\n")}
18
+ </div>
19
+ <div style="color: red; text-decoration: underline;">
20
+ #{self.private_methods(false).join("<br/>\n")}
21
+ </div>
22
+ </div>
23
+ EOF
24
+ end
25
+ def oidb
26
+ puts self.about
27
+ end
28
+ def me
29
+ self
30
+ end
31
+ end
32
+
33
+ class Module
34
+ def about
35
+ if defined?(Colored)
36
+ sc = " < " + "Module".cyan.bold
37
+ return "-- " + self.name.cyan.bold + sc + " methods".magenta.bold + " --\n" + (self.instance_methods(false)).join("\n").green.bold.underline + "\n" + (self.private_instance_methods(false)).join("\n").red.bold.underline + "\n-- " + self.name.cyan.bold + sc + " namespaces".magenta.bold + " --\n" + (self.constants).join("\n").blue.bold.underline
38
+ else
39
+ sc = " < " + "Module"
40
+ return "-- #{self.name + sc} methods --\n" + (self.instance_methods(false)).join("\n") + "\n" + (self.private_instance_methods(false)).join("\n") + "\n-- #{self.name + sc} namespaces --\n" + (self.constants).join("\n")
41
+ end
42
+ end
43
+ def htdoc(lsuper=false)
44
+ sc = " < <strong style=\"color: cyan;\">" + "Module" + "</strong>"
45
+ return <<EOF
46
+ <div style="background-color: black; color: white; font-family: Courier New; padding: 7px;">
47
+ -- <strong style="color: cyan;">#{self.name}</strong>#{sc} <strong style="color: magenta;">methods</strong> --
48
+ <div style="color: lime; text-decoration: underline;">
49
+ #{(self.instance_methods(false)).join("<br/>\n")}
50
+ </div>
51
+ <div style="color: red; text-decoration: underline;">
52
+ #{self.private_instance_methods(false).join("<br/>\n")}
53
+ </div>
54
+ -- <strong style="color: cyan;">#{self.name}</strong>#{sc} <strong style="color: magenta;">namespaces</strong> --
55
+ <div style="color: lightblue; text-decoration: underline;">
56
+ #{(self.constants).join("<br/>\n")}
57
+ </div>
58
+ </div>
59
+ EOF
60
+ end
61
+ end
62
+
63
+ class Class
64
+ def about
65
+ if defined?(Colored)
66
+ sc = ""
67
+ sc = " < " + self.superclass.name.cyan.bold if self.superclass
68
+ return "-- " + self.name.cyan.bold + sc + " methods".magenta.bold + " --\n" + (self.methods(self == Object) - self.instance_methods(false)).join("\n").yellow.bold.underline + "\n" + (self.private_methods(self == Object) - self.private_instance_methods(self == Object)).join("\n").red.bold.underline + "\n-- " + self.name.cyan.bold + " < " + sc + " instance methods".magenta.bold + " --\n" + self.instance_methods(self == Object).join("\n").green.bold.underline + "\n" + (self.private_instance_methods(self == Object)).join("\n").red.bold.underline + "\n-- " + self.name.cyan.bold + sc + " namespaces".magenta.bold + " --\n" + (self.constants).join("\n").blue.bold.underline
69
+ else
70
+ sc = ""
71
+ sc = " < " + self.superclass.name if self.superclass
72
+ return "-- #{self.name + sc} methods --\n" + (self.methods(self == Object) - self.instance_methods(self == Object)).join("\n") + "\n" + (self.private_methods(self == Object) - self.private_instance_methods(self == Object)).join("\n") + "\n-- #{self.name + sc} instance methods --\n" + self.instance_methods(self == Object).join("\n") + "\n" + (self.private_instance_methods(self == Object)).join("\n") + "\n-- #{self.name + sc} namespaces --\n" + (self.constants).join("\n")
73
+ end
74
+ end
75
+ def htdoc(lsuper=false)
76
+ a1 = "" ; a2 = ""
77
+ a1 = "<a href=\"#{self.superclass.name + ".doc.htm" rescue ""}\" style=\"color: cyan;\">" if lsuper
78
+ a2 = "</a>" if lsuper
79
+ sc = ""
80
+ sc = " < <strong style=\"color: cyan;\">" + a1 + self.superclass.name + a2 + "</strong>" if self.superclass
81
+ return <<EOF
82
+ <div style="background-color: black; color: white; font-family: Courier New; padding: 7px;">
83
+ -- <strong style="color: cyan;">#{self.name}</strong>#{sc} <strong style="color: magenta;">methods</strong> --
84
+ <div style="color: yellow; text-decoration: underline;">
85
+ #{(self.methods(self == Object) - self.instance_methods(self == Object)).join("<br/>\n")}
86
+ </div>
87
+ <div style="color: red; text-decoration: underline;">
88
+ #{(self.private_methods(self == Object) - self.private_instance_methods(self == Object)).join(" private!<br/>\n")}
89
+ </div>
90
+ -- <strong style="color: cyan;">#{self.name}</strong>#{sc} <strong style="color: magenta;">instance methods</strong> --
91
+ <div style="color: lime; text-decoration: underline;">
92
+ #{(self.instance_methods(self == Object)).join("<br/>\n")}
93
+ </div>
94
+ <div style="color: red; text-decoration: underline;">
95
+ #{(self.private_instance_methods(self == Object)).join(" private!<br/>\n")}
96
+ </div>
97
+ -- <strong style="color: cyan;">#{self.name}</strong>#{sc} <strong style="color: magenta;">namespaces</strong> --
98
+ <div style="color: lightblue; text-decoration: underline;">
99
+ #{(self.constants).join("<br/>\n")}
100
+ </div>
101
+ </div>
102
+ EOF
103
+ end
104
+ end
105
+
106
+ # = Opal
107
+ # === A neat language directly into Ruby code.
108
+ # This is Opal. It has cool syntax. It uses all of the same classes, base, etc. as Ruby!
109
+ # * Shell like
110
+ # * As fast as Ruby
111
+ # * Programs are easily translated
112
+ # * Interactive Shell
113
+ # And much more... Here's a comparison: Ruby code...
114
+ # "Hello World".gsub("l", "d") #=> Heddo Wordd
115
+ # vs. Opal code...
116
+ # "Hello World" gsub "l" "d" -- => Heddo Wordd
117
+ # But just to tell you, the Ruby code:
118
+ # exit 1
119
+ # or something similar is equivlant to the Opal code:
120
+ # Kernel exit 1 -- or (System quit)
121
+ # Why? Because Opal would process the Ruby code as
122
+ # exit.send(:'1')
123
+ # 'cause it thinks it's a method.
124
+ #
125
+ # I forgot to mention variables. They're different than your general Ruby variables. No, these are special:
126
+ # monkey = Animal.new("Monkey")
127
+ # That's Ruby, but what about Opal?
128
+ # %monkey= Animal new "Monkey"
129
+ # And to get it again:
130
+ # %monkey
131
+ # Don't forget to use this exact syntax,
132
+ # %monkey = Animal new "Monkey"
133
+ # or
134
+ # % monkey
135
+ # won't work.
136
+ #
137
+ module Opal ; extend self
138
+
139
+ # A standard Opal error.
140
+ class OpalError < Exception ; end
141
+ # An Opal compiler error.
142
+ class OpalCompileError < OpalError ; end
143
+ # An Opal syntax error.
144
+ class OpalSyntaxError < OpalError ; end
145
+ # An Opal runtime error.
146
+ class OpalRuntimeError < OpalError ; end
147
+ # An Opal system error. Raised when I/O errors, etc.
148
+ class OpalSystemError < OpalError ; end
149
+
150
+ # This is Version. Opal uses it for it's non-Float Version.
151
+ class Version
152
+
153
+ attr_accessor :arr
154
+
155
+ # Create a new Version.
156
+ def initialize(major, minor, build, revision)
157
+ @arr = [major, minor, build, revision]
158
+ end
159
+
160
+ # Add two versions.
161
+ def +(v)
162
+ raise ArgumentError unless v.class == Version
163
+ (0..3).to_a.each { |t| @arr[t] += v.arr[t] }
164
+ end
165
+
166
+ # Increment the version by one.
167
+ def inc!
168
+ @arr[3] += 1
169
+ if @arr[3] == 10
170
+ @arr[3] = 0
171
+ @arr[2] += 1
172
+ if @arr[2] == 10
173
+ @arr[2] = 0
174
+ @arr[1] += 1
175
+ if @arr[1] == 10
176
+ @arr[1] = 0
177
+ @arr[0] += 1
178
+ end
179
+ end
180
+ end
181
+ return self
182
+ end
183
+
184
+ # Inspect the object.
185
+ def inspect
186
+ @arr.join "."
187
+ end
188
+
189
+ alias to_s inspect
190
+
191
+ end
192
+
193
+ # Get the version.
194
+ def version ; Opal::Version.new(0,2,5,0) ; end
195
+
196
+ # The Environ.
197
+ module Env ; extend self
198
+ module System ; extend self
199
+ # Shortcut to "version"
200
+ def version
201
+ Opal.version
202
+ end
203
+
204
+ # Shortcut for Ruby "print".
205
+ def out(s)
206
+ print s
207
+ end
208
+
209
+ # Shortcut for Ruby "puts".
210
+ def put(s)
211
+ puts s
212
+ end
213
+
214
+ # Shortcut for Ruby "$stdin.read".
215
+ def tin
216
+ return $stdin.read
217
+ end
218
+
219
+ # Shortcut for Ruby "$stdin.readline".
220
+ def get
221
+ return $stdin.readline
222
+ end
223
+
224
+ # Shortcut for making arrays.
225
+ def array(*args)
226
+ return args
227
+ end
228
+
229
+ # Shortcut for Ruby "require".
230
+ def import(name)
231
+ require name.to_s
232
+ end
233
+
234
+ # Shortcut for making blocks.
235
+ def block(code)
236
+ return proc do
237
+ Opal.opal_eval code
238
+ end
239
+ end
240
+
241
+ # Shortcut for making classes (Opal: Namespaces).
242
+ def namespace(n)
243
+ return eval("Opal::Env::#{n.to_s} = Class.new")
244
+ end
245
+
246
+ # Shortcut for defining methods under classes (Opal: Namespaces).
247
+ def namespace_def(ns, nm, code, *argnm)
248
+ eval("Opal::Env::#{ns.to_s}.class_eval do\ndef #{nm.to_s}(#{argnm.join(", ")})\n#{code}\nend\nend")
249
+ end
250
+
251
+ # Equal to "IO.popen".
252
+ def run(s)
253
+ IO.popen(s).read
254
+ end
255
+
256
+ # Quit. (Exit)
257
+ def quit
258
+ exit
259
+ end
260
+ end
261
+ def quit
262
+ exit
263
+ end
264
+ def bind(vars)
265
+ binding
266
+ end
267
+ end
268
+
269
+ # old! The Opal Compiler (to ruby)
270
+ def compile(s, head=true)
271
+ rbcode = ""
272
+ rbcode << "#!/usr/bin/env ruby\n# Compiled by DCWare Opal #{Opal.version.to_s}\nrequire 'opal'\ninclude Opal::Env\ncmdargs = ARGV\n" if head
273
+ s.split(/;|\n/).each do |line|
274
+ rbcode << Opal.transform(line) + "\n"
275
+ end
276
+ return rbcode
277
+ end
278
+
279
+ # old! Transform Opal code into Ruby code.
280
+ def transform(line)
281
+ ret = ""
282
+ ln = line.split(" ")
283
+ ln.each do |pt|
284
+ pt.gsub! "`", " "
285
+ if pt =~ /^%/
286
+ pt[0] = ''
287
+ ln[ln.index(pt)] = "$vars[:#{pt}]" if $useglobal
288
+ end
289
+ end
290
+ if line =~ /--*/
291
+ line[0..1] = ''
292
+ ret = "##{line}"
293
+ elsif line =~ /^#!/
294
+ elsif line =~ /%*=\ /
295
+ enil = line.split(/%*=\ /)
296
+ enil[0][0] = ''
297
+ c = Opal.transform(enil[1])
298
+ if $useglobal
299
+ ret = "$vars[:#{enil[0]}] = #{c}"
300
+ else
301
+ ret = "#{enil[0]} = #{c}"
302
+ end
303
+ elsif ln.length > 2
304
+ ret = "#{ln[0]}.#{ln[1]}(#{ln[2..-1].join(", ")})"
305
+ elsif ln.length > 1
306
+ ret = "#{ln[0]}.#{ln[1]}"
307
+ elsif ln.length == 1
308
+ ret = ln[0]
309
+ end
310
+ return ret
311
+ end
312
+
313
+ # updated! The Opal interactive mode. Requires "colored" and "readline".
314
+ def interactive
315
+ require 'colored' unless defined?(Colored)
316
+ require 'readline' unless defined?(Readline)
317
+ yexit = false
318
+ v = {:cmdargs => [], :file => "(opal-interactive)"}
319
+ trap "INT" do
320
+ yexit = true
321
+ end
322
+ until yexit
323
+ $useglobal = true
324
+ print "(opal-interactive)".cyan.bold + ">> ".magenta.bold
325
+ s = (Readline.readline rescue $stdin.readline.chomp)
326
+ begin
327
+ rets = []
328
+ s.split(';').each do |cm|
329
+ rets << Opal.run(cm, v).inspect.green.bold
330
+ end
331
+ puts "=> ".magenta.bold + rets.join("; ".magenta.bold)
332
+ rescue SystemExit
333
+ yexit = true
334
+ rescue Exception
335
+ puts ($!.class.name.sub(/^Opal::/, "") + ": ").magenta.bold + $!.message.red.bold
336
+ end
337
+ end
338
+ $useglobal = nil
339
+ end
340
+
341
+ # updated! Evaluate!
342
+ def opal_eval(s, args=[], fname="(opal-eval)")
343
+ v = {:cmdargs => args, :file => fname}
344
+ lns = s.split(/\r|\n|;/)
345
+ lns.each do |line|
346
+ Opal.run(line, v)
347
+ end
348
+ end
349
+
350
+ # new!
351
+ # The parser. <i>may contain traces of <b>regular expressions</b>.</i>
352
+ def run(linz, vars={})
353
+ line = linz.gsub(/[ \t]*--[^\n]+/, "")
354
+ if line =~ /^%[A-Za-z0-9]+\=\ /
355
+ eq = line.split /\=\ /
356
+ eq[0].sub! /^%/, ""
357
+ vars[eq[0].intern] = Opal.run(eq[1])
358
+ return true
359
+ elsif line =~ /^[\ ]*$/
360
+ return true
361
+ else
362
+ ln = line.split(" ")
363
+ ln.each do |pt|
364
+ pt.gsub! "`", " "
365
+ vars.each do |nm, vl|
366
+ pt.sub! "%#{nm.to_s}", "vars[#{nm.inspect}]"
367
+ end
368
+ end
369
+ Opal.joins(ln)
370
+ Opal.joins(ln, /^'/, /'$/)
371
+ Opal.joins(ln, /^\[/, /\]$/)
372
+ Opal.joins(ln, /^\{/, /\}$/)
373
+ on = eval(ln[0], Opal::Env.bind(vars)) rescue nil
374
+ raise OpalCompileError, "Cannot work on \"#{ln[0]}\" because it does not exist." unless on
375
+ mt = (ln[1] or "me").intern
376
+ rgs = []
377
+ (ln[2..-1] or []).each do |r|
378
+ rgs << eval(r)
379
+ end
380
+ begin
381
+ return on.send(mt, *rgs)
382
+ rescue NameError
383
+ raise OpalCompileError, $!.message
384
+ rescue TypeError
385
+ raise OpalSyntaxError, $!.message
386
+ rescue NoMethodError
387
+ raise OpalCompileError, $!.message
388
+ rescue SyntaxError
389
+ raise OpalSyntaxError, $!.message
390
+ rescue RuntimeError
391
+ raise OpalRuntimeError, $!.message
392
+ end
393
+ end
394
+ end
395
+
396
+ # new! Joins an array based on delimiters.
397
+ def joins(a, sreg=/^"/, ereg=/"$/)
398
+ inst = false
399
+ a.each do |pt|
400
+ inst = a.index(pt) if (pt =~ sreg) and not (inst)
401
+ if (pt =~ ereg) and (inst)
402
+ rng = inst..(a.index(pt))
403
+ a[rng] = a[rng].join(" ")
404
+ inst = false
405
+ end
406
+ end
407
+ return a
408
+ end
409
+
410
+ end
411
+
412
+ def errwrite(s)
413
+ $errwrite_used = true
414
+ $stderr.write s + "\n" unless $silent
415
+ exit 1
416
+ end
417
+
418
+ if __FILE__ == $0
419
+ action = (ARGV[0].dup rescue nil)
420
+ act_on = ARGV[1]
421
+ args = ARGV[2..-1]
422
+ if action =~ /^nocolor-/
423
+ $nocolor = true
424
+ action.sub! /nocolor-*/, ""
425
+ end
426
+ if action =~ /^silent-/
427
+ $silent = true
428
+ action.sub! /silent-*/, ""
429
+ end
430
+ require 'opcolor'
431
+ puts "DCWare Opal ".cyan.underline + Opal.version.to_s.yellow.bold.underline unless $silent
432
+ unless action and act_on
433
+ unless (action) and ((action.to_s.downcase == "shell") or (action.to_s.downcase == "help") or (action.to_s.downcase == "test")) # ...
434
+ errwrite "Error: ".magenta.bold + "No action, or nothing to act on.".red.bold
435
+ end
436
+ end
437
+ ay = ""
438
+ ay = " ".yellow + act_on.yellow.bold if act_on
439
+ puts "Action: ".yellow + action.downcase.yellow.bold + ay unless $silent
440
+ puts unless $silent
441
+ begin
442
+ case action.downcase
443
+ when "test"
444
+ Opal.opal_eval <<EOF
445
+ Kernel puts "testing numerics..."
446
+ Kernel print "96 + 37 = "
447
+ %t1= 96 + 37
448
+ Kernel puts %t1
449
+ Kernel puts "okay."
450
+ Kernel puts "testing blocks..."
451
+ %t2= System block "Kernel puts 'okay.'"
452
+ %t2 call
453
+ Kernel puts "testing strings..."
454
+ Kernel print "'Blah' in upper case is: "
455
+ %t3= 'Blah' upcase
456
+ Kernel print %t3
457
+ Kernel print ", and 'FrOdO' in lower case is: "
458
+ %t4= 'FrOdO' downcase
459
+ Kernel puts %t4
460
+ Kernel puts "okay."
461
+ Kernel puts "testing sleep..."
462
+ Kernel puts "will sleep for 3 seconds."
463
+ Kernel sleep 3
464
+ Kernel puts "okay."
465
+ Kernel puts "testing comments..."
466
+ Kernel puts "you should not see the message 'Hello' if all goes well below:"
467
+ -- Kernel puts 'Hello'
468
+ Kernel puts "okay."
469
+ Kernel puts "all done!"
470
+ EOF
471
+ when "run"
472
+ Opal.opal_eval File.open(act_on, "r").read, args, act_on
473
+ when "compile"
474
+ warn "Compile is deprecated."
475
+ print "Please wait... " unless $silent
476
+ f = File.open(act_on.gsub(".opl", ".cop.rb"), "w")
477
+ c = Opal.compile(File.open(act_on, "r").read)
478
+ f.write c
479
+ f.close
480
+ puts "done! (#{c.size}B)" unless $silent
481
+ when "shell"
482
+ Opal.interactive
483
+ when "lookup"
484
+ for a in ([act_on] + args)
485
+ begin
486
+ eval("#{a}.oidb")
487
+ puts ("~" * 12).red
488
+ rescue Exception
489
+ $stderr.write "Opal OIDB ".blue.bold + "could not find #{a}.\nPlease make sure you spelled the item correctly.\n".magenta.bold unless $silent
490
+ end
491
+ end
492
+ when "htlook"
493
+ for a in ([act_on] + args)
494
+ begin
495
+ File.open(a + ".doc.htm", "w") do |f|
496
+ f.write eval("#{a}.htdoc")
497
+ end
498
+ puts "Opal OIDB ".blue.bold + "put the result into ".green.bold + "#{a}.doc.htm".yellow.bold + ".".green.bold unless $silent
499
+ rescue Exception
500
+ $stderr.write "Opal OIDB ".blue.bold + "could not find #{a}.\nPlease make sure you spelled the item correctly.\n".magenta.bold unless $silent
501
+ end
502
+ end
503
+ when "help"
504
+ puts $0.blue.bold + " (nocolor-)(silent-)".green.bold
505
+ puts "...\t\thelp".yellow.bold
506
+ puts "...\t\trun ".yellow.bold + "<file> <args...>".green.bold
507
+ puts "...\t\tcompile ".yellow.bold + "<file> (=> \"file.cop.rb\")".green.bold
508
+ puts "...\t\tshell ".yellow.bold
509
+ puts "...\t\tlookup ".yellow.bold + "<object...>".green.bold
510
+ puts "...\t\thtlook ".yellow.bold + "<object...>".green.bold
511
+ else
512
+ errwrite "Opal ".blue.bold + "did not understand the action ".magenta.bold + action.yellow.bold + ".\nTry ".magenta.bold + "help".yellow.bold + ".".magenta.bold
513
+ end
514
+ rescue SystemExit
515
+ exit 1 if $errwrite_used
516
+ rescue Exception
517
+ $stderr.write "#{action.downcase.capitalize} failed with #{$!.class.name} status!\nDetails:\n".magenta.bold unless $silent
518
+ $stderr.write $!.message.red.underline + "\n" unless $silent
519
+ ecd = 0
520
+ $!.class.name.split(" ").each do |cd|
521
+ ecd += cd[0]
522
+ end
523
+ exit ecd
524
+ end
525
+ puts unless $silent
526
+ puts "#{action.downcase.capitalize} finished!".green.bold unless $silent
527
+ end
@@ -0,0 +1,94 @@
1
+ require 'Win32/Console/ANSI' if PLATFORM =~ /win32/
2
+
3
+ ##
4
+ # cute.
5
+ #
6
+ # >> "this is red".red
7
+ #
8
+ # >> "this is red with a blue background (read: ugly)".red_on_blue
9
+ #
10
+ # >> "this is red with an underline".red.underline
11
+ #
12
+ # >> "this is really bold and really blue".bold.blue
13
+ #
14
+ # >> Colored.red "This is red" # but this part is mostly untested
15
+ #
16
+ # modded to opal standards
17
+ module Colored
18
+ extend self
19
+
20
+ COLORS = {
21
+ 'black' => 30,
22
+ 'red' => 31,
23
+ 'green' => 32,
24
+ 'yellow' => 33,
25
+ 'blue' => 34,
26
+ 'magenta' => 35,
27
+ 'cyan' => 36,
28
+ 'white' => 37
29
+ }
30
+
31
+ EXTRAS = {
32
+ 'clear' => 0,
33
+ 'bold' => 1,
34
+ 'underline' => 4,
35
+ 'reversed' => 7
36
+ }
37
+
38
+ COLORS.each do |color, value|
39
+ define_method(color) do
40
+ colorize(self, :foreground => color)
41
+ end
42
+
43
+ define_method("on_#{color}") do
44
+ colorize(self, :background => color)
45
+ end
46
+
47
+ COLORS.each do |highlight, value|
48
+ next if color == highlight
49
+ define_method("#{color}_on_#{highlight}") do
50
+ colorize(self, :foreground => color, :background => highlight)
51
+ end
52
+ end
53
+ end
54
+
55
+ EXTRAS.each do |extra, value|
56
+ next if extra == 'clear'
57
+ define_method(extra) do
58
+ colorize(self, :extra => extra)
59
+ end
60
+ end
61
+
62
+ define_method(:to_eol) do
63
+ tmp = sub(/^(\e\[[\[\e0-9;m]+m)/, "\\1\e[2K")
64
+ if tmp == self
65
+ return "\e[2K" << self
66
+ end
67
+ tmp
68
+ end
69
+
70
+ def colorize(string, options = {})
71
+ colored = ""
72
+ colored = [color(options[:foreground]), color("on_#{options[:background]}"), extra(options[:extra])].compact * '' unless $nocolor
73
+ colored << string
74
+ colored << extra(:clear)
75
+ end
76
+
77
+ def colors
78
+ @@colors ||= COLORS.keys.sort
79
+ end
80
+
81
+ def extra(extra_name)
82
+ extra_name = extra_name.to_s
83
+ "\e[#{EXTRAS[extra_name]}m" if EXTRAS[extra_name]
84
+ end
85
+
86
+ def color(color_name)
87
+ background = color_name.to_s =~ /on_/
88
+ color_name = color_name.to_s.sub('on_', '')
89
+ return unless color_name && COLORS[color_name]
90
+ "\e[#{COLORS[color_name] + (background ? 10 : 0)}m"
91
+ end
92
+ end unless Object.const_defined? :Colored
93
+
94
+ String.send(:include, Colored)
metadata ADDED
@@ -0,0 +1,47 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: Opal
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.2.5.0
7
+ date: 2007-11-10 00:00:00 -08:00
8
+ summary: The Opal language for simplicity and Smalltalk-like syntax.
9
+ require_paths:
10
+ - lib
11
+ email: dcwareadmin@gmail.com
12
+ homepage: http://www.dcware.co.nr/
13
+ rubyforge_project:
14
+ description:
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ post_install_message:
29
+ authors:
30
+ - DCWare Inc.
31
+ files:
32
+ - lib/opcolor.rb
33
+ - lib/opal.rb
34
+ test_files: []
35
+
36
+ rdoc_options: []
37
+
38
+ extra_rdoc_files: []
39
+
40
+ executables: []
41
+
42
+ extensions: []
43
+
44
+ requirements: []
45
+
46
+ dependencies: []
47
+