Class: Blab::Printer
- Inherits:
-
Object
- Object
- Blab::Printer
- Defined in:
- lib/blab/printer.rb
Constant Summary collapse
- DEFAULT_CLASS_NAME_WIDTH =
5
- DEFAULT_CODE_LINES_WIDTH =
120
- DEFAULT_EVENT_WIDTH =
6
- DEFAULT_FILE_LINES_WIDTH =
60
- DEFAULT_RU_MAXSS_WIDTH =
50
- DEFAULT_METHOD_NAME_WIDTH =
10
- DEFAULT_TIME_WIDTH =
12
- PRINT_FIELDS =
[ :class_name, :event, :method_name, :time ].freeze
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
Instance Method Summary collapse
- #code_lines(options = {}) ⇒ Object
- #file_lines(options = {}) ⇒ Object
- #files_map ⇒ Object
-
#initialize(config, logger) ⇒ Printer
constructor
A new instance of Printer.
- #print(options = {}) ⇒ Object
- #reset_files ⇒ Object
-
#source_line(file, line) ⇒ Object
TODO: show all relevant file-lines.
Constructor Details
#initialize(config, logger) ⇒ Printer
Returns a new instance of Printer.
22 23 24 25 |
# File 'lib/blab/printer.rb', line 22 def initialize(config, logger) @config = config @logger = logger end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
20 21 22 |
# File 'lib/blab/printer.rb', line 20 def config @config end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
20 21 22 |
# File 'lib/blab/printer.rb', line 20 def logger @logger end |
Instance Method Details
#code_lines(options = {}) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/blab/printer.rb', line 50 def code_lines(= {}) file = [:file] line = [:line] width = [:width] || DEFAULT_CODE_LINES_WIDTH [source_line(file, line).scan(/.{#{width}}|.+/), width] end |
#file_lines(options = {}) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/blab/printer.rb', line 43 def file_lines( = {}) file = [:file] line = [:line] width = [:width] || DEFAULT_FILE_LINES_WIDTH ["#{file}:#{line}".scan(/.{#{width}}|.+/), width] end |
#files_map ⇒ Object
69 70 71 72 73 |
# File 'lib/blab/printer.rb', line 69 def files_map @files_map ||= Hash.new do |h, f| h[f] = File.readlines(f) end end |
#print(options = {}) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/blab/printer.rb', line 27 def print( = {}) strings = config.map do |(type, width)| send(type, .merge(width: width)) end config_length = config.length final = strings.map { |e| e.first.length }.max.times.map do |i| config_length.times.map do |j| str = strings[j][0][i] || "" # TODO: do not ljust the last element config_length == (j + 1) ? str : str.ljust(strings[j][1]) end.join(" ") end logger.info(final.join("\n")) end |
#reset_files ⇒ Object
65 66 67 |
# File 'lib/blab/printer.rb', line 65 def reset_files @files_map && @files_map.keys.each { |key| @files_map.delete(key) } end |
#source_line(file, line) ⇒ Object
TODO: show all relevant file-lines
76 77 78 79 80 81 82 |
# File 'lib/blab/printer.rb', line 76 def source_line(file, line) begin files_map[file][line - 1] rescue "source is unavailable" end end |