Pretty cool!
I have some suggestions to your code:
1) Use local variables:
--Configs
local title = "CustomSwitch V2" --Title of program, set it to "" for no title.
local status = 0 --0 for On by default, 1 for Off by default
...
2) You can use booleans: (true/false, you dont need them in a string)
showstatustext = true -- set to false to do not show the status of light.
3) The description you are looking for is: (though, it's not easy to describe)
local ontext = "On" --you know
local offtext = "Off" --The world 'Off' in your language.
4) Use if then else:
function os.pullEvent()
local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then
--if TerminalMode == 0 then -- We don't need to check for this, as we are doing nothing! (' for cleaness)
--end
if TerminalMode == 1 then
os.reboot()
elseif TerminalMode == 2 then -- This has no effect on the code, really, since we reboot if above is true, but it's easier to see what's happening.
error( "Terminated" )
end
end
return event, p1, p2, p3, p4, p5
end
5) You could also take a look at string.format, but it may be a bit confusing:
local statustext = "Light is currently %s" --Can be something else than a light, you choose the use!
local toggletext = "Press Spacebar to turn %s!" --You can customise aswell!
Now, you might ask what %s is.
You can use string.format like so, to change %s to represent on/off:
if showstatustext then
local statetext, rstatetext = offtext, ontext
if status == 1 then
statetext = ontext
rstatetext = offtext -- this is reversed, for "space to turn on/off"
end
print(string.format(statustext, statetext))
print(string.format(toggletext, rstatetext))
end
6) I have a few more suggestions, but I dont' have time to put them in code right now:
Check that all the configuration settings are acceptable! User-error is very common.
Try to remove the display code from the loop, and make a function for it, and call that function in the loop instead.
This'll just make it easier to read.
You could do the same for the event code, but, this isn't as important as the above, in my opinion, but it will make your code more portable.
Loop would look something like:
while true do
local breakLoop = false
updateScreen()
breakLoop = breakLoop or checkEvents() -- Return true in checkEvents function to break the loop.
-- the "or" statement means that, if breakLoop is true, it will not set it to false if checkEvents returns that.
-- Basically, so you can have multiple functions that can stop the loop without them interfering -- I can elaborate on this if you want.
if breakLoop then
break
end
end