Sha256: aa8ad92c52761643f6f4d557b5b0d10b51da26610cdccefc0dc9cda936d8a15f

Contents?: true

Size: 1.52 KB

Versions: 6509

Compression:

Stored size: 1.52 KB

Contents

# frozen_string_literal: true

class Pry
  class Command
    class 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_instance)
        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
end

Version data entries

6,509 entries across 6,505 versions & 30 rubygems

Version Path
ory-client-0.0.1.alpha41 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
ory-client-0.0.1.alpha40 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
ory-client-0.0.1.alpha39 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
ory-client-0.0.1.alpha38 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
ory-client-0.0.1.alpha37 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
ory-client-0.0.1.alpha36 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
ory-kratos-client-0.8.2.alpha1 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
ory-client-0.0.1.alpha31 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
primary_connect_proto-0.4.0 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
primary_connect_proto-0.3.0 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
primary_connect_proto-0.2.0 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
primary_connect_proto-0.1.0 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
ory-client-0.0.1.alpha30 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
ory-client-0.0.1.alpha29 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
primary_connect_proto-0.0.6 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
ory-client-0.0.1.alpha28 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
primary_connect_proto-0.0.5 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
ory-client-0.0.1.alpha27 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
ory-client-0.0.1.alpha24 vendor/bundle/ruby/2.5.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb
primary_connect_proto-0.0.4 vendor/bundle/ruby/2.6.0/gems/pry-0.13.1/lib/pry/commands/save_file.rb