Sha256: d1c1c26b9c57f5b93d7e6eb60409e65bd0d5e0ff21f345fb1cb4b78bf5a28571

Contents?: true

Size: 1.92 KB

Versions: 9

Compression:

Stored size: 1.92 KB

Contents

require 'warp/dir'
require 'warp/dir/errors'
require 'colored'
require 'thread'
module Warp
  module Dir
    class Formatter
      DEFAULT_FORMAT = :ascii

      attr_accessor :store

      def initialize(store)
        @store  = store
        @config = store.config
      end

      def unhappy(exception: nil, message: nil)
        out = 'Whoops! – '.white
        out << "#{exception.message} ".red if exception && !message
        out << "#{message} ".red if !exception && message
        out << "#{exception.message}:\n#{message}".red if message && exception
        out << "\n"
        print ? STDERR.printf(out) : out
      end

      def format_point(point, *args)
        PointFormatter.new(point).format(*args)
      end

      def format_store(*args)
        StoreFormatter.new(store).format(*args)
      end

      def happy(message: nil)
        STDOUT.printf(message.blue.bold)
      end

      private

      class PointFormatter
        attr_accessor :point

        def initialize(point)
          @point = point
        end

        def format(type = DEFAULT_FORMAT, width = 0)
          case type
            when :ascii
              point.to_s(width)
            else
              raise ArgumentError.new("Type #{type} is not recognized.")
          end
        end
      end

      class StoreFormatter
        attr_accessor :store

        def initialize(store)
          @store = store
        end

        # find the widest warp point name, and indent them all based on that.
        # make it easy to extend to other types, and allow the caller to
        # sort by one of the fields.
        def format(type = DEFAULT_FORMAT, sort_field = :name)
          longest_key_length = store.points.map(&:name).map(&:length).sort.last
          Warp::Dir.sort_by(store.points, sort_field).map do |point|
            PointFormatter.new(point).format(type, longest_key_length)
          end.join("\n")
        end
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
warp-dir-1.5.0 lib/warp/dir/formatter.rb
warp-dir-1.3.0 lib/warp/dir/formatter.rb
warp-dir-1.2.0 lib/warp/dir/formatter.rb
warp-dir-1.1.5 lib/warp/dir/formatter.rb
warp-dir-1.1.4 lib/warp/dir/formatter.rb
warp-dir-1.1.3 lib/warp/dir/formatter.rb
warp-dir-1.1.2 lib/warp/dir/formatter.rb
warp-dir-1.1.1 lib/warp/dir/formatter.rb
warp-dir-1.1.0 lib/warp/dir/formatter.rb