Advertisement
ndfjay

rAPI

Apr 13th, 2017
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.57 KB | None | 0 0
  1. rColor = function(col)
  2.     colors = {
  3.         [1] = {decimal=1, name="white", hexadecimal="0x1", paint="0", hex="#F0F0F0", hex2="#f0f0f0"},
  4.         [2] = {decimal=2, name="orange", hexadecimal="0x2", paint="1", hex="#F2B233", hex2="#f2b233"},
  5.         [4] = {decimal=4, name="magenta", hexadecimal="0x4", paint="2", hex="#E57FD8", hex2="#e57fd8"},
  6.         [8] = {decimal=8, name="lightBlue", hexadecimal="0x8", paint="3", hex="#99B2F2", hex2="#99b2f2"},
  7.         [16] = {decimal=16, name="yellow", hexadecimal="0x10", paint="4", hex="#DEDE6C", hex2="#dede6c"},
  8.         [32] = {decimal=32, name="lime", hexadecimal="0x20", paint="5", hex="#7FCC19", hex2="#7fcc19"},
  9.         [64] = {decimal=64, name="pink", hexadecimal="0x40", paint="6", hex="#F2B2CC", hex2="#f2b2cc"},
  10.         [128] = {decimal=128, name="gray", hexadecimal="0x80", paint="7", hex="#4C4C4C", hex2="#4c4c4c"},
  11.         [256] = {decimal=256, name="lightGray", hexadecimal="0x100", paint="8", hex="#999999", hex2="#999999"},
  12.         [512] = {decimal=512, name="cyan", hexadecimal="0x200", paint="9", hex="#4C99B2", hex2="#4c99b2"},
  13.         [1024] = {decimal=1024, name="purple", hexadecimal="0x400", paint="a", hex="#B266E5", hex2="#b266e5"},
  14.         [2048] = {decimal=2048, name="blue", hexadecimal="0x800", paint="b", hex="#3366CC", hex2="#3366cc"},
  15.         [4096] = {decimal=4096, name="brown", hexadecimal="0x1000", paint="c", hex="#7F664C", hex2="#7f664c"},
  16.         [8192] = {decimal=8192, name="green", hexadecimal="0x2000", paint="d", hex="#57A64E", hex2="#57a64e"},
  17.         [16384] = {decimal=16384, name="red", hexadecimal="0x4000", paint="e", hex="#CC4C4C", hex2="#cc4c4c"},
  18.         [32768] = {decimal=32768, name="black", hexadecimal="0x8000", paint="f", hex="#191919", hex2="#191919"},
  19.     }
  20.     match = false
  21.     for k,x in pairs(colors) do
  22.         for i,v in pairs(x) do
  23.             if v == col then
  24.                 return k
  25.             end
  26.         end
  27.     end
  28.  
  29. end
  30.  
  31. rClear = function(...)
  32.     clearArgs = {...}
  33.     -- Args [1] = Line (-ln) or screen (-scr)
  34.     -- Args [2][1] = Colour
  35.     -- Args [2][2] = Y Pos (Applies to Line clear)
  36.     term.setBackgroundColor(rColor(clearArgs[2][1]))
  37.     if clearArgs[1] == "-ln" or clearArgs[1] == "-line" or clearArgs[1] == "-l" or clearArgs[1] == "-L" then
  38.         term.setCursorPos(1,clearArgs[2][2])
  39.         term.clearLine()
  40.     elseif clearArgs[1] == "-scr" or clearArgs[1] == "-a" or clearArgs[1] == "-A" or clearArgs[1] == "-all" or clearArgs[1] == "-screen" then
  41.         term.clear()
  42.     end
  43. end
  44.  
  45. rPrint = function(...)
  46.     local w,h = term.getSize()
  47.     printArgs = {...}
  48.     -- Args[1] = Alignment
  49.     -- Args[2][1] = X Pos (Needed but only applies to left align)
  50.     -- Args[2][2] = Y pos
  51.     -- Args[3][...] = Text to print
  52.     -- Args[4][1] = Text Colour
  53.     -- Args[4][2] = Background Colour
  54.     -- Args[5] = Right align offset
  55.     --(alignment, {x, y}, {Text}, {textColor}, {Alight offset})
  56.     local tmpStr = ""
  57.     for i,v in pairs(printArgs[3]) do
  58.             tmpStr = tmpStr..v.." "
  59.     end
  60.     local yOffset = 0
  61.     if printArgs[5] then
  62.         yOffset = printArgs[5]
  63.     end
  64.     if printArgs[4][1] then
  65.         term.setTextColor(rColor(printArgs[4][1]))
  66.     end
  67.     if printArgs[4][2] then
  68.         term.setBackgroundColor(rColor(printArgs[4][2]))
  69.     end
  70.     if printArgs[1] == "-l" or printArgs[1] == "-L" or printArgs[1] == "-left" then
  71.         term.setCursorPos(printArgs[2][1], printArgs[2][2])
  72.         print(tmpStr)
  73.     elseif printArgs[1] == "-r" or printArgs[1] == "-R" or printArgs[1] == "-right" then
  74.         term.setCursorPos(math.floor(w)-tonumber(#tmpStr+yOffset), printArgs[2][2])
  75.         print(tmpStr)
  76.     elseif printArgs[1] == "-c" or printArgs[1] == "-C" or printArgs[1] == "-center" then
  77.         term.setCursorPos(math.floor(w/2)-tonumber(#tmpStr/2), printArgs[2][2])
  78.         print(tmpStr)
  79.     end
  80. end
  81.  
  82. function exists(path)
  83.     if fs.exists(path) then
  84.         return true
  85.     end
  86.    
  87.     return false
  88. end
  89.  
  90. function getTable(path)
  91.     if exists(path) then
  92.         local file = io.open(path, "r")
  93.         local lines = {}
  94.         local i = 1
  95.         local line = file:read("*l")
  96.         while line ~= nil do
  97.             lines[i] = line
  98.             line = file:read("*l")
  99.             i = i + 1
  100.         end
  101.         file:close()
  102.         return lines
  103.     end
  104.     return {}
  105. end
  106.  
  107. function getLine(path, n)
  108.     if exists(path) then
  109.         local lines = getTable(path)
  110.         return lines[n]
  111.     end
  112.     return ""
  113. end
  114.  
  115. function getText(path)
  116.     if exists(path) then
  117.         local file = assert(io.open(path, "r"))
  118.         return file:read("*a")
  119.     end
  120.     return ""
  121. end
  122.  
  123. function fappend(path, text)
  124.     local file = assert(io.open(path, "a"))
  125.     file:write(text.."\n")
  126.     file:close()
  127. end
  128.  
  129. function fwrite(path, text)
  130.     local file = assert(io.open(path, "w"))
  131.     file:write(text)
  132.     file:close()
  133. end
  134.  
  135. function fwriteAtStart(path, text)
  136.     local _text = getText(path)
  137.     fwrite(path, text.."\n".._text)
  138. end
  139.  
  140. function fwriteFromTable(path, t)
  141.     local text = ""
  142.     for _, line in pairs(t) do
  143.         text = text..line.."\n"
  144.     end
  145.     fwrite(path, text)
  146. end
  147.  
  148. function fappendFromTable(path, t)
  149.     local text = ""
  150.     for _, line in pairs(t) do
  151.         text = text..line.."\n"
  152.     end
  153.     fappend(path, text)
  154. end
  155.  
  156. function fwriteAtStartFromTable(path, t)
  157.     local text = ""
  158.     for _, line in pairs(t) do
  159.         text = text..line.."\n"
  160.     end
  161.     fwriteAtStart(path, text)
  162. end
  163.  
  164. function replaceLine(path, n, text)
  165.     local lines = getTable(path)
  166.     lines[n] = text
  167.     fwriteFromTable(path, lines)
  168. end
  169.  
  170. function getName(path)
  171.     if exists(path) then
  172.         local lastSlashPos = 1
  173.         for i = 1, path:len() do
  174.             if path:sub(i, i) == "/" then
  175.                 lastSlashPos = i
  176.             end
  177.         end
  178.        
  179.         return path:sub(lastSlashPos + 1)
  180.     end
  181.     return ""
  182. end
  183.  
  184. function getPath(path)
  185.     if exists(path) then
  186.         local lastSlashPos = 1
  187.         for i = 1, path:len() do
  188.             if path:sub(i, i) == "/" then
  189.                 lastSlashPos = i
  190.             end
  191.         end
  192.        
  193.         return path:sub(1, lastSlashPos)
  194.     end
  195.     return ""
  196. end
  197.  
  198. function fremove(path)
  199.     os.remove(path)
  200. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement