lib/ro_commands/helpers/out.rb in ro_commands-0.0.1 vs lib/ro_commands/helpers/out.rb in ro_commands-0.0.2
- old
+ new
@@ -1,10 +1,69 @@
+require "fileutils"
+require "term/ansicolor"
class Out
class << self
- def out(msg)
+ include ::Term::ANSIColor
+
+ def out(msg, color=nil)
m = msg
- puts m
+ if color
+ puts send(color.to_sym, m)
+ else
+ puts m
+ end
+ #write_tmp(m)
msgs << m
+ end
+
+ def write_tmp(ctn)
+ if save?
+ unless ctn.match(%r{^\s*$})
+ tmp_ctn = tmp
+ tmp_ctn << "At #{Time.now}"
+ tmp_ctn << ctn
+ File.write(tmp_path, tmp_ctn.join("\n"))
+ end
+ else
+ if test(?s, tmp_path) and test(?s, tmp_path) > 10_000
+ FileUtils.rm_f(tmp_path)
+ end
+ end
+ end
+
+ def save?
+ @save
+ end
+
+ def save_on
+ File.write(tmp_path, "save_on at #{Time.now}")
+ @save = true
+ end
+
+ def save_off
+ File.write(tmp_path, "save_on at #{Time.now}")
+
+ @save = false
+ end
+
+ def save
+ @save ||= false
+ end
+
+ def tmp_path
+ FileUtils.touch "/tmp/ro_out"
+ @tmp_path ||= "/tmp/ro_out"
+ end
+
+ def tmp
+ if test(?f, tmp_path)
+ File.readlines(tmp_path).delete_if do |l|
+ l.blank?
+ end
+ else
+ FileUtils.touch tmp_path
+ []
+ end
end
def reset
@msgs = []
end