Skip to content

Instantly share code, notes, and snippets.

@EmmaKnijn
Created July 31, 2023 09:09
Show Gist options
  • Save EmmaKnijn/bbc844bb33db24dbe110666a03c191ff to your computer and use it in GitHub Desktop.
Save EmmaKnijn/bbc844bb33db24dbe110666a03c191ff to your computer and use it in GitHub Desktop.
local buffer = {}
signlib = {}
local cursorX = 1
local cursorY = 1
local empty = {}
local lines = 4
local width = 15
local writecolor = "c"
signlib.colors = {}
signlib.colors.black = 0
signlib.colors.darkBlue = 1
signlib.colors.darkGreen = 2
signlib.colors.darkAqua = 3
signlib.colors.darkRed = 4
signlib.colors.darkPurple = 5
signlib.colors.gold = 6
signlib.colors.gray = 7
signlib.colors.darkGray = 8
signlib.colors.blue = 9
signlib.colors.green = "a"
signlib.colors.aqua = "b"
signlib.colors.red = "c"
signlib.colors.lightPurple = "d"
signlib.colors.yellow = "e"
signlib.colors.white = "f"
for y=1,lines do
table.insert(buffer,{})
for x=1,width do
table.insert(buffer[y]," ")
end
end
function string.insert(str1, str2, pos) -- thanks stackoverflow https://stackoverflow.com/questions/59561776/how-do-i-insert-a-string-into-another-string-in-lua
return str1:sub(1,pos)..str2..str1:sub(pos+1)
end
signlib.drawBuffer = function(sign) -- takes a wrapped peripheral
while true do
local lines = {} -- this is really expensive but makes messing with insertion easier
for linenum,line in pairs(buffer) do
lines[linenum] = ""
for xnum,char in pairs(line) do
lines[linenum] = lines[linenum] .. char
end
end
--print(lines[1])
sign.setSignText(lines[1],lines[2],lines[3],lines[4])
sleep(0)
end
end
signlib.setTextColor = function(colorcode)
writecolor = colorcode
end
-- 15 wide
signlib.setCursorPos = function(x,y)
if y >= 1 and y <= lines then
if x >= 1 and x <= width then
cursorX = x
cursorY = y
end
end
end
signlib.write = function(string)
string = tostring(string)
for x = 1, #string do
local char = string:sub(x,x)
buffer[cursorY][cursorX + x - 1] = "\u{00a7}" .. writecolor .. char
end
end
signlib.clear = function()
for y,_ in pairs(buffer) do
for x,_ in pairs(buffer[y]) do
buffer[y][x] = " "
end
end
end
return signlib
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment