Advertisement
programcreator

Printf

Oct 27th, 2015
425
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.41 KB | None | 0 0
  1. local function printf(...)
  2.      --[[
  3.         Author: Wassil Janssen a.k.a. Creator
  4.      ]]--
  5.     local clrs = {
  6.         ["0"] = colors.white,
  7.         ["1"] = colors.orange,
  8.         ["2"] = colors.magenta,
  9.         ["3"] = colors.lightBlue,
  10.         ["4"] = colors.yellow,
  11.         ["5"] = colors.lime,
  12.         ["6"] = colors.pink,
  13.         ["7"] = colors.gray,
  14.         ["8"] = colors.lightGray,
  15.         ["9"] = colors.cyan,
  16.         ["a"] = colors.purple,
  17.         ["b"] = colors.blue,
  18.         ["c"] = colors.brown,
  19.         ["d"] = colors.green,
  20.         ["e"] = colors.red,
  21.         ["f"] = colors.black,
  22.     }
  23.     local prefixes = {
  24.         ["+"] = term.setTextColor,
  25.         ["-"] = term.setBackgroundColor,
  26.     }
  27.     local str = ""
  28.     for i,v in pairs({...}) do
  29.         str = str.." "..tostring(v)
  30.     end
  31.     local pos = 1
  32.     local toPrint = ""
  33.     while true do
  34.         local skip = 0
  35.         local char = str:sub(pos,pos)
  36.         if char == "\027" then
  37.             term.write(toPrint)
  38.             toPrint = ""
  39.             local n = str:sub(pos+1,pos+1)
  40.             local nn = str:sub(pos+2,pos+2)
  41.             if prefixes[n] and clrs[nn] then
  42.                 prefixes[n](clrs[nn])
  43.                 skip = skip +2
  44.                 if str:sub(pos+3,pos+3) ~= "@" then
  45.                     local n = str:sub(pos+3,pos+3)
  46.                     local nn = str:sub(pos+4,pos+4)
  47.                     if prefixes[n] and clrs[nn] then
  48.                         prefixes[n](clrs[nn])
  49.                         skip = skip +2
  50.                     end
  51.                 else
  52.                     skip = skip + 1
  53.                 end
  54.             end
  55.         elseif char == "\n" then
  56.             print("")
  57.         else
  58.             toPrint = toPrint..char
  59.         end
  60.         if pos == #str then break end
  61.         pos = pos + 1 + skip
  62.     end
  63. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement