Class: Out

Inherits:
Object show all
Extended by:
Term::ANSIColor, Thor::Shell
Defined in:
lib/ro_helpers/out.rb

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.msgObject

Returns the value of attribute msg.



8
9
10
# File 'lib/ro_helpers/out.rb', line 8

def msg
  @msg
end

.outputObject

Returns the value of attribute output.



31
32
33
# File 'lib/ro_helpers/out.rb', line 31

def output
  @output
end

Class Method Details

.debug(msg, color = nil) ⇒ Object



50
51
52
53
54
# File 'lib/ro_helpers/out.rb', line 50

def debug(msg, color=nil)
  if $RODEBUG
    out(msg, color)
  end
end

.handle_msg(msg) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/ro_helpers/out.rb', line 66

def handle_msg(msg)
  if msg.is_a?(Array)
    msg.join("\n")
  else
    msg
  end
end

.out(msg, color = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'lib/ro_helpers/out.rb', line 56

def out(msg, color=nil)
  reset
  if color
    say send(:"#{color}", bold(msg))
  else
    say msg
  end
  @msg = handle_msg(msg)
end

.raw_out(msg, color = nil) ⇒ Object



74
75
76
77
78
79
80
81
82
83
# File 'lib/ro_helpers/out.rb', line 74

def raw_out(msg, color=nil)
  reset
  if color
    p send(color.to_sym, msg)
  else
    p msg
  end
  #write_tmp(m)
  @msg = msg
end

.resetObject



136
137
138
# File 'lib/ro_helpers/out.rb', line 136

def reset
  @msg = ''
end

.saveObject



116
117
118
# File 'lib/ro_helpers/out.rb', line 116

def save
  @save ||= false
end

.save?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/ro_helpers/out.rb', line 101

def save?
  @save
end

.save_offObject



110
111
112
113
114
# File 'lib/ro_helpers/out.rb', line 110

def save_off
  File.write(tmp_path, "save_on at #{Time.now}")

  @save = false
end

.save_onObject



105
106
107
108
# File 'lib/ro_helpers/out.rb', line 105

def save_on
  File.write(tmp_path, "save_on at #{Time.now}")
  @save = true
end

.say(*args) ⇒ Object



45
46
47
48
# File 'lib/ro_helpers/out.rb', line 45

def say(*args)
  output.puts "#{args.flatten.join(" ")}"
  output.flush
end

.say_with_color(status, new_msg, other, color = nil) ⇒ Object



26
27
28
29
# File 'lib/ro_helpers/out.rb', line 26

def say_with_color(status, new_msg, other, color=nil)
  color ||= "green"
  say "#{time_now}#{send(:"#{color}", bold(status))}  #{new_msg} #{other}"
end

.table(status, msg, other = nil, color = nil) ⇒ Object



19
20
21
22
23
24
# File 'lib/ro_helpers/out.rb', line 19

def table(status, msg, other=nil, color=nil)

  status = status.ljust(7)
  new_msg = msg.ljust(20)
  say_with_color(status, new_msg, other, color)
end

.time_nowObject



37
38
39
# File 'lib/ro_helpers/out.rb', line 37

def time_now
  Time.now.strftime("%H:%M:%S - ")
end

.time_say(*args) ⇒ Object



41
42
43
# File 'lib/ro_helpers/out.rb', line 41

def time_say(*args)
  say([time.now, args])
end

.tmpObject



125
126
127
128
129
130
131
132
133
134
# File 'lib/ro_helpers/out.rb', line 125

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

.tmp_pathObject



120
121
122
123
# File 'lib/ro_helpers/out.rb', line 120

def tmp_path
  FileUtils.touch "/tmp/ro_out"
  @tmp_path ||= "/tmp/ro_out"
end

.warn(msg, other = nil) ⇒ Object

%w(say say_status).each do |m|

define_method(:"#{m}") do |*args|
  shell.send(:"#{m}", *args)
end

end



15
16
17
# File 'lib/ro_helpers/out.rb', line 15

def warn(msg, other=nil)
  table("WARNING", msg, other, "red")
end

.write_tmp(ctn) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ro_helpers/out.rb', line 86

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