Skip to content

Respect RSpec color mode when applicable #261

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 6 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Remove unused Csi methods
It's hard to keep Csi.color_enabled and
SuperDiff::Configuration.color_enabled in sync. But Csi.color_enabled is
only serving Csi.colorize, which in turn only exists for
Csi.inspect_colors_in, which is apparently unused elsewhere in this
project.

Since Csi is a library private to this project, we can remove them.
  • Loading branch information
jas14 committed Sep 21, 2024
commit 00158ac407b214ac788e4a837771119692d92d45
1 change: 0 additions & 1 deletion lib/super_diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ def self.const_missing(missing_const_name)

def self.configure
yield configuration
configuration.updated
end

def self.configuration
Expand Down
6 changes: 0 additions & 6 deletions lib/super_diff/core/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,6 @@ def merge!(configuration_or_options)
end

options.each { |key, value| instance_variable_set("@#{key}", value) }

updated
end

def updated
SuperDiff::Csi.color_enabled = color_enabled?
end

def add_extra_diff_formatter_classes(*classes)
Expand Down
2 changes: 1 addition & 1 deletion lib/super_diff/core/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Helpers
# TODO: Simplify this
def style(*args, color_enabled: true, **opts, &block)
klass =
if color_enabled && Csi.color_enabled?
if color_enabled && SuperDiff.configuration.color_enabled?
Csi::ColorizedDocument
else
Csi::UncolorizedDocument
Expand Down
32 changes: 0 additions & 32 deletions lib/super_diff/csi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,48 +12,16 @@ module Csi
autoload :TwentyFourBitColor, "super_diff/csi/twenty_four_bit_color"
autoload :UncolorizedDocument, "super_diff/csi/uncolorized_document"

class << self
attr_writer :color_enabled
end

def self.reset_sequence
ResetSequence.new
end

def self.color_enabled?
@color_enabled
end

def self.colorize(*args, **opts, &block)
if color_enabled?
ColorizedDocument.new(*args, **opts, &block)
else
UncolorizedDocument.new(*args, **opts, &block)
end
end

def self.decolorize(text)
text.gsub(/\e\[\d+(?:;\d+)*m(.+?)\e\[0m/, '\1')
end

def self.already_colorized?(text)
text.match?(/\e\[\d+m/)
end

def self.inspect_colors_in(text)
[FourBitColor, EightBitColor, TwentyFourBitColor].reduce(
text
) do |str, klass|
klass.sub_colorized_areas_in(str) do |area, color|
color_block = colorize("◼︎", color.to_foreground)

layer_indicator = (color.foreground? ? "(fg)" : "(bg)")

"#{color_block} #{layer_indicator} ❮#{area}❯"
end
end
end

self.color_enabled = false
end
end