Advertisement
JackMacWindows

ansiWrite.lua

Apr 30th, 2020 (edited)
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.56 KB | None | 0 0
  1. local nativewrite = write
  2. _G.write = function(str)
  3.     local seq = nil
  4.     local bold = false
  5.     local lines = 0
  6.     local function getnum(d)
  7.         if seq == "[" then return d or 1
  8.         elseif string.find(seq, ";") then return
  9.             tonumber(string.sub(seq, 2, string.find(seq, ";") - 1)),
  10.             tonumber(string.sub(seq, string.find(seq, ";") + 1))
  11.         else return tonumber(string.sub(seq, 2)) end
  12.     end
  13.     for c in string.gmatch(str, ".") do
  14.         if seq == "\27" then
  15.             if c == "c" then
  16.                 term.setBackgroundColor(colors.black)
  17.                 term.setTextColor(colors.white)
  18.                 term.setCursorBlink(true)
  19.             elseif c == "[" then seq = "["
  20.             else seq = nil end
  21.         elseif seq ~= nil and string.sub(seq, 1, 1) == "[" then
  22.             if tonumber(c) ~= nil or c == ';' then seq = seq .. c else
  23.                 if c == "A" then term.setCursorPos(term.getCursorPos(), select(2, term.getCursorPos()) - getnum())
  24.                 elseif c == "B" then term.setCursorPos(term.getCursorPos(), select(2, term.getCursorPos()) + getnum())
  25.                 elseif c == "C" then term.setCursorPos(term.getCursorPos() + getnum(), select(2, term.getCursorPos()))
  26.                 elseif c == "D" then term.setCursorPos(term.getCursorPos() - getnum(), select(2, term.getCursorPos()))
  27.                 elseif c == "E" then term.setCursorPos(1, select(2, term.getCursorPos()) + getnum())
  28.                 elseif c == "F" then term.setCursorPos(1, select(2, term.getCursorPos()) - getnum())
  29.                 elseif c == "G" then term.setCursorPos(getnum(), select(2, term.getCursorPos()))
  30.                 elseif c == "H" then term.setCursorPos(getnum())
  31.                 elseif c == "J" then term.clear() -- ?
  32.                 elseif c == "K" then term.clearLine() -- ?
  33.                 elseif c == "T" then term.scroll(getnum())
  34.                 elseif c == "f" then term.setCursorPos(getnum())
  35.                 elseif c == "m" then
  36.                     local n, m = getnum(0)
  37.                     if n == 0 then
  38.                         term.setBackgroundColor(colors.black)
  39.                         term.setTextColor(colors.white)
  40.                     elseif n == 1 then bold = true
  41.                     elseif n == 7 or n == 27 then
  42.                         local bg = term.getBackgroundColor()
  43.                         term.setBackgroundColor(term.getTextColor())
  44.                         term.setTextColor(bg)
  45.                     elseif n == 22 then bold = false
  46.                     elseif n >= 30 and n <= 37 then term.setTextColor(2^(15 - (n - 30) - (bold and 8 or 0)))
  47.                     elseif n == 39 then term.setTextColor(colors.white)
  48.                     elseif n >= 40 and n <= 47 then term.setBackgroundColor(2^(15 - (n - 40) - (bold and 8 or 0)))
  49.                     elseif n == 49 then term.setBackgroundColor(colors.black)
  50.                     elseif n >= 90 and n <= 97 then term.setTextColor(2^(15 - (n - 90) - 8))
  51.                     elseif n >= 100 and n <= 107 then term.setBackgroundColor(2^(15 - (n - 100) - 8)) end
  52.                     if m ~= nil then
  53.                         if m == 0 then
  54.                             term.setBackgroundColor(colors.black)
  55.                             term.setTextColor(colors.white)
  56.                         elseif m == 1 then bold = true
  57.                         elseif m == 7 or m == 27 then
  58.                             local bg = term.getBackgroundColor()
  59.                             term.setBackgroundColor(term.getTextColor())
  60.                             term.setTextColor(bg)
  61.                         elseif m == 22 then bold = false
  62.                         elseif m >= 30 and m <= 37 then term.setTextColor(2^(15 - (m - 30) - (bold and 8 or 0)))
  63.                         elseif m == 39 then term.setTextColor(colors.white)
  64.                         elseif m >= 40 and m <= 47 then term.setBackgroundColor(2^(15 - (m - 40) - (bold and 8 or 0)))
  65.                         elseif m == 49 then term.setBackgroundColor(colors.black)
  66.                         elseif n >= 90 and n <= 97 then term.setTextColor(2^(15 - (n - 90) - 8))
  67.                         elseif n >= 100 and n <= 107 then term.setBackgroundColor(2^(15 - (n - 100) - 8)) end
  68.                     end
  69.                 end
  70.                 seq = nil
  71.             end
  72.         elseif c == string.char(0x1b) then seq = "\27"
  73.         else lines = lines + nativewrite(c) end
  74.     end
  75.     return lines
  76. end
  77.  
  78. if _HOST and _HOST:find("ComputerCraft 1%.[89]%d") and not _HOST:find("CraftOS%-PC") then io.stdout._handle.write = write end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement