Closed mcchrish closed 2 years ago
Hmm.
hsl
types will convert to hex when they're coerced into strings, so I would opt to call tostring
in apply_template automatically. Any value you pass in you explicitly want as a string in the template, and this lets other types with a tostring
work automatically too.
local function apply_template(template, map)
-- ensure that each replacement value is a string
local replacements = {}
for key, val in pairs(map) do
replacements[key] = tostring(val)
end
local output = string.gsub(template, "$([%w%d_]+)", replacements)
return output
end
This does mean if you pass in a table you'll get table 0xdeadbeef
instead of a lua invalid replacement
error, but I think it's probably the right step anyway.
Wonder if it's worth checking for table|function 0x\d+
as a value and warning. People who want that value explicitly could pass that in as a string if they really want it. Sometimes that kind of "smarts" can be annoying though, especially if it's applied inconsistently.
Fixed (?) in https://github.com/rktjmp/shipwright.nvim/commit/114c3a1a7260d6320b7141e25a3833275575ca9d
Not sure if it's still worth having a to_hex
transform if having templates work automatically probably covers most use cases?
If there was some other internal transform you had to do, I think you'd be operating at a point where you're already looping a whole spec and can just add .hex
to that function, or it might be working automatically if you're doing some kind of Normal.fg .. "-something-something"
?
Not a no, but a concrete usecase might be needed.
Not sure if it's still worth having a
to_hex
transform if having templates work automatically probably covers most use cases?
No need I think and the current solution is better imo.
Most of the transforms in shipwright and also most terminal configs uses hex anyway so it's handy to have something to transform the whole specs into a table of hex values. An alternative to doing
.hex
all the time: https://github.com/mcchrish/zenbones.nvim/blob/72959ba5153818b6c02d108fe9c482467a9e6362/lua/zenbones/shipwright/runners/lightline.lua#L63-L82