CCCPaste

docsgenerator

local function makeDocs(name, docs)
  if type(docs) ~= "table" then
    error("Invalid docs. Not a table")
  end
  local file = fs.open(("%s_docs.txt"):format(name), "w")

  if not file then
    error("Unable to create file.")
  end

  for key, value in pairs(docs) do
    local parts = {}
    for text in string.gmatch(value, "([^-]+)") do
      table.insert(parts, text)
    end

    local returnValue = parts[1]
    local description = parts[2]
    file.write(key)
    file.write("   ")
    file.write(returnValue)
    file.write("\n")
    file.write("->")
    file.writeLine(description)
  end

  file.close()
end

local peripherals = peripheral.getNames()
for _, value in ipairs(peripherals) do
  local perip = peripheral.wrap(value)
    if not (perip["getInkLevel"] and perip["getPageSize"]) then -- Skip printing docs if peripheral is printer
        local docs = perip.getDocs()
        makeDocs(peripheral.getName(perip), docs)
    end
end