summaryrefslogtreecommitdiff
path: root/lib/pry/commands/ls/formatter.rb
blob: c5163ca57d0495cedfdbe4093691b249f554b2fa (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
# frozen_string_literal: true

class Pry
  class Command
    class Ls < Pry::ClassCommand
      class Formatter
        attr_writer :grep
        attr_reader :pry_instance

        def initialize(pry_instance)
          @pry_instance = pry_instance
          @target = pry_instance.current_context
          @default_switch = nil
        end

        def write_out
          return false unless correct_opts?

          output_self
        end

        private

        def color(type, str)
          Pry::Helpers::Text.send pry_instance.config.ls.send("#{type}_color"), str
        end

        # Add a new section to the output.
        # Outputs nothing if the section would be empty.
        def output_section(heading, body)
          return '' if body.compact.empty?

          fancy_heading = Pry::Helpers::Text.bold(color(:heading, heading))
          Pry::Helpers.tablify_or_one_line(fancy_heading, body, @pry_instance.config)
        end

        def format_value(value)
          Pry::ColorPrinter.pp(value, '')
        end

        def correct_opts?
          @default_switch
        end

        def output_self
          raise NotImplementedError
        end

        def grep
          @grep || proc { |x| x }
        end
      end
    end
  end
end