Guest User

terminal

a guest
Apr 3rd, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.26 KB | None | 0 0
  1. ---------This Program is By EvasiveParacite and
  2. ---------is used to send commands to the main
  3. ---------computer
  4.  
  5. --------Configuration (you can change this)---------
  6. local channel = 92 --must be the same as replyChannel on main computer
  7. local modemSide = "bottom"
  8. local promptTextColor = colors.orange
  9. local userTextColor = colors.yellow
  10. local confirmTextColor = colors.red --color of command when programs asks for confirmation
  11. local helpTextColor = colors.green
  12. local helpPageDelay = .8 --delay between writing each like in the help page in seconds
  13.  
  14. --------Initializaton (do NOT change this)---------
  15. local modem = peripheral.wrap(modemSide)
  16. local command = " "
  17. local commandStart = " " --first word of command
  18. local parameters = "" --all but first word of command
  19. local commands = {"help","/ "}
  20. local commandDescriptions = {"displays a list of commands, no parameters","runs the parameters as command block text"}
  21. local answer = " "
  22. local answered = false
  23. local isValid = false
  24.  
  25. local event = "modem_message"
  26. local side = modemSide
  27. local senderChannel = channel
  28. local replyChannel = channel
  29. local message = ""
  30. local distance = 0
  31.  
  32. ------------------Functions------------------------
  33. local function readCommand(string)
  34.   local count = 0
  35.   for i in string.gmatch(string,"%S+") do
  36.     count = count + 1
  37.     if count == 1 then
  38.       commandStart = i
  39.     elseif count == 2 then
  40.       parameters = i
  41.     else
  42.       parameters = parameters.." "..i
  43.     end
  44.   end
  45. end
  46.  
  47. local function moveCursorDown()
  48.   local x = 0
  49.   local y = 0
  50.   x, y = term.getCursorPos()
  51.   term.setCursorPos(1,y+1)
  52. end
  53.  
  54. local function isCommandValid(startString) --string must be commandStart
  55.   local tempBool = false
  56.   for i=1, #commands do
  57.     if startString == commands[i] then
  58.       tempBool = true
  59.     end
  60.   end
  61.   return tempBool
  62. end
  63.  
  64. local function confirm()
  65.   term.setTextColor(promptTextColor)
  66.   term.write("[y/n]: ")
  67.   term.setTextColor(userTextColor)
  68.   answer = io.read()
  69.   term.setTextColor(promptTextColor)
  70.   if answer == "y" or answer == "n" then
  71.     answered = true
  72.   else
  73.     answered = false
  74.     while not answered do
  75.       print("you must enter 'y' or 'n'...")
  76.       term.write("[y/n]: ")
  77.       term.setTextColor(userTextColor)
  78.       answer = io.read()
  79.       if answer == "y" or answer == "n" then
  80.         answered = true
  81.       end
  82.       term.setTextColor(promptTextColor)
  83.     end
  84.   end
  85. end
  86.  
  87. local function prompt()
  88.   term.setTextColor(promptTextColor)
  89.   term.write("Please enter your command or 'help' for more")
  90.   moveCursorDown()
  91.   term.write("info: ")  
  92.   term.setTextColor(userTextColor)  
  93.  
  94.   command = io.read()
  95.   print("")  
  96.   readCommand(command)
  97.   isValid = isCommandValid(commandStart)
  98.  
  99.   term.setTextColor(colors.red)
  100.   if commandStart == "help" then
  101.     term.clear()
  102.     term.setCursorPos(1,1)
  103.     answer = "y"
  104.   elseif parameters == nil or parameters == "" then
  105.      print("you have not entered any parameters, your command must be at least 2 words unless it is 'help'")
  106.   elseif isValid == false then
  107.      print("'"..commandStart.."'".." is not a valid command. Would you like to go to the help page?")
  108.      confirm()
  109.      if answer == "y" then
  110.        commandStart = "help"
  111.        commandEnd = ""
  112.      end
  113.   else
  114.     term.setTextColor(confirmTextColor)
  115.     print("'"..commandStart.."' + '"..parameters.."'")
  116.     term.setTextColor(promptTextColor)  
  117.     print("Is this the command you would like to use?")
  118.    
  119.     confirm()
  120.    
  121.   end
  122.   term.setTextColor(promptTextColor)
  123. end
  124.  
  125. local function displayHelp()
  126.   term.setTextColor(helpTextColor)
  127.   print("Welcome to the help page!")
  128.   print(" ")
  129.   print(" ")
  130.   sleep(helpPageDelay/3)
  131.   print("Command Formatting: command + ' ' + parameters")
  132.   sleep(helpPageDelay/3)
  133.   print(" ")
  134.   print("Commands: ")
  135.   sleep(helpPageDelay)
  136.   print(" ")
  137.   for i=1, #commands do
  138.     term.write("'"..commands[i].."' : ")
  139.     print(commandDescriptions[i])
  140.     sleep(helpPageDelay)
  141.   end
  142.   term.setTextColor(promptTextColor)
  143.   sleep(helpPageDelay)
  144.   print(" ")
  145.   print(" ")
  146.  
  147. end
  148.  
  149. local function transmit()
  150.  
  151. end
  152.  
  153. local function waitForReply()
  154.   local count = 0
  155.   local correctReply = false
  156.   local correctEvent = false
  157.  
  158.   while count < 5 and (not correctReply or not correctEvent) do
  159.     print("listening for a confirmation of command execution...  if a valid reply is not found within aproximatly "..60-i*20.." seconds the attemp will be abandoned")
  160.     count = count + 1
  161.     os.startTimer(20)
  162.     event, side, senderChannel, replyChannel, message, distance = os.pullEvent("modem_message")
  163.     if event == "modem_message" then
  164.       correctEvent = true
  165.     end
  166.     if message == distance then
  167.       correctReply == true
  168.     end
  169.   end
  170.   print("")
  171.   if not correctReply and not correctEvent then
  172.     term.setTextColor(colors.red)
  173.     print("Command Execution Failed")
  174.   else
  175.     term.setTextColor(colors.green)
  176.     print("Success! Command Executed")
  177.   end
  178.   term.setTextColor(promptTextColor)
  179. end
  180.  
  181. ------------------Main Program---------------------
  182. modem.open(channel)
  183.  
  184. while true do
  185.   prompt()
  186.   if answer == "y" and isValid == true then
  187.     if commandStart == "help" then
  188.       displayHelp()
  189.     else
  190.       transmit()
  191.       waitForReply()
  192.     end
  193.   end
  194. end
Advertisement
Add Comment
Please, Sign In to add comment