Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Adds the default variable types
- --@OptionalArg table tab: The table to add the default types to
- --@Returns table tab: The table with the default types added
- local function getDefaultTypes(tab)
- local types = {
- ["any"] = {colors.white,"Any type of value, typically for tables"},
- ["string"]= {colors.red,"Lua string"},
- ["number"]= {colors.yellow,"64 bit floating point"},
- ["table"]= {colors.lightBlue,"Lua table"},
- ["boolean"]= {colors.lime,"true/false value"},
- ["function"]= {colors.purple,"A function which you can pass arguments into"},
- ["Color"]={colors.pink,"One of the 16 paint colors from the colors api"},
- ["File"]={colors.blue,"A file handle that can be read from and written onto"}
- }
- if type(tab) ~= "table" then tab = {} end
- for k,v in pairs(types) do
- if not tab[k] then tab[k] = v end
- end
- return tab
- end
- --Reads the color and information about a specific variable type
- --@OptionalArg string varType: the variable type to be read. If not provided, will read all the variable types
- --@Returns boolean false,string errorMsg: If the varType was not found
- --@Returns table information: {Color color, string comment}, the information for the varType specified
- --@Returns table varTypeInfo: A table of varType info for all registered variable types
- function readVariableType(varType)
- if not fs.exists("variable_types.json") then
- local file = fs.open("variable_types.json","w")
- local tab = getDefaultTypes()
- local data = textutils.serialiseJSON(tab)
- file.write(data)
- file.close()
- if type(varType) ~= "string" then return tab
- elseif tab[varType] then return tab[varType]
- else return false,"Could not find varType "..varType end
- end
- local file = fs.open("variable_types.json","r")
- local data = textutils.unserialiseJSON(file.readAll())
- if not data then return false, "Invalid JSON in variable_types.json" end
- if type(varType) ~= "string" then return data
- elseif data[varType] then return data[varType]
- else return false,"Could not find varType "..varType end
- end
- --Registers a new variable type (or edits an old one) for documentation purposes
- --@Arg string varType: The variable type to be registered
- --@OptionalArg Color color: The color that the variable type will be displayed as
- --@OptionalArg string comment: Information about the variable type
- function registerType(varType,color,comment)
- local types,errorMsg = readVariableType()
- if type(types) ~= "table" then types = {} end
- if types[varType] then
- types[varType] = {color or varType[1],comment or varType[2]}
- else
- types[varType] = {color or colors.gray,comment or ""}
- end
- local data = textutils.serialiseJSON(types)
- local file = fs.open("variable_types.json","w")
- file.write(data)
- file.close()
- end
- --Seperates a string into a table by a seperator character
- --@Arg string input: The input string to be split
- --@OptionalArg string sep: The character, or character class to seperate the string by
- --@Returns table splitString: A list of strings that got seperated
- local function split(input,sep)
- sep = sep or "%s"
- local t = {}
- for str in string.gmatch(input,"([^"..sep.."]+)") do
- table.insert(t,str)
- end
- return t
- end
- --Gets the arguments from a comment line
- --@Arg string comment: the comment before a function
- --@OptionalArg table details: the details table where the data gets stored
- --@Returns table details: The details table with the comment added
- local function addCommentDetail(comment,details)
- if type(details) ~= "table" then details = {} end
- local commentType = string.match(comment,"^@%w+") or "Comments"
- commentType = string.gsub(commentType,"@","")
- if commentType == "Arg" then
- --Too lazy to fill this out
- end
- if type(details[commentType]) ~= "table" then details[commentType] = {} end
- local str = string.gsub(comment,"^@%w+ ","")
- table.insert(details[commentType],str)
- return details
- end
- --Simple printing function to write arguments with fancy colors
- --@Arg string argType: the argument type (string,boolean,table,function,etc)
- --@Arg string argName: the argument name
- local function writeArg(argType,argName)
- argType = string.gsub(argType," ","")
- local typeTable = readVariableType(argType)
- if typeTable then term.setTextColor(typeTable[1]) else term.setTextColor(colors.lightGray) end
- if argType and argType ~= "None" then term.write(argType.." ")
- else term.setTextColor(colors.red) end
- term.write(argName)
- end
- --Gets all the methods in a lua file, returning the functions and their documentation
- --@Arg string api: absolute path of the file to read documentation
- --@OptionalArg number test: Does nothing, just to show OptionalArg
- --@Returns boolean False, string errorMsg: If the api could not be found
- --@Returns table methods: A list of the valid functions, and their documentation
- function getAllMethods(api,test)
- if not fs.exists(api) then return false,"could not find api" end
- local file = fs.open(api,"r")
- local comment = {}
- local methods = {}
- local functionName = ""
- local line = file.readLine()
- while line do
- line = string.gsub(line,"$[%s\t]*","") --Removing whitespace
- if string.sub(line,1,2) == "--" then
- table.insert(comment,string.sub(line,3))
- elseif string.match(line,"^function") or string.match(line,"^local function") then
- local method = {}
- if string.sub(line,1,5) == "local" then
- method.type = "private"
- else
- method.type = "public"
- end
- for k,v in pairs(comment) do
- addCommentDetail(v,method)
- end
- method.header = string.gsub(line,"%w* ?function ","")
- method.args = string.match(method.header,"%b()")
- local args = string.gsub(method.args,"%.","%%.")
- functionName = string.gsub(method.header,args,"")
- functionName = string.gsub(functionName,"[%(%)]","")
- method.name = functionName
- table.insert(methods,method)
- comment = {}
- else
- comment = {}
- end
- line = file.readLine()
- end
- file.close()
- return methods
- end
- --Prints all of the methods in an api
- --@Arg string api: The absolute path of the file to read documentation
- --@OptionalArg string method: The specific method to show. If not specified, prints all methods
- --@OptionalArg boolean showLocals: Default false, whether local methods should also be shown
- --@OptionalArg boolean showReturnComments: Default true, whether comments on the return values should be shown
- --@OptionalArg boolean showComments: Default false, whether the comments on the method should be shown
- function printMethodSummary(api,method,showLocals,showReturnComments,showAllComments)
- if type(showReturnComments) ~= "boolean" then showReturnComments = true end
- local methods,err = getAllMethods(api)
- if not methods then
- textutils.setTextColor(colors.red)
- print(err)
- textutils.setTextColor(colors.white)
- return
- end
- if type(method) == "string" then
- for k,v in pairs(methods) do
- if v.name == method then
- methods = {v}
- showLocals = true
- end
- end
- end
- for k,v in pairs(methods) do
- if v.type == "public" or (showLocals and type(showLocals) == "boolean") then
- local color = colors.gray
- if v.type == "public" then color = colors.yellow end
- term.setTextColor(color)
- term.clearLine()
- term.write(v.name)
- if type(v.Arg) == "table" or type(v.OptionalArg) == "table" or type(v.Args) == "table" then
- term.write("(")
- if type(v.Arg) == "table" and #v.Arg >= 1 then
- for j,w in pairs(v.Arg) do
- local argType = string.match(w,"^%w+")
- local argName = string.match(w," %w+:") or " Errored_Arg"
- argName = string.gsub(argName,"[ :]","")
- writeArg(argType,argName)
- term.setTextColor(color)
- term.write(", ")
- end
- --local x,y = term.getCursorPos()
- --term.setCursorPos(x-2,y) --Clearing the extra comma
- --term.write(" ")
- --term.setCursorPos(x-2,y)
- end
- if type(v.OptionalArg) == "table" and #v.OptionalArg >= 1 then
- term.write("[")
- for j,w in pairs(v.OptionalArg) do
- local argType = string.match(w,"^%w+")
- local argName = string.match(w," %w+:") or " Errored_Arg"
- argName = string.gsub(argName,"[ :]","")
- writeArg(argType,argName)
- term.setTextColor(color)
- term.write(", ")
- end
- local x,y = term.getCursorPos()
- term.setCursorPos(x-2,y)
- term.write(" ")
- term.setCursorPos(x-2,y)
- term.setTextColor(color)
- term.write("], ")
- end
- if type(v.Args) == "table" and #v.Args >= 1 then
- for j,w in pairs(v.Args) do
- local argType = string.match(w,"^%w+")
- local argName = string.match(w," %w+:") or " Errored_Arg"
- argName = string.gsub(argName,"[ :]","")
- writeArg(argType,argName)
- term.setTextColor(color)
- term.write("...")
- end
- else
- local x,y = term.getCursorPos()
- term.setCursorPos(x-2,y) --Clearing the extra comma
- term.write(" ")
- term.setCursorPos(x-2,y)
- end
- term.setTextColor(color)
- term.write(")")
- else
- term.write(v.args)
- end
- if type(v.Returns) == "table" then
- for j,w in pairs(v.Returns) do
- print()
- term.clearLine()
- term.setTextColor(colors.red)
- term.write(" returns:")
- local splitString = split(w,":")
- local returnArgs = split(splitString[1],",")
- for l,u in pairs(returnArgs) do
- local argType = string.match(u,"^.?%w+")
- argType = string.gsub(argType," ","")
- local argName = string.match(u,"%w+$")
- writeArg(argType,argName)
- term.setTextColor(color)
- term.write(", ")
- end
- local x,y = term.getCursorPos()
- term.setCursorPos(x-2,y)
- term.write(" ")
- term.setCursorPos(x-2,y)
- if (showReturnComments or showComments) and type(splitString[2]) == "string" and #splitString[2] >= 1 then
- term.setTextColor(colors.green)
- term.write(" --"..splitString[2])
- end
- end
- end
- if showComments then
- term.setTextColor(colors.green)
- for j,w in pairs(v) do
- if j ~= "Returns" and type(w) == "table" and #w > 0 then
- print()
- term.clearLine()
- term.write(" "..j)
- for l,u in pairs(w) do
- print()
- term.clearLine()
- term.write(" "..u)
- end
- end
- end
- end
- print()
- end
- end
- term.setTextColor(colors.white)
- end
Advertisement
Add Comment
Please, Sign In to add comment