copas.lua
local sys = require("system")
local copas = require("copas")
local UI = {}
function UI.progressBar(current, total)
local widthOfBar = 50
local progress = math.floor((current / total) * widthOfBar)
local remaining = widthOfBar - progress
local bar = "[" .. string.rep("=", progress) .. string.rep(" ", remaining) .. "]"
io.write("\r" .. bar .. math.floor((current / total) * 100) .. "%") io.flush()
end
function UI.prompt(message)
print(message .. " (y/n):")
local response = sys.readansi(math.huge, copas.pause) if response == "y" then return true
elseif response == "n" then return false
else print("Invalid input")
return UI.prompt(message)
end
end
local function displayMenu()
print("=============")
print("1. Check Time")
print("2. Get Mono Time")
print("3. Give Feedback")
print("4. Progress Bar Demo")
print("6. Exit")
print("=============")
end
local function getTime()
local time = math.floor(sys.gettime()) local date = os.date("Current Time: %Y-%m-%d %H:%M:%S", time)
print(date)
end
local function monoTime()
local response = sys.monotime()
print(response)
end
local function uiPrompt()
local response = UI.prompt("Do you like lua?")
if response == true then
print("Thats great!")
else
print("So sad to hear :(")
end
end
copas.addthread(function () while true do
displayMenu()
io.write("Select an Option: ")
local char = sys.readansi(math.huge, copas.pause) local choice = tonumber(char)
if choice == 1 then
getTime()
elseif choice == 2 then
monoTime()
elseif choice == 3 then
uiPrompt()
elseif choice == 4 then
copas.addthread(function ()
local total = 100
for i=1, total do
UI.progressBar(i, total)
copas.pause(0.1)
end
print()
end)
elseif choice == 6 then
break
end
end
end)
sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING)
sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdin) + sys.CIF_VIRTUAL_TERMINAL_INPUT)
local of_attr = sys.tcgetattr(io.stdin)
sys.setnonblock(io.stdin, true)
sys.tcsetattr(io.stdin, sys.TCSANOW, {
lflag = of_attr.lflag - sys.L_ICANON - sys.L_ECHO, })
copas.loop()
sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) - sys.COF_VIRTUAL_TERMINAL_PROCESSING)
sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdin) - sys.CIF_VIRTUAL_TERMINAL_INPUT)
local of_attr = sys.tcgetattr(io.stdin)
sys.setnonblock(io.stdin, false)
sys.tcsetattr(io.stdin, sys.TCSANOW, {
lflag = of_attr.lflag + sys.L_ICANON + sys.L_ECHO,
})