Class: Blab::Printer

Inherits:
Object
  • Object
show all
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
[
  :class_name,
  :event,
  :method_name,
  :time
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#configObject (readonly)

Returns the value of attribute config.



20
21
22
# File 'lib/blab/printer.rb', line 20

def config
  @config
end

#loggerObject (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(options= {})
  file  = options[:file]
  line  = options[:line]
  width = options[: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(options = {})
  file  = options[:file]
  line  = options[:line]
  width = options[:width] || DEFAULT_FILE_LINES_WIDTH
  ["#{file}:#{line}".scan(/.{#{width}}|.+/), width]
end

#files_mapObject



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


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/blab/printer.rb', line 27

def print(options = {})
  strings = config.map do |(type, width)|
    send(type, options.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_filesObject



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