summaryrefslogtreecommitdiff
path: root/lib/irb/command/measure.rb
blob: ee7927b010bdc0a8c38c3cad57abc5d5bfe4177e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module IRB
  # :stopdoc:

  module Command
    class Measure < Base
      category "Misc"
      description "`measure` enables the mode to measure processing time. `measure :off` disables it."

      def initialize(*args)
        super(*args)
      end

      def execute(type = nil, arg = nil)
        # Please check IRB.init_config in lib/irb/init.rb that sets
        # IRB.conf[:MEASURE_PROC] to register default "measure" methods,
        # "measure :time" (abbreviated as "measure") and "measure :stackprof".

        if block_given?
          warn 'Configure IRB.conf[:MEASURE_PROC] to add custom measure methods.'
          return
        end

        case type
        when :off
          IRB.unset_measure_callback(arg)
        when :list
          IRB.conf[:MEASURE_CALLBACKS].each do |type_name, _, arg_val|
            puts "- #{type_name}" + (arg_val ? "(#{arg_val.inspect})" : '')
          end
        when :on
          added = IRB.set_measure_callback(arg)
          puts "#{added[0]} is added." if added
        else
          added = IRB.set_measure_callback(type, arg)
          puts "#{added[0]} is added." if added
        end
        nil
      end
    end
  end

  # :startdoc:
end