ComputerCraft Archive

Pcutils

computer networking unknown forum

Description

PCutils by (Mr)robotica34So, I was trying to test my basic skillz of Lua by writing a computer API, which "helps you do common tasks when making computer programs".The code itself is pretty simple, bu...

Installation

Copy one of these commands into your ComputerCraft terminal:

Pastebin:pastebin get f1wL29PW pcutils
wget:wget https://pastebin.com/raw/f1wL29PW pcutils
Archive:wget https://cc.shobie.xyz/cc/get/pb-f1wL29PW pcutils
Quick Install: wget https://cc.shobie.xyz/cc/get/pb-f1wL29PW Pcutils

Usage

Run the program after downloading

Tags

networkingforumapis-and-utilities

Source

View Original Source

Code Preview

--[[
  A computer API to simplify
  common computer tasks
  Made by (Mr)robotica34
]]--
local ver = "1.2"

availableColors = {
"white", "orange", "magenta", "lime", "yellow", "gray", "grey", "lightGray", "lightGrey",
"green", "brown", "blue", "lightBlue", "pink", "purple", "cyan", "red", "black"
}
correctSides = {
"front", "back", "left", "right", "top", "bottom"
}
local function log2(num)
  return math.log(num) / math.log(2)
end
local function parseColor(input)
  if type(input) == "string" then
    for k, v in pairs(availableColors) do
      correctChoice = false
      if input == v then
        correctChoice = true
        break
      end
    end
    return correctChoice
  elseif type(input) == "number" then
    col = log2(input)
    if col < 0 or col > 15 then
      return false
    end
    if col % 1 ~= 0 then
      return false
    end
    return true
  else
    return false
  end
end
local function son(input)
  if type(input) == "string" then return true
  elseif type(input) == "number" then return false end
end
local function cfp(ismodem)
  availableChoices = {}
  if ismodem then
    for k, v in pairs(correctSides) do
      if peripheral.isPresent(v) and peripheral.getType(v) == "modem" then
        table.insert(availableChoices, v)
      end
    end
    return availableChoices
  else
    for k, v in pairs(correctSides) do
      if peripheral.isPresent(v) then
        pertype = peripheral.getType(v)
        table.insert(availableChoices, v..": "..pertype)
      end
    end
    return availableChoices
  end
