summaryrefslogtreecommitdiff
path: root/lib/pry/commands/save_file.rb
blob: 6d3ab68905d164500038eb282c2f47c2b5971844 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
require 'pry/commands/code_collector'

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_)
        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