Advertisement
KnightMiner

[CC] control

Jul 12th, 2015
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.31 KB | None | 0 0
  1. -- define arguments
  2. local startArgs = { ... }
  3.  
  4. -- validate argument or print usage
  5. if #startArgs < 1 or #startArgs > 2 or startArgs[1] == "usage" then
  6.   print( "Usage: control <receiver> [port]" )
  7.   return
  8. end
  9.  
  10. -- any programs that cannot be run wirelessly
  11. local blacklist = {
  12.  
  13.   -- core programs
  14.   startup = true,
  15.   edit = true,
  16.   lua = true,
  17.  
  18.   -- custom programs
  19.   control = true,
  20.   wireless = true
  21. }
  22.  
  23. -- localize some variables for efficency
  24. local string = string
  25. local find = string.find
  26. local table = table
  27. local rednet = rednet
  28.  
  29. local port = startArgs[2] or "back"
  30. rednet.open( port )
  31.  
  32. local name, done, timeout
  33.  
  34. function validateName( newName, exit )
  35.   while true do
  36.     local lookup = { rednet.lookup( "wireless", newName ) }
  37.     if lookup[1] then
  38.       name = newName
  39.       break
  40.     else
  41.       print( "Invalid name. Please try a different name" )
  42.       local input = read()
  43.       if input == "abort" then
  44.         if exit then
  45.           done = true
  46.         end
  47.         break
  48.       else
  49.         newName = input
  50.       end
  51.     end
  52.   end
  53. end
  54.  
  55. validateName( startArgs[1], true )
  56. local history = {}
  57.  
  58. -- function to process user input
  59. -- basically separates args
  60. -- while allowing DOS style spaces
  61. function processArgs()
  62.   local cmd = read( nil, history )
  63.   table.insert( history, cmd )
  64.   local quote = {}
  65.   local args = {}
  66.  
  67.   for match in string.gmatch( cmd, '%S+' ) do
  68.     if quote[1] then
  69.       table.insert( quote, match )
  70.       if find( match, '"$' ) then
  71.         table.insert( args, table.concat( quote, " " ) )
  72.         quote = {}
  73.         table.insert( args, rep )
  74.       end
  75.     elseif find( match, '^"' ) then
  76.       if find( match, '"$' ) then
  77.         local rep = string.gsub(
  78.           match, '^"(.+)"$', "%1"
  79.         )
  80.         table.insert( args, rep )
  81.       else
  82.         table.insert( quote, match )
  83.       end
  84.     else
  85.       table.insert( args, match )
  86.     end
  87.   end
  88.  
  89.   return args
  90. end
  91.  
  92. function receive()
  93.   if timeout ~= 0 then
  94.     local _, messages = rednet.receive( "remoteResult", timeout )
  95.     for _, v in pairs( messages ) do
  96.       if v.type == "setCursorPos" then
  97.         local x,y = term.getCursorPos()
  98.         if v.args[2] ~= 1 then
  99.           term.setCursorPos( v.args[1], y )
  100.         else
  101.           term.setCursorPos( v.args[1], 1 )
  102.         end
  103.       elseif term[v.type] then
  104.         term[v.type]( unpack( v.args ) )
  105.       end
  106.     end
  107.   end
  108. end
  109.  
  110. function cancel()
  111.   while true do
  112.     local _, key = os.pullEvent( "key" )
  113.     if key == keys["end"] then
  114.       break
  115.     end
  116.   end
  117. end
  118.  
  119. -- print startup text
  120. local colored = term.isColor
  121. local setColor = term.setTextColor
  122. if colored then
  123.   setColor( colors.yellow )
  124. end
  125. print( "* Type the name of a program to run" )
  126. print( '* Run "ctrlPrograms" to list additional programs available to control' )
  127. print( '* Press end while waiting for a message to cancel it' )
  128. setColor( colors.white )
  129.  
  130. -- main loop
  131. while not done do
  132.   write( "\n" .. name .. "] " )
  133.   local args = processArgs()
  134.  
  135.   if args[1] == "exit" then
  136.     done = true
  137.     print( "Exiting control" )
  138.   elseif args[1] == "switchTo" then
  139.     local oldName = name
  140.     validateName( args[2] )
  141.     if name == oldName then
  142.       print( "Name did not change" )
  143.     else
  144.       print( "Now brodcasting to " .. name )
  145.     end
  146.   elseif args[1] == "setTimeout" then
  147.     if args[2] == "false" then
  148.       timeout = nil
  149.     else
  150.       timeout = tonumber( args[2] )
  151.     end
  152.   elseif args[1] == "ctrlClear" then
  153.     term.clear()
  154.     term.setCursorPos( 1, 1 )
  155.   elseif args[1] == "ctrlPrograms" then
  156.     if colored then
  157.       setColor( colors.yellow )
  158.     end
  159.     print( '* ctrlClear: clears the screen' )
  160.     print( '* ctrlPrograms: lists additional programs available to control' )
  161.     print( '* exit: exits control' )
  162.     print( '* setTimeout: changes timeout settings' )
  163.     print( '* switchTo: changes device name' )
  164.     setColor( colors.white )
  165.   elseif blacklist[args[1]] then
  166.     print( args[1] .. ' cannot be run wirelessly' )
  167.   elseif args[1] then
  168.     rednet.broadcast( {
  169.       name = name,
  170.       args = args
  171.     }, "remote" )
  172.     print( table.concat( {
  173.       'Told ',
  174.       name,
  175.       ' to run "',
  176.       args[1],
  177.       '"'
  178.     } ) )
  179.     parallel.waitForAny( receive, cancel )
  180.   end
  181. end
  182.  
  183. rednet.close( port )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement