shiryavsky

Red Timer

Dec 12th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.00 KB | None | 0 0
  1. precision = 10
  2. defaultLength = 12000
  3. lines = 4
  4. args = { ... }
  5. timerLength = false
  6.  
  7. function main()
  8.   if #args > 0 then
  9.      timerLength = tonumber(args[1])
  10.      setFS("time",timerLength)
  11.      setFS("cycle","0")
  12.      setFS("pos","0")
  13.   else
  14.      timerLength = tonumber(getFS("time"))
  15.      if not timerLength then
  16.         timerLength = defaultLength
  17.      end
  18.   end
  19.   resuming = getFS("cycle")
  20.   cycle = 1
  21.   term.clear()
  22.   writeCentered("[RED TIMER]",0,lines)
  23.   while (true) do
  24.     if resuming then
  25.       cycle = resuming
  26.       resuming = false
  27.       timerPos = tonumber(getFS("pos"))
  28.     else
  29.       timerPos = timerLength
  30.     end
  31.     setFS("cycle",cycle)
  32.     while timerPos > 0 do
  33.       percent = 100 - math.ceil( timerPos*100 / timerLength )
  34.       writeCentered("cycle: "..cycle,1,lines)
  35.       writeCentered(timerPos.."/"..timerLength.."  "..percent.."%",2,lines)
  36.       writeCentered(getpString(percent),3,lines)
  37.       os.sleep(precision)
  38.       timerPos = timerPos - precision
  39.       setFS("pos",timerPos)
  40.     end
  41.     rs.setOutput("back", true)
  42.     writeCentered("[EMIT SIGNAL]",3,lines)
  43.     sleep(2)
  44.     rs.setOutput("back", false)
  45.     cycle  = cycle + 1 
  46.   end
  47. end
  48.  
  49. function setFS(name,value)
  50.    file = io.open("timer."..name,"w")
  51.    file:write(value)
  52.    file:close()
  53. end
  54.  
  55. function getFS(name)
  56.   local result = false
  57.   file = io.open("timer."..name, "r")
  58.   if file==nil then
  59.     return 0
  60.   end  
  61.   result = file:read()
  62.   file:close()
  63.   return result
  64. end
  65.  
  66. function getpString(percent)
  67.   local x, y = term.getSize()
  68.   local string = "["
  69.   for i = 0,x-8 do
  70.      if (i < (percent/100 * (x - 8))) then
  71.        string = string.."#"
  72.      else
  73.        string = string.."-"
  74.      end
  75.   end
  76.   return string.."]"
  77. end
  78.  
  79. function writeCentered( text , yd , yLines)
  80.   local x, y = term.getSize()
  81.   local centerYPos = ((y - yLines) / 2)
  82.   local centerXStartingPos = ( x - string.len(text) ) / 2
  83.   term.setCursorPos( centerXStartingPos, centerYPos + yd )
  84.   term.clearLine()
  85.   write( text )
  86. end
  87.  
  88. main()
Advertisement
Add Comment
Please, Sign In to add comment