Skip to content

Commit

Permalink
Merge pull request #209 from buty4649/colorize-for-json
Browse files Browse the repository at this point in the history
Colorize output for json
  • Loading branch information
buty4649 authored Jul 8, 2024
2 parents 877e622 + eb7f49d commit fad4ae2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 40 deletions.
20 changes: 10 additions & 10 deletions build_config.rb.lock
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ builds:
https://github.com/buty4649/mruby-yyjson.git:
url: https://github.com/buty4649/mruby-yyjson.git
branch: main
commit: 3ecdfacf5a1bacee5d28292dbaf45b5febc1fbbf
version: 1.4.0
commit: 9c39053267fd7f48fa6368fb5f07bb08780120f4
version: 1.5.0
linux-amd64:
https://github.com/fastly/mruby-optparse.git:
url: https://github.com/fastly/mruby-optparse.git
Expand Down Expand Up @@ -98,8 +98,8 @@ builds:
https://github.com/buty4649/mruby-yyjson.git:
url: https://github.com/buty4649/mruby-yyjson.git
branch: main
commit: 3ecdfacf5a1bacee5d28292dbaf45b5febc1fbbf
version: 1.4.0
commit: 9c39053267fd7f48fa6368fb5f07bb08780120f4
version: 1.5.0
linux-arm64:
https://github.com/fastly/mruby-optparse.git:
url: https://github.com/fastly/mruby-optparse.git
Expand Down Expand Up @@ -144,8 +144,8 @@ builds:
https://github.com/buty4649/mruby-yyjson.git:
url: https://github.com/buty4649/mruby-yyjson.git
branch: main
commit: 3ecdfacf5a1bacee5d28292dbaf45b5febc1fbbf
version: 1.4.0
commit: 9c39053267fd7f48fa6368fb5f07bb08780120f4
version: 1.5.0
darwin-amd64:
https://github.com/fastly/mruby-optparse.git:
url: https://github.com/fastly/mruby-optparse.git
Expand Down Expand Up @@ -226,8 +226,8 @@ builds:
https://github.com/buty4649/mruby-yyjson.git:
url: https://github.com/buty4649/mruby-yyjson.git
branch: main
commit: 3ecdfacf5a1bacee5d28292dbaf45b5febc1fbbf
version: 1.4.0
commit: 9c39053267fd7f48fa6368fb5f07bb08780120f4
version: 1.5.0
windows-amd64:
https://github.com/fastly/mruby-optparse.git:
url: https://github.com/fastly/mruby-optparse.git
Expand Down Expand Up @@ -272,5 +272,5 @@ builds:
https://github.com/buty4649/mruby-yyjson.git:
url: https://github.com/buty4649/mruby-yyjson.git
branch: main
commit: 3ecdfacf5a1bacee5d28292dbaf45b5febc1fbbf
version: 1.4.0
commit: 9c39053267fd7f48fa6368fb5f07bb08780120f4
version: 1.5.0
34 changes: 27 additions & 7 deletions mrblib/rf/00filter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ module Filter
class Json < Base
class << self
def config
@config ||= Struct.new(:raw, :boolean_mode).new.tap do |config|
@config ||= Struct.new(:raw, :boolean_mode, :minify).new.tap do |config|
config.boolean_mode = true
config.minify = false
end
end

Expand All @@ -15,6 +16,9 @@ def configure(opt)
opt.on('--disable-boolean-mode', 'consider true/false/null as json literal') do
config.boolean_mode = false
end
opt.on('-m', '--minify', 'minify json output') do
config.minify = true
end
end

def raw?
Expand All @@ -25,26 +29,42 @@ def boolean_mode?
config.boolean_mode
end

def pretty_print
!config.minify
end

def format(val, record)
case val
when String
raw? ? val : val.to_json
string_to_json(val)
when MatchData
record.to_json
record.to_json(colorize:, pretty_print:)
when Regexp
val.match(record.to_s) { record.to_json }
val.match(record.to_s) { record.to_json(colorize:, pretty_print:) }
when true, false, nil
boolean_or_nil_to_json(val, record)
else
val.to_json
val.to_json(colorize:, pretty_print:)
end
end

def string_to_json(str)
if raw?
if colorize
JSON.colorize(str, JSON.color_string)
else
str
end
else
str.to_json(colorize:, pretty_print:)
end
end

def boolean_or_nil_to_json(boolean_or_nil, record)
if boolean_mode?
record.to_json if boolean_or_nil == true
record.to_json(colorize:, pretty_print:) if boolean_or_nil == true
else
boolean_or_nil.to_json
boolean_or_nil.to_json(colorize:, pretty_print:)
end
end

Expand Down
7 changes: 0 additions & 7 deletions mrblib/rf/features.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ def add_features
add_features_to_float
add_features_to_string
add_features_to_hash
add_features_to_json
add_features_to_nil_class
end

Expand Down Expand Up @@ -89,12 +88,6 @@ def add_features_to_hash
end
end

def add_features_to_json
Object.define_method(:to_json) do
JSON.pretty_generate(self)
end
end

def add_features_to_nil_class
NilClass.define_method(:+) do |other|
if other.is_a?(Integer) || other.is_a?(Float)
Expand Down
16 changes: 0 additions & 16 deletions spec/feature/to_json_spec.rb

This file was deleted.

0 comments on commit fad4ae2

Please sign in to comment.