Class: Rblineprof::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/rblineprof/browser.rb,
lib/rblineprof/browser/version.rb

Constant Summary collapse

VERSION =
"1.0.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile) ⇒ Browser

Returns a new instance of Browser.



19
20
21
# File 'lib/rblineprof/browser.rb', line 19

def initialize(profile)
  @profile = profile
end

Instance Attribute Details

#profileObject (readonly)

Returns the value of attribute profile.



8
9
10
# File 'lib/rblineprof/browser.rb', line 8

def profile
  @profile
end

Class Method Details

.from_lineprof(regex = /./, &block) ⇒ Object



10
11
12
13
# File 'lib/rblineprof/browser.rb', line 10

def self.from_lineprof(regex = /./, &block)
  profile = lineprof(regex, &block)
  new(profile)
end

.profile_and_browse(regex = /./, &block) ⇒ Object



15
16
17
# File 'lib/rblineprof/browser.rb', line 15

def self.profile_and_browse(regex = /./, &block)
  from_lineprof(regex, &block).browse
end

Instance Method Details

#browseObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rblineprof/browser.rb', line 49

def browse
  done = false
  while !done do
    highline.choose do |menu|
      menu.prompt = "Choose a file to view the profile of: "

      profile.keys.sort.each do |filename|
        menu.choice(filename) { print_profile(filename) }
      end

      menu.choice("Quit") { done = true }
    end
  end
end

#highlight(fn) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/rblineprof/browser.rb', line 35

def highlight(fn)
  begin
    Pygments.highlight(File.read(fn), :lexer => 'ruby', :formatter => 'terminal').split("\n")
  rescue
    (0..profile[fn].size).map { |i| "File not found: #{fn}" }
  end
end


23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rblineprof/browser.rb', line 23

def print_profile(fn)
  highlight(fn).each_with_index do |line, num|
    if (sample = profile[fn][num+1]) && (sample[0] > 0)
      puts "% 8.1fms, % 8.1fms excl | %s" % [sample[0]/1000.0, sample[2] / 1000.0, line]
    else
      puts "                            | %s" % [line]
    end
  end

  nil
end

#usageObject



43
44
45
46
47
# File 'lib/rblineprof/browser.rb', line 43

def usage
  puts "Usage: print_profile(filename)"
  puts "Filenames:"
  profile.keys.sort.map { |fn| " - #{fn}" }.each { |line| puts line }
end