LegoStax

Peripheral Manager

Jul 2nd, 2016
1,157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 72.96 KB | None | 0 0
  1. --[[ Peripheral Manager by LegoStax
  2.     Peripheral functions: line 597
  3.  
  4.     TODO:
  5.     - fix channel range sorter
  6. ]]--
  7.  
  8. if not term.isColor() or not term.isColour() then
  9.     print("Advanced computer required")
  10.     return
  11. end
  12.  
  13. -- Init variables
  14. -- local w,h = term.getSize()
  15. local w,h = 51,19
  16. local displaymode = "full"
  17. local displaymenu = "main"
  18. local pmang = {RUNNING = true}
  19. local networkperis = false
  20.  
  21. local noteiconxpos = 0
  22. local notemsgs = {}
  23. local unreadnotes = false
  24. local scrollpos = 1
  25. local pospossible = nil
  26.  
  27. local peris = {
  28.     ["sides"] = {
  29.         top = nil,
  30.         bottom = nil,
  31.         left = nil,
  32.         right = nil,
  33.         front = nil,
  34.         back = nil,
  35.     },
  36.     ["network"] = {},
  37. }
  38. local boxes = {
  39.     ["top"] = {x = 0, y = 0},
  40.     ["bottom"] = {x = 0, y = 0},
  41.     ["front"] = {x = 0, y = 0},
  42.     ["left"] = {x = 0, y = 0},
  43.     ["right"] = {x = 0, y = 0},
  44.     ["back"] = {x = 0, y = 0},
  45. }
  46. local mchannels = {}
  47.  
  48. -- DRAWING
  49. -- 0 = black
  50. -- 1 = gray
  51. -- 2 = lightgray
  52. -- 3 = yellow
  53. local pcolor = {
  54.     ["drive"] = {
  55.         "11111",
  56.         "11111",
  57.         "22222",
  58.         "22222",
  59.     },
  60.     ["printer"] = {
  61.         "11111",
  62.         "11111",
  63.         "21112",
  64.         "22222",
  65.     },
  66.     ["monitor"] = {
  67.         "11111",
  68.         "10001",
  69.         "10001",
  70.         "11111",
  71.     },
  72.     ["amonitor"] = {
  73.         "33333",
  74.         "30003",
  75.         "30003",
  76.         "33333",
  77.     },
  78.     ["modem"] = {
  79.         "11111",
  80.         "11111",
  81.         "11111",
  82.         "11111",
  83.     },
  84.     ["wmodem"] = {
  85.         "11111",
  86.         "11111",
  87.         "11111",
  88.         "11111",
  89.     },
  90.     ["computer"] = {
  91.         "11111",
  92.         "10001",
  93.         "20002",
  94.         "22222",
  95.     },
  96. }
  97. local ptxt = {
  98.     ["drive"] = {
  99.         " ___ ",
  100.         " --- ",
  101.         "     ",
  102.         "    =",
  103.     },
  104.     ["printer"] = {
  105.         " ___ ",
  106.         " --- ",
  107.         " === ",
  108.         "    -",
  109.     },
  110.     ["monitor"] = {
  111.         "     ",
  112.         "     ",
  113.         "     ",
  114.         "     ",
  115.     },
  116.     ["amonitor"] = {
  117.         "     ",
  118.         "     ",
  119.         "     ",
  120.         "     ",
  121.     },
  122.     ["modem"] = {
  123.         "     ",
  124.         "  @  ",
  125.         "  @  ",
  126.         "     ",
  127.     },
  128.     ["wmodem"] = {
  129.         "     ",
  130.         "((@))",
  131.         "((@))",
  132.         "     ",
  133.     },
  134.     ["computer"] = {
  135.         "     ",
  136.         " >   ",
  137.         "     ",
  138.         "    -",
  139.     },
  140. }
  141. -- Utils
  142. local function logmsg(msg)
  143.     local f = fs.open("/log", "a")
  144.     f.writeLine(msg)
  145.     f.close()
  146. end
  147. local function mod(a,b)
  148.     return a - math.floor(a/b)*b
  149. end
  150. local function scanPeripherals(p)
  151.     local sides = {"top", "bottom", "front", "left", "right", "back"}
  152.     if p then
  153.         local s = table.concat(sides)
  154.         if string.find(s, p) then
  155.             local ref = peripheral.getType(p)
  156.             if ref == "modem" then
  157.                 if peripheral.call(p, "isWireless") then
  158.                     ref = "wmodem"
  159.                 else
  160.                     ref = "modem"
  161.                 end
  162.             elseif ref == "monitor" then
  163.                 if peripheral.call(p, "isColor") then
  164.                     ref = "amonitor"
  165.                 else
  166.                     ref = "monitor"
  167.                 end
  168.             end
  169.             peris["sides"][p] = ref
  170.         else
  171.             table.insert(peris.network, p)
  172.         end
  173.         return
  174.     end
  175.     local allperis = peripheral.getNames()
  176.     for a = 1,#allperis do
  177.         local failed = 0
  178.         for i = 1,#sides do
  179.             if allperis[a] ~= sides[i] then
  180.                 failed = failed+1
  181.             end
  182.         end
  183.         if failed == 6 then
  184.             networkperis = true
  185.            
  186.             table.insert(peris.network, allperis[a])
  187.         end
  188.     end
  189.     for i = 1,#sides do
  190.         if peripheral.isPresent(sides[i]) then
  191.             local ref = peripheral.getType(sides[i])
  192.             if ref == "modem" then
  193.                 if peripheral.call(sides[i], "isWireless") then
  194.                     ref = "wmodem"
  195.                 else
  196.                     ref = "modem"
  197.                 end
  198.             elseif ref == "monitor" then
  199.                 if peripheral.call(sides[i], "isColor") then
  200.                     ref = "amonitor"
  201.                 else
  202.                     ref = "monitor"
  203.                 end
  204.             end
  205.             peris["sides"][sides[i]] = ref
  206.         end
  207.     end
  208. end
  209. local function drawTopBar()
  210.     term.setTextColor(colors.white)
  211.     term.setBackgroundColor(colors.gray)
  212.     term.setCursorPos(1,1)
  213.     term.clearLine()
  214.     if w < 26 then
  215.         term.write(" pmang")
  216.     else
  217.         term.write(" Peripheral Manager")
  218.     end
  219.     term.setCursorPos(w-5,1)
  220.     noteiconxpos = w-5
  221.     if unreadnotes then term.setBackgroundColor(colors.yellow) end
  222.     if displaymenu == "notecenter" then term.setBackgroundColor(colors.lightGray) end
  223.     term.write(" ! ")
  224.     term.setBackgroundColor(colors.red)
  225.     term.setCursorPos(w,1)
  226.     term.write("X")
  227. end
  228. local function drawPeri(t,xpos,ypos)
  229.     term.setTextColor(colors.black)
  230.    
  231.     for y = 1,4 do
  232.         for x = 1,5 do
  233.             local pos = string.sub(pcolor[t][y], x, x)
  234.             if pos == "0" then
  235.                 term.setBackgroundColor(colors.black)
  236.             elseif pos == "1" then
  237.                 term.setBackgroundColor(colors.gray)
  238.             elseif pos == "2" then
  239.                 term.setBackgroundColor(colors.lightGray)
  240.             elseif pos == "3" then
  241.                 term.setBackgroundColor(colors.yellow)
  242.             end
  243.             term.setCursorPos(x+(xpos-1), y+ypos)
  244.             if string.sub(ptxt[t][y],x,x) == ">" then
  245.                 term.setTextColor(colors.white)
  246.                 term.write(string.sub(ptxt[t][y], x, x))
  247.                 term.setTextColor(colors.black)
  248.             else
  249.                 term.write(string.sub(ptxt[t][y], x, x))
  250.             end
  251.         end
  252.     end
  253. end
  254. local function createNote(pointer, msg)
  255.     msg = textutils.formatTime(os.time(),true)..":"..pointer..": "..msg
  256.     if msg:len() > w then
  257.         table.insert(notemsgs, string.sub(msg, 1, w))
  258.         for i = w+1,msg:len(),w do
  259.             table.insert(notemsgs, string.sub(msg, i, i+w))
  260.         end
  261.     else
  262.         table.insert(notemsgs, msg)
  263.     end
  264.     if displaymenu ~= "notecenter" then
  265.         unreadnotes = true
  266.     end
  267.     os.queueEvent("note_center")
  268. end
  269. local function processMessage(pointer, channel, reply, msg, dist)
  270.     local note = "C"..channel.."/"..reply.." D:"..dist..": "
  271.     if type(msg) == "table" and msg.message ~= nil then
  272.         note = note..msg.message
  273.     else
  274.         note = note..msg
  275.     end
  276.     createNote(pointer, note)
  277. end
  278.  
  279. local function explorerDialog(pointer, canDir)
  280.     if not canDir then canDir = false end
  281.     local RUNNING = true
  282.     local PATH = "/"
  283.     local beginFiles = 0
  284.     local scrollpos = 1
  285.     local greatestpos = 1
  286.     local pospossible = 1
  287.     local oldItems = nil
  288.     local pos = nil
  289.     local selectedItem = -1
  290.     local returnpath = ""
  291.     local selectedPath = "/"
  292.    
  293.     local function clear(bg)
  294.         bg = bg or colors.black
  295.         term.setBackgroundColor(bg)
  296.         term.clear()
  297.         term.setCursorPos(1,1)
  298.     end
  299.  
  300.     local function drawTopBar()
  301.         term.setTextColor(colors.white)
  302.         term.setBackgroundColor(colors.gray)
  303.         term.setCursorPos(1,1)
  304.         term.clearLine()
  305.         if w < 16 then
  306.             term.write(" exp")
  307.         else
  308.             term.write(" Explorer")
  309.         end
  310.         term.setCursorPos(w-5,1)
  311.         noteiconxpos = w-5
  312.         if unreadnotes then term.setBackgroundColor(colors.yellow) end
  313.         term.write(" ! ")
  314.         term.setBackgroundColor(colors.red)
  315.         term.setCursorPos(w,1)
  316.         term.write("X")
  317.     end
  318.    
  319.     local function printPos(msg,x,y,bg,fg)
  320.         if bg then term.setBackgroundColor(bg) end
  321.         if fg then term.setTextColor(fg) end
  322.         term.setCursorPos(x,y)
  323.         term.write(msg)
  324.     end
  325.    
  326.     local function listFiles(d)
  327.         local dir = nil
  328.         if d then
  329.             dir = shell.resolve(d)
  330.         else
  331.             dir = shell.dir()
  332.         end
  333.        
  334.         local all = fs.list(dir)
  335.         local files = {}
  336.         local folders = {}
  337.         local hidden = settings.get("list.show_hidden")
  338.         for n, item in pairs(all) do
  339.             if hidden or string.sub(item,1,1) ~= "." then
  340.                 local path = fs.combine(dir, item)
  341.                 if fs.isDir(path) then
  342.                     table.insert(folders, item)
  343.                 else
  344.                     table.insert(files, item)
  345.                 end
  346.             end
  347.         end
  348.         table.sort(folders)
  349.         table.sort(files)
  350.         return folders, files
  351.     end
  352.    
  353.     local function calculateItems()
  354.         local folders, files = listFiles(PATH)
  355.         local items = folders
  356.         beginFiles = #folders+1
  357.         for i = 1,#files do
  358.             table.insert(items,files[i])
  359.         end
  360.         -- calculate greatestpos
  361.         for i = 1,#items do
  362.             if items[i]:len() > greatestpos then
  363.                 greatestpos = items[i]:len()
  364.             end
  365.         end
  366.         pospossible = #items-(h-6)
  367.         return items
  368.     end
  369.    
  370.     local function drawCurrentPath()
  371.         term.setBackgroundColor(colors.lightGray)
  372.         term.setCursorPos(1,2)
  373.         term.clearLine()
  374.         printPos(" <  "..PATH,1,2,colors.lightGray)
  375.     end
  376.    
  377.     local function drawItems(items)
  378.         if not items then
  379.             items = oldItems
  380.         end
  381.         oldItems = items
  382.        
  383.         term.setBackgroundColor(colors.white)
  384.         term.setTextColor(colors.blue)
  385.         term.setCursorPos(5,3)
  386.         for i = scrollpos,#items do
  387.             if i > #items then break end
  388.             if i == beginFiles then term.setTextColor(colors.black) end
  389.             if i == selectedItem then
  390.                 term.setBackgroundColor(colors.blue)
  391.                 term.setTextColor(colors.white)
  392.             else
  393.                 term.setBackgroundColor(colors.white)
  394.                 if i < beginFiles then term.setTextColor(colors.blue)
  395.                 else term.setTextColor(colors.black) end
  396.             end
  397.             term.write(items[i])
  398.             term.setBackgroundColor(colors.white)
  399.             for i = 1,greatestpos do term.write(" ") end
  400.             local x,y = term.getCursorPos()
  401.             y = y+1
  402.             term.setCursorPos(5,y)
  403.             if y > h-3 then break end
  404.         end
  405.     end
  406.    
  407.     local function drawBottomBar()
  408.         term.setBackgroundColor(colors.gray)
  409.         for i = h-2,h do
  410.             term.setCursorPos(1,i)
  411.             term.clearLine()
  412.         end
  413.         printPos(" Filename: ",1,h-1,colors.gray,colors.white)
  414.         if selectedPath ~= "" then
  415.             printPos(selectedPath,12,h-1)
  416.         end
  417.         printPos(" Select ",w-8,h,colors.lightGray)
  418.     end
  419.    
  420.     local function animSelected(msg,x,y,bg,fg)
  421.         printPos(msg,x,y,bg,fg)
  422.         sleep(0.1)
  423.     end
  424.    
  425.     local function drawScreen()
  426.         clear(colors.white)
  427.         drawTopBar()
  428.         drawCurrentPath()
  429.         drawItems(calculateItems())
  430.         drawBottomBar()
  431.         WALL = ""
  432.     end
  433.    
  434.     drawScreen()
  435.    
  436.     while RUNNING and pmang.RUNNING do
  437.         local e = {os.pullEvent()}
  438.         if e[1] == "mouse_click" and e[2] == 1 and e[3] == w and e[4] == 1 then
  439.             RUNNING = false
  440.             pmang.RUNNING = false
  441.             break
  442.         elseif e[1] == "key" and e[2] == keys.q then
  443.             RUNNING = false
  444.             pmang.RUNNING = false
  445.             break
  446.         elseif e[1] == "mouse_scroll" then
  447.             if e[2] == 1 and scrollpos < pospossible then
  448.                 scrollpos = scrollpos+1
  449.             elseif e[2] == -1 and scrollpos > 1 then
  450.                 scrollpos = scrollpos-1
  451.             end
  452.             drawItems()
  453.         elseif e[1] == "mouse_click" and e[2] == 1 and e[3] >= 1 and e[3] <= 3 and e[4] == 2 then
  454.             animSelected(" < ",1,2,colors.gray,colors.lightGray)
  455.             if PATH ~= "/" then
  456.                 for i = PATH:len()-1,1,-1 do
  457.                     if string.sub(PATH,i,i) == "/" then
  458.                         PATH = string.sub(PATH,1,i)
  459.                         break
  460.                     end
  461.                 end
  462.                 selectedItem = -1
  463.                 scrollpos = 1
  464.                 drawScreen()
  465.             else
  466.                 printPos(" < ",1,2,colors.lightGray,colors.white)
  467.             end
  468.         elseif e[1] == "mouse_click" then
  469.             if e[3] >= noteiconxpos and e[3] <= noteiconxpos+2 and e[4] == 1 then -- NOTE CENTER HANDLER
  470.                 displaymenu = "notecenter"
  471.                 drawNotes()
  472.                 displaymenu = "explorerdialog"
  473.                 drawScreen()
  474.             elseif e[3] >= w-8 and e[3] <= w and e[4] == h and selectedPath ~= "" then -- select button
  475.                 if selectedPath == "/" then
  476.                     if canDir then
  477.                         returnpath = selectedPath
  478.                         RUNNING = false
  479.                         break
  480.                     end
  481.                 else
  482.                     returnpath = selectedPath
  483.                     RUNNING = false
  484.                     break
  485.                 end
  486.             elseif e[4] >= 3 then
  487.                 pos = (e[4]-3)+scrollpos
  488.                 if pos <= #oldItems then
  489.                     if e[3] >= 5 and e[3] <= oldItems[pos]:len()+4 then
  490.                         if pos == selectedItem then
  491.                             if pos < beginFiles and e[2] == 1 then -- move into folder
  492.                                 PATH = PATH..oldItems[pos].."/"
  493.                                 selectedItem = -1
  494.                                 scrollpos = 1
  495.                                 drawScreen()
  496.                             elseif pos >= beginFiles and e[2] == 1 then
  497.                                 if selectedPath == "/" then
  498.                                     if canDir then
  499.                                         returnpath = selectedPath
  500.                                         RUNNING = false
  501.                                         break
  502.                                     end
  503.                                 else
  504.                                     returnpath = selectedPath
  505.                                     RUNNING = false
  506.                                     break
  507.                                 end
  508.                             end
  509.                         else
  510.                             selectedItem = pos
  511.                             selectedPath = PATH..oldItems[pos]
  512.                             if fs.isDir(selectedPath) and not canDir then
  513.                                 selectedPath = "/"
  514.                             end
  515.                             drawItems()
  516.                             drawBottomBar()
  517.                         end
  518.                     else
  519.                         selectedItem = -1
  520.                         selectedPath = "/"
  521.                         drawItems()
  522.                         drawBottomBar()
  523.                     end
  524.                 else
  525.                     selectedItem = -1
  526.                     selectedPath = "/"
  527.                     drawItems()
  528.                     drawBottomBar()
  529.                 end
  530.             end
  531.         elseif e[1] == "term_resize" then
  532.             w,h = term.getSize()
  533.             drawScreen()
  534.         elseif e[1] == "peripheral" then
  535.             scanPeripherals(e[2])
  536.             createNote(e[2], "Attached")
  537.         elseif e[1] == "peripheral_detach" then
  538.             createNote(e[2], "Detached")
  539.             local success = false
  540.             local sides = {"top", "bottom", "front", "left", "right", "back"}
  541.             for i = 1,#peris["network"] do
  542.                 if e[2] == peris["network"][i] then
  543.                     table.remove(peris["network"], i)
  544.                     success = true
  545.                     break
  546.                 end
  547.             end
  548.             if not success then
  549.                 for i = 1,6 do
  550.                     if e[2] == sides[i] then
  551.                         peris["sides"][sides[i]] = nil
  552.                         break
  553.                     end
  554.                 end
  555.             end
  556.            
  557.             if e[2] == pointer then
  558.                 clear()
  559.                 term.setTextColor(colors.red)
  560.                 term.setCursorPos(1,3)
  561.                 term.write("Peripheral removed!")
  562.                 sleep(3)
  563.                 returnpath = "$$removed"
  564.                 break
  565.             end
  566.         elseif e[1] == "note_center" then
  567.             drawTopBar()
  568.         elseif e[1] == "disk" then
  569.             createNote(e[2], "Disk inserted")
  570.         elseif e[1] == "disk_eject" then
  571.             createNote(e[2], "Disk ejected")
  572.         elseif e[1] == "monitor_resize" then
  573.             local mw,mh = peripheral.call(e[2], "getSize")
  574.             createNote(e[2], "Screen size changed to "..mw.."x"..mh)
  575.         elseif e[1] == "monitor_touch" then
  576.             createNote(e[2], "touched at "..e[3]..", "..e[4])
  577.         elseif e[1] == "modem_message" then
  578.             processMessage(e[2], e[3], e[4], e[5], e[6])
  579.         elseif e[1] == "key" and e[2] == keys.q then
  580.             pmang.RUNNING = false
  581.             break
  582.         end
  583.     end
  584.    
  585.    
  586.    
  587.     if pmang.RUNNING then
  588.         if fs.isDir(returnpath) then
  589.             returnpath = returnpath.."/"
  590.         end
  591.         return returnpath
  592.     else
  593.         return ""
  594.     end
  595. end
  596.  
  597. -- Peripheral Functions
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.  
  622. local function drivePeripheral(pointer)
  623.     displaymenu = "maindrive"
  624.    
  625.     local isPlaying = false
  626.     local label = nil
  627.     local RUNNING = true
  628.    
  629.     local function clear()
  630.         term.setBackgroundColor(colors.white)
  631.         term.clear()
  632.         drawTopBar()
  633.         term.setBackgroundColor(colors.white)
  634.         term.setTextColor(colors.black)
  635.     end
  636.     local function drawCopyMove()
  637.         local doCopy = true
  638.         local sourcepath = ""
  639.         local targetpath = ""
  640.         local cenx = 0
  641.         local returnvalue = true
  642.         local function draw()
  643.             clear()
  644.             term.setBackgroundColor(colors.lightGray)
  645.             term.setTextColor(colors.white)
  646.             term.setCursorPos(1,2)
  647.             term.write(" << Back ")
  648.            
  649.             term.setBackgroundColor(colors.white)
  650.             term.setTextColor(colors.black)
  651.             term.setCursorPos((w-17)/2,4)
  652.             term.write("Copy/Move File(s)")
  653.            
  654.             term.setTextColor(colors.black)
  655.             if doCopy then
  656.                 term.setBackgroundColor(colors.lime)
  657.             else
  658.                 term.setBackgroundColor(colors.lightGray)
  659.             end
  660.             term.setCursorPos(3,6)
  661.             term.write(" ")
  662.             term.setBackgroundColor(colors.white)
  663.             term.write(" Copy")
  664.             if not doCopy then
  665.                 term.setBackgroundColor(colors.lime)
  666.             else
  667.                 term.setBackgroundColor(colors.lightGray)
  668.             end
  669.             term.setCursorPos(11,6)
  670.             term.write(" ")
  671.             term.setBackgroundColor(colors.white)
  672.             term.write(" Move")
  673.            
  674.             term.setCursorPos(3,8)
  675.             term.write("Source: "..sourcepath)
  676.             term.setCursorPos(3,9)
  677.             term.write("Target: "..targetpath)
  678.            
  679.             if sourcepath == "" then
  680.                 term.setBackgroundColor(colors.red)
  681.             else
  682.                 term.setBackgroundColor(colors.lime)
  683.             end
  684.             term.setTextColor(colors.white)
  685.             cenx = (w-20)/2
  686.             term.setCursorPos(cenx,11)
  687.             term.write(" Source ")
  688.             if targetpath == "" then
  689.                 term.setBackgroundColor(colors.red)
  690.             else
  691.                 term.setBackgroundColor(colors.lime)
  692.             end
  693.             term.setCursorPos(cenx+12,11)
  694.             term.write(" Target ")
  695.            
  696.             term.setBackgroundColor(colors.lightGray)
  697.             term.setCursorPos(w-6,h)
  698.             term.write(" Go >> ")
  699.         end
  700.         draw()
  701.         while true and pmang.RUNNING do
  702.             local e = {os.pullEvent()}
  703.             if e[1] == "mouse_click" and e[2] == 1 then
  704.                 if e[3] == w and e[4] == 1 then
  705.                     pmang.RUNNING = false
  706.                     break
  707.                 elseif e[3] >= 1 and e[3] <= 9 and e[4] == 2 then -- back button
  708.                     break
  709.                 elseif e[3] >= 3 and e[3] <= 8 and e[4] == 6 then -- copy radio button
  710.                     doCopy = true
  711.                     draw()
  712.                 elseif e[3] >= 11 and e[3] <= 16 and e[4] == 6 then -- move radio button
  713.                     doCopy = false
  714.                     draw()
  715.                 elseif e[3] >= cenx and e[3] <= cenx+7 and e[4] == 11 then -- source button
  716.                     sourcepath = explorerDialog(pointer, true)
  717.                     if sourcepath == "$$removed" then
  718.                         returnvalue = false
  719.                         break
  720.                     end
  721.                     draw()
  722.                 elseif e[3] >= cenx+12 and e[3] <= cenx+19 and e[4] == 11 then -- target button
  723.                     targetpath = explorerDialog(pointer, true)
  724.                     if targetpath == "$$removed" then
  725.                         returnvalue = false
  726.                         break
  727.                     end
  728.                     draw()
  729.                 elseif e[3] >= w-6 and e[3] <= w and e[4] == h and sourcepath ~= "" and targetpath ~= "" then -- go button
  730.                     if doCopy then
  731.                         if fs.exists(targetpath..fs.getName(sourcepath)) then
  732.                             clear()
  733.                             term.setTextColor(colors.red)
  734.                             term.setCursorPos(2,3)
  735.                             term.write("Path exists")
  736.                             sleep(2)
  737.                             draw()
  738.                         else
  739.                             fs.copy(sourcepath, targetpath..fs.getName(sourcepath))
  740.                             break
  741.                         end
  742.                     else
  743.                         if fs.exists(targetpath..fs.getName(sourcepath)) then
  744.                             clear()
  745.                             term.setTextColor(colors.red)
  746.                             term.setCursorPos(2,3)
  747.                             term.write("Path exists")
  748.                             sleep(2)
  749.                             draw()
  750.                         else
  751.                             fs.move(sourcepath, targetpath..fs.getName(sourcepath))
  752.                             break
  753.                         end
  754.                     end
  755.                 elseif e[3] >= noteiconxpos and e[3] <= noteiconxpos+2 and e[4] == 1 then -- NOTE CENTER HANDLER
  756.                     displaymenu = "notecenter"
  757.                     drawNotes()
  758.                     displaymenu = "maindrive"
  759.                     draw()
  760.                 end
  761.             elseif e[1] == "term_resize" then
  762.                 w,h = term.getSize()
  763.                 drawTopBar()
  764.                 draw()
  765.             elseif e[1] == "peripheral" then
  766.                 scanPeripherals(e[2])
  767.                 createNote(e[2], "Attached")
  768.             elseif e[1] == "peripheral_detach" then
  769.                 createNote(e[2], "Detached")
  770.                 local success = false
  771.                 local sides = {"top", "bottom", "front", "left", "right", "back"}
  772.                 for i = 1,#peris["network"] do
  773.                     if e[2] == peris["network"][i] then
  774.                         table.remove(peris["network"], i)
  775.                         success = true
  776.                         break
  777.                     end
  778.                 end
  779.                 if not success then
  780.                     for i = 1,6 do
  781.                         if e[2] == sides[i] then
  782.                             peris["sides"][sides[i]] = nil
  783.                             break
  784.                         end
  785.                     end
  786.                 end
  787.                
  788.                 if e[2] == pointer then
  789.                     clear()
  790.                     term.setTextColor(colors.red)
  791.                     term.setCursorPos(1,3)
  792.                     term.write("Peripheral removed!")
  793.                     sleep(3)
  794.                     RUNNING = false
  795.                     break
  796.                 end
  797.             elseif e[1] == "note_center" then
  798.                 drawTopBar()
  799.             elseif e[1] == "disk" then
  800.                 createNote(e[2], "Disk inserted")
  801.             elseif e[1] == "disk_eject" then
  802.                 createNote(e[2], "Disk ejected")
  803.             elseif e[1] == "monitor_resize" then
  804.                 local mw,mh = peripheral.call(e[2], "getSize")
  805.                 createNote(e[2], "Monitor size changed to "..mw.."x"..mh)
  806.             elseif e[1] == "monitor_touch" then
  807.                 createNote(e[2], "touched at "..e[3]..", "..e[4])
  808.             elseif e[1] == "modem_message" then
  809.                 processMessage(e[2], e[3], e[4], e[5], e[6])
  810.             elseif e[1] == "key" and e[2] == keys.q then
  811.                 pmang.RUNNING = false
  812.                 break
  813.             end
  814.         end
  815.         return returnvalue
  816.     end
  817.     local function draw()
  818.         clear()
  819.         term.setBackgroundColor(colors.lightGray)
  820.         term.setTextColor(colors.white)
  821.         term.setCursorPos(1,2)
  822.         term.write(" << Back ")
  823.        
  824.         term.setBackgroundColor(colors.white)
  825.         term.setTextColor(colors.black)
  826.         term.setCursorPos(3,4)
  827.         term.write("Disk Drive")
  828.         drawPeri("drive",3,5)
  829.         term.setBackgroundColor(colors.white)
  830.         term.setCursorPos(10,6)
  831.         term.write("Name: "..pointer)
  832.         term.setCursorPos(10,7)
  833.         if not peripheral.call(pointer, "isDiskPresent") then -- if there's a disk
  834.             term.setTextColor(colors.red)
  835.             term.write("No disk!")
  836.         else
  837.             if peripheral.call(pointer, "hasAudio") then -- if it's a music disk
  838.                 term.write("Title: "..peripheral.call(pointer, "getAudioTitle"))
  839.                 if not isPlaying then
  840.                     term.setBackgroundColor(colors.lime)
  841.                     term.setTextColor(colors.white)
  842.                     term.setCursorPos(5,13)
  843.                     term.write(" Play ")
  844.                 else
  845.                     term.setBackgroundColor(colors.red)
  846.                     term.setTextColor(colors.white)
  847.                     term.setCursorPos(5,13)
  848.                     term.write(" Stop ")
  849.                 end
  850.             else
  851.                 local mount = peripheral.call(pointer, "getMountPath")
  852.                 term.write("Mount point: "..mount)
  853.                 term.setCursorPos(10,8)
  854.                 term.write("ID: "..peripheral.call(pointer, "getDiskID"))
  855.                 term.setCursorPos(10,9)
  856.                 label = peripheral.call(pointer, "getDiskLabel")
  857.                 if label then
  858.                     term.write("Label: "..label)
  859.                 else
  860.                     term.write("Label: ")
  861.                     term.setTextColor(colors.lightGray)
  862.                     term.write("none")
  863.                     term.setTextColor(colors.black)
  864.                 end
  865.                 term.setCursorPos(10,10)
  866.                 term.write("Free space: "..(fs.getFreeSpace(mount)/1000).." KB")
  867.                
  868.                 term.setBackgroundColor(colors.gray)
  869.                 term.setTextColor(colors.white)
  870.                 term.setCursorPos(5,13)
  871.                 term.write(" Set Label ")
  872.                
  873.                 term.setCursorPos(5,15)
  874.                 term.write(" Copy/Move Files ")
  875.             end
  876.             term.setBackgroundColor(colors.gray)
  877.             term.setTextColor(colors.white)
  878.             term.setCursorPos(5,11)
  879.             term.write(" Eject ")
  880.         end
  881.     end
  882.     draw()
  883.     while RUNNING and pmang.RUNNING do
  884.         local e = {os.pullEvent()}
  885.         if e[1] == "mouse_click" and e[2] == 1 then
  886.             if e[3] >= noteiconxpos and e[3] <= noteiconxpos+2 and e[4] == 1 then -- NOTE CENTER HANDLER
  887.                 displaymenu = "notecenter"
  888.                 drawNotes()
  889.                 displaymenu = "maindrive"
  890.                 draw()
  891.             elseif e[3] == w and e[4] == 1 then
  892.                 pmang.RUNNING = false
  893.                 break
  894.             elseif e[3] >= 1 and e[3] <= 9 and e[4] == 2 then
  895.                 RUNNING = false
  896.                 break
  897.             elseif e[3] >= 5 and e[3] <= 11 and e[4] == 11 then
  898.                 peripheral.call(pointer, "ejectDisk")
  899.             elseif e[3] >= 5 and e[3] <= 10 and e[4] == 13 and peripheral.call(pointer, "hasAudio") then
  900.                 if isPlaying then
  901.                     peripheral.call(pointer, "stopAudio")
  902.                 else
  903.                     peripheral.call(pointer, "playAudio")
  904.                 end
  905.                 isPlaying = not isPlaying
  906.                 draw()
  907.             elseif e[3] >= 5 and e[3] <= 15 and e[4] == 13 then
  908.                 term.setBackgroundColor(colors.lightGray)
  909.                 term.setTextColor(colors.white)
  910.                 local cenx = (w-10)/2
  911.                 local ceny = (h-4)/2
  912.                 term.setCursorPos(cenx,ceny)
  913.                 term.write("Set Label ")
  914.                 term.setCursorPos(cenx,ceny+1)
  915.                 term.write("          ")
  916.                 term.setCursorPos(cenx,ceny+2)
  917.                 term.write("          ")
  918.                 term.setCursorPos(cenx,ceny+3)
  919.                 term.write("          ")
  920.                 if label then
  921.                     for i = 1,label:len() do
  922.                         os.queueEvent("char", string.sub(label,i,i))
  923.                     end
  924.                 end
  925.                 term.setCursorPos(cenx+1,ceny+2)
  926.                 local input = read()
  927.                 if input == "" then
  928.                     input = nil
  929.                 end
  930.                 peripheral.call(pointer, "setDiskLabel", input)
  931.                 draw()
  932.             elseif e[3] >= 5 and e[3] <= 21 and e[4] == 15 then
  933.                 clear()
  934.                 displaymenu = "copymove"
  935.                 if drawCopyMove() then
  936.                     displaymenu = "maindrive"
  937.                     draw()
  938.                 else
  939.                     clear()
  940.                     term.setTextColor(colors.red)
  941.                     term.setCursorPos(1,3)
  942.                     term.write("Peripheral removed!")
  943.                     sleep(3)
  944.                     RUNNING = false
  945.                     break
  946.                 end
  947.             end
  948.         elseif e[1] == "disk" then
  949.             if e[2] == pointer then
  950.                 draw()
  951.             else
  952.                 createNote(e[2], "Disk inserted")
  953.             end
  954.         elseif e[1] == "disk_eject" then
  955.             if e[2] == pointer then
  956.                 draw()
  957.                 isPlaying = false
  958.             else
  959.                 createNote(e[2], "Disk ejected")
  960.             end
  961.         elseif e[1] == "term_resize" then
  962.             w,h = term.getSize()
  963.             drawTopBar()
  964.             draw()
  965.         elseif e[1] == "peripheral" then
  966.             scanPeripherals(e[2])
  967.             createNote(e[2], "Attached")
  968.         elseif e[1] == "peripheral_detach" then
  969.             createNote(e[2], "Detached")
  970.             local success = false
  971.             local sides = {"top", "bottom", "front", "left", "right", "back"}
  972.             for i = 1,#peris["network"] do
  973.                 if e[2] == peris["network"][i] then
  974.                     table.remove(peris["network"], i)
  975.                     success = true
  976.                     break
  977.                 end
  978.             end
  979.             if not success then
  980.                 for i = 1,6 do
  981.                     if e[2] == sides[i] then
  982.                         peris["sides"][sides[i]] = nil
  983.                         break
  984.                     end
  985.                 end
  986.             end
  987.            
  988.             if e[2] == pointer then
  989.                 clear()
  990.                 term.setTextColor(colors.red)
  991.                 term.setCursorPos(1,3)
  992.                 term.write("Peripheral removed!")
  993.                 sleep(3)
  994.                 break
  995.             end
  996.         elseif e[1] == "note_center" then
  997.             drawTopBar()
  998.         elseif e[1] == "monitor_resize" then
  999.             local mw,mh = peripheral.call(e[2], "getSize")
  1000.             createNote(e[2], "Monitor size changed to "..mw.."x"..mh)
  1001.         elseif e[1] == "monitor_touch" then
  1002.             createNote(e[2], "touched at "..e[3]..", "..e[4])
  1003.         elseif e[1] == "modem_message" then
  1004.             processMessage(e[2], e[3], e[4], e[5], e[6])
  1005.         elseif e[1] == "key" and e[2] == keys.q then
  1006.             pmang.RUNNING = false
  1007.             break
  1008.         end
  1009.     end
  1010. end
  1011.  
  1012.  
  1013.  
  1014.  
  1015. local function printerPeripheral(pointer)
  1016.     displaymenu = "mainprinter"
  1017.     local pagew, pageh = 25,21
  1018.    
  1019.     local function clear()
  1020.         term.setBackgroundColor(colors.white)
  1021.         term.clear()
  1022.         drawTopBar()
  1023.         term.setBackgroundColor(colors.white)
  1024.         term.setTextColor(colors.black)
  1025.     end
  1026.     local function printFromFile()
  1027.        
  1028.         local msg = true
  1029.         local returnvalue = true
  1030.         local sourcepath = ""
  1031.         local canPrint = false
  1032.         local cenx = 0
  1033.         local printLines = {}
  1034.        
  1035.         local function readFile(path)
  1036.             local flines = {}
  1037.             for line in io.lines(path) do
  1038.                 table.insert(flines, line)
  1039.             end
  1040.             return flines
  1041.         end
  1042.        
  1043.         local function calculatePrintJob()
  1044.             printLines = {}
  1045.             local flines = readFile(sourcepath)
  1046.             local plevel = peripheral.call(pointer, "getPaperLevel")
  1047.             local ilevel = peripheral.call(pointer, "getInkLevel")
  1048.            
  1049.             for line = 1,#flines do
  1050.                 if flines[line]:len() > pagew then
  1051.                     table.insert(printLines, string.sub(flines[line],1,pagew))
  1052.                     for i = pagew+1,flines[line]:len(),pagew do
  1053.                         table.insert(printLines, string.sub(flines[line],i,i+pagew))
  1054.                     end
  1055.                 else
  1056.                     table.insert(printLines, flines[line])
  1057.                 end
  1058.             end
  1059.             msg = true
  1060.             if #printLines > plevel*pageh then
  1061.                 local excesslines = #printLines - (plevel*pageh)
  1062.                 msg = math.ceil(excesslines/pageh).." extra pages required"
  1063.             elseif #printLines > ilevel*pageh then
  1064.                 local excesslines = #printLines - (ilevel*pageh)
  1065.                 msg = math.ceil(excesslines/pageh).." extra ink required"
  1066.             end
  1067.             return msg
  1068.         end
  1069.        
  1070.         local function printFile()
  1071.             clear()
  1072.             term.setTextColor(colors.black)
  1073.             term.setBackgroundColor(colors.white)
  1074.             term.setCursorPos(2,3)
  1075.             term.write("Printing...")
  1076.            
  1077.             peripheral.call(pointer, "newPage")
  1078.             peripheral.call(pointer, "setPageTitle", fs.getName(sourcepath).." #1")
  1079.             local currentline = 1
  1080.             local currentpage = 1
  1081.             for i = 1,#printLines do
  1082.                 peripheral.call(pointer, "setCursorPos", 1, currentline)
  1083.                 peripheral.call(pointer, "write", printLines[i])
  1084.                 currentline = currentline+1
  1085.                 if currentline > pageh and i < #printLines then
  1086.                     currentline = 1
  1087.                     if not peripheral.call(pointer, "endPage") then
  1088.                         clear()
  1089.                         term.setTextColor(colors.red)
  1090.                         term.setBackgroundColor(colors.white)
  1091.                         term.setCursorPos(2,3)
  1092.                         print("Please remove printed pages to continue")
  1093.                         while not peripheral.call(pointer, "endPage") do
  1094.                             sleep(1)
  1095.                         end
  1096.                         clear()
  1097.                         term.setTextColor(colors.black)
  1098.                         term.setBackgroundColor(colors.white)
  1099.                         term.setCursorPos(2,3)
  1100.                         term.write("Printing...")
  1101.                     end
  1102.                     peripheral.call(pointer, "newPage")
  1103.                     currentpage = currentpage+1
  1104.                     peripheral.call(pointer, "setPageTitle", fs.getName(sourcepath).." #"..currentpage)
  1105.                 end
  1106.             end
  1107.             peripheral.call(pointer, "endPage")
  1108.         end
  1109.        
  1110.         local function draw()
  1111.             clear()
  1112.             term.setBackgroundColor(colors.lightGray)
  1113.             term.setTextColor(colors.white)
  1114.             term.setCursorPos(1,2)
  1115.             term.write(" << Back ")
  1116.            
  1117.             term.setBackgroundColor(colors.white)
  1118.             term.setTextColor(colors.black)
  1119.             term.setCursorPos((w-15)/2,4)
  1120.             term.write("Print from File")
  1121.            
  1122.             cenx = (w-19)/2
  1123.            
  1124.             term.setCursorPos(cenx,6)
  1125.             term.write("Source: "..sourcepath)
  1126.            
  1127.             term.setCursorPos(cenx,8)
  1128.             if msg == true then
  1129.                 term.write("Status: OK")
  1130.             else
  1131.                 term.write("Status: "..msg)
  1132.             end
  1133.            
  1134.             term.setBackgroundColor(colors.gray)
  1135.             term.setTextColor(colors.white)
  1136.             term.setCursorPos(cenx,10)
  1137.             term.write(" Refresh ")
  1138.            
  1139.             term.setCursorPos(cenx,12)
  1140.             term.write(" Browse ")
  1141.            
  1142.             if canPrint then
  1143.                 term.setBackgroundColor(colors.lime)
  1144.             else
  1145.                 term.setBackgroundColor(colors.red)
  1146.             end
  1147.             term.setCursorPos(cenx+12,12)
  1148.             term.write(" Print ")
  1149.         end
  1150.         draw()
  1151.         while true do
  1152.             local e = {os.pullEvent()}
  1153.             if e[1] == "mouse_click" and e[2] == 1 then
  1154.                 if e[3] == w and e[4] == 1 then
  1155.                     pmang.RUNNING = false
  1156.                     break
  1157.                 elseif e[3] >= 1 and e[3] <= 9 and e[4] == 2 then
  1158.                     break
  1159.                 elseif e[3] >= noteiconxpos and e[3] <= noteiconxpos+2 and e[4] == 1 then -- NOTE CENTER HANDLER
  1160.                     displaymenu = "notecenter"
  1161.                     drawNotes()
  1162.                     displaymenu = "printfromfile"
  1163.                     draw()
  1164.                 elseif e[3] >= 1 and e[3] <= 9 and e[4] == 2 then -- back button
  1165.                     break
  1166.                 elseif e[3] >= cenx and e[3] <= cenx+8 and e[4] == 10 then -- refresh button
  1167.                     msg = calculatePrintJob()
  1168.                     if msg == true then
  1169.                         canPrint = true
  1170.                     else
  1171.                         canPrint = false
  1172.                     end
  1173.                     draw()
  1174.                 elseif e[3] >= cenx and e[3] <= cenx+7 and e[4] == 12 then -- browse button
  1175.                     sourcepath = explorerDialog(pointer)
  1176.                     if sourcepath == "$$removed" then
  1177.                         returnvalue = false
  1178.                         break
  1179.                     end
  1180.                     msg = calculatePrintJob()
  1181.                     if msg == true then
  1182.                         canPrint = true
  1183.                     else
  1184.                         canPrint = false
  1185.                     end
  1186.                     draw()
  1187.                 elseif e[3] >= cenx+12 and e[3] <= cenx+18 and e[4] == 12 and canPrint then -- print button
  1188.                     printFile()
  1189.                     break
  1190.                 end
  1191.             elseif e[1] == "term_resize" then
  1192.                 w,h = term.getSize()
  1193.                 drawTopBar()
  1194.                 draw()
  1195.             elseif e[1] == "peripheral" then
  1196.                 scanPeripherals(e[2])
  1197.                 createNote(e[2], "Attached")
  1198.             elseif e[1] == "peripheral_detach" then
  1199.                 createNote(e[2], "Detached")
  1200.                 local success = false
  1201.                 local sides = {"top", "bottom", "front", "left", "right", "back"}
  1202.                 for i = 1,#peris["network"] do
  1203.                     if e[2] == peris["network"][i] then
  1204.                         table.remove(peris["network"], i)
  1205.                         success = true
  1206.                         break
  1207.                     end
  1208.                 end
  1209.                 if not success then
  1210.                     for i = 1,6 do
  1211.                         if e[2] == sides[i] then
  1212.                             peris["sides"][sides[i]] = nil
  1213.                             break
  1214.                         end
  1215.                     end
  1216.                 end
  1217.                
  1218.                 if e[2] == pointer then
  1219.                     returnvalue = false
  1220.                     break
  1221.                 end
  1222.             elseif e[1] == "note_center" then
  1223.                 drawTopBar()
  1224.             elseif e[1] == "disk" then
  1225.                 createNote(e[2], "Disk inserted")
  1226.             elseif e[1] == "disk_eject" then
  1227.                 createNote(e[2], "Disk ejected")
  1228.             elseif e[1] == "monitor_resize" then
  1229.                 local mw,mh = peripheral.call(e[2], "getSize")
  1230.                 createNote(e[2], "Monitor size changed to "..mw.."x"..mh)
  1231.             elseif e[1] == "monitor_touch" then
  1232.                 createNote(e[2], "touched at "..e[3]..", "..e[4])
  1233.             elseif e[1] == "modem_message" then
  1234.                 processMessage(e[2], e[3], e[4], e[5], e[6])
  1235.             elseif e[1] == "key" and e[2] == keys.q then
  1236.                 pmang.RUNNING = false
  1237.                 break
  1238.             end
  1239.         end
  1240.         return returnvalue
  1241.     end
  1242.     local function draw()
  1243.         clear()
  1244.         term.setBackgroundColor(colors.lightGray)
  1245.         term.setTextColor(colors.white)
  1246.         term.setCursorPos(1,2)
  1247.         term.write(" << Back ")
  1248.        
  1249.         term.setBackgroundColor(colors.white)
  1250.         term.setTextColor(colors.black)
  1251.         term.setCursorPos(3,4)
  1252.         term.write("Printer")
  1253.         drawPeri("printer",3,5)
  1254.         term.setBackgroundColor(colors.white)
  1255.         term.setCursorPos(10,6)
  1256.         term.write("Name: "..pointer)
  1257.         term.setCursorPos(10,7)
  1258.         term.write("Ink level: "..peripheral.call(pointer, "getInkLevel").."/64")
  1259.         term.setCursorPos(10,8)
  1260.         term.write("Paper level: "..peripheral.call(pointer, "getPaperLevel").."/384")
  1261.         term.setCursorPos(10,9)
  1262.         term.write("Paper size: 25x21")
  1263.        
  1264.         if peripheral.call(pointer, "getInkLevel") < 1 or peripheral.call(pointer, "getPaperLevel") < 1 then
  1265.             term.setBackgroundColor(colors.red)
  1266.         else
  1267.             term.setBackgroundColor(colors.gray)
  1268.         end
  1269.         term.setTextColor(colors.white)
  1270.         term.setCursorPos(5,11)
  1271.         term.write(" Print from File ")
  1272.     end
  1273.     draw()
  1274.     while true and pmang.RUNNING do
  1275.         local e = {os.pullEvent()}
  1276.         if e[1] == "mouse_click" and e[2] == 1 then
  1277.             if e[3] == w and e[4] == 1 then
  1278.                 pmang.RUNNING = false
  1279.                 break
  1280.             elseif e[3] >= 1 and e[3] <= 9 and e[4] == 2 then
  1281.                 break
  1282.             elseif e[3] >= noteiconxpos and e[3] <= noteiconxpos+2 and e[4] == 1 then -- NOTE CENTER HANDLER
  1283.                 displaymenu = "notecenter"
  1284.                 drawNotes()
  1285.                 displaymenu = "mainprinter"
  1286.                 draw()
  1287.             elseif e[3] >= 5 and e[3] <= 21 and e[4] == 11 then
  1288.                 if peripheral.call(pointer, "getInkLevel") > 0 and peripheral.call(pointer, "getPaperLevel") > 0 then
  1289.                     displaymenu = "printfromfile"
  1290.                     if printFromFile() then
  1291.                         displaymenu = "mainprinter"
  1292.                         draw()
  1293.                     else
  1294.                         clear()
  1295.                         term.setTextColor(colors.red)
  1296.                         term.setCursorPos(1,3)
  1297.                         term.write("Peripheral removed!")
  1298.                         sleep(3)
  1299.                         break
  1300.                     end
  1301.                 else
  1302.                     draw()
  1303.                 end
  1304.             end
  1305.         elseif e[1] == "term_resize" then
  1306.             w,h = term.getSize()
  1307.             drawTopBar()
  1308.             draw()
  1309.         elseif e[1] == "peripheral" then
  1310.             scanPeripherals(e[2])
  1311.             createNote(e[2], "Attached")
  1312.         elseif e[1] == "peripheral_detach" then
  1313.             createNote(e[2], "Detached")
  1314.             local success = false
  1315.             local sides = {"top", "bottom", "front", "left", "right", "back"}
  1316.             for i = 1,#peris["network"] do
  1317.                 if e[2] == peris["network"][i] then
  1318.                     table.remove(peris["network"], i)
  1319.                     success = true
  1320.                     break
  1321.                 end
  1322.             end
  1323.             if not success then
  1324.                 for i = 1,6 do
  1325.                     if e[2] == sides[i] then
  1326.                         peris["sides"][sides[i]] = nil
  1327.                         break
  1328.                     end
  1329.                 end
  1330.             end
  1331.            
  1332.             if e[2] == pointer then
  1333.                 clear()
  1334.                 term.setTextColor(colors.red)
  1335.                 term.setCursorPos(1,3)
  1336.                 term.write("Peripheral removed!")
  1337.                 sleep(3)
  1338.                 break
  1339.             end
  1340.         elseif e[1] == "note_center" then
  1341.             drawTopBar()
  1342.         elseif e[1] == "disk" then
  1343.             createNote(e[2], "Disk inserted")
  1344.         elseif e[1] == "disk_eject" then
  1345.             createNote(e[2], "Disk ejected")
  1346.         elseif e[1] == "monitor_resize" then
  1347.             local mw,mh = peripheral.call(e[2], "getSize")
  1348.             createNote(e[2], "Monitor size changed to "..mw.."x"..mh)
  1349.         elseif e[1] == "monitor_touch" then
  1350.             createNote(e[2], "touched at "..e[3]..", "..e[4])
  1351.         elseif e[1] == "modem_message" then
  1352.             processMessage(e[2], e[3], e[4], e[5], e[6])
  1353.         elseif e[1] == "key" and e[2] == keys.q then
  1354.             pmang.RUNNING = false
  1355.             break
  1356.         end
  1357.     end
  1358. end
  1359.  
  1360.  
  1361.  
  1362.  
  1363. local function monitorPeripheral(pointer)
  1364.     displaymenu = "mainmonitor"
  1365.    
  1366.     if peripheral.call(pointer, "isColor") then
  1367.         dispname = "Advanced Monitor"
  1368.         peritype = "amonitor"
  1369.     else
  1370.         dispname = "Monitor"
  1371.         peritype = "monitor"
  1372.     end
  1373.    
  1374.     local mw, mh = nil,nil
  1375.    
  1376.     local function clear()
  1377.         term.setBackgroundColor(colors.white)
  1378.         term.clear()
  1379.         drawTopBar()
  1380.         term.setBackgroundColor(colors.white)
  1381.         term.setTextColor(colors.black)
  1382.     end
  1383.     local function runProgram()
  1384.         local returnvalue = true
  1385.         local sourcepath = ""
  1386.         local cenx = 0
  1387.        
  1388.         local function draw()
  1389.             clear()
  1390.             term.setBackgroundColor(colors.lightGray)
  1391.             term.setTextColor(colors.white)
  1392.             term.setCursorPos(1,2)
  1393.             term.write(" << Back ")
  1394.            
  1395.             term.setBackgroundColor(colors.white)
  1396.             term.setTextColor(colors.black)
  1397.             term.setCursorPos((w-22)/2,3)
  1398.             term.write("Run Program on monitor")
  1399.            
  1400.             cenx = (w-17)/2
  1401.            
  1402.             term.setCursorPos(cenx,5)
  1403.             term.write("Source: "..sourcepath)
  1404.            
  1405.             term.setBackgroundColor(colors.gray)
  1406.             term.setTextColor(colors.white)
  1407.             term.setCursorPos(cenx,7)
  1408.             term.write(" Browse ")
  1409.             term.setCursorPos(cenx+11,7)
  1410.             term.write(" Run ")
  1411.         end
  1412.         draw()
  1413.         while true do
  1414.             local e = {os.pullEvent()}
  1415.             if e[1] == "mouse_click" and e[2] == 1 then
  1416.                 if e[3] == w and e[4] == 1 then
  1417.                     pmang.RUNNING = false
  1418.                     break
  1419.                 elseif e[3] >= 1 and e[3] <= 9 and e[4] == 2 then -- back button
  1420.                     break
  1421.                 elseif e[3] >= noteiconxpos and e[3] <= noteiconxpos+2 and e[4] == 1 then -- NOTE CENTER HANDLER
  1422.                     displaymenu = "notecenter"
  1423.                     drawNotes()
  1424.                     displaymenu = "mainmonitor"
  1425.                     draw()
  1426.                 elseif e[3] >= cenx and e[3] <= cenx+7 and e[4] == 7 then -- browse button
  1427.                     sourcepath = explorerDialog(pointer)
  1428.                     if sourcepath == "$$removed" then
  1429.                         returnvalue = false
  1430.                         break
  1431.                     end
  1432.                     draw()
  1433.                 elseif e[3] >= cenx+11 and e[3] <= cenx+15 and e[4] == 7 and sourcepath ~= "" then -- run button
  1434.                     term.setBackgroundColor(colors.lightGray)
  1435.                     term.setTextColor(colors.white)
  1436.                     local cenx = (w-10)/2
  1437.                     local ceny = (h-4)/2
  1438.                     term.setCursorPos(cenx,ceny)
  1439.                     term.write("Arguments ")
  1440.                     term.setCursorPos(cenx,ceny+1)
  1441.                     term.write("          ")
  1442.                     term.setCursorPos(cenx,ceny+2)
  1443.                     term.write("          ")
  1444.                     term.setCursorPos(cenx,ceny+3)
  1445.                     term.write("          ")
  1446.                    
  1447.                     term.setCursorPos(cenx+1, ceny+2)
  1448.                     local input = read()
  1449.                     shell.switchTab(shell.openTab("monitor", pointer, sourcepath, input))
  1450.                     break
  1451.                 end
  1452.             elseif e[1] == "term_resize" then
  1453.                 w,h = term.getSize()
  1454.                 drawTopBar()
  1455.                 draw()
  1456.             elseif e[1] == "peripheral" then
  1457.                 scanPeripherals(e[2])
  1458.                 createNote(e[2], "Attached")
  1459.             elseif e[1] == "peripheral_detach" then
  1460.                 createNote(e[2], "Detached")
  1461.                 local success = false
  1462.                 local sides = {"top", "bottom", "front", "left", "right", "back"}
  1463.                 for i = 1,#peris["network"] do
  1464.                     if e[2] == peris["network"][i] then
  1465.                         table.remove(peris["network"], i)
  1466.                         success = true
  1467.                         break
  1468.                     end
  1469.                 end
  1470.                 if not success then
  1471.                     for i = 1,6 do
  1472.                         if e[2] == sides[i] then
  1473.                             peris["sides"][sides[i]] = nil
  1474.                             break
  1475.                         end
  1476.                     end
  1477.                 end
  1478.                
  1479.                 if e[2] == pointer then
  1480.                     returnvalue = false
  1481.                     break
  1482.                 end
  1483.             elseif e[1] == "note_center" then
  1484.                 drawTopBar()
  1485.             elseif e[1] == "disk" then
  1486.                 createNote(e[2], "Disk inserted")
  1487.             elseif e[1] == "disk_eject" then
  1488.                 createNote(e[2], "Disk ejected")
  1489.             elseif e[1] == "monitor_resize" then
  1490.                 local mw,mh = peripheral.call(e[2], "getSize")
  1491.                 createNote(e[2], "Monitor size changed to "..mw.."x"..mh)
  1492.             elseif e[1] == "monitor_touch" then
  1493.                 createNote(e[2], "touched at "..e[3]..", "..e[4])
  1494.             elseif e[1] == "modem_message" then
  1495.                 processMessage(e[2], e[3], e[4], e[5], e[6])
  1496.             elseif e[1] == "key" and e[2] == keys.q then
  1497.                 pmang.RUNNING = false
  1498.                 break
  1499.             end
  1500.         end
  1501.         return returnvalue
  1502.     end
  1503.     local function draw()
  1504.         clear()
  1505.         term.setBackgroundColor(colors.lightGray)
  1506.         term.setTextColor(colors.white)
  1507.         term.setCursorPos(1,2)
  1508.         term.write(" << Back ")
  1509.        
  1510.         term.setBackgroundColor(colors.white)
  1511.         term.setTextColor(colors.black)
  1512.         term.setCursorPos(3,4)
  1513.         term.write(dispname)
  1514.         drawPeri(peritype,3,5)
  1515.         term.setBackgroundColor(colors.white)
  1516.         term.setCursorPos(10,6)
  1517.         term.write("Name: "..pointer)
  1518.         term.setCursorPos(10,7)
  1519.         mw,mh = peripheral.call(pointer, "getSize")
  1520.         term.write("Screen Size: "..mw.."x"..mh)
  1521.        
  1522.         term.setBackgroundColor(colors.gray)
  1523.         term.setTextColor(colors.white)
  1524.         term.setCursorPos(5,11)
  1525.         term.write(" Run program ")
  1526.     end
  1527.     draw()
  1528.     while true and pmang.RUNNING do
  1529.         local e = {os.pullEvent()}
  1530.         if e[1] == "mouse_click" and e[2] == 1 then
  1531.             if e[3] == w and e[4] == 1 then
  1532.                 pmang.RUNNING = false
  1533.                 break
  1534.             elseif e[3] >= 1 and e[3] <= 9 and e[4] == 2 then -- back button
  1535.                 break
  1536.             elseif e[3] >= noteiconxpos and e[3] <= noteiconxpos+2 and e[4] == 1 then -- NOTE CENTER HANDLER
  1537.                 displaymenu = "notecenter"
  1538.                 drawNotes()
  1539.                 displaymenu = "mainmonitor"
  1540.                 draw()
  1541.             elseif e[3] >= 5 and e[3] <= 17 and e[4] == 11 then -- run program
  1542.                 displaymenu = "runmonprogram"
  1543.                 if not runProgram() then
  1544.                     clear()
  1545.                     term.setTextColor(colors.red)
  1546.                     term.setCursorPos(1,3)
  1547.                     term.write("Peripheral removed!")
  1548.                     sleep(3)
  1549.                     break
  1550.                 else
  1551.                     displaymenu = "mainmonitor"
  1552.                     draw()
  1553.                 end
  1554.             end
  1555.         elseif e[1] == "term_resize" then
  1556.             w,h = term.getSize()
  1557.             drawTopBar()
  1558.             draw()
  1559.         elseif e[1] == "peripheral" then
  1560.             scanPeripherals(e[2])
  1561.             createNote(e[2], "Attached")
  1562.         elseif e[1] == "peripheral_detach" then
  1563.             createNote(e[2], "Detached")
  1564.             local success = false
  1565.             local sides = {"top", "bottom", "front", "left", "right", "back"}
  1566.             for i = 1,#peris["network"] do
  1567.                 if e[2] == peris["network"][i] then
  1568.                     table.remove(peris["network"], i)
  1569.                     success = true
  1570.                     break
  1571.                 end
  1572.             end
  1573.             if not success then
  1574.                 for i = 1,6 do
  1575.                     if e[2] == sides[i] then
  1576.                         peris["sides"][sides[i]] = nil
  1577.                         break
  1578.                     end
  1579.                 end
  1580.             end
  1581.            
  1582.             if e[2] == pointer then
  1583.                 clear()
  1584.                 term.setTextColor(colors.red)
  1585.                 term.setCursorPos(1,3)
  1586.                 term.write("Peripheral removed!")
  1587.                 sleep(3)
  1588.                 break
  1589.             end
  1590.         elseif e[1] == "note_center" then
  1591.             drawTopBar()
  1592.         elseif e[1] == "disk" then
  1593.             createNote(e[2], "Disk inserted")
  1594.         elseif e[1] == "disk_eject" then
  1595.             createNote(e[2], "Disk ejected")
  1596.         elseif e[1] == "monitor_resize" then
  1597.             local mw,mh = peripheral.call(e[2], "getSize")
  1598.             createNote(e[2], "Monitor size changed to "..mw.."x"..mh)
  1599.         elseif e[1] == "monitor_touch" then
  1600.             createNote(e[2], "touched at "..e[3]..", "..e[4])
  1601.         elseif e[1] == "modem_message" then
  1602.             processMessage(e[2], e[3], e[4], e[5], e[6])
  1603.         elseif e[1] == "key" and e[2] == keys.q then
  1604.             pmang.RUNNING = false
  1605.             break
  1606.         end
  1607.     end
  1608. end
  1609.  
  1610.  
  1611.  
  1612.  
  1613.  
  1614. local function modemPeripheral(pointer)
  1615.     displaymenu = "mainmodem"
  1616.    
  1617.     if peripheral.call(pointer, "isWireless") then
  1618.         dispname = "Wireless Modem"
  1619.         peritype = "wmodem"
  1620.     else
  1621.         dispname = "Wired Modem"
  1622.         peritype = "modem"
  1623.     end
  1624.    
  1625.     mchannels[pointer] = {}
  1626.    
  1627.     for i = 0,65535 do
  1628.         if peripheral.call(pointer, "isOpen", i) then
  1629.             table.insert(mchannels[pointer], i)
  1630.         end
  1631.     end
  1632.    
  1633.     local function sortNumbers(input)
  1634.         local terms = {}
  1635.         local range = {}
  1636.         if string.find(input, " ") then
  1637.             local termstart = 1
  1638.             for i = 1,input:len() do
  1639.                 if string.sub(input,i,i) == " " then
  1640.                     table.insert(terms, string.sub(input,termstart,i-1))
  1641.                     termstart = i+1
  1642.                 end
  1643.             end
  1644.         end
  1645.         for i = 1,#terms do
  1646.             if string.find(terms[i], "-") then -- range
  1647.                 local ref = terms[i]
  1648.                 for l = 1,ref:len() do
  1649.                     if string.sub(ref,l,l) == "-" then
  1650.                         local termstart = string.sub(ref,1,l-1)
  1651.                         local termend = string.sub(ref,l+1,ref:len())
  1652.                         if termend-termstart < 129 then
  1653.                             for n = termstart,termend do
  1654.                                 table.insert(range,n)
  1655.                             end
  1656.                         end
  1657.                     end
  1658.                 end
  1659.             else
  1660.                 table.insert(range, terms[i])
  1661.             end
  1662.         end
  1663.         return range
  1664.     end
  1665.    
  1666.     local function reverseSort(range)
  1667.         local terms = ""
  1668.         local termstart = range[1]
  1669.         local nextterm = termstart
  1670.         for i = 1,#range do
  1671.             nextterm = nextterm+1
  1672.             if range[i+1] ~= nextterm then
  1673.                 if nextterm-termstart > 1 then
  1674.                     terms = terms .. termstart.."-"..nextterm..","
  1675.                 else
  1676.                     terms = terms .. termstart..","
  1677.                 end
  1678.                 termstart = range[i+1]
  1679.             end
  1680.         end
  1681.         return terms
  1682.     end
  1683.    
  1684.     local function removeChannel(chan)
  1685.         peripheral.call(pointer, "close", tonumber(chan))
  1686.         for i = 1,#mchannels[pointer] do
  1687.             if tonumber(chan) == mchannels[pointer][i] then
  1688.                 table.remove(mchannels[pointer], i)
  1689.                 break
  1690.             end
  1691.         end
  1692.     end
  1693.    
  1694.     local function clear()
  1695.         term.setBackgroundColor(colors.white)
  1696.         term.clear()
  1697.         drawTopBar()
  1698.         term.setBackgroundColor(colors.white)
  1699.         term.setTextColor(colors.black)
  1700.     end
  1701.     local function sendMessages()
  1702.         local returnvalue = true
  1703.         local cenx = 0
  1704.         local chan = mchannels[pointer][1] or 1
  1705.         local repchan = mchannels[pointer][1] or 1
  1706.         local message = ""
  1707.        
  1708.         local function draw()
  1709.             clear()
  1710.             term.setBackgroundColor(colors.lightGray)
  1711.             term.setTextColor(colors.white)
  1712.             term.setCursorPos(1,2)
  1713.             term.write(" << Back ")
  1714.            
  1715.             term.setBackgroundColor(colors.white)
  1716.             term.setTextColor(colors.black)
  1717.             term.setCursorPos((w-13)/2,3)
  1718.             term.write("Send Messages")
  1719.            
  1720.             term.setCursorPos(15,5)
  1721.             term.write("Channel: "..chan)
  1722.            
  1723.             term.setCursorPos(15,7)
  1724.             term.write("Reply Channel: "..repchan)
  1725.            
  1726.             term.setCursorPos(15,9)
  1727.             term.write("Message: "..message)
  1728.            
  1729.             term.setBackgroundColor(colors.gray)
  1730.             term.setTextColor(colors.white)
  1731.             term.setCursorPos(9,5)
  1732.             term.write(" Set ")
  1733.             term.setCursorPos(9,7)
  1734.             term.write(" Set ")
  1735.             term.setCursorPos(9,9)
  1736.             term.write(" Set ")
  1737.            
  1738.             cenx = (w-6)/2
  1739.             term.setCursorPos(cenx,11)
  1740.             term.write(" Send ")
  1741.         end
  1742.         draw()
  1743.         while true do
  1744.             local e = {os.pullEvent()}
  1745.             if e[1] == "mouse_click" and e[2] == 1 then
  1746.                 if e[3] == w and e[4] == 1 then
  1747.                     pmang.RUNNING = false
  1748.                     break
  1749.                 elseif e[3] >= 1 and e[3] <= 9 and e[4] == 2 then
  1750.                     break
  1751.                 elseif e[3] >= noteiconxpos and e[3] <= noteiconxpos+2 and e[4] == 1 then -- NOTE CENTER HANDLER
  1752.                     displaymenu = "notecenter"
  1753.                     drawNotes()
  1754.                     displaymenu = "mainmonitor"
  1755.                     draw()
  1756.                 elseif e[3] >= 9 and e[3] <= 13 then
  1757.                     if e[4] == 5 then -- set channel
  1758.                         term.setBackgroundColor(colors.lightGray)
  1759.                         term.setTextColor(colors.white)
  1760.                         local cenx = (w-10)/2
  1761.                         local ceny = (h-4)/2
  1762.                         term.setCursorPos(cenx,ceny)
  1763.                         term.write("Channel   ")
  1764.                         term.setCursorPos(cenx,ceny+1)
  1765.                         term.write("          ")
  1766.                         term.setCursorPos(cenx,ceny+2)
  1767.                         term.write("          ")
  1768.                         term.setCursorPos(cenx,ceny+3)
  1769.                         term.write("          ")
  1770.                        
  1771.                         term.setCursorPos(cenx+1,ceny+2)
  1772.                         local input = read()
  1773.                         if tonumber(input) then
  1774.                             chan = tonumber(input)
  1775.                         end
  1776.                         draw()
  1777.                     elseif e[4] == 7 then -- set reply channel
  1778.                         term.setBackgroundColor(colors.lightGray)
  1779.                         term.setTextColor(colors.white)
  1780.                         local cenx = (w-10)/2
  1781.                         local ceny = (h-4)/2
  1782.                         term.setCursorPos(cenx,ceny)
  1783.                         term.write("Reply     ")
  1784.                         term.setCursorPos(cenx,ceny+1)
  1785.                         term.write("          ")
  1786.                         term.setCursorPos(cenx,ceny+2)
  1787.                         term.write("          ")
  1788.                         term.setCursorPos(cenx,ceny+3)
  1789.                         term.write("          ")
  1790.                        
  1791.                         term.setCursorPos(cenx+1,ceny+2)
  1792.                         local input = read()
  1793.                         if tonumber(input) then
  1794.                             repchan = tonumber(input)
  1795.                         end
  1796.                         draw()
  1797.                     elseif e[4] == 9 then -- set message
  1798.                         term.setBackgroundColor(colors.lightGray)
  1799.                         term.setTextColor(colors.white)
  1800.                         local cenx = (w-10)/2
  1801.                         local ceny = (h-4)/2
  1802.                         term.setCursorPos(cenx,ceny)
  1803.                         term.write("Message   ")
  1804.                         term.setCursorPos(cenx,ceny+1)
  1805.                         term.write("          ")
  1806.                         term.setCursorPos(cenx,ceny+2)
  1807.                         term.write("          ")
  1808.                         term.setCursorPos(cenx,ceny+3)
  1809.                         term.write("          ")
  1810.                        
  1811.                         term.setCursorPos(cenx+1,ceny+2)
  1812.                         message = read()
  1813.                         draw()
  1814.                     end
  1815.                 elseif e[3] >= cenx and e[3] <= cenx+5 and e[4] == 11 then -- send
  1816.                     peripheral.call(pointer, "transmit", chan, repchan, message)
  1817.                     break
  1818.                 end
  1819.             elseif e[1] == "term_resize" then
  1820.                 w,h = term.getSize()
  1821.                 drawTopBar()
  1822.                 draw()
  1823.             elseif e[1] == "peripheral" then
  1824.                 scanPeripherals(e[2])
  1825.                 createNote(e[2], "Attached")
  1826.             elseif e[1] == "peripheral_detach" then
  1827.                 createNote(e[2], "Detached")
  1828.                 local success = false
  1829.                 local sides = {"top", "bottom", "front", "left", "right", "back"}
  1830.                 for i = 1,#peris["network"] do
  1831.                     if e[2] == peris["network"][i] then
  1832.                         table.remove(peris["network"], i)
  1833.                         success = true
  1834.                         break
  1835.                     end
  1836.                 end
  1837.                 if not success then
  1838.                     for i = 1,6 do
  1839.                         if e[2] == sides[i] then
  1840.                             peris["sides"][sides[i]] = nil
  1841.                             break
  1842.                         end
  1843.                     end
  1844.                 end
  1845.                
  1846.                 if e[2] == pointer then
  1847.                     returnvalue = false
  1848.                     break
  1849.                 end
  1850.             elseif e[1] == "note_center" then
  1851.                 drawTopBar()
  1852.             elseif e[1] == "disk" then
  1853.                 createNote(e[2], "Disk inserted")
  1854.             elseif e[1] == "disk_eject" then
  1855.                 createNote(e[2], "Disk ejected")
  1856.             elseif e[1] == "monitor_resize" then
  1857.                 local mw,mh = peripheral.call(e[2], "getSize")
  1858.                 createNote(e[2], "Monitor size changed to "..mw.."x"..mh)
  1859.             elseif e[1] == "monitor_touch" then
  1860.                 createNote(e[2], "touched at "..e[3]..", "..e[4])
  1861.             elseif e[1] == "modem_message" then
  1862.                 processMessage(e[2], e[3], e[4], e[5], e[6])
  1863.             elseif e[1] == "key" and e[2] == keys.q then
  1864.                 pmang.RUNNING = false
  1865.                 break
  1866.             end
  1867.         end
  1868.        
  1869.         return returnvalue
  1870.     end
  1871.     local function draw()
  1872.         clear()
  1873.         term.setBackgroundColor(colors.lightGray)
  1874.         term.setTextColor(colors.white)
  1875.         term.setCursorPos(1,2)
  1876.         term.write(" << Back ")
  1877.        
  1878.         term.setBackgroundColor(colors.white)
  1879.         term.setTextColor(colors.black)
  1880.         term.setCursorPos(3,4)
  1881.         term.write(dispname)
  1882.         drawPeri(peritype,3,5)
  1883.         term.setBackgroundColor(colors.white)
  1884.         term.setCursorPos(10,6)
  1885.         term.write("Name: "..pointer)
  1886.         term.setCursorPos(10,7)
  1887.         term.write("Open Channels: ")
  1888.         term.setCursorPos(10,8)
  1889.         -- term.write(reverseSort(channels))
  1890.         for i = 1,#mchannels[pointer] do
  1891.             term.write(mchannels[pointer][i]..",")
  1892.         end
  1893.        
  1894.         term.setBackgroundColor(colors.gray)
  1895.         term.setTextColor(colors.white)
  1896.         term.setCursorPos(5,11)
  1897.         term.write(" Open channel ")
  1898.         term.setCursorPos(5,13)
  1899.         term.write(" Close channel ")
  1900.         term.setCursorPos(5,15)
  1901.         term.write(" Close all ")
  1902.         term.setCursorPos(5,17)
  1903.         term.write(" Send ")
  1904.     end
  1905.     draw()
  1906.     while true and pmang.RUNNING do
  1907.         local e = {os.pullEvent()}
  1908.         if e[1] == "mouse_click" and e[2] == 1 then
  1909.             if e[3] == w and e[4] == 1 then
  1910.                 pmang.RUNNING = false
  1911.                 break
  1912.             elseif e[3] >= 1 and e[3] <= 9 and e[4] == 2 then
  1913.                 break
  1914.             elseif e[3] >= noteiconxpos and e[3] <= noteiconxpos+2 and e[4] == 1 then -- NOTE CENTER HANDLER
  1915.                 displaymenu = "notecenter"
  1916.                 drawNotes()
  1917.                 displaymenu = "mainmonitor"
  1918.                 draw()
  1919.             elseif e[3] >= 5 and e[3] <= 18 and e[4] == 11 then -- open channel
  1920.                 term.setBackgroundColor(colors.lightGray)
  1921.                 term.setTextColor(colors.white)
  1922.                 local cenx = (w-10)/2
  1923.                 local ceny = (h-4)/2
  1924.                 term.setCursorPos(cenx,ceny)
  1925.                 term.write("Open      ")
  1926.                 term.setCursorPos(cenx,ceny+1)
  1927.                 term.write("          ")
  1928.                 term.setCursorPos(cenx,ceny+2)
  1929.                 term.write("          ")
  1930.                 term.setCursorPos(cenx,ceny+3)
  1931.                 term.write("          ")
  1932.                
  1933.                 term.setCursorPos(cenx+1,ceny+2)
  1934.                 local input = read()
  1935.                 if input ~= "" then
  1936.                     --[[if string.find(input, "-") or string.find(input, " ") then
  1937.                         local newchannels = sortNumbers(input)
  1938.                         for i = 1,#newchannels do
  1939.                             if not peripheral.call(pointer, "isOpen", newchannels[i]) then
  1940.                                 peripheral.call(pointer, "open", newchannels[i])
  1941.                             end
  1942.                         end
  1943.                     else]]--
  1944.                         if tonumber(input) then
  1945.                             peripheral.call(pointer, "open", tonumber(input))
  1946.                             table.insert(mchannels[pointer], tonumber(input))
  1947.                         end
  1948.                     --end
  1949.                 end
  1950.                 draw()
  1951.             elseif e[3] >= 5 and e[3] <= 19 and e[4] == 13 then -- close channel
  1952.                 term.setBackgroundColor(colors.lightGray)
  1953.                 term.setTextColor(colors.white)
  1954.                 local cenx = (w-10)/2
  1955.                 local ceny = (h-4)/2
  1956.                 term.setCursorPos(cenx,ceny)
  1957.                 term.write("Close     ")
  1958.                 term.setCursorPos(cenx,ceny+1)
  1959.                 term.write("          ")
  1960.                 term.setCursorPos(cenx,ceny+2)
  1961.                 term.write("          ")
  1962.                 term.setCursorPos(cenx,ceny+3)
  1963.                 term.write("          ")
  1964.                
  1965.                 term.setCursorPos(cenx+1,ceny+2)
  1966.                 local input = read()
  1967.                 if input ~= "" then
  1968.                     --[[if string.find(input, "-") or string.find(input, " ") then
  1969.                         local newchannels = sortNumbers(input)
  1970.                         for i = 1,#newchannels do
  1971.                             peripheral.call(pointer, "close", newchannels[i])
  1972.                         end
  1973.                     else]]--
  1974.                         if tonumber(input) then removeChannel(input) end
  1975.                     --end
  1976.                 end
  1977.                 draw()
  1978.             elseif e[3] >= 5 and e[3] <= 15 and e[4] == 15 then -- close all
  1979.                 peripheral.call(pointer, "closeAll")
  1980.                 mchannels[pointer] = {}
  1981.                 draw()
  1982.             elseif e[3] >= 5 and e[3] <= 11 and e[4] == 17 then -- send
  1983.                 displaymenu = "modemsend"
  1984.                 if not sendMessages() then
  1985.                     clear()
  1986.                     term.setTextColor(colors.red)
  1987.                     term.setCursorPos(1,3)
  1988.                     term.write("Peripheral removed!")
  1989.                     mchannels[pointer] = {}
  1990.                     sleep(3)
  1991.                     break
  1992.                 else
  1993.                     displaymenu = "mainmodem"
  1994.                     draw()
  1995.                 end
  1996.             end
  1997.         elseif e[1] == "term_resize" then
  1998.             w,h = term.getSize()
  1999.             drawTopBar()
  2000.             draw()
  2001.         elseif e[1] == "peripheral" then
  2002.             scanPeripherals(e[2])
  2003.             createNote(e[2], "Attached")
  2004.         elseif e[1] == "peripheral_detach" then
  2005.             createNote(e[2], "Detached")
  2006.             local success = false
  2007.             local sides = {"top", "bottom", "front", "left", "right", "back"}
  2008.             for i = 1,#peris["network"] do
  2009.                 if e[2] == peris["network"][i] then
  2010.                     table.remove(peris["network"], i)
  2011.                     success = true
  2012.                     break
  2013.                 end
  2014.             end
  2015.             if not success then
  2016.                 for i = 1,6 do
  2017.                     if e[2] == sides[i] then
  2018.                         peris["sides"][sides[i]] = nil
  2019.                         break
  2020.                     end
  2021.                 end
  2022.             end
  2023.            
  2024.             if e[2] == pointer then
  2025.                 clear()
  2026.                 term.setTextColor(colors.red)
  2027.                 term.setCursorPos(1,3)
  2028.                 term.write("Peripheral removed!")
  2029.                 mchannels[pointer] = {}
  2030.                 sleep(3)
  2031.                 break
  2032.             end
  2033.         elseif e[1] == "note_center" then
  2034.             drawTopBar()
  2035.         elseif e[1] == "disk" then
  2036.             createNote(e[2], "Disk inserted")
  2037.         elseif e[1] == "disk_eject" then
  2038.             createNote(e[2], "Disk ejected")
  2039.         elseif e[1] == "monitor_resize" then
  2040.             local mw,mh = peripheral.call(e[2], "getSize")
  2041.             createNote(e[2], "Monitor size changed to "..mw.."x"..mh)
  2042.         elseif e[1] == "monitor_touch" then
  2043.             createNote(e[2], "touched at "..e[3]..", "..e[4])
  2044.         elseif e[1] == "modem_message" then
  2045.             processMessage(e[2], e[3], e[4], e[5], e[6])
  2046.         elseif e[1] == "key" and e[2] == keys.q then
  2047.             pmang.RUNNING = false
  2048.             break
  2049.         end
  2050.     end
  2051. end
  2052.  
  2053.  
  2054.  
  2055.  
  2056.  
  2057. local function computerPeripheral(pointer)
  2058.     displaymenu = "maincomputer"
  2059.     local status = peripheral.call(pointer, "isOn")
  2060.     local function clear()
  2061.         term.setBackgroundColor(colors.white)
  2062.         term.clear()
  2063.         drawTopBar()
  2064.         term.setBackgroundColor(colors.white)
  2065.         term.setTextColor(colors.black)
  2066.     end
  2067.     local function draw()
  2068.         clear()
  2069.         term.setBackgroundColor(colors.lightGray)
  2070.         term.setTextColor(colors.white)
  2071.         term.setCursorPos(1,2)
  2072.         term.write(" << Back ")
  2073.        
  2074.         term.setBackgroundColor(colors.white)
  2075.         term.setTextColor(colors.black)
  2076.         term.setCursorPos(3,4)
  2077.         term.write("Computer")
  2078.         drawPeri("computer",3,5)
  2079.        
  2080.         term.setBackgroundColor(colors.white)
  2081.         term.setCursorPos(10,6)
  2082.         term.write("Name: "..pointer)
  2083.         term.setCursorPos(10,7)
  2084.         term.write("ID: "..peripheral.call(pointer, "getID"))
  2085.        
  2086.         term.setCursorPos(10,8)
  2087.         term.write("Status: ")
  2088.         status = peripheral.call(pointer, "isOn")
  2089.         if status then
  2090.             term.setTextColor(colors.lime)
  2091.             term.write("Online")
  2092.         else
  2093.             term.setTextColor(colors.lightGray)
  2094.             term.write("Offline")
  2095.         end
  2096.        
  2097.         term.setBackgroundColor(colors.gray)
  2098.         term.setTextColor(colors.white)
  2099.         term.setCursorPos(5,11)
  2100.         term.write(" Refresh ")
  2101.         term.setCursorPos(5,13)
  2102.         if status then
  2103.             term.write(" Shutdown ")
  2104.             term.setCursorPos(5,15)
  2105.             term.write(" Reboot ")
  2106.         else
  2107.             term.write(" Turn On ")
  2108.         end
  2109.     end
  2110.     draw()
  2111.     while true do
  2112.         local e = {os.pullEvent()}
  2113.         if e[1] == "mouse_click" and e[2] == 1 then
  2114.             if e[3] == w and e[4] == 1 then
  2115.                 pmang.RUNNING = false
  2116.                 break
  2117.             elseif e[3] >= 1 and e[3] <= 9 and e[4] == 2 then -- back button
  2118.                 break
  2119.             elseif e[3] >= noteiconxpos and e[3] <= noteiconxpos+2 and e[4] == 1 then -- NOTE CENTER HANDLER
  2120.                 displaymenu = "notecenter"
  2121.                 drawNotes()
  2122.                 displaymenu = "mainmonitor"
  2123.                 draw()
  2124.             elseif e[3] >= 5 and e[3] <= 13 and e[4] == 11 then -- refresh button
  2125.                 status = peripheral.call(pointer, "isOn")
  2126.                 draw()
  2127.             else
  2128.                 if status then
  2129.                     if e[3] >= 5 and e[3] <= 14 and e[4] == 13 then -- shutdown button
  2130.                         peripheral.call(pointer, "shutdown")
  2131.                         -- status = false
  2132.                         draw()
  2133.                     elseif e[3] >= 5 and e[3] <= 12 and e[4] == 15 then -- reboot button
  2134.                         peripheral.call(pointer, "reboot")
  2135.                     end
  2136.                 else
  2137.                     if e[3] >= 5 and e[3] <= 13 and e[4] == 13 then -- turn on button
  2138.                         peripheral.call(pointer, "turnOn")
  2139.                         sleep(0.1)
  2140.                         -- status = true
  2141.                         draw()
  2142.                     end
  2143.                 end
  2144.             end
  2145.         elseif e[1] == "term_resize" then
  2146.             w,h = term.getSize()
  2147.             drawTopBar()
  2148.             draw()
  2149.         elseif e[1] == "peripheral" then
  2150.             scanPeripherals(e[2])
  2151.             createNote(e[2], "Attached")
  2152.         elseif e[1] == "peripheral_detach" then
  2153.             createNote(e[2], "Detached")
  2154.             local success = false
  2155.             local sides = {"top", "bottom", "front", "left", "right", "back"}
  2156.             for i = 1,#peris["network"] do
  2157.                 if e[2] == peris["network"][i] then
  2158.                     table.remove(peris["network"], i)
  2159.                     success = true
  2160.                     break
  2161.                 end
  2162.             end
  2163.             if not success then
  2164.                 for i = 1,6 do
  2165.                     if e[2] == sides[i] then
  2166.                         peris["sides"][sides[i]] = nil
  2167.                         break
  2168.                     end
  2169.                 end
  2170.             end
  2171.            
  2172.             if e[2] == pointer then
  2173.                 clear()
  2174.                 term.setTextColor(colors.red)
  2175.                 term.setCursorPos(1,3)
  2176.                 term.write("Peripheral removed!")
  2177.                 sleep(3)
  2178.                 break
  2179.             end
  2180.         elseif e[1] == "note_center" then
  2181.             drawTopBar()
  2182.         elseif e[1] == "disk" then
  2183.             createNote(e[2], "Disk inserted")
  2184.         elseif e[1] == "disk_eject" then
  2185.             createNote(e[2], "Disk ejected")
  2186.         elseif e[1] == "monitor_resize" then
  2187.             local mw,mh = peripheral.call(e[2], "getSize")
  2188.             createNote(e[2], "Monitor size changed to "..mw.."x"..mh)
  2189.         elseif e[1] == "monitor_touch" then
  2190.             createNote(e[2], "touched at "..e[3]..", "..e[4])
  2191.         elseif e[1] == "modem_message" then
  2192.             processMessage(e[2], e[3], e[4], e[5], e[6])
  2193.         elseif e[1] == "key" and e[2] == keys.q then
  2194.             pmang.RUNNING = false
  2195.             break
  2196.         end
  2197.     end
  2198. end
  2199.  
  2200.  
  2201.  
  2202.  
  2203.  
  2204.  
  2205.  
  2206.  
  2207.  
  2208.  
  2209.  
  2210.  
  2211.  
  2212.  
  2213.  
  2214.  
  2215.  
  2216.  
  2217.  
  2218.  
  2219.  
  2220. local function redirectToType(pointer, isSide)
  2221.     if isSide then
  2222.         local ref = peris["sides"][pointer]
  2223.         if ref == "drive" then
  2224.             drivePeripheral(pointer)
  2225.         elseif ref == "printer" then
  2226.             printerPeripheral(pointer)
  2227.         elseif ref == "monitor" or ref == "amonitor" then
  2228.             monitorPeripheral(pointer)
  2229.         elseif ref == "modem" or ref == "wmodem" then
  2230.             modemPeripheral(pointer)
  2231.         elseif ref == "computer" then
  2232.             computerPeripheral(pointer)
  2233.         end
  2234.     else
  2235.         local undscrpos = string.find(pointer, "_")
  2236.         local ref = string.sub(pointer, 1, undscrpos-1)
  2237.         if ref == "drive" then
  2238.             drivePeripheral(pointer)
  2239.         elseif ref == "printer" then
  2240.             printerPeripheral(pointer)
  2241.         elseif ref == "monitor" then
  2242.             monitorPeripheral(pointer)
  2243.         elseif ref == "modem" then
  2244.             modemPeripheral(pointer)
  2245.         elseif ref == "computer" then
  2246.             computerPeripheral(pointer)
  2247.         end
  2248.     end
  2249. end
  2250.  
  2251.  
  2252.  
  2253.  
  2254.  
  2255.  
  2256. -- Main menu
  2257. local function box(startx,starty)
  2258.    
  2259.     -- Box width: 11
  2260.     -- Box Height: 7
  2261.     -- table width: 41
  2262.     -- table height: 15
  2263.    
  2264.     term.setCursorPos(startx,starty)
  2265.     term.write("___________")
  2266.     for y = starty+1,starty+5 do
  2267.         term.setCursorPos(startx,y)
  2268.         term.write("|         |")
  2269.     end
  2270.     term.setCursorPos(startx,starty+6)
  2271.     term.write("-----------")
  2272. end
  2273. local function drawBoxes()
  2274.     term.setBackgroundColor(colors.white)
  2275.     term.setTextColor(colors.lightGray)
  2276.     if displaymode == "full" then
  2277.         local starty = h-16
  2278.         local startx = w-45
  2279.         boxes["top"].x = startx
  2280.         boxes["top"].y = starty
  2281.         boxes["bottom"].x = startx+15
  2282.         boxes["bottom"].y = starty
  2283.         boxes["front"].x = startx+30
  2284.         boxes["front"].y = starty
  2285.         boxes["left"].x = startx
  2286.         boxes["left"].y = starty+8
  2287.         boxes["right"].x = startx+15
  2288.         boxes["right"].y = starty+8
  2289.         boxes["back"].x = startx+30
  2290.         boxes["back"].y = starty+8
  2291.         local sides = {"top", "bottom", "front", "left", "right", "back"}
  2292.         local sidestep = 1
  2293.         for x = startx,startx+30,15 do
  2294.             box(x,starty)
  2295.             local cenx = (11-sides[sidestep]:len())/2
  2296.             term.setCursorPos(x+cenx,starty+1)
  2297.             term.write(sides[sidestep])
  2298.             sidestep = sidestep+1
  2299.         end
  2300.         starty = starty+8
  2301.         for x = startx,startx+30,15 do
  2302.             box(x,starty)
  2303.             local cenx = (11-sides[sidestep]:len())/2
  2304.             term.setCursorPos(x+cenx,starty+1)
  2305.             term.write(sides[sidestep])
  2306.             sidestep = sidestep+1
  2307.         end
  2308.     end
  2309. end
  2310. local function drawPeripherals()
  2311.     if displaymenu == "main" then
  2312.         if displaymode == "full" then
  2313.             local xicon = 3
  2314.             local yicon = 1
  2315.             if peris["sides"]["top"] ~= nil then
  2316.                 drawPeri(peris["sides"]["top"], boxes["top"].x+xicon, boxes["top"].y+yicon)
  2317.             end
  2318.             if peris["sides"]["bottom"] ~= nil then
  2319.                 drawPeri(peris["sides"]["bottom"], boxes["bottom"].x+xicon, boxes["bottom"].y+yicon)
  2320.             end
  2321.             if peris["sides"]["front"] ~= nil then
  2322.                 drawPeri(peris["sides"]["front"], boxes["front"].x+xicon, boxes["front"].y+yicon)
  2323.             end
  2324.             if peris["sides"]["left"] ~= nil then
  2325.                 drawPeri(peris["sides"]["left"], boxes["left"].x+xicon, boxes["left"].y+yicon)
  2326.             end
  2327.             if peris["sides"]["right"] ~= nil then
  2328.                 drawPeri(peris["sides"]["right"], boxes["right"].x+xicon, boxes["left"].y+yicon)
  2329.             end
  2330.             if peris["sides"]["back"] ~= nil then
  2331.                 drawPeri(peris["sides"]["back"], boxes["back"].x+xicon, boxes["left"].y+yicon)
  2332.             end
  2333.             local sides = {"top", "bottom", "front", "left", "right", "back"}
  2334.             term.setTextColor(colors.black)
  2335.             term.setBackgroundColor(colors.white)
  2336.             for i = 1,6 do
  2337.                 local ref = peris["sides"][sides[i]]
  2338.                 local dispname = nil
  2339.                 if ref ~= nil then
  2340.                     if ref == "drive" then dispname = "Disk Drive"
  2341.                     elseif ref == "printer" then dispname = "Printer"
  2342.                     elseif ref == "monitor" then dispname = "Monitor"
  2343.                     elseif ref == "amonitor" then dispname = "Adv Monitor"
  2344.                     elseif ref == "modem" then dispname = "Wired Modem"
  2345.                     elseif ref == "wmodem" then dispname = "Wireless Modem"
  2346.                     elseif ref == "computer" then dispname = "Computer"
  2347.                     end
  2348.                     local cenx = (11-dispname:len())/2
  2349.                     term.setCursorPos(cenx+boxes[sides[i]].x, boxes[sides[i]].y+7)
  2350.                     term.write(dispname)
  2351.                 end
  2352.             end
  2353.         elseif displaymode == "frag" then
  2354.             local sides = {"top", "bottom", "front", "left", "right", "back"}
  2355.             term.setTextColor(colors.white)
  2356.             local y = ((h-12)/2)-1
  2357.             for i = 1,6 do
  2358.                 if peris["sides"][sides[i]] ~= nil then
  2359.                     local ref = peris["sides"][sides[i]]
  2360.                     if ref == "drive" then dispname = "Disk  Drive"
  2361.                     elseif ref == "printer" then dispname = "Printer"
  2362.                     elseif ref == "monitor" then dispname = "Monitor"
  2363.                     elseif ref == "amonitor" then dispname = "Adv Monitor"
  2364.                     elseif ref == "modem" then dispname = "Wired Modem"
  2365.                     elseif ref == "wmodem" then dispname = "Wireless Modem"
  2366.                     end
  2367.                     local cenx = (w-dispname:len())/2
  2368.                     term.setBackgroundColor(colors.lime)
  2369.                     term.setCursorPos(cenx, y+i)
  2370.                     term.write(" "..dispname.." ")
  2371.                 else
  2372.                     local cenx = (w-sides[i]:len())/2
  2373.                     term.setBackgroundColor(colors.red)
  2374.                     term.setCursorPos(cenx, y+i)
  2375.                     term.write(" "..sides[i].." ")
  2376.                 end
  2377.                 y = y+1
  2378.             end
  2379.         end
  2380.     elseif displaymenu == "mainall" then
  2381.         term.setBackgroundColor(colors.white)
  2382.         term.clear()
  2383.         drawTopBar()
  2384.         local scrollpos = 1
  2385.         local pospossible = 1
  2386.         local data = {}
  2387.         local RUNNING = true
  2388.         local function clear()
  2389.             term.setBackgroundColor(colors.white)
  2390.             term.clear()
  2391.             drawTopBar()
  2392.             term.setBackgroundColor(colors.white)
  2393.             term.setTextColor(colors.black)
  2394.         end
  2395.         local function draw()
  2396.             clear()
  2397.             term.setBackgroundColor(colors.lightGray)
  2398.             term.setTextColor(colors.white)
  2399.             term.setCursorPos(1,2)
  2400.             term.write(" << Back ")
  2401.             term.setBackgroundColor(colors.white)
  2402.             term.setTextColor(colors.black)
  2403.             term.write(" All Peripherals")
  2404.            
  2405.             local sides = {"top", "bottom", "front", "left", "right", "back"}
  2406.             data = {}
  2407.             for i = 1,6 do
  2408.                 if peris["sides"][sides[i]] ~= nil then
  2409.                     table.insert(data, sides[i])
  2410.                 end
  2411.             end
  2412.             for i = 1,#peris["network"] do
  2413.                 if peris["network"][i] ~= nil then
  2414.                     table.insert(data, peris["network"][i])
  2415.                 end
  2416.             end
  2417.             local greatestpos = 1
  2418.             for i = 1,#data do
  2419.                 local ref = data[i]:len()
  2420.                 if ref > greatestpos then
  2421.                     greatestpos = ref
  2422.                 end
  2423.             end
  2424.             pospossible = #data-(h-3)
  2425.             if pospossible < 1 then pospossible = 1 end
  2426.            
  2427.             term.setCursorPos(2,3)
  2428.             for i = scrollpos,#data do
  2429.                 if i > #data then break end
  2430.                 term.write(data[i])
  2431.                 for i = 1,greatestpos do
  2432.                     term.write(" ")
  2433.                 end
  2434.                 local x,y = term.getCursorPos()
  2435.                 y = y+1
  2436.                 term.setCursorPos(2,y)
  2437.                 if y > h then break end
  2438.             end
  2439.         end
  2440.         draw()
  2441.         while RUNNING and pmang.RUNNING do -- MAINALL handler
  2442.             local e = {os.pullEvent()}
  2443.             if e[1] == "term_resize" then
  2444.                 w,h = term.getSize()
  2445.                 drawScreen()
  2446.             elseif e[1] == "peripheral" then
  2447.                 scanPeripherals(e[2])
  2448.                 createNote(e[2], "Attached")
  2449.                 draw()
  2450.             elseif e[1] == "peripheral_detach" then
  2451.                 createNote(e[2], "Detached")
  2452.                 local success = false
  2453.                 local sides = {"top", "bottom", "front", "left", "right", "back"}
  2454.                 for i = 1,#peris["network"] do
  2455.                     if e[2] == peris["network"][i] then
  2456.                         table.remove(peris["network"], i)
  2457.                         success = true
  2458.                         break
  2459.                     end
  2460.                 end
  2461.                 if not success then
  2462.                     for i = 1,6 do
  2463.                         if e[2] == sides[i] then
  2464.                             peris["sides"][sides[i]] = nil
  2465.                             break
  2466.                         end
  2467.                     end
  2468.                 end
  2469.                 draw()
  2470.             elseif e[1] == "note_center" then
  2471.                 drawTopBar()
  2472.             elseif e[1] == "disk" then
  2473.                 createNote(e[2], "Disk inserted")
  2474.             elseif e[1] == "disk_eject" then
  2475.                 createNote(e[2], "Disk ejected")
  2476.             elseif e[1] == "monitor_resize" then
  2477.                 local mw,mh = peripheral.call(e[2], "getSize")
  2478.                 createNote(e[2], "Monitor size changed to "..mw.."x"..mh)
  2479.             elseif e[1] == "monitor_touch" then
  2480.                 createNote(e[2], "touched at "..e[3]..", "..e[4])
  2481.             elseif e[1] == "modem_message" then
  2482.                 processMessage(e[2], e[3], e[4], e[5], e[6])
  2483.             elseif e[1] == "key" and e[2] == keys.q then
  2484.                 pmang.RUNNING = false
  2485.                 break
  2486.             elseif e[1] == "mouse_scroll" then
  2487.                 if e[2] == 1 and scrollpos < pospossible then
  2488.                     scrollpos = scrollpos+1
  2489.                 elseif e[2] == -1 and scrollpos > 1 then
  2490.                     scrollpos = scrollpos-1
  2491.                 end
  2492.                 draw()
  2493.             elseif e[1] == "mouse_click" and e[2] == 1 then
  2494.                 if e[3] == w and e[4] == 1 then
  2495.                     pmang.RUNNING = false
  2496.                     break
  2497.                 elseif e[3] >= 1 and e[3] <= 9 and e[4] == 2 then
  2498.                     break
  2499.                 elseif e[3] >= noteiconxpos and e[3] <= noteiconxpos+2 and e[4] == 1 then -- NOTE CENTER HANDLER
  2500.                     displaymenu = "notecenter"
  2501.                     drawNotes()
  2502.                     displaymenu = "mainall"
  2503.                     draw()
  2504.                 elseif e[4] >= 3 then
  2505.                     pos = (e[4]-3)+scrollpos
  2506.                     if pos <= #data then
  2507.                         if e[3] >= 2 and e[3] <= data[pos]:len() then -- selected peripheral
  2508.                             if data[pos] == "top" or data[pos] == "bottom" or data[pos] == "front" or data[pos] == "left" or data[pos] == "right" or data[pos] == "back" then
  2509.                                 redirectToType(data[pos], true)
  2510.                             else
  2511.                                 redirectToType(data[pos])
  2512.                             end
  2513.                             displaymenu = "mainall"
  2514.                             draw()
  2515.                         end
  2516.                     end
  2517.                 end
  2518.             end
  2519.         end
  2520.         displaymenu = "main"
  2521.     end
  2522. end
  2523.  
  2524. -- Draw wrapper function
  2525. local function drawScreen()
  2526.     term.setBackgroundColor(colors.white)
  2527.     term.clear()
  2528.     if h < 18 or w < 46 then
  2529.         displaymode = "frag"
  2530.     else
  2531.         displaymode = "full"
  2532.     end
  2533.     drawTopBar()
  2534.     if displaymenu == "main" then
  2535.         drawBoxes()
  2536.         drawPeripherals()
  2537.         if networkperis then
  2538.             term.setBackgroundColor(colors.lightGray)
  2539.             term.setTextColor(colors.white)
  2540.             term.setCursorPos(1,2)
  2541.             term.write(" More >> ")
  2542.         end
  2543.     elseif displaymenu == "mainall" then
  2544.         drawPeripherals()
  2545.     end
  2546. end
  2547.  
  2548. -- Notification Center
  2549. function drawNotes()
  2550.     term.setBackgroundColor(colors.white)
  2551.     term.clear()
  2552.     drawTopBar()
  2553.     term.setBackgroundColor(colors.lightGray)
  2554.     term.setTextColor(colors.white)
  2555.     term.setCursorPos(noteiconxpos,1)
  2556.     term.write(" ! ")
  2557.     term.setBackgroundColor(colors.white)
  2558.     term.setTextColor(colors.black)
  2559.     term.setCursorPos(1,2)
  2560.     term.write("Notification Center")
  2561.     local function draw()
  2562.         -- calculate vars
  2563.         local greatestpos = 1
  2564.         for i = 1,#notemsgs do
  2565.             local ref = notemsgs[i]:len()
  2566.             if ref > greatestpos then
  2567.                 greatestpos = ref
  2568.             end
  2569.         end
  2570.        
  2571.         pospossible = #notemsgs-(h-4)
  2572.         if pospossible < 1 then pospossible = 1 end
  2573.        
  2574.         -- draw
  2575.         term.setCursorPos(1,4)
  2576.         term.setBackgroundColor(colors.white)
  2577.         term.setTextColor(colors.black)
  2578.         for i = scrollpos,#notemsgs do
  2579.             if i > #notemsgs then break end
  2580.             term.write(notemsgs[i])
  2581.             for i = 1,greatestpos do
  2582.                 term.write(" ")
  2583.             end
  2584.             local x,y = term.getCursorPos()
  2585.             y = y+1
  2586.             term.setCursorPos(1,y)
  2587.             if y > h then break end
  2588.         end
  2589.     end
  2590.     pospossible = #notemsgs-(h-4)
  2591.     if pospossible < 1 then pospossible = 1 end
  2592.     if pospossible then scrollpos = pospossible end
  2593.     draw()
  2594.     unreadnotes = false
  2595.     while true and pmang.RUNNING do -- NOTE CENTER LOOP
  2596.         local e = {os.pullEvent()}
  2597.         if e[1] == "term_resize" then
  2598.             w,h = term.getSize()
  2599.             drawScreen()
  2600.         elseif e[1] == "peripheral" then
  2601.             scanPeripherals(e[2])
  2602.             createNote(e[2], "Attached")
  2603.         elseif e[1] == "peripheral_detach" then
  2604.             createNote(e[2], "Detached")
  2605.             local success = false
  2606.             local sides = {"top", "bottom", "front", "left", "right", "back"}
  2607.             for i = 1,#peris["network"] do
  2608.                 if e[2] == peris["network"][i] then
  2609.                     table.remove(peris["network"], i)
  2610.                     success = true
  2611.                     break
  2612.                 end
  2613.             end
  2614.             if not success then
  2615.                 for i = 1,6 do
  2616.                     if e[2] == sides[i] then
  2617.                         peris["sides"][sides[i]] = nil
  2618.                         break
  2619.                     end
  2620.                 end
  2621.             end
  2622.             draw()
  2623.         elseif e[1] == "note_center" then
  2624.             drawTopBar()
  2625.             pospossible = #notemsgs-(h-4)
  2626.             if pospossible < 1 then pospossible = 1 end
  2627.             scrollpos = pospossible
  2628.             draw()
  2629.         elseif e[1] == "disk" then
  2630.             createNote(e[2], "Disk inserted")
  2631.         elseif e[1] == "disk_eject" then
  2632.             createNote(e[2], "Disk ejected")
  2633.         elseif e[1] == "monitor_resize" then
  2634.             local mw,mh = peripheral.call(e[2], "getSize")
  2635.             createNote(e[2], "Monitor size changed to "..mw.."x"..mh)
  2636.         elseif e[1] == "monitor_touch" then
  2637.             createNote(e[2], "touched at "..e[3]..", "..e[4])
  2638.         elseif e[1] == "modem_message" then
  2639.             processMessage(e[2], e[3], e[4], e[5], e[6])
  2640.         elseif e[1] == "key" and e[2] == keys.q then
  2641.             pmang.RUNNING = false
  2642.             break
  2643.         elseif e[1] == "mouse_click" and e[2] == 1 then
  2644.             if e[3] >= noteiconxpos and e[3] <= noteiconxpos+2 and e[4] == 1 then
  2645.                 drawScreen()
  2646.                 break
  2647.             elseif e[3] == w and e[4] == 1 then
  2648.                 pmang.RUNNING = false
  2649.                 break
  2650.             end
  2651.         elseif e[1] == "mouse_scroll" then
  2652.             if e[2] == 1 and scrollpos < pospossible then
  2653.                 scrollpos = scrollpos+1
  2654.             elseif e[2] == -1 and scrollpos > 1 then
  2655.                 scrollpos = scrollpos-1
  2656.             end
  2657.             draw()
  2658.         end
  2659.     end
  2660. end
  2661.  
  2662. -- Handler
  2663. scanPeripherals()
  2664. local function evtHandler()
  2665.     while pmang.RUNNING do
  2666.         local e = {os.pullEvent()}
  2667.         if e[1] == "term_resize" then
  2668.             w,h = term.getSize()
  2669.             drawScreen()
  2670.         elseif e[1] == "peripheral" then
  2671.             createNote(e[2], "Attached")
  2672.             scanPeripherals(e[2])
  2673.             drawScreen()
  2674.         elseif e[1] == "peripheral_detach" then
  2675.             createNote(e[2], "Detached")
  2676.             local success = false
  2677.             local sides = {"top", "bottom", "front", "left", "right", "back"}
  2678.             for i = 1,#peris["network"] do
  2679.                 if e[2] == peris["network"][i] then
  2680.                     table.remove(peris["network"], i)
  2681.                     success = true
  2682.                     break
  2683.                 end
  2684.             end
  2685.             if not success then
  2686.                 for i = 1,6 do
  2687.                     if e[2] == sides[i] then
  2688.                         peris["sides"][sides[i]] = nil
  2689.                         break
  2690.                     end
  2691.                 end
  2692.             end
  2693.            
  2694.             drawScreen()
  2695.         elseif e[1] == "note_center" then
  2696.             drawTopBar()
  2697.         elseif e[1] == "disk" then
  2698.             createNote(e[2], "Disk inserted")
  2699.         elseif e[1] == "disk_eject" then
  2700.             createNote(e[2], "Disk ejected")
  2701.         elseif e[1] == "monitor_resize" then
  2702.             local mw,mh = peripheral.call(e[2], "getSize")
  2703.             createNote(e[2], "Monitor size changed to "..mw.."x"..mh)
  2704.         elseif e[1] == "monitor_touch" then
  2705.             createNote(e[2], "touched at "..e[3]..", "..e[4])
  2706.         elseif e[1] == "modem_message" then
  2707.             processMessage(e[2], e[3], e[4], e[5], e[6])
  2708.         elseif e[1] == "mouse_click" and e[2] == 1 then
  2709.             if e[3] == w and e[4] == 1 then break
  2710.             elseif e[3] >= 1 and e[3] <= 9 and e[4] == 2 and networkperis and displaymenu == "main" then
  2711.                 displaymenu = "mainall"
  2712.                 drawPeripherals()
  2713.                 drawScreen()
  2714.             elseif e[3] >= noteiconxpos and e[3] <= noteiconxpos+2 and e[4] == 1 then -- NOTE CENTER HANDLER
  2715.                 displaymenu = "notecenter"
  2716.                 drawNotes()
  2717.                 displaymenu = "main"
  2718.                 drawScreen()
  2719.             elseif displaymenu == "main" and displaymode == "full" then -- click detection for full boxes
  2720.                 if e[3] >= boxes.top.x and e[3] <= boxes.top.x+10 and e[4] >= boxes.top.y and e[4] <= boxes.top.y+6 then -- TOP
  2721.                     redirectToType("top", true)
  2722.                     displaymenu = "main"
  2723.                     drawScreen()
  2724.                 elseif e[3] >= boxes.bottom.x and e[3] <= boxes.bottom.x+10 and e[4] >= boxes.bottom.y and e[4] <= boxes.bottom.y+6 then -- BOTTOM
  2725.                     redirectToType("bottom", true)
  2726.                     displaymenu = "main"
  2727.                     drawScreen()
  2728.                 elseif e[3] >= boxes.front.x and e[3] <= boxes.front.x+10 and e[4] >= boxes.front.y and e[4] <= boxes.front.y+6 then -- FRONT
  2729.                     redirectToType("front", true)
  2730.                     displaymenu = "main"
  2731.                     drawScreen()
  2732.                 elseif e[3] >= boxes.left.x and e[3] <= boxes.left.x+10 and e[4] >= boxes.left.y and e[4] <= boxes.left.y+6 then -- LEFT
  2733.                     redirectToType("left", true)
  2734.                     displaymenu = "main"
  2735.                     drawScreen()
  2736.                 elseif e[3] >= boxes.right.x and e[3] <= boxes.right.x+10 and e[4] >= boxes.right.y and e[4] <= boxes.right.y+6 then -- RIGHT
  2737.                     redirectToType("right", true)
  2738.                     displaymenu = "main"
  2739.                     drawScreen()
  2740.                 elseif e[3] >= boxes.back.x and e[3] <= boxes.back.x+10 and e[4] >= boxes.back.y and e[4] <= boxes.back.y+6 then -- BACK
  2741.                     redirectToType("back", true)
  2742.                     displaymenu = "main"
  2743.                     drawScreen()
  2744.                 end
  2745.             end
  2746.         elseif e[1] == "key" and e[2] == keys.q then break
  2747.         end
  2748.     end
  2749. end
  2750. drawScreen()
  2751. evtHandler()
  2752. term.setBackgroundColor(colors.black)
  2753. term.setTextColor(colors.white)
  2754. term.clear()
  2755. term.setCursorPos(1,1)
  2756. print("Thank you for using")
  2757. print("Peripheral Manager by LegoStax")
  2758. coroutine.yield()
Advertisement
Add Comment
Please, Sign In to add comment