end
function splitString(text, times)
  ss = {}
  charsPerLine = math.ceil(#text / times)
  for i = 1, times do
    ss[i] = text:sub(charsPerLine * (i-1) + 1, charsPerLine * i)
  end
  return ss
end
local function checkIfButton(array)
  if
  type(array[1]) == "number" and
  type(array[2]) == "number" and
  type(array[3]) == "number" and
  type(array[4]) == "number" then
    return true
  else
    return false
  end
end
local function drawButton(array, color, text)
  buttonWidth = array[2] - array[1] + 1
  buttonHeight = array[4] - array[3] + 1
  ok = false
  j = 0
  while not ok do
    j = j + 1
    if not (#text > buttonWidth * j) then
      ok = true
    end
  end
  modText = splitString(text, j)
  spotForText = {}
  for k, v in pairs(modText) do
    spotForText[k] = {}
    spotx = buttonWidth - #modText[k]
    spoty = buttonHeight - #modText
    spotForText[k][1] = math.ceil(spotx / 2) + array[1]
    spotForText[k][2] = math.ceil(spoty / 2 + k) + array[3] - 1
  end
  k = 1
  for j = array[3], array[4] do
    for i = array[1], array[2] do
      csPos(i, j)
      write(" ")
    end
  end
  for k, v in pairs(spotForText) do
    csPos(spotForText[k][1], spotForText[k][2])
    write(modText[k])
  end
  backCl("black")
end
function version()
  print("PC API v"..ver.."\nCopyright robotica34")
end
function textCl(input)
  if parseColor(input) then
    if son(input) then
      term.setTextColor(colors[input])
    else
      term.setTextColor(input)
    end
  else
    error("Invalid argument #1: invalid color", 2)
  end
end
function backCl(input, doclear)
  if doclear == nil then doclear = false
  elseif type(doclear) ~= "boolean" then
    error("Invalid argument #2: nil or boolean expected, got "..type(doclear), 2)
  end
  if parseColor(input) then
    if son(input) then
      term.setBackgroundColor(colors[input])
    else
      term.setBackgroundColor(input)
    end
  else
    error("Invalid argument #1: not valid color", 2)
  end
  if doclear then
    term.clear()
  end
end
function wrapPer(side)
  ac = cfp()
  if type(side) ~= "nil" and type(side) ~= "string" then
    error("Invalid argument #1: nil or string expected, got "..type(side), 2)
  end
  if side then
    for k, v in pairs(ac) do
      if side == v then
        per = peripheral.wrap(side)
        return per
      end
    end
    error("Invalid argument #1: invalid side", 2)
  else
    print("Peripherals: ")
    for k, v in pairs(ac) do
      print(v)
    end
    write("Choice (side): ")
    choice = string.lower( read() )
    for k, v in pairs(correctSides) do
      correctChoice = false
      if choice == v then
        correctChoice = true
        break
      end
    end
    if correctChoice then
      if peripheral.isPresent(choice) then
        per = peripheral.wrap(choice)
        return per
      else
        error("Incorrect choice", 2)
      end
    else
      error("Incorrect choice", 2)
    end
  end
end
function clear()
  textCl(colors.white)
  backCl(colors.black, true)
  csPos(1, 1)
end
function openRednet(side)
  ac = cfp(true)
  if #ac == 0 then
    error("No modems found", 2)
  end
  if type(side) ~= "nil" and type(side) ~= "string" then
    error("Invalid argument #1: nil or string expected, got "..type(side), 2)
  end
  if side then
    for k, v in pairs(ac) do
      if side == v then
        if peripheral.isPresent(side) and peripheral.getType(side) == "modem" then
          rednet.open(side)
          return
        end
      end
    end
    error("Invalid argument #1: invalid side", 2)
  else
    print("Modems in: ")
    for k, v in pairs(ac) do
      print(v)
    end
    write("Choice (side): ")
    choice = string.lower( read() )
    if peripheral.isPresent(choice) and peripheral.getType(choice) == "modem" then
      rednet.open(choice)
    else
      error("Incorrect choice", 2)
    end
  end
end
function drawLine(color, startx, endx, height)
  if type(color) ~= "string" and type(color) ~= "number" then
    error("string or number (color) expected, got "..type(color), 2)
  end
  if type(startx) ~= "number" then 
    error("number expected, got "..type(startx), 2)
  end
  if type(endx) ~= "number" then
    error("number expected, got "..type(starty), 2)
  end
  if type(height) ~= "number" then
    error("number expected, got "..type(height), 2)
  end
  if parseColor(color) then
    backCl(color)
    for i = startx, endx do
      csPos(i, height)
      write(" ")
    end
  end
end
function fillBox(color, widths, widthe, heights, heighte)
  if type(color) ~= "string" and type(color) ~= "number" then
    error("Invalid argument #1: color expected, got "..type(color), 2)
  end
  if type(widths) ~= "number" then
    error("Invalid argument #2: number expected, got "..type(widths), 2)
  end
  if type(widthe) ~= "number" then
    error("Invalid argument #3: number expected, got "..type(widthe), 2)
  end
  if type(heights) ~= "number" then
    error("Invalid argument #4: number expected, got "..type(heights), 2)
  end
  if type(heighte) ~= "number" then
    error("Invalid argument #5: number expected, got "..type(heighte), 2)
  end
  if parseColor(color) then
    backCl(color)
  else
    error("Invalid argument #1: invalid color")
  end
  for j = heights, heighte do
    for i = widths, widthe do
      csPos(i, j)
      write(" ")
    end
  end
  backCl("black")
end
function checkerBoard(fc, sc, widths, widthe, heights, heighte)
  if type(fc) ~= "string" and type(fc) ~= "number" then
    error("Invalid argument #1: string or number (color) expected, got "..type(fc), 2)
  end
  if type(sc) ~= "string" and type(sc) ~= "number" then
    error("Invalid argument #2: string or number expected, got "..type(sc), 2)
  end
  if type(widths) ~= "number" then
    error("Invalid argument #3: number expected, got "..type(widths), 2)
  end
  if type(widthe) ~= "number" then
    error("Invalid argument #4: number expected, got "..type(widthe), 2)
  end
  if type(heights) ~= "number" then
    error("Invalid argument #5: number expected, got "..type(heights), 2)
  end
  if type(heighte) ~= "number" then
    error("Invalid argument #6: number expected, got "..type(heighte), 2)
  end
  if parseColor(fc) and parseColor(sc) then
    local bool = true
    for j = heights, heighte do
      for i = widths, widthe do
        if bool then
          backCl(fc)
          bool = false
        else
          backCl(sc)
          bool = true
        end
        csPos(i, j)
        write(" ")
      end
    end
  else
    error("Invalid argument #1: invalid color")
  end
  backCl("black")
end
function printTimes(str, int)
  if type(str) ~= "string" then
    error("Invalid argument #1: string expected, got "..type(str), 2)
  end
  if type(int) ~= "number" then
    error("Invalid argument #2: number expected, got "..type(int), 2)
  end
  for i = 1, int do
    print(str)
  end
end
function writeTimes(str, int)
  if type(str) ~= "string" then
    error("Invalid argument #1: string expected, got "..type(str), 2)
  end
  if type(int) ~= "number" then
    error("Invalid argument #2: number expected, got "..type(int), 2)
  end
  for i = 1, int do
    write(str)
  end
end
function mkButton(color, text, tlx, brx, tly, bry)
  if type(color) ~= "string" and type(color) ~= "number" then
    error("Invalid argument #1: color expected, got "..type(color), 2)
  end
  if type(text) ~= "string" then
    error("Invalid argument #2: string expected, got "..type(text), 2)
  end
  if type(tlx) ~= "number" then
    error("Invalid argument #3: number expected, got "..type(tlx), 2)
  end
  if type(tly) ~= "number" then
    error("Invalid argument #4: number expected, got "..type(tly), 2)
  end
  if type(brx) ~= "number" then
    error("Invalid argument #5: number expected, got "..type(brx), 2)
  end
  if type(bry) ~= "number" then
    error("Invalid argument #6: number expected, got "..type(bry), 2)
  end
  if tlx > brx then
    error("Top left corner X is higher than bottom right corner X", 2)
  end
  if tly > bry then
    error("Top left corner Y is higher than bottom right corner Y", 2)
  end
  local buttonWidth = brx - tlx + 1
  local buttonHeight = bry - tly + 1
  local buttonSpace = buttonWidth * buttonHeight
  if #text > buttonSpace then
    error("Text won't fit into button", 2)
  end
  local button = {tlx, brx, tly, bry}
  if parseColor(color) then backCl(color) else error("Invalid argument #1: invalid color", 2) end
  drawButton(button, color, text)
  return button
end
function redraw(array, color, text)
  if not checkIfButton(array) then
    error("Invalid argument #1: button expected", 2)
  end
  if type(color) ~= "string" and type(color) ~= "number" then
    error("Invalid argument #2: color expected, got "..type(color), 2)
  end
  if type(text) ~= "string" then
    error("Invalid argument #3: string expected, got "..type(text), 2)
  end
  local buttonWidth = array[2] - array[1] + 1
  local buttonHeight = array[4] - array[3] + 1
  local buttonSpace = buttonWidth * buttonHeight
  if #text > buttonSpace then
    error("Text won't fit into button", 2)
  end
  if parseColor(color) then backCl(color) else error("Invalid argument #1: invalid color", 2) end
  drawButton(array, color, text)
end
function checkIfClicked(array, par1, par2)
  if not checkIfButton(array) then
    error("Invalid argument #1: button expected", 2)
  end
  if type(par1) ~= "number" then
    error("Invalid argument #2: number expected, got "..type(par1), 2)
  end
  if type(par2) ~= "number" then
    error("Invalid argument #3: number expected, got "..type(par2), 2)
  end
  if par1 >= array[1] and par1 <= array[2] and par2 >= array[3] and par2 <= array[4] then
    return true
  else
    return false
  end
end
function removeButton(array, color)
  if not checkIfButton(array) then
    error("Invalid argument #1: button expected", 2)
  end
  if type(color) ~= "string" and type(color) ~= "number" then
    error("Invalid argument #2: color expected, got "..type(color), 2)
  end
  if parseColor(color) then backCl(color) else error("Invalid argument #2: invalid color", 2) end
  for j = array[3], array[4] do
    for i = array[1], array[2] do
      csPos(i, j)
      write(" ")
    end
  end
end
function middle()
  local x, y = term.getSize()
  local middleX = math.floor(x/2)
  local middleY = math.floor(y/2)
  return middleX, middleY
end
function csPos(x, y)
  if type(x) ~= "number" then
    error("Invalid argument #1: number expected, got "..type(x), 2)
  end
  if type(y) ~= "number" then
    error("Invalid argument #2: number expected, got "..type(y), 2)
  end
  term.setCursorPos(x, y)
end