Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local GMT_max = 13
- local GMT_min = -12
- local GMT_default = -11
- local updateTime_delay = 1
- local cur_time, cur_date = nil, nil
- local h, m, s, d, month, y = nil, nil, nil, nil, nil, nil
- local time_URL = "http://test-my-php.freeiz.com/time.php"
- local cur_dateFormat = "m/d/y"
- local screenX, screenY = term.getSize()
- -- Tables
- local dateFormats = {
- "m/d/y",
- "d/m/y",
- "y/d/m",
- "y/m/d"
- }
- local boxes = {
- "/------------------------\\",
- "| | |",
- "| | |",
- "| | |",
- "| | |",
- "\\------------------------/",
- }
- local watch = {
- " |----| ",
- " | | ",
- " |----| ",
- " | | ",
- " |----| ",
- " | | ",
- " |----| ",
- " / \\ ",
- "| |",
- "| |",
- "| |",
- " \\ /",
- " |----| ",
- " | | ",
- " |----| ",
- " | | ",
- " |----| ",
- " | | ",
- " |----| ",
- }
- local background = {
- 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbf0000000077777700000000',
- 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbf0000000078888700000000',
- 'bbbbbbbbbbbbbbbbbbbbbbbbbbbbf0000000077777700000000',
- '3333333333333333333333333333f0000000078888700000000',
- '3eeeeeeeeeeeeeeeeeeeeeeeeee3f0000000077777700000000',
- '3e333333333333e33333333333e3f0000000078888700000000',
- '3e333333333333e33333333333e3f0000000077777700000000',
- '3e333333333333e33333333333e3f0000000744444470000000',
- '3e333333333333e33333333333e3f0000007444444447000000',
- '3eeeeeeeeeeeeeeeeeeeeeeeeee3f0000007444444447000000',
- '3333333333333333333333333333f0000007444444447000000',
- '3333333333333333333333333333f0000000744444470000000',
- '3333333333333333333333333333f0000000077777700000000',
- '3333333333333333333333333333f0000000078888700000000',
- '9999999999999999999999999999f0000000077777700000000',
- '9999999999999999999999999999f0000000078888700000000',
- '9999999999999999999999999999f0000000077777700000000',
- '9999999999999999999999999999f0000000078888700000000',
- '9999999999999999999999999999f0000000077777700000000',
- }
- local tColours = {
- -- Background colours
- ["header"] = term.isColor() and colours.blue or colours.black,
- ["middle"] = term.isColor() and colours.lightBlue or colours.black,
- ["footer"] = term.isColor() and colours.cyan or colours.black,
- ["divider"] = term.isColor() and colours.black or colours.black,
- ["watchArea"] = term.isColor() and colours.white or colours.black,
- ["watchHead"] = term.isColor() and colours.yellow or colours.black,
- ["watchGMT"] = term.isColor() and colours.lightGrey or colours.black,
- ["watchOther"] = term.isColor() and colours.grey or colours.black,
- ["options"] = colours.black,
- -- Text colours
- ["info"] = term.isColor() and colours.yellow or colours.white,
- ["left"] = term.isColor() and colours.blue or colours.white,
- ["right"] = term.isColor() and colours.black or colours.white,
- ["UTC"] = term.isColor() and colours.red or colours.white,
- ["heading"] = term.isColor() and colours.red or colours.white,
- ["error"] = term.isColor() and colours.red or colours.white,
- ["other"] = term.isColor() and colours.lime or colours.white
- }
- local tButtons = {
- { text = "Options", x = 3, y = 12, col = tColours["info"] },
- { text = "Help", x = 20, y = 12, col = tColours["info"] },
- { text = "Exit", x = 13, y = 14, col = tColours["error"] },
- }
- local tHelp = {
- bgCol = colours.black,
- txtCol = term.isColor() and colours.yellow or colours.white,
- headCol = term.isColor() and colours.red or colours.white,
- text = {
- "(head)Time Program",
- "This program is for you to check the time for any",
- "given timezone, ranging from -12 to +13 GMT.",
- "",
- "(head)Left side:",
- "On the left hand side of the main screen, you will",
- "find the main heading of the program at the top,",
- "the date and time in a box in the middle area,",
- "buttons surrounded by square brackets and the",
- "current settings at the bottom (default, max",
- "and min GMT).",
- "",
- "(head)Right side:",
- "On the right hand side you will the amazing watch",
- "which allows you to change the current GMT hour.",
- "The GMT hours range from -12 to +13 and you are",
- "able to change the default within the program at",
- "the top. The GMT can be changed by either using",
- "the scroll on your mouse or the up/down arrows on",
- "your keyboard.",
- "",
- "(head)Credits:",
- "jag_e_nummer_ett: idea and background",
- "Mikk809h: idea for date formats"
- }
- }
- -- Functions
- local function hexLookup( char )
- local value = tonumber(char, 16)
- return (value and math.pow(2, value) or nil)
- end
- local function colorW(...)
- local curColor
- for i=1, #arg do -- arg is ...
- if type(arg[i]) == 'number' then
- curColor = arg[i]
- else
- if curColor then
- term.setTextColor(curColor)
- end
- write(arg[i])
- end
- end
- end
- local function drawBG()
- if term.isColor() then
- for i = 1, #background do
- term.setCursorPos( 1, i )
- for z = 1, #background[i] do
- tmp = string.sub(background[i], z, z)
- if tmp == " " then
- term.setBackgroundColour(colours.black)
- write(' ')
- else
- term.setBackgroundColour(hexLookup(tmp))
- write(' ')
- end
- end
- end
- else
- term.setBackgroundColour( colours.black )
- term.clear()
- for i = 1, #watch do
- term.setCursorPos( 36, i )
- write( watch[i] )
- end
- term.setBackgroundColour( tColours["middle"] )
- for i = 1, #boxes do
- term.setCursorPos( 2, i + 4 )
- write( boxes[i] )
- end
- end
- end
- local function drawMain()
- drawBG()
- -- Header
- term.setBackgroundColour( tColours["header"] )
- term.setCursorPos( 8, 2 ) colorW( tColours["other"], "WHATS THE TIME?" )
- -- Middle
- term.setBackgroundColour( tColours["middle"] )
- term.setCursorPos( 8, 6 ) colorW( tColours["other"], "DATE" )
- term.setCursorPos( 19, 6 ) colorW( tColours["other"], "TIME" )
- -- Draw the buttons
- for index, but in pairs( tButtons ) do
- term.setCursorPos( but.x, but.y )
- colorW( tColours["other"], "[", but.col, but.text, tColours["other"], "]" )
- end
- -- Footer
- term.setBackgroundColour( tColours["footer"] )
- term.setCursorPos( 5, 16 ) colorW( tColours["info"], "Default GMT: ", tColours["left"], tostring(GMT_default) )
- term.setCursorPos( 5, 17 ) colorW( tColours["info"], " Max GMT: ", tColours["left"], tostring(GMT_max) )
- term.setCursorPos( 5, 18 ) colorW( tColours["info"], " Min GMT: ", tColours["left"], tostring(GMT_min) )
- end
- local function getTimeAndDate( txt )
- term.setBackgroundColour( tColours["header"] )
- term.setCursorPos( 3, 2 )
- colorW( tColours["info"], txt .. " current date and" )
- term.setCursorPos( 3, 3 )
- colorW( tColours["info"], "time ..." )
- local res = http.get( time_URL )
- if res then
- local content = res.readAll()
- res.close()
- local _t, _d
- for t in content:gmatch( "<TIME>(.-)</TIME>" ) do
- _t = t
- end
- for d in content:gmatch( "<DATE>(.-)</DATE>" ) do
- _d = d
- end
- return _t, _d
- else
- return nil
- end
- end
- local function split( str, regex )
- if type( str ) ~= "string" then error( "Bad argument: String expected, got "..type( str ), 2 ) end
- if type( regex ) ~= "string" then error( "Bad argument: String expected, got "..type( regex ), 2 ) end
- local t = { }
- local fpat = "(.-)"..regex
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- return t
- end
- local function writeTimeAndDate( _h, _m, _s, _d, _mo, _y )
- term.setBackgroundColour( tColours["middle"] )
- term.setTextColour( tColours["left"] )
- term.setCursorPos( 4, 8 )
- if cur_dateFormat == "d/m/y" then
- write( ( #tostring(_d) == 1 and "0" or "") .. _d .. '/' .. ( #tostring(_mo) == 1 and "0" or "") .. _mo .. '/' .. _y )
- elseif cur_dateFormat == "m/d/y" then
- write( ( #tostring(_mo) == 1 and "0" or "") .. _mo .. '/' .. ( #tostring(_d) == 1 and "0" or "") .. _d .. '/' .. _y )
- elseif cur_dateFormat == "y/m/d" then
- write( ( #tostring(_y) == 1 and "0" or "") .. _y .. '/' .. ( #tostring(_mo) == 1 and "0" or "") .. _mo .. '/' .. ( #tostring(_d) == 1 and "0" or "") .. _d )
- elseif cur_dateFormat == "y/d/m" then
- write( _y .. '/' .. ( #tostring(_d) == 1 and "0" or "") .. _d .. '/' .. ( #tostring(_mo) == 1 and "0" or "") .. _mo )
- else
- error("Somehow cur_dateFormat has an invalid date format :(")
- end
- term.setCursorPos( 18, 8 )
- write( ( #tostring(_h) == 1 and "0" or "") .. _h .. ':' .. ( #tostring(_m) == 1 and "0" or "") .. _m .. ':' .. ( #tostring( _s ) == 1 and "0" or "") .. _s )
- end
- local function updateUTC( pos )
- term.setCursorPos( 38, 10 )
- term.setBackgroundColour( tColours["watchHead"] )
- colorW( tColours["UTC"], 'UTC', tColours["right"], (pos > -1 and "+" or ""), tostring(pos), ' ' )
- term.setTextColour( tColours["right"] )
- term.setBackgroundColour( tColours["watchGMT"] )
- -- Above the 'UTC'
- term.setCursorPos( 40, 2 )
- write( ( pos + 3 <= GMT_max and ( ( pos + 3 > -1 and "+" or "" ) .. pos + 3 ) or " " ) .. string.rep(" ", 2 - #tostring( math.abs( pos + 3 ) ) ) )
- term.setCursorPos( 40, 4 )
- write( ( pos + 2 <= GMT_max and ( ( pos + 2 > -1 and "+" or "" ) .. pos + 2 ) or " " ) .. string.rep(" ", 2 - #tostring( math.abs( pos + 2 ) ) ) )
- term.setCursorPos( 40, 6 )
- write( ( pos + 1 <= GMT_max and ( ( pos + 1 > -1 and "+" or "" ) .. pos + 1 ) or " " ) .. string.rep(" ", 2 - #tostring( math.abs( pos + 1 ) ) ) )
- -- Below the 'UTC'
- term.setCursorPos( 40, 14 )
- write( ( pos - 1 >= GMT_min and ( ( pos - 1 > -1 and "+" or "" ) .. pos - 1 ) or " " ) .. string.rep(" ", 2 - #tostring( math.abs( pos - 1 ) ) ) )
- term.setCursorPos( 40, 16 )
- write( ( pos - 2 >= GMT_min and ( ( pos - 2 > -1 and "+" or "" ) .. pos - 2 ) or " " ) .. string.rep(" ", 2 - #tostring( math.abs( pos - 2 ) ) ) )
- term.setCursorPos( 40, 18 )
- write( ( pos - 3 >= GMT_min and ( ( pos - 3 > -1 and "+" or "" ) .. pos - 3 ) or " " ) .. string.rep(" ", 2 - #tostring( math.abs( pos - 3 ) ) ) )
- end
- local function drawHelpScreen()
- term.setCursorPos( 1, 1 )
- term.setBackgroundColour( term.isColor() and colours.grey or colours.black )
- term.setTextColour( term.isColor() and colours.lightGrey or colours.white )
- write( " Help Screen" .. string.rep( " ", screenX - 13 ) )
- term.setBackgroundColour( term.isColor() and colours.red or colours.black )
- term.setTextColour( term.isColor() and colours.black or colours.white )
- write( "X" )
- local function cleanArea()
- term.setBackgroundColour( term.isColor() and tHelp.bgCol or colours.black )
- for y = 2, screenY do
- term.setCursorPos( 1, y )
- term.clearLine()
- end
- end
- yPos = 1
- while true do
- cleanArea()
- for i = 2, screenY do
- term.setCursorPos( 2, i )
- if tHelp.text[yPos + i - 2] then
- term.setTextColour( tHelp.text[yPos + i - 2]:sub( 1, 6) == "(head)" and tHelp.headCol or tHelp.txtCol )
- write( tHelp.text[yPos + i - 2]:sub( 1, 6) == "(head)" and tHelp.text[yPos + i - 2]:sub( 7 ) or tHelp.text[yPos + i - 2] )
- else
- break
- end
- end
- local event, but, x, y = os.pullEvent()
- if event == "mouse_scroll" or event == "key" then
- if ( but == -1 or but == keys.up ) and yPos > 1 then
- yPos = yPos - 1
- elseif ( but == 1 or but == keys.down ) and yPos < #tHelp.text - (screenY - 2) then
- yPos = yPos + 1
- end
- elseif event == "mouse_click" and but == 1 then
- if x == screenX and y == 1 then
- drawBG()
- drawMain()
- break
- end
- end
- end
- end
- local function optionScreen()
- local function topBar( heading )
- term.setCursorPos( 1, 1 )
- term.setBackgroundColour( tColours["options"] )
- term.clear()
- term.setBackgroundColour( term.isColor() and colours.white or colours.black )
- term.setTextColour( term.isColor() and colours.grey or colours.white )
- write( " " .. heading .. string.rep( " ", screenX - (#heading + 2) ) )
- term.setBackgroundColour( term.isColor() and colours.red or colours.black )
- term.setTextColour( term.isColor() and colours.black or colours.white )
- write( "X" )
- end
- local function drawArea( head )
- topBar( head )
- term.setCursorPos( 4, 4 )
- term.setBackgroundColour( tColours["options"] )
- colorW( tColours["other"], "[CHANGE] ", tColours["info"], "Date format: ", tColours["left"], cur_dateFormat )
- end
- local function change( tab, headText, mainHead )
- topBar( headText )
- term.setCursorPos( 3, 5 )
- term.setBackgroundColour( tColours["options"] )
- colorW( tColours["error"], mainHead )
- for i, sFormat in pairs( dateFormats ) do
- term.setCursorPos( 3, i + 5)
- colorW( tColours["info"], '[select] ', tColours["left"], sFormat )
- end
- while true do
- term.setCursorPos( 3, 3 )
- colorW( tColours["info"], "Current date format: ", tColours["left"], cur_dateFormat )
- local _, but, x, y = os.pullEvent( "mouse_click" )
- if but == 1 then
- if x == screenX and y == 1 then
- break
- else
- for i, v in pairs( dateFormats ) do
- if x >= 3 and x <= 10 and y == i + 5 then
- cur_dateFormat = v
- break
- end
- end
- end
- end
- end
- end
- while true do
- drawArea( 'Options Screen' )
- local event, but, x, y = os.pullEvent( "mouse_click" )
- if but == 1 then
- if x >= 4 and x <= 11 and y == 4 then
- change( cur_dateFormat, "Options Screen - changing date format", "Select new date format:" )
- elseif x == screenX and y == 1 then
- drawBG()
- drawMain()
- break
- end
- end
- end
- end
- local function updateTime()
- local updateTimer = os.startTimer( updateTime_delay )
- while true do
- local _, timer = os.pullEvent( "timer" )
- if timer == updateTimer then
- if s < 59 then
- s = s + 1
- elseif s == 59 then
- s = 0
- m = m + 1
- end
- if m == 60 then
- m = 0
- h = h + 1
- end
- if h == 24 then
- h = 0
- d = d + 1
- end
- updateTimer = os.startTimer( updateTime_delay )
- end
- end
- end
- local function main()
- local cur_gmt_pos = GMT_default
- while true do
- writeTimeAndDate( h, m, s, d, month, y )
- updateUTC( cur_gmt_pos )
- local ev = { os.pullEvent() }
- if ev[1] == "mouse_scroll" or ev[1] == "key" then
- if ( ev[2] == -1 or ev[2] == keys.up ) and cur_gmt_pos < GMT_max then -- Up in GMT
- cur_gmt_pos = cur_gmt_pos + 1
- h = h + 1
- elseif ( ev[2] == 1 or ev[2] == keys.down ) and cur_gmt_pos > GMT_min then -- Down in GMT
- cur_gmt_pos = cur_gmt_pos - 1
- h = h - 1
- end
- if h >= 24 then h = 24 - h d = d + 1
- elseif h < 0 then h = 23 d = d - 1 end
- elseif ev[1] == "timer" and ev[2] == updateTime then
- if s < 59 then
- s = s + 1
- elseif s == 59 then
- s = 0
- m = m + 1
- end
- if m == 60 then
- m = 0
- h = h + 1
- end
- if h == 24 then
- h = 0
- d = d + 1
- end
- updateTime = os.startTimer( updateTime_delay )
- elseif ev[1] == "mouse_click" and ev[2] == 1 then
- local opt
- for index, but in pairs( tButtons ) do
- if ev[3] >= but.x and ev[3] <= but.x + #but.text + 2
- and ev[4] == but.y then
- opt = but.text
- end
- end
- if opt == "Exit" then
- -- Cleaup
- term.setBackgroundColour( colours.black )
- term.setTextColour( colours.white )
- term.clear()
- term.setCursorPos( 1, 1 )
- colorW( tColours["info"], "Last set GMT: ", tColours["left"], tostring( cur_gmt_pos ) )
- term.setCursorPos( 1, 2 )
- colorW( tColours["info"], " DATE: ", tColours["left"], ( #tostring(month) == 1 and "0" or "") .. tostring( month ), tColours["error"], '/', tColours["left"], ( #tostring(d) == 1 and "0" or "") .. tostring( d ), tColours["error"], '/', tColours["left"], tostring( y ) )
- term.setCursorPos( 1, 3 )
- colorW( tColours["info"], " TIME: ", tColours["left"], ( #tostring(h) == 1 and "0" or "") .. tostring( h ), tColours["error"], ':', tColours["left"], ( #tostring(m) == 1 and "0" or "") .. tostring( m ), tColours["error"], ':', tColours["left"], ( #tostring(s) == 1 and "0" or "") .. tostring( s ) )
- term.setCursorPos( 1, 5 )
- break
- elseif opt == "Help" then
- drawHelpScreen()
- elseif opt == "Options" then
- optionScreen()
- end
- end
- end
- end
- if GMT_default > GMT_max or GMT_default < GMT_min then
- colorW( tColours["error"], "GMT_default value is out of range.\n Max GMT: +13\n Min GMT: -12")
- return
- end
- drawBG()
- -- Get the time from the server for first time
- cur_time, cur_date = getTimeAndDate( "Getting" )
- -- Split cur_time into hours, minutes, and seconds
- local t_time = split( cur_time, ':' )
- h, m, s = tonumber(t_time[1]), tonumber(t_time[2]), tonumber(t_time[3])
- h = h + GMT_default
- -- Split cur_date into day, month and year
- local t_date = split( cur_date, '/' )
- month, d, y = tonumber(t_date[1]), tonumber(t_date[2]), tonumber(t_date[3])
- if h >= 24 then
- h = h - 24
- d = d + 1
- elseif h < 0 then
- h = 24 + h
- d = d - 1
- end
- -- Main part
- drawMain()
- parallel.waitForAny( updateTime, main )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement