function readADV( _sReplaceChar, _tHistory ) -- slightly modified read function credit to dan200 for original
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
-- 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
else
redraw()
end
end
term.setCursorBlink( false )
term.setCursorPos( w + 1, sy )
return sLine
end
local function writer()
while true do
coroutine.yield()
term.setCursorPos(1,1)
term.clearLine()
if stat == "to" then
write("To :")
elseif stat == "mes" then
write("Mes :")
end
end
end
function writer2()
term.setCursorPos(1,1)
term.clearLine()
if stat == "to" then
write("To :")
elseif stat == "mes" then
write("Mes :")
end
end
local function send()
while true do
local sizX,sizY = term.getSize()
term.setCursorPos(6,1)
stat = "to"
writer2()
local id = tonumber(readADV())
term.setCursorPos(6,1)
stat = "mes"
writer2()
local message = readADV()
rednet.send(id,message)
end
end
local function recive()
local lastX,lastY = 1,2
while true do
term.setCursorBlink( true )
local event = {coroutine.yield()}
term.setCursorBlink( false )
if event[1] == "rednet_message" then
local sizX,sizY = term.getSize()
term.setCursorPos(1,lastY)
print("Frm: "..event[2].." Dist: "..event[4].."M Mes: "..event[3])
lastX,lastY = term.getCursorPos()
end
end
end
local function openRednet()
local listOfSides = rs.getSides()
for i = 1,6 do
if peripheral.isPresent(listOfSides[i]) and peripheral.getType(listOfSides[i]) == "modem" then
rednet.open(listOfSides[i])
return listOfSides[i]
end
end
end
modemOn = openRednet()
if modemOn == nil then
print("No WIFI Modem")
error()
else
print("Opened wifi on "..modemOn.." side")
end
term.clear()
term.setCursorPos(1,1)
local stat = nil
local reciveHandel = coroutine.create(recive)
local writerHandel = coroutine.create(writer)
local sendHandel = coroutine.create(send)
while true do -- start a loop
local e,e1,e2,e3,e4,e5 = os.pullEvent()
coroutine.resume(reciveHandel,e,e1,e2,e3,e4,e5)
coroutine.resume(writerHandel)
coroutine.resume(sendHandel,e,e1,e2,e3,e4,e5)
end