Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
ttscoff committed Feb 6, 2023
1 parent 9e36065 commit e28d1b8
Showing 1 changed file with 20 additions and 19 deletions.
39 changes: 20 additions & 19 deletions IncrementTemplated.popclipext/script.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,25 @@
NUMERIC_RX = /(\?\:)?##(\d+)(?:\.\.(\d+))?\.\.(\d+)##/.freeze
ARRAY_RX = /##(.*?)##/.freeze

def get_modifiers(input)
input.scan(%r{##(([ix0-9()+\-/*%]+)*)(#([^#]+))?##}).map do
m = Regexp.last_match
padding = if m[2].nil?
'%d'
else
t = m[2].match(/\b(0+)([1-9]\d*)?/)
t.nil? || m[2] =~ /^0$/ ? '%d' : "%0#{t[0].length}d"
end

options_array = m[3].nil? ? nil : m[4].split(/,/).map(&:strip)

base = m[1].nil? ? 'x' : m[1]
inc = m[2].nil? ? '' : m[2].gsub(/\b(0+\d+)/, &:to_i)

[Regexp.escape(m[0]), inc, padding, base, options_array]
end
end

def process_array(input)
template = input.match(ARRAY_RX)
replacements = template[1].split(/,/).map(&:strip)
Expand All @@ -32,25 +51,6 @@ def process_array(input)
output.join("\n")
end

def get_modifiers(input)
input.scan(%r{##(([ix0-9()+\-/*%]+)*)(#([^#]+))?##}).map do
m = Regexp.last_match
padding = if m[2].nil?
'%d'
else
t = m[2].match(/\b(0+)([1-9]\d*)?/)
t.nil? || m[2] =~ /^0$/ ? '%d' : "%0#{t[0].length}d"
end

options_array = m[3].nil? ? nil : m[4].split(/,/).map(&:strip)

base = m[1].nil? ? 'x' : m[1]
inc = m[2].nil? ? '' : m[2].gsub(/\b(0+\d+)/, &:to_i)

[Regexp.escape(m[0]), inc, padding, base, options_array]
end
end

def process_numeric(input)
template = input.match(NUMERIC_RX)
disp = template[1].nil? ? true : false
Expand All @@ -76,6 +76,7 @@ def process_numeric(input)
out = input.sub(/#{Regexp.escape(template[0])}/, replacement)
modified.each do |mod|
next if mod.nil?

equat = mod[3].gsub(/\b0+/, '').gsub(/x/, (idx + 1).to_s).gsub(/i/, idx.to_s)

if mod[4]
Expand Down

0 comments on commit e28d1b8

Please sign in to comment.