Added vararg

Updated log and render Documentation
This commit is contained in:
Robert Jelic
2025-02-17 10:21:03 +01:00
parent 5827984f0f
commit a422bfe3f1
3 changed files with 128 additions and 33 deletions

View File

@@ -1,8 +1,9 @@
---@class Log --- @class Log
---@field _logs table --- @field _logs table The complete log history
---@field _enabled boolean --- @field _enabled boolean If the logger is enabled
---@field _logToFile boolean --- @field _logToFile boolean If the logger should log to a file
---@field _logFile string --- @field _logFile string The file to log to
--- @field LEVEL table The log levels
local Log = {} local Log = {}
Log._logs = {} Log._logs = {}
Log._enabled = true Log._enabled = true
@@ -33,11 +34,13 @@ local levelColors = {
} }
--- Sets if the logger should log to a file. --- Sets if the logger should log to a file.
--- @shortDescription Sets if the logger should log to a file
function Log.setLogToFile(enable) function Log.setLogToFile(enable)
Log._logToFile = enable Log._logToFile = enable
end end
--- sets if the logger should log --- Sets if the logger should log
--- @shortDescription Sets if the logger should log
function Log.setEnabled(enable) function Log.setEnabled(enable)
Log._enabled = enable Log._enabled = enable
end end
@@ -83,16 +86,28 @@ local function log(level, ...)
end end
--- Sends a debug message to the logger. --- Sends a debug message to the logger.
--- @vararg string The message to log --- @shortDescription Sends a debug message
function Log.debug(...) log(Log.LEVEL.DEBUG, ...) end --- @usage Log.debug("This is a debug message")
function Log.debug(...)
log(Log.LEVEL.DEBUG, ...)
end
--- Sends an info message to the logger. --- Sends an info message to the logger.
--- @shortDescription Sends an info message
--- @vararg string The message to log --- @vararg string The message to log
--- @usage Log.info("This is an info message")
function Log.info(...) log(Log.LEVEL.INFO, ...) end function Log.info(...) log(Log.LEVEL.INFO, ...) end
--- Sends a warning message to the logger. --- Sends a warning message to the logger.
--- @shortDescription Sends a warning message
--- @vararg string The message to log --- @vararg string The message to log
--- @usage Log.warn("This is a warning message")
function Log.warn(...) log(Log.LEVEL.WARN, ...) end function Log.warn(...) log(Log.LEVEL.WARN, ...) end
--- Sends an error message to the logger. --- Sends an error message to the logger.
--- @shortDescription Sends an error message
--- @vararg string The message to log --- @vararg string The message to log
--- @usage Log.error("This is an error message")
function Log.error(...) log(Log.LEVEL.ERROR, ...) end function Log.error(...) log(Log.LEVEL.ERROR, ...) end
Log.info("Logger initialized") Log.info("Logger initialized")

View File

