Advertisement
da404lewzer

404 OS

Nov 30th, 2012
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 23.98 KB | None | 0 0
  1. --[[
  2.   File:      os
  3.   Purpose:   A shell replacement for ComputerCraft. Features include custom menus,
  4.              TurtleMarket integration, screen-saver idle detection, hot-keys,
  5.              multi-monitor support (including screen savers) and much more! Works
  6.              with all stable apps!
  7.   Author:    da404lewzer
  8.   Product:   A component of TurtleMarket by da404lewzer
  9.   License:   Creative Commons Attribution-ShareAlike 3.0 Unported License.
  10.              http://creativecommons.org/licenses/by-sa/3.0/
  11.            
  12.   Changes:  
  13.   0.1.45     11/25/2012
  14.              + new cleanUp() method ensures montiors are reset after running 3rd party apps
  15.              * Bug fixes
  16.              
  17.   0.1.40     11/24/2012
  18.              + Realtime-detection of all monitors (up to six, even on turtles!)
  19.              * Turtle support fixed, broken for a couple sub-revisions?
  20.              * ZOMG Bug fixes            
  21.              
  22.   0.1.25     11/23/2012
  23.              + Create more than 6 Custom Menus!
  24.              + Menu now displays scrollbars in term and on monitors if squished
  25.              * Bug fixes            
  26.              
  27.   0.1.20     11/21/2012
  28.              + Auto update over HTTP (via TurtleScripts: TurtleMarket)
  29.              * Bug fixes
  30.              
  31.   0.1.10     11/20/2012
  32.              + Added secondary monitor support. Usage: os
  33.              + Menus now support hotkeys
  34.              * Bug fixes
  35.  
  36.   0.1.5      11/19/2012
  37.              + Menu is now customizable via an external config file
  38.              + Basic screen saver support (auto activates when idle)
  39.              * Bug fixes
  40.  
  41.   0.1.0      11/18/2012
  42.              * Created Project
  43.              + Basic Fluid GUI elements in place
  44.              + Working menu and navigation keys
  45. ]]
  46.  
  47. local tArgs = { ... }
  48. local version = "0.1.45"
  49. local beta = true
  50. local screenSaverTimeout = 10
  51. local key = "404read" --set to "" if you don't want beta updates
  52. local w, h = 1, 1
  53. local mon, mon2
  54. local menus = {}
  55. local positions = {"top", "bottom", "left", "right", "front", "back"}
  56. local tmpOffset = {"init", "init", "init", "init", "init", "init"}
  57. local customFooterStr = "(CC BY-SA) TurtleScripts.com [#gjdgyz]"
  58. local ss_app = "ss_matrix"
  59.  
  60. local colorList = {}
  61. colorList[1] = {menuText=colors.white,
  62.                 menuTextBG=colors.black,
  63.                 menuTextHighlight=colors.black,
  64.                 menuTextBGHighlight=colors.white,
  65.                 pageTitleText=colors.black,
  66.                 pageTitleBG=colors.white,
  67.                 notifyBarText=colors.black,
  68.                 notifyBarBG=colors.white,
  69.                 footerText=colors.white,
  70.                 footerTextBG=colors.black,}
  71. colorList[2] = {menuText=colors.lightGray,
  72.                 menuTextBG=colors.black,
  73.                 menuTextHighlight=colors.white,
  74.                 menuTextBGHighlight=colors.orange,
  75.                 pageTitleText=colors.lime,
  76.                 pageTitleBG=colors.black,
  77.                 notifyBarText=colors.white,
  78.                 notifyBarBG=colors.blue,
  79.                 footerText=colors.yellow,
  80.                 footerTextBG=colors.black,}
  81. local mainMenuFooter = {
  82.     {'cmd_market',   'M> TurtleMarket', keys.m, ''},
  83.     {'cmd_exit',     'C> Console', keys.c, ''},
  84.     {'cmd_ss',     'L> Lock Screen', keys.l},
  85.     {'cmd_update',   'U> Update OS', keys.u, ''},
  86.     {'cmd_reboot',   'R> Restart', keys.r, ''},
  87.     {'cmd_shutdown', 'S> Shutdown', keys.s, ''}
  88. }
  89. local subMenuFooter = {
  90.     {'mainmenu',     'R> Return to Main Menu', keys.r, ''}
  91. }
  92. local currentMenu = {}
  93. local currentMenuName, currentMenuID
  94.  
  95. local function array_concat(a1,a2)
  96.     local t = {}
  97.     ii=0
  98.     for i = 1,#a1 do
  99.         ii=ii+1
  100.         t[ii] = a1[i]
  101.     end
  102.     for i = 1,#a2 do
  103.         ii=ii+1
  104.         t[ii] = a2[i]
  105.     end
  106.     return t
  107. end
  108.  
  109. local fontScale = 1
  110. local function cleanUp()
  111.     term.clear()
  112.     term.setCursorPos(1,1)
  113.     for i=1, #positions do
  114.         local mon = peripheral.wrap(positions[i])
  115.         if mon then
  116.             mon.setTextScale(fontScale)
  117.             mon.clear()
  118.             mon.setCursorPos(1,1)
  119.         end
  120.     end
  121. end
  122. local function announceTermMonitor(msgT, msgM, scale)
  123.     term.clear()
  124.     term.setCursorPos(1,1)
  125.     term.write(msgT)
  126.     term.setCursorPos(1,3)
  127.     for i=1, #positions do
  128.         local mon = peripheral.wrap(positions[i])
  129.         if mon then
  130.             mon.setTextScale(scale)
  131.             mon.clear()
  132.             mon.setCursorPos(1,1)
  133.             mon.write(msgM)
  134.             mon.setCursorPos(1,3)
  135.         end
  136.     end
  137. end
  138. cleanUp()
  139. local function createDefaultSettings() --for dev purposes, looks ugly without re-formatting
  140.     if not fs.exists("os.settings") then
  141.         myThing={
  142.             splash="",
  143.             menus={
  144.                     {
  145.                         name="Games Menu",
  146.                         items={
  147.                                 {
  148.                                     name="Worm!",
  149.                                     command="worm"
  150.                                 },{
  151.                                     name="Adventure",
  152.                                     command="adventure"
  153.                                 }
  154.                             }
  155.                     },{
  156.                         name="My Custom Menu",
  157.                         items={
  158.                                 {
  159.                                     name="My Custom #1",
  160.                                     command=""
  161.                                 },{
  162.                                     name="My Custom #2",
  163.                                     command=""
  164.                                 },{
  165.                                     name="My Custom #3",
  166.                                     command=""
  167.                                 },{
  168.                                     name="My Custom #4",
  169.                                     command=""
  170.                                 }
  171.                             }
  172.                     }
  173.                 }
  174.             }
  175.         sThing=textutils.serialize(myThing)
  176.         h = fs.open("os.settings", "w")
  177.         h.print('print("This is a settings file for OS :)")')
  178.         h.print('--splash value can be blank (default), none or a custom command')
  179.         h.print('--[[CONFIG:')
  180.         h.print(sThing)
  181.         h.print(']]')
  182.         h.close()
  183.     end
  184. end
  185. local function split(str, delimiter)
  186.     s = str
  187.     local i = 1
  188.     local returnvalue = {}
  189.     while string.find(s,delimiter, 1, true) do
  190.         returnvalue[i] = string.sub(s, 0, string.find(s,delimiter, 1, true)-1)
  191.         s = string.sub(s, string.find(s, delimiter, 1, true) + 1, string.len(s))
  192.         i = i + 1
  193.     end
  194.     return returnvalue
  195. end
  196. local function getColorMode(monitor)
  197.     if monitor.isColor() then
  198.         return 2
  199.     else
  200.         return 1
  201.     end
  202. end
  203. local nextAction = "" --blank for default action, menu
  204. local menuWidth = 10
  205. local running = true
  206. local selected = 1
  207. local startingRow = 4
  208. local pages = 1
  209. local count = #currentMenu
  210. local itemsPerPage = math.ceil(count / pages)
  211. local function loadMenu(mnu)
  212.     currentMenuID = mnu
  213.     selected = 1
  214.     local menu = {}
  215.     if mnu == "main" then
  216.         currentMenuName = "Main Menu"
  217.         n=0
  218.         count = #menus
  219.         if count > 9 then count = 9 end
  220.         for i,v in ipairs(menus) do
  221.             if n < count then
  222.                 n=n+1
  223.                 menu[n] = {"menu_"..i, n.."> "..v.name, n+1, ''}
  224.             end
  225.         end
  226.         if #menu > 0 then
  227.             menu[n+1] = {'','','',''}
  228.         end
  229.         currentMenu = array_concat(menu, mainMenuFooter)
  230.     else
  231.         n=0
  232.         for i,v in ipairs(menus) do
  233.             if "menu_"..i == mnu then
  234.                 currentMenuName = v.name
  235.                 count = #v.items
  236.                 for ii,vv in ipairs(v.items) do
  237.                     if n < count then
  238.                         n=n+1
  239.                         menu[n] = {"menucmd_"..i.."_"..ii, n.."> "..vv.name, n+1, vv}
  240.                     end
  241.                 end
  242.             end
  243.         end
  244.         if #menu > 0 then
  245.             menu[n+1] = {'','','',''}
  246.         end
  247.         currentMenu = array_concat(menu, subMenuFooter)
  248.     end
  249.  
  250.     -- Recalc our menu widths
  251.     menuWidth = 1
  252.     for i=1, #currentMenu do
  253.         len = string.len(currentMenu[i][2])
  254.         if len > menuWidth then
  255.             menuWidth = len
  256.         end
  257.     end
  258.     count = #currentMenu
  259.     itemsPerPage = math.ceil(count / pages)
  260. end
  261. local function loadSettings()
  262.     local foundMenus = {}
  263.     local cfg
  264.     if fs.exists("os.settings") then
  265.         h = fs.open("os.settings", "r")
  266.         while true do
  267.             data = h.readLine()
  268.             if data == nil then break end
  269.             if data == "--[[CONFIG:" then
  270.                 config = h.readAll()
  271.                 configStr = string.sub(config,1,string.len(config)-2)
  272.                 cfg=textutils.unserialize(configStr)
  273.                 --h2 = fs.open("os.settings2", "w") --DEBUG
  274.                 --h2.write(configStr)
  275.                 --h2.close()
  276.             end
  277.         end
  278.         h.close()
  279.     end
  280.     if cfg then
  281.         if cfg["custom_footer_message"] then
  282.             customFooterStr = cfg["custom_footer_message"]
  283.         end
  284.         if cfg["screensaver"] then
  285.             ss_app = cfg["screensaver"]
  286.         end
  287.         if cfg["menus"] then
  288.             menus = cfg["menus"]
  289.         end
  290.     end
  291.     loadMenu("main")
  292. end
  293. loadSettings()
  294. --saveSettings()
  295.  
  296. local function centerText(tY, tText)
  297.     local offX = w/2 - (string.len(tText) +1)/2
  298.     mon.setCursorPos(offX+1, tY)
  299.     mon.write(tText)
  300. end
  301. local function centerTextWidth(tY, tText, tX, tW)
  302.     local offX = tW/2 - string.len(tText)/2 + tX
  303.     mon.setCursorPos(offX+1, tY)
  304.     mon.write(tText)
  305. end
  306. local function repeatStr(tY, tText, count)
  307.     for i=1, count do
  308.         mon.setCursorPos(i, tY)
  309.         mon.write(tText)
  310.     end
  311. end
  312. local showingMessage = false
  313. local messageToShow = ""
  314. local function showMessage(msg)
  315.     showingMessage = true
  316.     messageToShow = msg
  317. end
  318. local function drawMessage()
  319.     for i=1, h do
  320.         repeatStr(i, "/", w)
  321.     end
  322.     local length = string.len(messageToShow)
  323.     local messageToHide = "[OK]"
  324.     local messageToHideLength = string.len(messageToHide)
  325.     local biggestWidth = length
  326.     if messageToHideLength > length then
  327.         biggestWidth = messageToHideLength
  328.     end
  329.     local offX = w/2 - biggestWidth / 2
  330.     local offX2 = w/2 - length / 2
  331.     local offX3 = w/2 - messageToHideLength / 2
  332.     local offY = h/2
  333.     mon.setBackgroundColor(colors.white)
  334.     mon.setTextColor(colors.black)
  335.     mon.setCursorPos(offX-1,offY-3)
  336.     mon.write("+"..string.rep("-",biggestWidth+2).."+")
  337.     for i=-2, 2 do
  338.         mon.setCursorPos(offX-1,offY+i)
  339.         mon.write("|"..string.rep(" ",biggestWidth+2).."|")
  340.     end
  341.     mon.setCursorPos(offX-1,offY+3)
  342.     mon.write("+"..string.rep("-",biggestWidth+2).."+")
  343.    
  344.     mon.setCursorPos(offX2+1,offY-1)
  345.     mon.write(messageToShow)
  346.     mon.setCursorPos(offX3+1,offY+2)
  347.     mon.write(messageToHide)
  348.    
  349.     -- Ensure we are using the correct colors
  350.     mon.setBackgroundColor(colors.black)
  351.     mon.setTextColor(colors.white)
  352. end
  353. local lastKeypress = os.clock()
  354. local function launchApp(command, args)
  355.     if command then
  356.         if args then
  357.             shell.run(command, args)
  358.         else
  359.             os.run({},command)
  360.         end
  361.     end
  362.     cleanUp()
  363. end
  364. local timer
  365. local function showSS()
  366.     launchApp(ss_app)
  367.     lastKeypress = os.clock()
  368.     timer = os.startTimer(0.1)
  369. end
  370. local function doMenuAction(action)
  371.     local item = currentMenu[action]
  372.     if item[1] == "cmd_exit" then
  373.         announceTermMonitor("Starting Console..", "Console [TERM]", 1)
  374.         term.setCursorPos(1,2)
  375.         term.write("To return type: exit")
  376.         term.setCursorPos(1,4)
  377.         sleep(0.1)
  378.         shell.run("shell")
  379.         lastKeypress = os.clock()
  380.         timer = os.startTimer(0.1)
  381.     elseif item[1] == "cmd_update" then
  382.         announceTermMonitor("Updating OS..", "Updating OS [TERM]", 1)
  383.         shell.run("market get gjdh07 market "..key.." y")
  384.         shell.run("market get gjdgzu os "..key.." y")
  385.         shell.run("market get gjdgzo ss_4d "..key.." y")
  386.         shell.run("market get gjdh08 ss_texty "..key.." y")
  387.         shell.run("market get gjdh0r ss_matrix "..key.." y")
  388.         sleep(2)
  389.         os.reboot()
  390.     elseif item[1] == "cmd_ss" then
  391.         showSS()
  392.         lastKeypress = os.clock()
  393.         timer = os.startTimer(0.1)
  394.     elseif item[1] == "cmd_reboot" then
  395.         os.reboot()
  396.     elseif item[1] == "cmd_shutdown" then
  397.         os.shutdown()
  398.     elseif item[1] == "cmd_market" then
  399.         sleep(0.1)
  400.         shell.run("market")
  401.         lastKeypress = os.clock()
  402.         timer = os.startTimer(0.1)
  403.     elseif string.sub(item[1],1,5) == "menu_" then
  404.         loadMenu(item[1])
  405.     elseif string.sub(item[1],1,8) == "menucmd_" then
  406.         if item[4].command == "" then
  407.             showMessage("No command associated!")
  408.         else
  409.             announceTermMonitor("Launching "..item[4].name.."...", item[4].name.." [TERM]", 1)
  410.             if item[4].args then
  411.                 launchApp(item[4].command, item[4].args)
  412.             else
  413.                 launchApp(item[4].command)
  414.             end
  415.             lastKeypress = os.clock()
  416.             timer = os.startTimer(0.1)
  417.         end
  418.     elseif string.sub(item[1],1,8) == "mainmenu" then
  419.         loadMenu("main")
  420.     else
  421.         showMessage("TODO: Not yet implemented")
  422.     end
  423. end
  424. local function lookupHotkeyAction(key)
  425.     for i=1, #currentMenu do
  426.         if currentMenu[i][3] == key then
  427.             selected = i
  428.             doMenuAction(selected)
  429.             break
  430.         end
  431.     end
  432. end
  433. local function splash(monitor)
  434.     mon = monitor
  435.     if mon then
  436.         w, h = mon.getSize()
  437.         local betaMsg = ""
  438.         if beta then
  439.             betaMsg = "BETA"
  440.         end
  441.         local data = {
  442.                 "",
  443.                 " 404",
  444.                 version,
  445.                 betaMsg
  446.             }
  447.         if w > 7 and h > 5 then
  448.             data = {
  449.                 '',
  450.                 "##  ##  ######  ##  ##",
  451.                 "##  ##  ##  ##  ##  ##",
  452.                 "######  ##  ##  ######",
  453.                 "    ##  ##  ##      ##",
  454.                 "    ##  ######      ##",
  455.                 " B R A N D    B I O S ",
  456.                 " v"..version.." "..betaMsg
  457.             }
  458.         end
  459.         local x = w/2 - string.len(data[1])/2
  460.         local y = h/2 - #data/2
  461.         mon.clear()
  462.         for i=1, #data do
  463.             centerText(y+i, data[i])
  464.         end
  465.     end
  466. end
  467.  
  468. splash(term)
  469. for i=1, #positions do
  470.     splash(peripheral.wrap(positions[i]))
  471. end
  472. sleep(1.5)
  473. --showSS()
  474. --showMessage("Welcome to CheetOS!")
  475. local function round(x)
  476.   if x%2 ~= 0.5 then
  477.     return math.floor(x+0.5)
  478.   end
  479.   return x-0.5
  480. end
  481. local function update(monitor, monID)
  482.     mon = monitor
  483.     if mon then
  484.         w, h = mon.getSize()
  485.         local dialogWidth = (menuWidth * pages) + (2 * pages)
  486.         local dialogOffset = (w - dialogWidth)/2;
  487.         -- Overdraw stuff
  488.         if showingMessage then
  489.             drawMessage()
  490.         else
  491.             cMode = getColorMode(mon)
  492.             mon.clear()
  493.             mon.setBackgroundColor(colorList[cMode].pageTitleBG)
  494.             mon.setTextColor(colorList[cMode].pageTitleText)
  495.             -- Menu Header
  496.             mon.setCursorPos(1,1)
  497.             mon.write(string.rep(" ", w))
  498.             centerText(1, currentMenuName)
  499.             mon.setBackgroundColor(colorList[cMode].notifyBarBG)
  500.             mon.setTextColor(colorList[cMode].notifyBarText)
  501.             mon.setCursorPos(1,2)
  502.             mon.write(string.rep(" ", w))
  503.            
  504.             -- Mail Messages
  505.             mon.setCursorPos(2,2)
  506.             local msgs = "0 MSGS";
  507.             mon.write(msgs)
  508.            
  509.             -- Time
  510.             local time = os.time()
  511.             timeFmt = textutils.formatTime(time, false)
  512.             timeLen = string.len(timeFmt)
  513.             mon.setCursorPos(w-timeLen,2)
  514.             mon.write(timeFmt)
  515.            
  516.  
  517.            
  518.             mon.setBackgroundColor(colorList[cMode].menuTextBG)
  519.             mon.setTextColor(colorList[cMode].menuText)
  520.            
  521.             -- Menu Separator
  522.             --repeatStr(2, "-", w)
  523.            
  524.             -- Draw Menu
  525.             local scrollY = 0
  526.             local viewH = h - startingRow - 2
  527.             if #currentMenu > viewH then
  528.                 local offset =  viewH / #currentMenu
  529.                 scrollY = round(offset * (selected-1))
  530.             end
  531.             local z = scrollY
  532.             for p=1, pages do
  533.                 local loopMax = itemsPerPage
  534.                 if loopMax > viewH+scrollY then
  535.                     loopMax = viewH+scrollY
  536.                     mon.setCursorPos(w, h-4)
  537.                     mon.write("|")
  538.                     mon.setCursorPos(w, h-3)
  539.                     mon.write("v")
  540.                 end
  541.                 if scrollY > 0 then
  542.                     loopMax = viewH+scrollY
  543.                     mon.setCursorPos(w, 4)
  544.                     mon.write("^")
  545.                     mon.setCursorPos(w, 5)
  546.                     mon.write("|")
  547.                 end
  548.                 for n=1+scrollY, loopMax do
  549.                     z=z+1
  550.                     if z <= count then
  551.                         local offsetY = startingRow + (n-1) - scrollY
  552.                         local offsetX = dialogOffset + (menuWidth * (p-1)) + (2 * (p-1))
  553.                         if offsetX < 1 then
  554.                             offsetX = 1
  555.                         end
  556.                         if z == selected then
  557.                             mon.setBackgroundColor(colorList[cMode].menuTextBGHighlight)
  558.                             mon.setTextColor(colorList[cMode].menuTextHighlight)
  559.                             centerTextWidth(offsetY,string.rep(" ",menuWidth + 2), offsetX, menuWidth)
  560.                         else
  561.                             mon.setBackgroundColor(colorList[cMode].menuTextBG)
  562.                             mon.setTextColor(colorList[cMode].menuText)
  563.                         end
  564.                         --centerTextWidth(offsetY, currentMenu[z][2], offsetX, menuWidth)
  565.                         mon.setCursorPos(offsetX+1, offsetY)
  566.                         mon.write(currentMenu[z][2])
  567.                     end
  568.                 end
  569.             end
  570.        
  571.             -- Ensure we are using the correct colors
  572.             mon.setBackgroundColor(colorList[cMode].footerTextBG)
  573.             mon.setTextColor(colorList[cMode].footerText)
  574.        
  575.             -- Menu Footer & Seperator
  576.             repeatStr(h-1, "-", w)
  577.             if mon == term then
  578.                 footerStr = "UP / DOWN - Pick, ENTER - Select"
  579.                 if string.len(footerStr) > w then
  580.                     footerStr = "UP/DN/ENTER"
  581.                 end
  582.                 centerText(h, footerStr)
  583.             else
  584.                 if tmpOffset[monID] == "init" then
  585.                     tmpOffset[monID] = w
  586.                 end
  587.                 tmpOffset[monID]=tmpOffset[monID]-1
  588.                 if tmpOffset[monID] < -string.len(customFooterStr) then
  589.                     tmpOffset[monID] = w
  590.                 end
  591.                 mon.setCursorPos(tmpOffset[monID], h)
  592.                 mon.write(customFooterStr)
  593.             end
  594.         end
  595.     end
  596. end
  597. local function updateSM(monitor, monID)
  598.     mon = monitor
  599.     if mon then
  600.         w, h = mon.getSize()
  601.         local dialogWidth = (menuWidth * pages) + (2 * pages)
  602.         local dialogOffset = (w - dialogWidth)/2;
  603.         -- Overdraw stuff
  604.         if showingMessage then
  605.             drawMessage()
  606.         else
  607.             cMode = getColorMode(mon)
  608.             mon.clear()
  609.             mon.setBackgroundColor(colorList[cMode].notifyBarBG)
  610.             mon.setTextColor(colorList[cMode].notifyBarText)
  611.             -- Menu Header
  612.             mon.setCursorPos(1,1)
  613.             mon.write(string.rep(" ", w))
  614.             mon.setCursorPos(1,2)
  615.             mon.write(string.rep(" ", w))
  616.            
  617.             -- Time
  618.             local time = os.time()
  619.             --we want to manually parse am/pm
  620.             timeFmt = textutils.formatTime(time, false)
  621.             local len = string.len(timeFmt)
  622.             local part = string.sub(timeFmt, len-1)
  623.             timeFmt = string.sub(timeFmt, 1, len-3)..part
  624.             timeLen = string.len(timeFmt)
  625.             if w == 7 then
  626.                 mon.setCursorPos(w-timeLen+1,1)
  627.             else
  628.                 mon.setCursorPos(w-timeLen,2)
  629.             end
  630.             mon.write(timeFmt)
  631.        
  632.             -- Mail Messages
  633.             mon.setCursorPos(2,2)
  634.             mon.write("0 MSGS")
  635.        
  636.             -- Ensure we are using the correct colors
  637.             mon.setBackgroundColor(colorList[cMode].footerTextBG)
  638.             mon.setTextColor(colorList[cMode].footerText)
  639.            
  640.             -- Menu Footer & Seperator
  641.             if mon == term then
  642.                 footerStr = "UP / DOWN - Pick, ENTER - Select"
  643.                 if string.len(footerStr) > w then
  644.                     footerStr = "UP/DN/ENTER"
  645.                 end
  646.                 centerText(h, footerStr)
  647.             else
  648.                 if tmpOffset[monID] == "init" then
  649.                     tmpOffset[monID] = w
  650.                 end
  651.                 tmpOffset[monID]=tmpOffset[monID]-1
  652.                 if tmpOffset[monID] < -string.len(customFooterStr) then
  653.                     tmpOffset[monID] = w
  654.                 end
  655.                 mon.setCursorPos(tmpOffset[monID], h-1)
  656.                 mon.write(customFooterStr)
  657.             end
  658.         end
  659.     end
  660. end
  661. local function updateTemplate(monitor, monID)
  662.     if monitor then
  663.         local w,h = monitor.getSize()
  664.         if w and h then
  665.             if w > 7 and h > 5 then
  666.                 update(monitor, monID)
  667.             else
  668.                 updateSM(monitor, monID)
  669.             end
  670.         end
  671.     end
  672. end
  673. local function updateAll()
  674.     updateTemplate(term, 0)
  675.     for i=1, #positions do
  676.         updateTemplate(peripheral.wrap(positions[i]), i)
  677.     end
  678. end
  679.  
  680. timer = os.startTimer(0) --redefined higher up for doMenuAction
  681. while running do
  682.     local event, p1, p2 = os.pullEvent()
  683.     mon = term
  684.     if #tArgs > 0 then
  685.       mon2 = peripheral.wrap(tArgs[1])
  686.     end
  687.     if event == "key" then
  688.         lastKeypress = os.clock()
  689.         if showingMessage then
  690.             showingMessage = false
  691.         else
  692.             if p1 == keys.up then
  693.                 selected=selected-1
  694.                 if selected < 1 then
  695.                     selected = count
  696.                 end
  697.                 if currentMenu[selected][1] == "" then
  698.                     selected=selected-1
  699.                     if selected < 1 then
  700.                         selected = count
  701.                     end
  702.                 end
  703.             elseif p1 == keys.down then
  704.                 selected=selected+1
  705.                 if selected > count then
  706.                     selected = 1
  707.                 end
  708.                 if currentMenu[selected][1] == "" then
  709.                     selected=selected+1
  710.                     if selected > count then
  711.                         selected = 1
  712.                     end
  713.                 end
  714.             elseif p1 == keys.enter or p1 == keys.space then
  715.                 doMenuAction(selected)
  716.             else
  717.                 lookupHotkeyAction(p1)
  718.             end
  719.         end
  720.     else
  721.    
  722.         if running then
  723.             if ss_app and math.floor(os.clock() - lastKeypress) > screenSaverTimeout then
  724.                 -- Run the screensaver
  725.                 showSS()
  726.             else
  727.                 updateAll()
  728.             end
  729.         end
  730.     end
  731.     if event == "timer" and p1 == timer then
  732.         timer = os.startTimer(0.1)
  733.     end
  734. end
  735.  
  736. cleanUp()
  737. --[[
  738. term.clear()
  739. term.setCursorPos(1,1)
  740. if #tArgs > 0 then
  741.     mon2 = peripheral.wrap(tArgs[1])
  742.     mon2.clear()
  743.     mon2.setCursorPos(1,1)
  744. end
  745. ]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement