@@ -3,42 +3,51 @@ module IRB
33
44 module Command
55 class Measure < Base
6- include RubyArgsExtractor
7-
86 category "Misc"
9- description "`measure` enables the mode to measure processing time. `measure :off` disables it."
10-
11- def initialize ( *args )
12- super ( *args )
13- end
7+ description "`measure` enables the mode to measure processing time. `measure off` disables it."
148
159 def execute ( arg )
1610 if arg &.match? ( /^do$|^do[^\w ]|^\{ / )
1711 warn 'Configure IRB.conf[:MEASURE_PROC] to add custom measure methods.'
1812 return
1913 end
20- args , kwargs = ruby_args ( arg )
21- execute_internal ( *args , **kwargs )
14+
15+ if arg . empty?
16+ execute_internal ( nil , nil )
17+ elsif arg . start_with? ':'
18+ # Legacy style `measure :stackprof`, `measure :off, :time`
19+ type , arg_val = arg . split ( /,\s */ , 2 ) . map { |v | v . sub ( /\A :/ , '' ) }
20+ warn "`measure #{ arg } ` is deprecated. Please use `measure #{ [ type , arg_val ] . compact . join ( ' ' ) } ` instead."
21+ execute_internal ( type . to_sym , arg_val )
22+ else
23+ type , arg_val = arg . split ( /\s +/ , 2 )
24+ execute_internal ( type . to_sym , arg_val )
25+ end
2226 end
2327
24- def execute_internal ( type = nil , arg = nil )
28+ def execute_internal ( type , arg )
2529 # Please check IRB.init_config in lib/irb/init.rb that sets
2630 # IRB.conf[:MEASURE_PROC] to register default "measure" methods,
27- # "measure : time" (abbreviated as "measure") and "measure : stackprof".
31+ # "measure time" (abbreviated as "measure") and "measure stackprof".
2832
2933 case type
3034 when :off
31- IRB . unset_measure_callback ( arg )
35+ IRB . unset_measure_callback ( arg &. to_sym )
3236 when :list
3337 IRB . conf [ :MEASURE_CALLBACKS ] . each do |type_name , _ , arg_val |
3438 puts "- #{ type_name } " + ( arg_val ? "(#{ arg_val . inspect } )" : '' )
3539 end
36- when :on
37- added = IRB . set_measure_callback ( arg )
38- puts "#{ added [ 0 ] } is added." if added
3940 else
40- added = IRB . set_measure_callback ( type , arg )
41- puts "#{ added [ 0 ] } is added." if added
41+ type , arg = arg &.to_sym , nil if type == :on
42+
43+ measure_methods = IRB . conf [ :MEASURE_PROC ] . keys . map ( &:downcase )
44+ if type && !measure_methods . include? ( type )
45+ puts "Measure method `#{ type } ` not found."
46+ puts "Available measure methods: %w[#{ measure_methods . join ( ' ' ) } ]."
47+ else
48+ added = IRB . set_measure_callback ( type &.to_sym , arg )
49+ puts "#{ added [ 0 ] } is added." if added
50+ end
4251 end
4352 nil
4453 end
0 commit comments