@@ -1,7 +1,18 @@
--- @class Render
--- @field terminal table The terminal object to render to
--- @field width number The width of the render
--- @field height number The height of the render
--- @field buffer table The buffer to render
--- @field xCursor number The x position of the cursor
--- @field yCursor number The y position of the cursor
--- @field blink boolean Whether the cursor should blink
local Render = {} local Render = {}
Render.__index = Render Render.__index = Render
local colorChars = require("libraries/colorHex") local colorChars = require("libraries/colorHex")
--- Creates a new Render object
--- @param terminal table The terminal object to render to
--- @return Render
function Render.new(terminal) function Render.new(terminal)
local self = setmetatable({}, Render) local self = setmetatable({}, Render)
self.terminal = terminal self.terminal = terminal
@@ -23,6 +34,12 @@ function Render.new(terminal)
return self return self
end end
--- Adds a dirty rectangle to the buffer
--- @param x number The x position of the rectangle
--- @param y number The y position of the rectangle
--- @param width number The width of the rectangle
--- @param height number The height of the rectangle
--- @returns Render
function Render:addDirtyRect(x, y, width, height) function Render:addDirtyRect(x, y, width, height)
table.insert(self.buffer.dirtyRects, { table.insert(self.buffer.dirtyRects, {
x = x, x = x,
@@ -30,8 +47,16 @@ function Render:addDirtyRect(x, y, width, height)
width = width, width = width,
height = height height = height
}) })
return self
end end
--- Blits text to the screen
--- @param x number The x position to blit to
--- @param y number The y position to blit to
--- @param text string The text to blit
--- @param fg string The foreground color of the text
--- @param bg string The background color of the text
--- @returns Render
function Render:blit(x, y, text, fg, bg) function Render:blit(x, y, text, fg, bg)
if y < 1 or y > self.height then return self end if y < 1 or y > self.height then return self end
if(#text ~= #fg or #text ~= #bg)then if(#text ~= #fg or #text ~= #bg)then
@@ -46,6 +71,15 @@ function Render:blit(x, y, text, fg, bg)
return self return self
end end
--- Blits text to the screen with multiple lines
--- @param x number The x position to blit to
--- @param y number The y position to blit to
--- @param width number The width of the text
--- @param height number The height of the text
--- @param text string The text to blit
--- @param fg colors The foreground color of the text
--- @param bg colors The background color of the text
--- @returns Render
function Render:multiBlit(x, y, width, height, text, fg, bg) function Render:multiBlit(x, y, width, height, text, fg, bg)
if y < 1 or y > self.height then return self end if y < 1 or y > self.height then return self end
if(#text ~= #fg or #text ~= #bg)then if(#text ~= #fg or #text ~= #bg)then
@@ -69,6 +103,11 @@ function Render:multiBlit(x, y, width, height, text, fg, bg)
return self return self
end end
--- Blits text to the screen with a foreground color
--- @param x number The x position to blit to
--- @param y number The y position to blit to
--- @param text string The text to blit
--- @param fg colors The foreground color of the text
function Render:textFg(x, y, text, fg) function Render:textFg(x, y, text, fg)
if y < 1 or y > self.height then return self end if y < 1 or y > self.height then return self end
fg = colorChars[fg] or "0" fg = colorChars[fg] or "0"
@@ -80,6 +119,12 @@ function Render:textFg(x, y, text, fg)
return self return self
end end
--- Blits text to the screen with a background color
--- @param x number The x position to blit to
--- @param y number The y position to blit to
--- @param text string The text to blit
--- @param bg colors The background color of the text
--- @returns Render
function Render:textBg(x, y, text, bg) function Render:textBg(x, y, text, bg)
if y < 1 or y > self.height then return self end if y < 1 or y > self.height then return self end
bg = colorChars[bg] or "f" bg = colorChars[bg] or "f"
@@ -91,6 +136,11 @@ function Render:textBg(x, y, text, bg)
return self return self
end end
--- Blits text to the screen
--- @param x number The x position to blit to
--- @param y number The y position to blit to
--- @param text string The text to blit
--- @returns Render
function Render:text(x, y, text) function Render:text(x, y, text)
if y < 1 or y > self.height then return self end if y < 1 or y > self.height then return self end
@@ -100,6 +150,11 @@ function Render:text(x, y, text)
return self return self
end end
--- Blits a foreground color to the screen
--- @param x number The x position
--- @param y number The y position
--- @param fg string The foreground color to blit
--- @returns Render
function Render:fg(x, y, fg) function Render:fg(x, y, fg)
if y < 1 or y > self.height then return self end if y < 1 or y > self.height then return self end
@@ -109,6 +164,11 @@ function Render:fg(x, y, fg)
return self return self
end end
--- Blits a background color to the screen
--- @param x number The x position
--- @param y number The y position
--- @param bg string The background color to blit
--- @returns Render
function Render:bg(x, y, bg) function Render:bg(x, y, bg)
if y < 1 or y > self.height then return self end if y < 1 or y > self.height then return self end
@@ -118,20 +178,9 @@ function Render:bg(x, y, bg)
return self return self
end end
function Render:blit(x, y, text, fg, bg) --- Clears the screen
if y < 1 or y > self.height then return self end --- @param bg colors The background color to clear the screen with
if(#text ~= #fg or #text ~= #bg)then --- @returns Render
error("Text, fg, and bg must be the same length")
end
self.buffer.text[y] = self.buffer.text[y]:sub(1,x-1) .. text .. self.buffer.text[y]:sub(x+#text)
self.buffer.fg[y] = self.buffer.fg[y]:sub(1,x-1) .. fg .. self.buffer.fg[y]:sub(x+#fg)
self.buffer.bg[y] = self.buffer.bg[y]:sub(1,x-1) .. bg .. self.buffer.bg[y]:sub(x+#bg)
self:addDirtyRect(x, y, #text, 1)
return self
end
function Render:clear(bg) function Render:clear(bg)
local bgChar = colorChars[bg] or "f" local bgChar = colorChars[bg] or "f"
for y=1, self.height do for y=1, self.height do
@@ -143,8 +192,9 @@ function Render:clear(bg)
return self return self
end end
--- Renders the buffer to the screen
--- @returns Render
function Render:render() function Render:render()
local mergedRects = {} local mergedRects = {}
for _, rect in ipairs(self.buffer.dirtyRects) do for _, rect in ipairs(self.buffer.dirtyRects) do
local merged = false local merged = false
@@ -185,6 +235,10 @@ function Render:render()
return self return self
end end
--- Checks if two rectangles overlap
--- @param r1 table The first rectangle
--- @param r2 table The second rectangle
--- @returns boolean
function Render:rectOverlaps(r1, r2) function Render:rectOverlaps(r1, r2)
return not (r1.x + r1.width <= r2.x or return not (r1.x + r1.width <= r2.x or
r2.x + r2.width <= r1.x or r2.x + r2.width <= r1.x or
@@ -192,6 +246,10 @@ function Render:rectOverlaps(r1, r2)
r2.y + r2.height <= r1.y) r2.y + r2.height <= r1.y)
end end
--- Merges two rectangles
--- @param target table The target rectangle
--- @param source table The source rectangle
--- @returns Render
function Render:mergeRects(target, source) function Render:mergeRects(target, source)
local x1 = math.min(target.x, source.x) local x1 = math.min(target.x, source.x)
local y1 = math.min(target.y, source.y) local y1 = math.min(target.y, source.y)
@@ -204,6 +262,10 @@ function Render:mergeRects(target, source)
target.height = y2 - y1 target.height = y2 - y1
end end
--- Sets the cursor position
--- @param x number The x position of the cursor
--- @param y number The y position of the cursor
--- @param blink boolean Whether the cursor should blink
function Render:setCursor(x, y, blink) function Render:setCursor(x, y, blink)
self.terminal.setCursorPos(x, y) self.terminal.setCursorPos(x, y)
self.terminal.setCursorBlink(blink) self.terminal.setCursorBlink(blink)
@@ -213,6 +275,12 @@ function Render:setCursor(x, y, blink)
return self return self
end end
--- Clears an area of the screen
--- @param x number The x position of the area
--- @param y number The y position of the area
--- @param width number The width of the area
--- @param height number The height of the area
--- @param bg colors The background color to clear the area with
function Render:clearArea(x, y, width, height, bg) function Render:clearArea(x, y, width, height, bg)
local bgChar = colorChars[bg] or "f" local bgChar = colorChars[bg] or "f"
for dy=0, height-1 do for dy=0, height-1 do
@@ -226,6 +294,8 @@ function Render:clearArea(x, y, width, height, bg)
return self return self
end end
--- Gets the size of the render
--- @returns number, number
function Render:getSize() function Render:getSize()
return self.width, self.height return self.width, self.height
end end

View File

@@ -16,7 +16,8 @@ local commentTypes = {
"event", "event",
"private", "private",
"protected", "protected",
"field" "field",
"vararg"
} }
local function extractComment(line) local function extractComment(line)
@@ -177,16 +178,25 @@ local function markdownFunction(block)
output = output .. line .. "\n" output = output .. line .. "\n"
end end
end end
if(block.param)then if(block.param or block.vararg)then
output = output .. "\n### Parameters\n" output = output .. "\n### Parameters\n"
for _, line in pairs(block.param) do if(block.param)then
local name, paramType, desc = line:match("([^%s]+)%s+([^%s]+)%s+(.*)") for _, line in pairs(block.param) do
fOutput = fOutput .. name .. (block.param[#block.param] == line and "" or ", ") local name, paramType, desc = line:match("([^%s]+)%s+([^%s]+)%s+(.*)")
if name:match("%?$") then fOutput = fOutput .. name .. (block.param[#block.param] == line and "" or ", ")
name = name:sub(1, -2) if name:match("%?$") then
output = output .. string.format("* `%s` *(optional)* `%s` %s\n", name, paramType, desc) name = name:sub(1, -2)
else output = output .. string.format("* `%s` *(optional)* `%s` %s\n", name, paramType, desc)
output = output .. string.format("* `%s` `%s` %s\n", name, paramType, desc) else
output = output .. string.format("* `%s` `%s` %s\n", name, paramType, desc)
end
end
end
if(block.vararg)then
for _, line in pairs(block.vararg) do
local paramType, desc = line:match("([^%s]+)%s+(.*)")
fOutput = fOutput .. "..."
output = output .. string.format("* `...` *(vararg)* `%s` %s\n", paramType, desc)
end end
end end
end end