Sha256: 619b75472fa169c44fc604a2c8a55fc0c8684ad5bf405ec480a8f0f36d22b248

Contents?: true

Size: 1.42 KB

Versions: 253

Compression:

Stored size: 1.42 KB

Contents

require 'pry/commands/code_collector'

class Pry
  class Command::SaveFile < Pry::ClassCommand
    match 'save-file'
    group 'Input and Output'
    description 'Export to a file using content from the REPL.'

    banner <<-'BANNER'
      Usage: save-file [OPTIONS] --to [FILE]

      Export to a file using content from the REPL.

      save-file my_method --to hello.rb
      save-file -i 1..10 --to hello.rb --append
      save-file show-method --to my_command.rb
      save-file sample_file.rb --lines 2..10 --to output_file.rb
    BANNER

    def options(opt)
      CodeCollector.inject_options(opt)

      opt.on :to=,        "Specify the output file path"
      opt.on :a, :append, "Append output to file"
    end

    def process
      @cc = CodeCollector.new(args, opts, _pry_)
      raise CommandError, "Found no code to save." if @cc.content.empty?

      if !file_name
        display_content
      else
        save_file
      end
    end

    def file_name
      opts[:to] || nil
    end

    def save_file
      File.open(file_name, mode) do |f|
        f.puts @cc.content
      end
      output.puts "#{file_name} successfully saved"
    end

    def display_content
      output.puts @cc.content
      output.puts "\n\n--\nPlease use `--to FILE` to export to a file."
      output.puts "No file saved!\n--"
    end

    def mode
      opts.present?(:append) ? "a" : "w"
    end
  end

  Pry::Commands.add_command(Pry::Command::SaveFile)
end

Version data entries

253 entries across 246 versions & 44 rubygems

Version Path
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.19.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.18.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.17.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.16.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.15.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.14.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.13 vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.12 vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.11.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb
logstash-output-scalyr-0.1.10.beta vendor/bundle/jruby/2.5.0/gems/pry-0.10.4-java/lib/pry/commands/save_file.rb