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

[1.31+] mULtit4Sk API (beta)

Started by ComputerCraftFan11, 02 May 2012 - 12:22 AM
ComputerCraftFan11 #1
Posted 02 May 2012 - 02:22 AM
mULtit4Sk API

Do everything at once


This is probably my first API that's not built into one of my programs.

This is the 'leaked' version of my API, it's not done but it's supposed to let you multitask.

(More info soon, this has NOT been tested yet)

Code:
Spoiler

--API
function read( _sReplaceChar, _tHistory, sEvent,  param )	
	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 == "key" then
		    if param == 28 then
				
				term.setCursorBlink( false )
				term.setCursorPos( w + 1, sy )
				print()
	
				return sLine
				
			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
	
	
end

Current functions:
APIname.read()
SpoilerIt takes 5 parameters (3 more then orig.)
It has to be in this format:

Event, param1, param2 = os.pullEvent()
TestAPI.read("same as before", "the chat history", Event, param1, param2)
This will let you have a read() and it won't stoppause the program so you can put more code after it.

Example program(s):
Spoiler

--PROGRAM
os.loadAPI("TestAPI")
while true do
   event, param1, param2 = os.pullEvent()
   if event == "rednet_message" then
      os.shutdown()
   end

   TestAPI.read(nil, nil, event, param1, param2)
end
If the code above worked correctly, it should add a read() and a rednet receiver that runs both at the somewhere near the same time
blipman17 #2
Posted 10 June 2012 - 02:14 PM
when the final version is there, can you give me a pm?
ComputerCraftFan11 #3
Posted 10 June 2012 - 10:05 PM
when the final version is there, can you give me a pm?

OK, I also made this before I discovered parallels.