CCCPaste

luautils

--- Simple utils to assist Lua programming v1.0
luautils = {}

-- Print text whit custom color
function luautils.printColored(text, color)
    local ogColor = term.getTextColor()
    if color then
        term.setTextColor(color)
    end
    print(text)
    term.setTextColor(ogColor)
end

--- Return true if the table contains the specified key
function luautils.containsKey(tbl, key)
    for k, _ in pairs(tbl) do
        if k == key then
            return true
        end
    end
    return false
end

--- Return true if the table contains the specified value
function luautils.containsValue(tbl, value)
    for _, v in pairs(tbl) do
        if v == value then
            return true
        end
    end
    return false
end

function luautils.fileExist(file)
    return fs.open(file, "r")
end

function luautils.getCurrentScriptName(includeExtension)
    local name = debug.getinfo(1, "S").short_src
    if includeExtension then
        name = name:match("(.+)%..+$")
    end
    return name
end