Skip to content

Commit

Permalink
Moving display width args slowly to kwargs
Browse files Browse the repository at this point in the history
  • Loading branch information
janlelis committed Nov 12, 2024
1 parent e794216 commit a37b736
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
31 changes: 19 additions & 12 deletions lib/unicode/display_width.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,26 @@ class DisplayWidth
EMOJI_NOT_POSSIBLE = /\A[#*0-9]\z/

# Returns monospace display width of string
def self.of(string, ambiguous = 1, overwrite = {}, options = { emoji: DEFAULT_EMOJI_SET })
if ambiguous != 1 && ambiguous != 2
def self.of(string, ambiguous = nil, overwrite = nil, options = { ambiguous: 1, overwrite: {}, emoji: DEFAULT_EMOJI_SET })
options[:ambiguous] = ambiguous if ambiguous

if options[:ambiguous] != 1 && options[:ambiguous] != 2
raise ArgumentError, "Unicode::DisplayWidth: ambiguous width must be 1 or 2"
end

if !overwrite.empty?
return width_frame(string, options.merge(ambiguous: ambiguous)) do |string, index_full, index_low, first_ambiguous|
width_all_features(string, index_full, index_low, first_ambiguous, overwrite)
if overwrite
# warn "Unicode::DisplayWidth: Deprecated, please use overwrite: {} keyword options instead of passing overwrites as third parameter"
options[:overwrite] = overwrite
end

if !options[:overwrite].empty?
return width_frame(string, options) do |string, index_full, index_low, first_ambiguous|
width_all_features(string, index_full, index_low, first_ambiguous, options[:overwrite])
end
end

if !string.ascii_only?
return width_frame(string, options.merge(ambiguous: ambiguous)) do |string, index_full, index_low, first_ambiguous|
return width_frame(string, options) do |string, index_full, index_low, first_ambiguous|
width_no_overwrite(string, index_full, index_low, first_ambiguous)
end
end
Expand Down Expand Up @@ -205,15 +212,15 @@ def initialize(ambiguous: 1, overwrite: {}, emoji: DEFAULT_EMOJI_SET)
end

def get_config(**kwargs)
[
kwargs[:ambiguous] || @ambiguous,
kwargs[:overwrite] || @overwrite,
{ emoji: kwargs[:emoji] || @emoji },
]
{
ambiguous: kwargs[:ambiguous] || @ambiguous,
overwrite: kwargs[:overwrite] || @overwrite,
emoji: kwargs[:emoji] || @emoji,
}
end

def of(string, **kwargs)
self.class.of(string, *get_config(**kwargs))
self.class.of(string, nil, nil, get_config(**kwargs))
end
end
end
Expand Down
6 changes: 5 additions & 1 deletion spec/display_width_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,9 +170,13 @@


describe '[overwrite]' do
it 'can be passed a 3rd parameter with overwrites' do
it 'can be passed a 3rd parameter with overwrites (old format)' do
expect( "\t".display_width(1, 0x09 => 12) ).to eq 12
end

it 'can be passed as :overwrite option' do
expect( "\t".display_width(1, nil, overwrite: { 0x09 => 12 }) ).to eq 12
end
end

describe '[encoding]' do
Expand Down

0 comments on commit a37b736

Please sign in to comment.