Class: Fiedl::Log::Log
- Inherits:
-
Object
- Object
- Fiedl::Log::Log
- Defined in:
- lib/fiedl/log/log.rb
Instance Method Summary collapse
- #configuration(hash) ⇒ Object
-
#ensure_file(filename, options = {}) ⇒ Object
Ensure that a certain file is present.
- #error(text) ⇒ Object
- #filter(expression) ⇒ Object
- #filter_out(expression) ⇒ Object
- #head(text) ⇒ Object
- #info(text) ⇒ Object
- #p(text) ⇒ Object
- #prompt(text) ⇒ Object
- #raise(text) ⇒ Object
- #section(heading) ⇒ Object
-
#shell(command, verbose: true) ⇒ Object
Print commant, execute it and display result.
- #success(text) ⇒ Object
- #variable(variable, variable_name) ⇒ Object
- #warning(text) ⇒ Object
- #write(text) ⇒ Object
Instance Method Details
#configuration(hash) ⇒ Object
32 33 34 |
# File 'lib/fiedl/log/log.rb', line 32 def configuration(hash) pp hash end |
#ensure_file(filename, options = {}) ⇒ Object
Ensure that a certain file is present.
78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/fiedl/log/log.rb', line 78 def ensure_file(filename, = {}) if File.exists?(filename) log.success "File: #{filename}" else log.error "Something went wrong. File #{filename} is missing." if [:show_log] log.section "Last log" shell "tail -n 20 #{[:show_log]}" end raise "File is missing." end end |
#error(text) ⇒ Object
20 21 22 |
# File 'lib/fiedl/log/log.rb', line 20 def error(text) self.write text.red end |
#filter(expression) ⇒ Object
49 50 51 |
# File 'lib/fiedl/log/log.rb', line 49 def filter(expression) filter_out expression end |
#filter_out(expression) ⇒ Object
45 46 47 48 |
# File 'lib/fiedl/log/log.rb', line 45 def filter_out(expression) @filter_out ||= [] @filter_out << expression end |
#head(text) ⇒ Object
3 4 5 6 7 8 |
# File 'lib/fiedl/log/log.rb', line 3 def head(text) info "" info "==========================================================".blue info text.blue info "==========================================================".blue end |
#info(text) ⇒ Object
14 15 16 |
# File 'lib/fiedl/log/log.rb', line 14 def info(text) self.write text end |
#p(text) ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/fiedl/log/log.rb', line 38 def p(text) @filter_out ||= [] @filter_out.each do |expression| text = text.gsub(expression, "[...]") end print text end |
#prompt(text) ⇒ Object
26 27 28 |
# File 'lib/fiedl/log/log.rb', line 26 def prompt(text) self.write "$ " + text.bold end |
#raise(text) ⇒ Object
53 54 55 56 |
# File 'lib/fiedl/log/log.rb', line 53 def raise(text) self.error(text.bold) super(text) end |
#section(heading) ⇒ Object
9 10 11 12 13 |
# File 'lib/fiedl/log/log.rb', line 9 def section(heading) info "" info heading.blue info "----------------------------------------------------------".blue end |
#shell(command, verbose: true) ⇒ Object
Print commant, execute it and display result. See also: stackoverflow.com/a/10224650/2066546
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/fiedl/log/log.rb', line 61 def shell(command, verbose: true) prompt command if verbose output = "" r, io = IO.pipe pid = fork do system(command, out: io, err: io) end io.close r.each_char{|c| (print c if verbose); output += c} Process.waitpid pid return output.strip end |
#success(text) ⇒ Object
17 18 19 |
# File 'lib/fiedl/log/log.rb', line 17 def success(text) self.write text.green end |
#variable(variable, variable_name) ⇒ Object
29 30 31 |
# File 'lib/fiedl/log/log.rb', line 29 def variable(variable, variable_name) self.write "#{variable_name.to_s.blue} = #{variable}" end |
#warning(text) ⇒ Object
23 24 25 |
# File 'lib/fiedl/log/log.rb', line 23 def warning(text) self.write "Warning: ".yellow.bold + text.yellow end |
#write(text) ⇒ Object
35 36 37 |
# File 'lib/fiedl/log/log.rb', line 35 def write(text) self.p "#{text}\n" end |