ComputerCraft/TUI71/TUI71-time.lua
2024-03-10 23:45:44 +03:00

148 lines
No EOL
3.1 KiB
Lua

--[[
Time module for TUI71
by Andrew-7-1
MIT License
]]
local time_api = {}
-- ASCII-art digits for bigger numbers
local ascii_digits = {
{
" 0000 ",
"00 00",
"00 00",
"00 00",
" 0000 "
},
{
"1111 ",
" 11 ",
" 11 ",
" 11 ",
"111111"
},
{
" 2222 ",
"22 22",
" 22 ",
" 22 ",
"222222",
},
{
" 3333 ",
"33 33",
" 333",
"33 33",
" 3333 ",
},
{
"44 44",
"44 44",
"444444",
" 44",
" 44"
},
{
"555555",
"55 ",
"55555 ",
" 55",
"55555 "
},
{
" 6666 ",
"66 ",
"66666 ",
"66 66",
" 6666 "
},
{
"777777",
" 77 ",
" 77 ",
" 77 ",
"77 "
},
{
" 8888 ",
"88 88",
" 8888 ",
"88 88",
" 8888 "
},
{
" 9999 ",
"99 99",
" 99999",
" 99",
" 9999 "
}
}
-- Seperator for time e.g. "22:01"
local seperator = {
" ",
" # ",
" ",
" # ",
" ",
}
-- Draw a big digit
function time_api.draw_digit(number, x, y, monitor, colour)
local digit = ascii_digits[number]
monitor.setCursorPos(x, y)
monitor.setTextColor(colour)
for i = 1, 5 do
monitor.write(digit[i])
monitor.setCursorPos(x, y + i)
end
end
-- Draw a big ascii-art clock type HH:MM.
function time_api.draw_time(x, y, monitor)
-- Get the current time
local h, m = tostring(os.date("%H")), tostring(os.date("%M"))
local h1, h2 = tonumber(string.sub(h, 1, 1)), tonumber(string.sub(h, 2, 2))
local m1, m2 = tonumber(string.sub(m, 1, 1)), tonumber(string.sub(m, 2, 2))
local s = os.date("%S")
monitor.setCursorPos(x, y) -- Set cursor position
-- Draw digits layer by layer
for i = 1, 5 do
monitor.setCursorPos(x, select(2, monitor.getCursorPos()) + 1)
monitor.write(ascii_digits[h1 + 1][i] .. " " .. ascii_digits[h2 + 1][i] .. seperator[i] .. ascii_digits[m1 + 1][i] .. " " .. ascii_digits[m2 + 1][i])
end
end
local function draw_timezone(x, y, tz_name, utc_offset, tz_monitor)
--[[
/SYD\
|22:34|
\_ _/
]]
if #tz_name ~= 3 then error("Invalid timezone name (not 3 letters)") end
local utc = os.date("!*t")
local tz_hour = utc.hour + utc_offset
if tz_hour > 23 then
tz_hour = -24 + tz_hour end
local day_part = {
["day"] = "",
["night"] = ""
}
local tz_symbol
if (tz_hour > 20 or tz_hour < 7) then tz_symbol = day_part["night"] else tz_symbol = day_part["day"] end
tz_monitor.setCursorPos(x, y)
tz_monitor.blit(" /" .. tz_name .. "\\", "f04440", "ffffff")
tz_monitor.setCursorPos(x, y + 1)
tz_monitor.write("|" .. tostring(utc.hour + utc_offset) .. ":" .. tostring(utc.min) .. "|")
tz_monitor.setCursorPos(x, y + 2)
tz_monitor.blit(" \\_" .. tz_symbol .. "_/ ", "f00400f", "fffffff")
end