Skip to content

Add configuration for custom colors #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,26 @@ require "super_diff/rspec"

## Configuration

### Custom colors

If you want to use something other than the default colors, you can
configure them by adding them to your test helper file
(`rails_helper` or `spec_helper`):

``` ruby
SuperDiff.configure do |config|
config.set_actual_color(:green)
config.set_expected_color(:red)
config.set_border_color(:yellow)
config.set_header_color(:yellow)
end
```

See [eight_bit_color.rb](lib/super_diff/csi/eight_bit_color.rb) for the list
of available colors.

### Diffing custom objects

As capable as this library is,
it doesn't know how to deal with every kind of object out there.
If you have a custom class,
Expand Down
7 changes: 0 additions & 7 deletions lib/super_diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,6 @@ module SuperDiff
autoload :Operations, "super_diff/operations"
autoload :RecursionGuard, "super_diff/recursion_guard"

COLORS = {
alpha: :magenta,
beta: :yellow,
border: :blue,
header: :white,
}.freeze

def self.configure
yield configuration
end
Expand Down
4 changes: 2 additions & 2 deletions lib/super_diff/colorized_document_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ def self.extended(extendee)
end

def alpha(*args, **opts, &block)
colorize(*args, **opts, fg: SuperDiff::COLORS.fetch(:alpha), &block)
colorize(*args, **opts, fg: SuperDiff.configuration.alpha_color, &block)
end

def beta(*args, **opts, &block)
colorize(*args, **opts, fg: SuperDiff::COLORS.fetch(:beta), &block)
colorize(*args, **opts, fg: SuperDiff.configuration.beta_color, &block)
end
end
end
32 changes: 32 additions & 0 deletions lib/super_diff/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ class Configuration
:extra_operation_tree_classes,
:extra_diff_formatter_classes,
:extra_inspector_classes,
:alpha_color,
:beta_color,
:border_color,
:header_color,
)

def initialize
Expand All @@ -14,6 +18,10 @@ def initialize
@extra_operation_tree_classes = [].freeze
@extra_diff_formatter_classes = [].freeze
@extra_inspector_classes = [].freeze
@alpha_color = :magenta
@beta_color = :yellow
@border_color = :blue
@header_color = :white
end

def add_extra_differ_classes(*classes)
Expand Down Expand Up @@ -56,5 +64,29 @@ def add_extra_inspector_classes(*classes)
:add_extra_inspector_class,
:add_extra_inspector_classes,
)

def set_alpha_color(color)
@alpha_color = color
end
alias_method(
:set_expected_color,
:set_alpha_color
)

def set_beta_color(color)
@beta_color = color
end
alias_method(
:set_actual_color,
:set_beta_color
)

def set_border_color(color)
@border_color = color
end

def set_header_color(color)
@header_color = color
end
end
end
4 changes: 2 additions & 2 deletions lib/super_diff/rspec/matcher_text_builders/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ def add_extra_after_error
end

def beta_color
SuperDiff::COLORS.fetch(:beta)
SuperDiff.configuration.beta_color
end

def alpha_color
SuperDiff::COLORS.fetch(:alpha)
SuperDiff.configuration.alpha_color
end

private
Expand Down
22 changes: 11 additions & 11 deletions lib/super_diff/rspec/monkey_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -272,34 +272,34 @@ def self.from(expected)
return expected if self === expected

text =
colorizer.wrap("Diff:", SuperDiff::COLORS.fetch(:header)) +
colorizer.wrap("Diff:", SuperDiff.configuration.header_color) +
"\n\n" +
colorizer.wrap(
"┌ (Key) ──────────────────────────┐",
SuperDiff::COLORS.fetch(:border)
SuperDiff.configuration.border_color
) +
"\n" +
colorizer.wrap("│ ", SuperDiff::COLORS.fetch(:border)) +
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
colorizer.wrap(
"‹-› in expected, not in actual",
SuperDiff::COLORS.fetch(:alpha)
SuperDiff.configuration.alpha_color
) +
colorizer.wrap(" │", SuperDiff::COLORS.fetch(:border)) +
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
"\n" +
colorizer.wrap("│ ", SuperDiff::COLORS.fetch(:border)) +
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
colorizer.wrap(
"‹+› in actual, not in expected",
SuperDiff::COLORS.fetch(:beta)
SuperDiff.configuration.beta_color
) +
colorizer.wrap(" │", SuperDiff::COLORS.fetch(:border)) +
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
"\n" +
colorizer.wrap("│ ", SuperDiff::COLORS.fetch(:border)) +
colorizer.wrap("│ ", SuperDiff.configuration.border_color) +
"‹ › in both expected and actual" +
colorizer.wrap(" │", SuperDiff::COLORS.fetch(:border)) +
colorizer.wrap(" │", SuperDiff.configuration.border_color) +
"\n" +
colorizer.wrap(
"└─────────────────────────────────┘",
SuperDiff::COLORS.fetch(:border)
SuperDiff.configuration.border_color
)

new([[expected, text]])
Expand Down