This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Beaurocks16's profile picture

[Question]How do I make a clock update every second?

Started by Beaurocks16, 30 April 2012 - 07:01 PM
Beaurocks16 #1
Posted 30 April 2012 - 09:01 PM
I have this so far, but I am wondering how to make it so my clock updates every second, but still allows me to type while it is updating?

local nTime = os.time()
term.clear()
term.setCursorPos(1, 1)
print ("It is "..textutils.formatTime(nTime, bTwentyFourHour))

could you help me?
libraryaddict #2
Posted 30 April 2012 - 10:18 PM

function DisplayTime()
local x, y = term.getCursorPos()
local nTime = os.time()
term.clear()
term.setCursorPos(1, 1)
print ("It is "..textutils.formatTime(nTime, bTwentyFourHour))
TimeDisplay = os.startTimer(1)
term.setCursorPos(x,y)
end

If you want to type while this updates. You will need to edit your own read()
So copy read() from bios.lua then edit os.pullEvents()
Add a timer check as well.
Then when you start your program have this started as well.

Or use this read with that function above.
function read( _sReplaceChar, _tHistory )    
    term.setCursorBlink( true )

    local sLine = ""
    local nHistoryPos = nil
    local nPos = 0
    if _sReplaceChar then
        _sReplaceChar = string.sub( _sReplaceChar, 1, 1 )
    end
    
    local w, h = term.getSize()
    local sx, sy = term.getCursorPos()    
    local function redraw()
        local nScroll = 0
        if sx + nPos >= w then
            nScroll = (sx + nPos) - w
        end
            
        term.setCursorPos( sx, sy )
        term.write( string.rep(" ", w - sx + 1) )
        term.setCursorPos( sx, sy )
        if _sReplaceChar then
            term.write( string.rep(_sReplaceChar, string.len(sLine) - nScroll) )
        else
            term.write( string.sub( sLine, nScroll + 1 ) )
        end
        term.setCursorPos( sx + nPos - nScroll, sy )
    end
    
    while true do
        local sEvent, param = os.pullEvent()
        if sEvent == "char" then
            sLine = string.sub( sLine, 1, nPos ) .. param .. string.sub( sLine, nPos + 1 )
            nPos = nPos + 1
            redraw()
            
    elseif sEvent == "timer" and param == TimeDisplay then
	  DisplayTime()
        elseif sEvent == "key" then
       	 if param == 28 then
                -- Enter
                break
                
            elseif param == 203 then
                -- Left
                if nPos > 0 then
                    nPos = nPos - 1
                    redraw()
                end
                
            elseif param == 205 then
                -- Right                
                if nPos < string.len(sLine) then
                    nPos = nPos + 1
                    redraw()
                end
            
            elseif param == 200 or param == 208 then
			    -- Up or down
                if _tHistory then
                    if param == 200 then
                        -- Up
                        if nHistoryPos == nil then
                            if #_tHistory > 0 then
                                nHistoryPos = #_tHistory
                            end
                        elseif nHistoryPos > 1 then
                            nHistoryPos = nHistoryPos - 1
                        end
                    else
                        -- Down
                        if nHistoryPos == #_tHistory then
                            nHistoryPos = nil
                        elseif nHistoryPos ~= nil then
                            nHistoryPos = nHistoryPos + 1
                        end                        
                    end
                    
                    if nHistoryPos then
				        sLine = _tHistory[nHistoryPos]
				        nPos = string.len( sLine )
				    else
                        sLine = ""
                        nPos = 0
                    end
                    redraw()
			    end
            elseif param == 14 then
                -- Backspace
                if nPos > 0 then
                    sLine = string.sub( sLine, 1, nPos - 1 ) .. string.sub( sLine, nPos + 1 )
                    nPos = nPos - 1                    
                    redraw()
                end
            end
        end
    end
    
    term.setCursorBlink( false )
    term.setCursorPos( w + 1, sy )
    print()
    
    return sLine
end
MysticT #3
Posted 30 April 2012 - 10:24 PM
Depending on the way you get the input, there's two ways:
If you use read(): use the parallel API.

local function updateClock()
  while true do
    local x, y = term.getCursorPos()
    term.setCursosPos(1, 1)
    write("It is "..textutils.formatTime(os.time()))
    term.setCursorPos(x, y)
    sleep(1)
  end
end

local function getInput()
  while true do
    local input = read()
    -- do wathever you need with the input
  end
end

parallel.waitForAny(getInput, updateClock())

If you handle the key and char events: use a timer to update the clock.

local function drawTime()
    local x, y = term.getCursorPos()
    term.setCursosPos(1, 1)
    write("It is "..textutils.formatTime(os.time()))
    term.setCursorPos(x, y)
end

local timer = os.startTimer(1)
while true do
  local sEvt, arg = os.pullEvent()
  if sEvt == "timer" then
    if arg == timer then
	  drawTime()
    end
  elseif sEvt == "key" or sEvt == "char" then
    -- handle key press
  end
end
BigSHinyToys #4
Posted 01 May 2012 - 01:42 AM

local function drawTime()
	local x, y = term.getCursorPos()
	term.setCursosPos(1, 1)
	write("It is "..textutils.formatTime(os.time()))
	term.setCursorPos(x, y)
end

local timer = os.startTimer(1)
while true do
  local sEvt, arg = os.pullEvent()
  if sEvt == "timer" then
	if arg == timer then
       timer = os.startTimer(1) 
	  drawTime()
	end
  elseif sEvt == "key" or sEvt == "char" then
	-- handle key press
  end
end
slight correction added a new timer so it will keep going.
Beaurocks16 #5
Posted 01 May 2012 - 01:52 AM
Libraryaddict, would I add the bottom piece of code to the top of my script? And would that just replace the read() command I have in there, so I don't have to go and editing files? thank you!
libraryaddict #6
Posted 01 May 2012 - 12:51 PM
That should replace the read() yes