Module: PryMacro
- Defined in:
- lib/pry-macro.rb,
lib/pry-macro/version.rb
Defined Under Namespace
Classes: MacroString
Constant Summary collapse
- Commands =
Pry::CommandSet.new do create_command 'record', 'Starts a recording session' do " Usage: record\n\n Starts recording a macro.\n BANNER\n\n def process\n # Define a few extra ivars on the current pry session so we can persist some state to use\n unless _pry_.instance_variable_defined?(:@record_stack)\n _pry_.instance_variable_set(:@record_stack, [])\n _pry_.instance_variable_set(:@macro_strings, [])\n _pry_.class.class_eval 'attr_accessor :record_stack, :macro_strings'\n end\n\n # By using a stack we can potentially have multiple sessions going. Use at your own peril though...\n _pry_.record_stack << Pry.history.session_line_count\n end\n end\n\n create_command 'stop', 'Stops a recording session, and saves a macro' do\n banner <<-BANNER\n Usage: stop [-n name] [-d desc]\n\n Stops recording a macro, loads the command, and caches it for later saving if\n desired. If no name is provided, user will be prompted for one. Descriptions \n may be provided, but will default to 'no description'\n BANNER\n\n def setup\n if !_pry_.instance_variable_defined?(:@record_stack) && _pry_.record_stack.empty?\n raise 'Cannot stop recording when no recorder is running'\n end\n\n session_begin = _pry_.record_stack.pop\n session_end = Pry.history.session_line_count\n\n # Get the history between the start and end of the recording session\n @history = \n Pry.history\n .to_a\n .last(session_end - session_begin - 1)\n .reduce(StringIO.new) { |io, item| io.puts(item); io }\n end\n\n def options(opts)\n opts.on :n, :name, \"Name to use to define the macro\",\n optional_argument: true, as: String\n opts.on :d, :desc, \"Description of the macro\",\n optional_argument: true, as: String\n opts.on :v, :verbose, \"Echoes back the macro definition\"\n end\n\n def process\n # Have to have a name to execute this later\n name = opts[:n] || ask('Macro Name: ')\n desc = opts[:d] || 'no description'\n\n history_lines = @history.string.lines.map { |s| \" \#{s}\"}.join.chomp.tap { |h|\n h.sub!(/^ {6}/,'') # First line won't need the spacing\n }\n\n # Save the command into a string, and make it look decent\n # Tinge of a heredocs hack\n command_string = <<-COMMAND_STRING.gsub(/^ {10}/, '')\n Pry::Commands.block_command '\#{name}', '\#{desc}' do\n _pry_.input = StringIO.new(\n <<-MACRO.gsub(/^ {4,6}/, '')\n \#{history_lines}\n MACRO\n )\n end\n COMMAND_STRING\n\n puts command_string if opts[:v]\n\n # ...so that we can save the contents for saving later (optional)\n _pry_.macro_strings << MacroString.new(@name, command_string)\n # ...as well as evaluating it and making it immediately usable to us.\n eval command_string\n end\n end\n\n create_command 'save-macro', 'Saves a named macro to your .pryrc file on the tail end' do\n banner <<-BANNER\n Usage: save-macro [-p path]\n\n Saves a cached macro to your ~/.pryrc or the path specified.\n BANNER\n\n def options(opts)\n opts.on :p, :path, \"Pathname to save macro in\",\n optional_argument: true, as: String\n end\n\n def process\n raise 'No Macros are defined!' unless _pry_.instance_variable_defined?(:@macro_strings)\n raise 'Invalid path!' if opts[:p] && !Dir[opts[:p]]\n raise 'Must specify the macro to save!' if args.empty?\n\n path = opts[:p] || Dir.home\n macro = _pry_.macro_strings.find(\n # If nothing is found, raise the error\n -> { raise \"Command \#{args.first} not found!\" }\n ) { |m| m.name == args.first }\n\n # Append the Pryrc with the macro, leaving blank lines\n File.open(File.join(path, '.pryrc'), 'a') { |f| f.puts '', macro.command, '' }\n end\n end\nend\n"
- VERSION =
'0.0.2'