Advertisement
cozzimoto

Timestamp Console v0.1

Feb 12th, 2013
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.19 KB | None | 0 0
  1. -- ########################################## --
  2. -- ## TheOnlyCozzy's Timestamp Console Log ## --
  3. -- #############################  v0.1  ##### --
  4.  
  5. createdTimers = {} -- timer IDs to store when messages gets recieved
  6. interval = 10 -- how often to update timer intervals
  7. tX,tY = term.getSize() -- get terminal dimensions
  8.  
  9. function createTimer(timerName,text) -- create new timer in next slot in createdTimers
  10.   timerName = os.startTimer(interval)
  11.   createdTimers[#createdTimers+1] = {timerID = timerName, interval = 0, message = text}
  12.  
  13. end
  14.  
  15. rednet.open("right")
  16. while true do
  17.   event,p1,p2,p3 = os.pullEvent()
  18.  
  19.   if event == "timer" then -- if a timer event fired
  20.     if #createdTimers > tY-1 then -- if the list is bigger than what can be displayed
  21.       table.remove(createdTimers,1)
  22.     end
  23.  
  24.     for i=1,#createdTimers do
  25.       if createdTimers[i].timerID == p1 then -- check for a match timer fired to list
  26.         createdTimers[i].interval = createdTimers[i].interval + 1 -- add one to the interval
  27.        
  28.         --print("timer "..createdTimers[i].timerID.." fired") -- debug
  29.         createdTimers[i].timerID = os.startTimer(interval) -- restart timer
  30.      
  31.       end --> end check for match
  32.      
  33.       timeElapsed = createdTimers[i].interval*interval -- store time to convert to seconds and minutes
  34.      
  35.       if timeElapsed < 60 then -- timestamp formatting
  36.         timeElapsed = tostring(timeElapsed).." sec"
  37.       elseif timeElapsed == 60 then
  38.         timeElapsed = "1 min"
  39.       elseif timeElapsed > 60 then
  40.         formatting = tostring(timeElapsed/60)
  41.         timeElapsed = string.sub(formatting,1,4).." min"
  42.       end
  43.      
  44.       term.setCursorPos(1,tY-(#createdTimers-i)) -- align messages
  45.       term.clearLine() -- clear line to remove excess text from previous message
  46.       term.write(timeElapsed..") "..createdTimers[i].message) -- write new message
  47.      
  48.     end --> end of for createdTimers loop
  49.   end --> end of timer event
  50.  
  51.   if event == "rednet_message" then -- if recieved a rednet message
  52.     rand = math.random(655355) -- generate random 16 bit number
  53.     createTimer(rand,p2)
  54.    
  55.   end --> end of rednet message event
  56. end --> end of while true do
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement