Sha256: ebe0360769d8da7002f52eaacbe1e5b6529a0a51831a572ec87796e7ac4720d7

Contents?: true

Size: 986 Bytes

Versions: 1

Compression:

Stored size: 986 Bytes

Contents

require_relative '../../hairballs'

# When any value is returned by evaluating some Ruby, this will check if it is
# JSON (by parsing it).  If it's JSON-like, it will get formatted prettily and
# using the +color+ given when calling +Hairballs.load_plugin(:colorize_json)+.
#
# This *must* get used/loaded after the :wirble plugin to work.
#
# TODO: fix to work with using/loading before Wirble.
Hairballs.add_plugin(:colorize_json, color: :blue) do |plugin|
  plugin.libraries %w(json colorize)

  plugin.on_load do
    IRB::Irb.class_eval do
      alias_method :old_output_value, :output_value

      define_method :output_value do
        is_json = JSON.parse(@context.last_value) rescue nil

        if is_json
          vputs "[#{plugin.name}] Return value is JSON-like"
          printf @context.return_format,
            JSON.pretty_generate(JSON.parse(@context.last_value)).colorize(plugin.color)
        else
          old_output_value
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hairballs-0.0.1 lib/hairballs/plugins/colorize_json.rb