Advertisement
moomoomoo309

WE_Comms

May 29th, 2016
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.68 KB | None | 0 0
  1. local fileStoragePath=".WE_Username"
  2. local function getUsernameFromFile()
  3.   if not fs.exists(fileStoragePath) then
  4.     fs.open(fileStoragePath,"w").close() --Make sure the file exists
  5.   end
  6.   local f=fs.open(fileStoragePath,"r")
  7.   local username=f.readAll()
  8.   f.close()
  9.   return username
  10. end
  11.  
  12. --lsh, created by Lyqyd, with only a few tweaks by me (mostly bugfixes to get it working without the entirety of the file, and changing the history path)
  13. local runHistory = {}
  14. if fs.exists(".history") then
  15.   local histFile=io.open(".history","r")
  16.   if histFile then
  17.     for line in histFile:lines() do
  18.       table.insert(runHistory, line)
  19.     end
  20.     histFile:close()
  21.   end
  22. end
  23. local sizeOfHistory = #runHistory
  24.  
  25. local function saveHistory()
  26.   if #runHistory > sizeOfHistory then
  27.     local histFile = io.open(".history", "a")
  28.     if histFile then
  29.       for i = sizeOfHistory + 1,#runHistory do
  30.         histFile:write(runHistory[i].."\n")
  31.       end
  32.       histFile:close()
  33.     end
  34.   end
  35. end
  36.  
  37. local function customRead(history)
  38.   term.setCursorBlink(true)
  39.  
  40.   local line = ""
  41.   local nHistoryPos = nil
  42.   local nPos = 0
  43.  
  44.   local w, h = term.getSize()
  45.   local sx, sy = term.getCursorPos()  
  46.  
  47.   local function redraw()
  48.     local nScroll = 0
  49.     if sx + nPos >= w then
  50.       nScroll = (sx + nPos) - w
  51.     end
  52.      
  53.     term.setCursorPos(sx, sy)
  54.     term.write(string.sub(line, nScroll + 1))
  55.     term.write(string.rep(" ", math.max(0,w - (#line - nScroll) - sx)))
  56.     term.setCursorPos(sx + nPos - nScroll, sy)
  57.   end
  58.  
  59.   while true do
  60.     local sEvent, param = os.pullEvent()
  61.     if sEvent == "char" then
  62.       line = string.sub(line, 1, nPos)..param..string.sub(line, nPos + 1)
  63.       nPos = nPos + 1
  64.       redraw()
  65.      
  66.     elseif sEvent == "key" then
  67.       if param == keys.enter then
  68.         table.insert(runHistory,line)
  69.         saveHistory()
  70.           -- Enter
  71.         break
  72.        
  73.       elseif param == keys.left then
  74.         -- Left
  75.         if nPos > 0 then
  76.           nPos = nPos - 1
  77.           redraw()
  78.         end
  79.        
  80.       elseif param == keys.right then
  81.         -- Right        
  82.         if nPos < string.len(line) then
  83.           nPos = nPos + 1
  84.           redraw()
  85.         end
  86.      
  87.       elseif param == keys.up or param == keys.down then
  88.         -- Up or down
  89.         if history then
  90.           if param == keys.up then
  91.             -- Up
  92.             if nHistoryPos == nil then
  93.               if #history > 0 then
  94.                 nHistoryPos = #history
  95.               end
  96.             elseif nHistoryPos > 1 then
  97.               nHistoryPos = nHistoryPos - 1
  98.             end
  99.           else
  100.             -- Down
  101.             if nHistoryPos == #history then
  102.               nHistoryPos = nil
  103.             elseif nHistoryPos ~= nil then
  104.               nHistoryPos = nHistoryPos + 1
  105.             end
  106.           end
  107.          
  108.           if nHistoryPos then
  109.             line = history[nHistoryPos]
  110.             nPos = string.len(line)
  111.           else
  112.             line = ""
  113.             nPos = 0
  114.           end
  115.           redraw()
  116.         end
  117.       elseif param == keys.backspace then
  118.         -- Backspace
  119.         if nPos > 0 then
  120.           line = string.sub(line, 1, nPos - 1) .. string.sub(line, nPos + 1)
  121.           nPos = nPos - 1
  122.           redraw()
  123.         end
  124.       elseif param == keys.home then
  125.         -- Home
  126.         nPos = 0
  127.         redraw()
  128.       elseif param == keys.delete then
  129.         if nPos < string.len(line) then
  130.           line = string.sub(line, 1, nPos) .. string.sub(line, nPos + 2)        
  131.           redraw()
  132.         end
  133.       elseif param == keys["end"] then
  134.         -- End
  135.         nPos = string.len(line)
  136.         redraw()
  137.       elseif param == keys.tab then
  138.         --tab autocomplete.
  139.       end
  140.     end
  141.   end
  142.  
  143.   term.setCursorBlink(false)
  144.   term.setCursorPos(w + 1, sy)
  145.   io.write"\n> "
  146.   return line
  147. end
  148. --End of lsh
  149.  
  150. local function getUsername(username) --Prompts the user for their username.
  151.   if username~="" then
  152.     print("Is the name \""..username.."\" correct? (Y/N)")
  153.     while true do
  154.       local input=io.read():lower()
  155.       if input=="t" or input=="true" or input=="y" or input=="yes" then
  156.         return username
  157.       elseif input=="f" or input=="false" or input=="n" or input=="no" then
  158.         break
  159.       else
  160.         print("That's not a yes or no answer!")
  161.       end
  162.     end
  163.   end
  164.   while true do
  165.     print"What is your username, exactly as it appears in-game? (case sensitive!)"
  166.     username=io.read()
  167.     print("Is the name \""..username.."\" correct? (Y/N)")
  168.     if io.read():lower()=="y" then
  169.       break
  170.     end
  171.   end
  172.   local f=fs.open(fileStoragePath,"w")
  173.   f.write(username)
  174.   f.close()
  175.   return username
  176. end
  177.  
  178. local username=getUsername(getUsernameFromFile())
  179. local function sendMessage(message,username)
  180.   local modem=false
  181.   while true do
  182.     for k,v in pairs(peripheral.getNames()) do
  183.       if peripheral.getType(v)=="modem" then
  184.         modem=true
  185.         rednet.open(v)
  186.       end
  187.     end
  188.     if modem then
  189.       rednet.broadcast(message,"WE_Input"..username)
  190.       return
  191.     end
  192.     print"Connect a modem and press enter to continue."
  193.     io.read()
  194.   end
  195. end
  196.  
  197. local function clear()
  198.   term.clear()
  199.   term.setCursorPos(1,1)
  200.   print"Type your command:"
  201.   io.write"> "
  202. end
  203.  
  204. io.write"Type your command:\n> "
  205. local message
  206. while true do --Main loop
  207.   message=customRead(runHistory)
  208.   if message:lower()=="changeusername" or message:lower()=="change username" then
  209.     username=getUsername(username)
  210.   elseif message:lower()=="clear" or message:lower()=="reboot" then
  211.     clear()
  212.     sendMessage(message,username)
  213.   else
  214.     sendMessage(message,username)
  215.   end
  216. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement