local euro_price_augend = augend.user.new({
find = function(text, pos)
local pattern = "(%d+)%.(%d%d)€"
local s, e = text:find(pattern, pos)
if s then
return { s = s, e = e, text = text:sub(s, e) }
else
return nil
end
end,
add = function(text, addend, cursor)
local whole, fractional = text:match("(%d+)%.(%d%d)€")
whole = tonumber(whole)
fractional = tonumber(fractional)
if cursor <= #whole then
-- Cursor is on the whole part, increment whole part
whole = whole + addend
if whole < 0 then
whole = 0
end
fractional = 0
else
-- Cursor is on the fractional part, increment fractional part
fractional = (fractional + addend) % 100
if fractional < 0 then
fractional = fractional + 100
end
end
local new_text = string.format("%d.%02d€", whole, fractional)
return { text = new_text, cursor = #new_text - 1 }
end,
})
local price_eur_augend = augend.user.new({
find = require("dial.augend.common").find_pattern("(%d+)%.(%d%d)€"),
add = function(text, addend, cursor)
local euros, cents = text:match("(%d+)%.(%d%d)€")
local total_cents = tonumber(euros) * 100 + tonumber(cents)
-- Get the start position of the match
local match_start = text:find("(%d+)%.(%d%d)€")
-- Calculate relative cursor position within the match
local relative_cursor = tonumber(cursor) - match_start + 1
-- Find the decimal point position within the match
local decimal_pos = #euros + 1
-- If cursor is in euros part
print("cursor:", cursor)
print("match_start:", match_start)
print("relative_cursor:", relative_cursor)
print("decimal_pos:", decimal_pos)
if relative_cursor <= decimal_pos then
total_cents = total_cents + (addend * 100)
-- If cursor is in cents part
else
total_cents = total_cents + addend
end
-- Format the new price
local new_euros = math.floor(total_cents / 100)
local new_cents = total_cents % 100
local new_text = string.format("%d.%02d€", new_euros, new_cents)
return {
text = new_text,
}
end,
})