Skip to content

Commit

Permalink
Make library work with --enable-string-literal
Browse files Browse the repository at this point in the history
even for ancient rubies. Fixes #38.
  • Loading branch information
flori committed Aug 1, 2024
1 parent 429f14c commit eac5eea
Show file tree
Hide file tree
Showing 11 changed files with 29 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .all_images.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ script: &script |-
ruby -v
echo -e "\e[0m"
bundle
rake test
rake test RUBYOPT="--enable-frozen-string-literal --debug-frozen-string-literal"

images:
ruby:3.3-alpine: *script
Expand Down
1 change: 0 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ GemHadar do
executables.merge Dir['bin/*'].map { |x| File.basename(x) }

dependency 'tins', '~>1.0'
dependency 'mize', '~>0.5'
development_dependency 'simplecov'
development_dependency 'test-unit'
development_dependency 'utils'
Expand Down
6 changes: 3 additions & 3 deletions lib/term/ansicolor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,14 @@ def uncolor(string = nil) # :yields:
elsif respond_to?(:to_str)
to_str.gsub(COLORED_REGEXP, '')
else
''
''.dup
end.extend(Term::ANSIColor)
end

alias uncolored uncolor

def apply_code(code, string = nil, &block)
result = ''
result = ''.dup
result << "\e[#{code}m" if Term::ANSIColor.coloring?
if block_given?
result << yield.to_s
Expand Down Expand Up @@ -112,7 +112,7 @@ def on_color(name, string = nil, &block)

class << self
# Returns an array of all Term::ANSIColor attributes as symbols.
memoize method: def term_ansicolor_attributes
def term_ansicolor_attributes
::Term::ANSIColor::Attribute.attributes.map(&:name)
end

Expand Down
3 changes: 0 additions & 3 deletions lib/term/ansicolor/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ def self.get(name)
end

class << self
memoize method:
def rgb_colors(options = {}, &block)
colors = attributes.select(&:rgb_color?)
if options.key?(:gray) && !options[:gray]
Expand All @@ -59,12 +58,10 @@ def rgb_colors(options = {}, &block)
colors.each(&block)
end

memoize method:
def rgb_foreground_colors(options = {}, &block)
rgb_colors(options).reject(&:background?).each(&block)
end

memoize method:
def rgb_background_colors(options = {}, &block)
rgb_colors(options).select(&:background?).each(&block)
end
Expand Down
2 changes: 1 addition & 1 deletion lib/term/ansicolor/hyperlink.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def hyperlink(link, string = nil, id: nil, as_link: false)
end
result = ''
if Term::ANSIColor.coloring?
result = "\e]8;#{"id=#{id}" unless id.nil?};" << link.to_str << "\e\\"
result = "\e]8;#{"id=#{id}" unless id.nil?};".dup << link.to_str << "\e\\"
end
if block_given?
result << yield.to_s
Expand Down
1 change: 1 addition & 0 deletions lib/term/ansicolor/movement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def hide_cursor(string = nil, &block)
private

def move_command(move, string = nil)
move = move.dup
if block_given?
move << yield.to_s
elsif string.respond_to?(:to_str)
Expand Down
40 changes: 20 additions & 20 deletions lib/term/ansicolor/ppm_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ class PPMReader
def initialize(io, options = {})
@io = io
@options = options
@buffer = ''
@true_coloring = options[:true_coloring]
@buffer = ''.dup
if options[:true_coloring]
@color = -> pixel { on_color Attribute.true_color(pixel, @options) }
else
@color = -> pixel { on_color Attribute.nearest_rgb_color(pixel, @options) }
end
end

def reset_io
Expand All @@ -18,36 +22,32 @@ def reset_io
parse_header
end

def each_row
def rows
reset_io
@height.times do
yield parse_row

Enumerator.new do |yielder|
@height.times do
yielder.yield parse_row
end
end
end

def to_a
enum_for(:each_row).to_a
rows.to_a
end

def to_s
result = ''
each_row do |row|
rows.map do |row|
last_pixel = nil
for pixel in row
row.map do |pixel|
if pixel != last_pixel
color = if @true_coloring
Attribute.true_color(pixel, @options)
else
Attribute.nearest_rgb_color(pixel, @options)
end
result << on_color(color)
last_pixel = pixel
@color.(pixel) << ' '
else
' '
end
result << ' '
end
result << reset << "\n"
end
result
end.join << reset << ?\n
end.join
end

private
Expand Down
4 changes: 1 addition & 3 deletions lib/term/ansicolor/rgb_triple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ def gray?
end

def html
s = '#'
@values.each { |c| s << '%02x' % c }
s
'#%02x%02x%02x' % @values
end

def css(percentage: false)
Expand Down
1 change: 0 additions & 1 deletion term-ansicolor.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,4 @@ Gem::Specification.new do |s|
s.add_development_dependency(%q<test-unit>.freeze, [">= 0".freeze])
s.add_development_dependency(%q<utils>.freeze, [">= 0".freeze])
s.add_runtime_dependency(%q<tins>.freeze, ["~> 1.0".freeze])
s.add_runtime_dependency(%q<mize>.freeze, ["~> 0.5".freeze])
end
2 changes: 1 addition & 1 deletion tests/ansicolor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ def test_coloring_string_like
end

def test_frozen
string = 'foo'
string = 'foo'.dup
red = string.red
string.extend(Term::ANSIColor).freeze
assert string.frozen?
Expand Down
2 changes: 1 addition & 1 deletion tests/hyperlink_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_hyperlink_block_arg
end

def test_with_stringy_self
string = 'foo'
string = 'foo'.dup
string.extend Term::ANSIColor
assert_equal "\e]8;;#@link\e\\foo\e]8;;\e\\", string.hyperlink(@link)
end
Expand Down

0 comments on commit eac5eea

Please sign in to comment.