Computercraft/src/main/programs/lib/basic.lua
2024-06-02 01:29:56 +02:00

62 lines
1.2 KiB
Lua
Executable File

function clearDisplay()
term.clear()
term.setCursorPos(1,1)
end
function writeToFile(file, content)
file = fs.open(file, "w")
file.write(content)
file.close()
end
function appendToFile(file, content)
file = fs.open(file, "a")
file.write(content)
file.close()
end
function appendToFileNl(file, content)
file = fs.open(file, "a")
file.writeLine(content)
file.close()
end
function readFileOneline(file)
file = fs.open(file, "r")
data = file.readLine()
file.close()
if (data == "true") then data = true elseif data == "false" then data = false end
return data
end
function readFileAll(file)
file = fs.open(file, "r")
if (file == nil) then
print("File does not exist.")
else
data = file.readAll()
file.close()
end
return data
end
function splitStringToArray(input_str)
table = {}
i = 0
for str in string.gmatch(input_str, "([^"..",".."]+)") do
table[i] = str
i = i + 1
end
return table
end
return
{
clear_display=clear_display,
write_to_file=write_to_file,
append_to_file=append_to_file,
append_to_file_nl=append_to_file_nl,
read_file_oneline=read_file_oneline,
read_file_all=read_file_all,
split_string_to_array=split_string_to_array,
}