Advertisement
fatboychummy

Cursed term.blit formatting thing

Dec 18th, 2023 (edited)
588
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.46 KB | None | 0 0
  1. --- Combine values into valid blit strings, in the format of
  2. --- `a1 .. a2 .. a3, b1 .. b2 .. b3, c1 .. c2 .. c3`.
  3. --- The input values are expected to be in the format of
  4. --- `a1, b1, c1, a2, b2, c2, a3, b3, c3`.
  5. ---@vararg string|table The values to combine. Tables will be pulled apart to allow for multiple values if calling functions.
  6. ---@return string text The combined text values.
  7. ---@return string text_color The combined text color values.
  8. ---@return string bg_color The combined background color values.
  9. local function combine_blit(...)
  10.   local text = ""
  11.   local text_color = ""
  12.   local bg_color = ""
  13.  
  14.   local args = table.pack(...)
  15.  
  16.   local fixed_args = {}
  17.  
  18.   -- Rip subtables out and insert them as if they were arguments.
  19.   for i = 1, args.n do
  20.     local arg = args[i]
  21.     if type(arg) == "table" then
  22.       for j = 1, #arg do
  23.         table.insert(fixed_args, arg[j])
  24.       end
  25.     else
  26.       table.insert(fixed_args, arg)
  27.     end
  28.   end
  29.  
  30.   for i = 1, #fixed_args, 3 do
  31.     local s1, s2, s3 = fixed_args[i], fixed_args[i + 1], fixed_args[i + 2]
  32.     expect(i, s1, "string")
  33.     expect(i + 1, s2, "string")
  34.     expect(i + 2, s3, "string")
  35.  
  36.     if #s1 ~= #s2 or #s1 ~= #s3 then
  37.       error(("Bad arguments %d, %d, %d: Must be same length (got %d, %d, %d)"):format(i, i + 1, i + 2, #s1, #s2, #s3), 2)
  38.     end
  39.  
  40.     text = text .. s1
  41.     text_color = text_color .. s2
  42.     bg_color = bg_color .. s3
  43.   end
  44.  
  45.   return text, text_color, bg_color
  46. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement