April_The_Sergal

methodPrinter

Sep 28th, 2025 (edited)
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 26.12 KB | None | 0 0
  1. local borderColour = 128
  2. local chartBGColour = 128
  3. local inputChartColour = 32
  4. local outputChartColour = 16384
  5. local batteryChargeColour = 8
  6. local idleColour = 16
  7. local sectionTitleColour = 1
  8. local baseTextColour = 1
  9.  
  10. local screenRefreshRate = 0.1
  11. local unitFormatCaps = true
  12. local dataCorrection = 2.5
  13.  
  14. local function findMonitorAndInduction()
  15.   local foundMonitor, foundInduction
  16.   local function consider(name, getType)
  17.     local pType = (getType and getType(name)) or peripheral.getType(name)
  18.     local lname = tostring(name):lower()
  19.     if (not foundMonitor) and (pType == "monitor" or lname:find("monitor", 1, true)) then
  20.       local ok, p = pcall(peripheral.wrap, name); if ok and p then foundMonitor = p end
  21.     end
  22.     if not foundInduction then
  23.       local looksLikeIM = (pType == "inductionPort")
  24.         or (pType and pType:lower():find("induction", 1, true))
  25.         or lname:find("induction", 1, true)
  26.         or lname:find("matrix", 1, true)
  27.       if looksLikeIM then
  28.         local ok, p = pcall(peripheral.wrap, name); if ok and p then foundInduction = p end
  29.       end
  30.     end
  31.   end
  32.   for _, name in ipairs(peripheral.getNames()) do
  33.     local pType = peripheral.getType(name)
  34.     consider(name, peripheral.getType)
  35.     if pType == "modem" then
  36.       local mdm = peripheral.wrap(name)
  37.       if mdm and mdm.isWireless and mdm.getNamesRemote then
  38.         local isWireless = false; pcall(function() isWireless = mdm.isWireless() end)
  39.         if not isWireless then
  40.           local ok, remotes = pcall(mdm.getNamesRemote)
  41.           if ok and type(remotes) == "table" then
  42.             for _, rname in ipairs(remotes) do
  43.               local getTypeRemote = function(n)
  44.                 if mdm.getTypeRemote then return mdm.getTypeRemote(n) end
  45.                 return nil
  46.               end
  47.               consider(rname, getTypeRemote)
  48.               if foundMonitor and foundInduction then return foundMonitor, foundInduction end
  49.             end
  50.           end
  51.         end
  52.       end
  53.     end
  54.     if foundMonitor and foundInduction then break end
  55.   end
  56.   return foundMonitor, foundInduction
  57. end
  58.  
  59. local m, im = findMonitorAndInduction()
  60. local w, h = nil
  61.  
  62. local function hasMethod(p, name) return p and type(p[name]) == "function" end
  63. local function getLastInput()
  64.   if hasMethod(im, "getLastInput") then return im.getLastInput() end
  65.   if hasMethod(im, "getInput") then return im.getInput() end
  66.   return 0
  67. end
  68. local function getLastOutput()
  69.   if hasMethod(im, "getLastOutput") then return im.getLastOutput() end
  70.   if hasMethod(im, "getOutput") then return im.getOutput() end
  71.   return 0
  72. end
  73. local function getFilledPct()
  74.   if hasMethod(im, "getEnergyFilledPercentage") then return im.getEnergyFilledPercentage() end
  75.   local e = im.getEnergy and im.getEnergy() or 0
  76.   local mE = im.getMaxEnergy and im.getMaxEnergy() or 0
  77.   if mE > 0 then return e / mE end
  78.   return 0
  79. end
  80.  
  81. local colorText = {"White","Orange","Magenta","Light blue","Yellow","Lime","Pink","Gray","Light gray","Cyan","Purple","Blue","Brown","Green","Red","Black"}
  82. local colorDec = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}
  83. local refreshRates = { values = {0.1,0.3,0.5,1.0,1.0,2.0}, display = {"Fastest","Fast","Normal","Slow","Very Slow","Slowest"} }
  84. local unitStyle = { values = {true,false}, display = {"X.X KRF","X.XkRF"} }
  85.  
  86. local function fromBlit(hex)
  87.   if #hex ~= 1 then return nil end
  88.   local value = tonumber(hex, 16); if not value then return nil end
  89.   return 2 ^ value
  90. end
  91.  
  92. local function draw(xStart, xLen, yStart, yLen, c, target)
  93.   target = target or m
  94.   local ob = target.getBackgroundColour()
  95.   target.setBackgroundColor(c)
  96.   target.setCursorPos(xStart, yStart)
  97.   if xLen > 1 then
  98.     for i = 1, xLen do
  99.       target.write(" "); target.setCursorPos(xStart+i, yStart)
  100.     end
  101.   elseif xLen ~= 0 and yLen ~= 0 then
  102.     target.write(" ")
  103.   end
  104.   if yLen > 1 then
  105.     for k = 1, yLen-1 do
  106.       target.setCursorPos(xStart, yStart+k); target.write(" ")
  107.     end
  108.   end
  109.   target.setBackgroundColor(ob)
  110. end
  111.  
  112. local function drawBar(xmin, xmax, y, r, c)
  113.   for i=1, r do draw(xmin, xmax, y+i-1, 1, c) end
  114. end
  115.  
  116. local function writeAt(x, y, t, c, b, target)
  117.   target = target or m
  118.   local oc = target.getTextColour and target.getTextColour() or colors.white
  119.   local ob = target.getBackgroundColour and target.getBackgroundColour() or colors.black
  120.   if target.setTextColour then target.setTextColour(c or oc) end
  121.   if target.setBackgroundColour then target.setBackgroundColour(b or ob) end
  122.   target.setCursorPos(x, y); target.write(t)
  123.   if target.setTextColour then target.setTextColour(oc) end
  124.   if target.setBackgroundColour then target.setBackgroundColour(ob) end
  125. end
  126.  
  127. local function newWindow(target, x, y, w_, h_, bg)
  128.   local win = window.create(target, x, y, w_, h_)
  129.   win.setBackgroundColor(bg or 32768); win.clear()
  130.   return win
  131. end
  132.  
  133. local function displayError(index)
  134.   local x, y = m.getSize()
  135.   local errorText = "ERROR"
  136.   local errors = {
  137.     "The Induction Matrix has been tampered with.",
  138.     "Induction Port not connected.",
  139.     "The Induction Matrix is incomplete."
  140.   }
  141.   m.clear()
  142.   m.setTextColour(16384)
  143.   m.setCursorPos((x/2) - (string.len(errorText)/2), (y/2)-1)
  144.   m.write(errorText)
  145.   writeAt((x/2) - (string.len(errors[index])/2), y/2, errors[index], baseTextColour)
  146.   printError("ERROR: " .. errors[index])
  147.   local timer = 10
  148.   while timer > -1 do
  149.     local countdownStr = "Next attempt in " .. timer
  150.     draw((x/2) - (string.len(countdownStr)/2), string.len(countdownStr) + 1, (y/2)+2, 1, 32768)
  151.     writeAt((x/2) - (string.len(countdownStr)/2), (y/2)+2, countdownStr, baseTextColour)
  152.     print(countdownStr)
  153.     timer = timer -1
  154.     os.sleep(1)
  155.   end
  156.   os.reboot()
  157. end
  158.  
  159. if m ~= nil then
  160.   pcall(function() m.setTextScale(0.5) end)
  161.   local x, y = m.getSize()
  162.   if x ~= 100 or y ~= 38 then
  163.     print("WARNING: Monitor size is "..x.."x"..y..". Designed for 100x38.")
  164.   end
  165.   w, h = m.getSize()
  166.   m.setBackgroundColor(32768); m.clear(); m.setTextColour(1)
  167. else
  168.   print("No monitor found (local or via wired modem)."); return
  169. end
  170.  
  171. if not im then
  172.   displayError(2)
  173. else
  174.   if hasMethod(im, "isFormed") then
  175.     while not im.isFormed() do displayError(3) end
  176.   end
  177. end
  178.  
  179. local function removeOverflow(tab, maxDataCount)
  180.   while #tab >= maxDataCount do table.remove(tab, 1) end
  181.   return tab
  182. end
  183.  
  184. local function getRange(input)
  185.   local max, min = 0, 0
  186.   for i = 1, #input do
  187.     if i == 1 then max = input[i]; min = input[i] end
  188.     if input[i] > max then max = input[i] end
  189.     if input[i] < min then min = input[i] end
  190.   end
  191.   return min, max
  192. end
  193.  
  194. local function round(x, decimal)
  195.   local m_ = 10^(decimal or 0)
  196.   local result = math.floor(x * m_ + 0.5) / m_
  197.   return string.format("%." .. (decimal or 0) .. "f", result)
  198. end
  199.  
  200. local function roundD(exact, quantum)
  201.   local quant,frac = math.modf(exact/quantum)
  202.   return quantum * (quant + (frac > 0.5 and 1 or 0))
  203. end
  204.  
  205. local timeUnits = {
  206.   {threshold = 8553600, unit = "99+ days"},
  207.   {threshold = 86400, unit = "days", divisor = 86400},
  208.   {threshold = 3600, unit = "hours", divisor = 3600},
  209.   {threshold = 60, unit = "min", divisor = 60},
  210.   {threshold = 1, unit = "sec", divisor = 1}
  211. }
  212. local function adjustTime(time)
  213.   for _, unitData in ipairs(timeUnits) do
  214.     if time >= unitData.threshold then
  215.       local adjustedTime
  216.       if unitData.divisor and unitData.divisor ~= 1 then
  217.         adjustedTime = string.format("%.1f", time / unitData.divisor)
  218.       elseif unitData.divisor then
  219.         adjustedTime = math.floor(time / unitData.divisor)
  220.       else
  221.         adjustedTime = ""
  222.       end
  223.       return string.format("%s %s", adjustedTime, unitData.unit)
  224.     end
  225.   end
  226.   return "0 sec."
  227. end
  228.  
  229. local function conversion(exact, text, addPlus, afterDecimalPoint)
  230.   afterDecimalPoint = afterDecimalPoint or 0.1
  231.   addPlus = addPlus or false
  232.   local units = unitFormatCaps and {"", "K", "M", "G", "T", "P", "E", "Z", "Y"} or {"", "k", "m", "g", "t", "p", "e", "z", "y"}
  233.   local pot = 1
  234.   local absExact = math.abs(exact)
  235.   while absExact >= (1000^pot) do pot = pot + 1 end
  236.   local value = roundD(exact / (1000^(pot - 1)), afterDecimalPoint)
  237.   local out = tostring(value)
  238.   if addPlus and value > 0 then out = "+" .. out end
  239.   if text then out = out .. (unitFormatCaps and " " .. units[pot] or units[pot]) end
  240.   return out
  241. end
  242.  
  243. local function protCall(func, errIndex)
  244.   local success, result = pcall(func)
  245.   if success and result ~= nil then return result else displayError(errIndex) end
  246. end
  247.  
  248. local function indexOf(array, value)
  249.   for i, v in ipairs(array) do if v == value then return i end end
  250.   return nil
  251. end
  252.  
  253. local energy, maxEnergy, installedCells, installedProviders, lastInput, lastOutput, filledPercentage, maxTransfer = nil
  254. local inputHistory, outputHistory, flowHistory = {}, {}, {}
  255.  
  256. local function drawStaticElements()
  257.   local p = 2
  258.   local chartBGColourLocal = 256
  259.   draw(p, w-3, p, 1, borderColour)
  260.   draw(p, w-3, h-1, 1, borderColour)
  261.   draw(p, 1, p, h-p, borderColour)
  262.   draw(w-1, 1, p, h-p, borderColour)
  263.   draw(27, 1, p, h-p, borderColour)
  264.   draw(28, 71, h/2+2, 1, borderColour)
  265.   draw(63, 1, h/2+3, 15, borderColour)
  266.   draw(2, 25, 28, 1, borderColour)
  267.   writeAt(5, 2, "  BATTERY  ", sectionTitleColour)
  268.   writeAt(30, 2, "  ENERGY FLOW:  ", sectionTitleColour)
  269.   writeAt(5, 28, "  STATS:  ", sectionTitleColour)
  270.   local configButtonText = "SETTINGS " .. string.char(26)
  271.   local configButtonSpaceMaker = "  "
  272.   local buttonX = w - #configButtonText - 3
  273.   local buttonY = 2
  274.   for i = 1, #configButtonText do configButtonSpaceMaker = configButtonSpaceMaker .. " " end
  275.   writeAt(buttonX - 1, buttonY, configButtonSpaceMaker)
  276.   writeAt(buttonX, buttonY, configButtonText, colors.white, colors.gray)
  277.   writeAt(30, 21, "  INPUT:  ", sectionTitleColour)
  278.   writeAt(66, 21, "  OUTPUT:  ", sectionTitleColour)
  279. end
  280.  
  281. local function drawInputGraph()
  282.   table.insert(inputHistory, lastInput); removeOverflow(inputHistory, 34)
  283.   local inGraph = newWindow(m, 29, 23, 33, 13, chartBGColour)
  284.   local ox, oy = inGraph.getSize()
  285.   local rf_min, rf_max = getRange(inputHistory)
  286.   for i = 0, #inputHistory do
  287.     local value = inputHistory[#inputHistory-i]; if not value then break end
  288.     local result = math.floor(math.min(math.max((value * (13 / (rf_max == 0 and 1 or rf_max))), 0), 13) + 0.5)
  289.     if result ~= result then result = 0 end
  290.     draw(ox-i, 1, oy-result+1, result, inputChartColour, inGraph)
  291.   end
  292.   local maxRf = conversion(rf_max, true, true) .. "RF/t"
  293.   local _, _, bgtop = inGraph.getLine(1)
  294.   for i = 0, #maxRf do
  295.     local char = maxRf:sub(1+i, 1+i)
  296.     local b = fromBlit(bgtop:sub(1+i, 1+i)) or chartBGColour
  297.     writeAt(1+i, 1, char, baseTextColour, b, inGraph)
  298.   end
  299.   local maxPercent = tostring(round((rf_max/(maxTransfer==0 and 1 or maxTransfer))*100) .. "%")
  300.   for i = 0, #maxPercent do
  301.     local c = ox-#maxPercent+i+1
  302.     local char = maxPercent:sub(1+i, 1+i)
  303.     local b = fromBlit(bgtop:sub(c, c)) or chartBGColour
  304.     writeAt(c, 1, char, baseTextColour, b, inGraph)
  305.   end
  306.   local minRf = "0RF/t"
  307.   local _, _, bglow = inGraph.getLine(oy)
  308.   for i = 0, #minRf do
  309.     local char = minRf:sub(1+i, 1+i)
  310.     local b = fromBlit(bglow:sub(1+i, 1+i)) or chartBGColour
  311.     writeAt(1+i, oy, char, baseTextColour, b, inGraph)
  312.   end
  313.   local minPercent = "0%"
  314.   for i = 0, #minPercent do
  315.     local c = ox-#minPercent+i+1
  316.     local char = minPercent:sub(1+i, 1+i)
  317.     local b = fromBlit(bglow:sub(c, c)) or chartBGColour
  318.     writeAt(c, oy, char, baseTextColour, b, inGraph)
  319.   end
  320.   draw(39, 22, 21, 1, borderColour)
  321.   local rf_tick = conversion(lastInput, true, true) .. "RF/t "
  322.   writeAt(39, 21, rf_tick, (lastInput > 0 and inputChartColour or idleColour))
  323.   writeAt(39+#rf_tick, 21, "(" .. tostring(round((lastInput/(maxTransfer==0 and 1 or maxTransfer))*100) .. "%)  "))
  324.   if lastInput == maxTransfer and maxTransfer > 0 then
  325.     local maxStrings = {"MAX INPUT!","Add / Upgrade","Providers."}
  326.     drawBar(46-(math.max(#maxStrings[1], #maxStrings[2], #maxStrings[3])/2)-1, #maxStrings[2]+2, 27, #maxStrings+2, 1)
  327.     writeAt(46-(#maxStrings[1]/2), 28, maxStrings[1], outputChartColour, 1)
  328.     writeAt(46-(#maxStrings[2]/2), 29, maxStrings[2], outputChartColour, 1)
  329.     writeAt(46-(#maxStrings[3]/2), 30, maxStrings[3], outputChartColour, 1)
  330.   end
  331. end
  332.  
  333. local function drawOutputGraph()
  334.   table.insert(outputHistory, lastOutput); removeOverflow(outputHistory, 34)
  335.   local outGraph = newWindow(m, 65, 23, 33, 13, chartBGColour)
  336.   local ox, oy = outGraph.getSize()
  337.   local rf_min, rf_max = getRange(outputHistory)
  338.   for i = 0, #outputHistory do
  339.     local value = outputHistory[#outputHistory-i]; if not value then break end
  340.     local result = math.floor(math.min(math.max((value * (13 / (rf_max == 0 and 1 or rf_max))), 0), 13) + 0.5)
  341.     if result ~= result then result = 0 end
  342.     draw(ox-i, 1, oy-result+1, result, outputChartColour, outGraph)
  343.   end
  344.   local maxRf = conversion(rf_max*-1, true, true) .. "RF/t"
  345.   local _, _, bgtop = outGraph.getLine(1)
  346.   for i = 0, #maxRf do
  347.     local char = maxRf:sub(1+i, 1+i)
  348.     local b = fromBlit(bgtop:sub(1+i, 1+i)) or chartBGColour
  349.     writeAt(1+i, 1, char, baseTextColour, b, outGraph)
  350.   end
  351.   local maxPercent = tostring(round((rf_max/(maxTransfer==0 and 1 or maxTransfer))*100) .. "%")
  352.   for i = 0, #maxPercent do
  353.     local c = ox-#maxPercent+i+1
  354.     local char = maxPercent:sub(1+i, 1+i)
  355.     local b = fromBlit(bgtop:sub(c, c)) or chartBGColour
  356.     writeAt(c, 1, char, baseTextColour, b, outGraph)
  357.   end
  358.   local minRf = "0RF/t"
  359.   local _, _, bglow = outGraph.getLine(oy)
  360.   for i = 0, #minRf do
  361.     local char = minRf:sub(1+i, 1+i)
  362.     local b = fromBlit(bglow:sub(1+i, 1+i)) or chartBGColour
  363.     writeAt(1+i, oy, char, baseTextColour, b, outGraph)
  364.   end
  365.   local minPercent = "0%"
  366.   for i = 0, #minPercent do
  367.     local c = ox-#minPercent+i+1
  368.     local char = minPercent:sub(1+i, 1+i)
  369.     local b = fromBlit(bglow:sub(c, c)) or chartBGColour
  370.     writeAt(c, oy, char, baseTextColour, b, outGraph)
  371.   end
  372.   draw(76, 22, 21, 1, borderColour)
  373.   local rf_tick = conversion(lastOutput*-1, true) .. "RF/t "
  374.   writeAt(76, 21, rf_tick, (lastOutput > 0 and outputChartColour or idleColour))
  375.   writeAt(76+#rf_tick, 21, "(" .. tostring(round((lastOutput/(maxTransfer==0 and 1 or maxTransfer))*100) .. "%)  "))
  376.   if lastOutput == maxTransfer and maxTransfer > 0 then
  377.     local maxStrings = {"MAX OUTPUT!","Add / Upgrade","Providers."}
  378.     drawBar(82-(math.max(#maxStrings[1], #maxStrings[2], #maxStrings[3])/2)-1, #maxStrings[2]+2, 27, #maxStrings+2, 1)
  379.     writeAt(82-(#maxStrings[1]/2), 28, maxStrings[1], outputChartColour, 1)
  380.     writeAt(82-(#maxStrings[2]/2), 29, maxStrings[2], outputChartColour, 1)
  381.     writeAt(82-(#maxStrings[3]/2), 30, maxStrings[3], outputChartColour, 1)
  382.   end
  383. end
  384.  
  385. local function drawFlowGraph()
  386.   table.insert(flowHistory, lastInput-lastOutput); removeOverflow(flowHistory, 70)
  387.   local flowGraph = newWindow(m, 29, 4, 69, 16, chartBGColour)
  388.   local fx, fy = flowGraph.getSize()
  389.   local rf_min, rf_max = getRange(flowHistory)
  390.   local scale = math.max(math.abs(rf_min), rf_max)
  391.   if scale == 0 then scale = 1 end
  392.   for i = 0, #flowHistory do
  393.     local value = flowHistory[#flowHistory-i]; if not value then break end
  394.     value = math.abs(value)
  395.     local result = math.floor(math.min(math.max((value * (8 / scale)), 0), 8) + 0.5)
  396.     if result ~= result then result = 0 end
  397.     if result == 0 then
  398.       writeAt(fx-i, 8, "_", 32768, chartBGColour, flowGraph)
  399.     elseif flowHistory[#flowHistory-i] < 0 then
  400.       draw(fx-i, 1, 9, result, outputChartColour, flowGraph)
  401.     else
  402.       draw(fx-i, 1, 9-result, result, inputChartColour, flowGraph)
  403.     end
  404.   end
  405.   local lastFlow = flowHistory[#flowHistory]
  406.   draw(45, 15, 2, 1, borderColour)
  407.   writeAt(45, 2, conversion(lastFlow, true, true) .. "RF/t  ", (lastFlow == 0 and idleColour or (lastFlow > 0 and inputChartColour or outputChartColour)))
  408.   local positiveRf = conversion(scale, true, true) .. "RF/t"
  409.   local _, _, bgtop = flowGraph.getLine(1)
  410.   for i = 0, #positiveRf do
  411.     local char = positiveRf:sub(1+i, 1+i)
  412.     local b = fromBlit(bgtop:sub(1+i, 1+i)) or chartBGColour
  413.     writeAt(1+i, 1, char, baseTextColour, b, flowGraph)
  414.   end
  415.   local noRF = "0 RF/t"
  416.   local _, _, bgn = flowGraph.getLine(8)
  417.   for i = 0, #noRF do
  418.     local char = noRF:sub(1+i, 1+i)
  419.     local b = fromBlit(bgn:sub(1+i, 1+i)) or chartBGColour
  420.     writeAt(1+i, 8, char, baseTextColour, b, flowGraph)
  421.   end
  422.   local negativeRf = conversion(scale*-1, true, true) .. "RF/t"
  423.   local _, _, bgbot = flowGraph.getLine(16)
  424.   for i = 0, #negativeRf do
  425.     local char = negativeRf:sub(1+i, 1+i)
  426.     local b = fromBlit(bgbot:sub(1+i, 1+i)) or chartBGColour
  427.     writeAt(1+i, 16, char, baseTextColour, b, flowGraph)
  428.   end
  429. end
  430.  
  431. local imWidth, imLength, imHeight = nil, nil, nil
  432.  
  433. local function drawStatistics()
  434.   local row = 30
  435.   if imWidth and imHeight and imLength then
  436.     writeAt(4, row, "Size: " .. imWidth .. " x " .. imHeight .. " x " .. imLength, baseTextColour); row = row + 1
  437.   end
  438.   if installedCells ~= nil then writeAt(4, row, "Cells: " .. installedCells, baseTextColour); row = row + 1 end
  439.   if installedProviders ~= nil then writeAt(4, row, "Providers: " .. installedProviders, baseTextColour); row = row + 1 end
  440.   if imWidth and imHeight and imLength and installedCells and installedProviders then
  441.     local empty = ((imWidth - 2) * (imLength - 2) * (imHeight - 2)) - installedCells - installedProviders
  442.     writeAt(4, row, "Empty Slots: " .. tostring(empty), baseTextColour); row = row + 1
  443.   end
  444.   writeAt(4, row, "MaxTransfer: " .. conversion(maxTransfer, true) .. "/t", baseTextColour); row = row + 1
  445.   writeAt(4, row, "MaxCapacity: " .. conversion(maxEnergy, true), baseTextColour)
  446. end
  447.  
  448. installedCells     = hasMethod(im, "getInstalledCells")     and protCall(im.getInstalledCells, 1)     or nil
  449. installedProviders = hasMethod(im, "getInstalledProviders") and protCall(im.getInstalledProviders, 1) or nil
  450. maxEnergy          = (protCall(im.getMaxEnergy, 1) or 0) / dataCorrection
  451. maxTransfer        = (protCall(im.getTransferCap, 1) or 0) / dataCorrection
  452. imWidth            = hasMethod(im, "getWidth")  and protCall(im.getWidth, 1)  or nil
  453. imLength           = hasMethod(im, "getLength") and protCall(im.getLength, 1) or nil
  454. imHeight           = hasMethod(im, "getHeight") and protCall(im.getHeight, 1) or nil
  455.  
  456. drawStaticElements()
  457. drawStatistics()
  458.  
  459. local monitor_touched, x_touch, y_touch
  460. local function touch()
  461.   while true do
  462.     local event_data = table.pack(os.pullEvent())
  463.     if event_data[1] == "monitor_touch" then
  464.       monitor_touched, x_touch, y_touch = table.unpack(event_data, 2, event_data.n)
  465.       break
  466.     end
  467.   end
  468. end
  469.  
  470. local function display()
  471.   while true do
  472.     energy = protCall(im.getEnergy, 1) / dataCorrection
  473.     lastInput = protCall(getLastInput, 1) / dataCorrection
  474.     lastOutput = protCall(getLastOutput, 1) / dataCorrection
  475.     filledPercentage = protCall(getFilledPct, 1)
  476.     drawInputGraph()
  477.     drawOutputGraph()
  478.     drawFlowGraph()
  479.     drawBattery()
  480.     os.sleep(screenRefreshRate)
  481.   end
  482. end
  483.  
  484. function saveConfig(name, value)
  485.   local filename = shell.getRunningProgram()
  486.   local lines = {}
  487.   for line in io.lines(filename) do table.insert(lines, line) end
  488.   local settingsMap = {
  489.     borderColour = { var = "borderColour", line = 1+1 },
  490.     chartBGColour = { var = "chartBGColour", line = 2+1 },
  491.     inputChartColour = { var = "inputChartColour", line = 3+1 },
  492.     outputChartColour = { var = "outputChartColour", line = 4+1 },
  493.     batteryChargeColour = { var = "batteryChargeColour", line = 5+1 },
  494.     idleColour = { var = "idleColour", line = 6+1 },
  495.     sectionTitleColour = { var = "sectionTitleColour", line = 7+1 },
  496.     baseTextColour = { var = "baseTextColour", line = 8+1 },
  497.     screenRefreshRate = { var = "screenRefreshRate", line = 10+1 },
  498.     unitFormatCaps = { var = "unitFormatCaps", line = 11+1 },
  499.   }
  500.   if settingsMap[name] then
  501.     if name == "borderColour" then borderColour = value
  502.     elseif name == "chartBGColour" then chartBGColour = value
  503.     elseif name == "inputChartColour" then inputChartColour = value
  504.     elseif name == "outputChartColour" then outputChartColour = value
  505.     elseif name == "batteryChargeColour" then batteryChargeColour = value
  506.     elseif name == "idleColour" then idleColour = value
  507.     elseif name == "sectionTitleColour" then sectionTitleColour = value
  508.     elseif name == "baseTextColour" then baseTextColour = value
  509.     elseif name == "screenRefreshRate" then screenRefreshRate = value
  510.     elseif name == "unitFormatCaps" then unitFormatCaps = value
  511.     end
  512.     lines[settingsMap[name].line] = "local " .. settingsMap[name].var .. " = " .. tostring(value)
  513.   end
  514.   local h_ = io.open(filename, 'w'); for _, line in ipairs(lines) do h_:write(line .. "\n") end; h_:close()
  515. end
  516.  
  517. local function dropdownMenu(x, y, options)
  518.   local width = 0
  519.   local optionText = ""
  520.   for i = 1, #options do options[i] = tostring(options[i]); if #options[i] > width then width = #options[i] end end
  521.   for i = 1, #options do
  522.     optionText = " " .. options[i] .. "  "
  523.     for j = 1, width - #options[i] do optionText = optionText .. " " end
  524.     writeAt(x - 2 - width, y + i, optionText, 1, 256)
  525.   end
  526.   while true do
  527.     local event, side, ox, oy = os.pullEvent("monitor_touch")
  528.     if (ox >= x - 2 - width and ox <= x and oy >= y + 1 and oy <= y + #options) then
  529.       return oy-y
  530.     else
  531.       return nil
  532.     end
  533.   end
  534. end
  535.  
  536. local function drawSettings(settingsX, settingsY, sWidth, sHeight)
  537.   local sTextColour = 1
  538.   local sBGColour = 128
  539.   drawBar(settingsX - 2, sWidth, settingsY - 2, 1, sBGColour)
  540.   drawBar(settingsX - 2, sWidth, settingsY + sHeight - 3, 1, sBGColour)
  541.   drawBar(settingsX - 2, 1, settingsY - 2, sHeight, sBGColour)
  542.   drawBar(settingsX - 2 + sWidth, 1, settingsY - 2, sHeight, sBGColour)
  543.   local labels = {"Borders: ","Chart Background: ","Input Chart: ","Output Chart: ","Battery: ","Idle: ","Section Titles: ","Base Text: "}
  544.   for i, label in ipairs(labels) do writeAt(settingsX, i + settingsY - 1, label) end
  545.   writeAt(settingsX, settingsY + 9, "Refresh Rate: ")
  546.   writeAt(settingsX, settingsY + 10, "Unit Style: ")
  547.   local configs = {
  548.     {row = 0, config = borderColour},
  549.     {row = 1, config = chartBGColour},
  550.     {row = 2, config = inputChartColour},
  551.     {row = 3, config = outputChartColour},
  552.     {row = 4, config = batteryChargeColour},
  553.     {row = 5, config = idleColour},
  554.     {row = 6, config = sectionTitleColour},
  555.     {row = 7, config = baseTextColour},
  556.     {row = 9, config = screenRefreshRate},
  557.     {row = 10, config = unitFormatCaps},
  558.   }
  559.   for i, data in ipairs(configs) do
  560.     local row = data.row
  561.     local config = data.config
  562.     local configText
  563.     if row <= 7 then
  564.       configText = tostring(colorText[indexOf(colorDec, config)]) .. " "
  565.       writeAt(settingsX + 18, settingsY + row, "  ", 1, config)
  566.       writeAt(settingsX + 20, settingsY + row, "            ", 1, sBGColour)
  567.     elseif row == 9 then
  568.       configText = tostring(refreshRates.display[indexOf(refreshRates.values, config)]) .. " "
  569.       writeAt(settingsX + 18, settingsY + row, "              ", 1, sBGColour)
  570.     elseif row == 10 then
  571.       configText = tostring(unitStyle.display[indexOf(unitStyle.values, config)]) .. " "
  572.       writeAt(settingsX + 18, settingsY + row, "              ", 1, sBGColour)
  573.     end
  574.     writeAt(settingsX + 21, settingsY + row, configText, sTextColour, sBGColour)
  575.     writeAt(settingsX + 32, settingsY + row, string.char(31), sTextColour, sBGColour)
  576.   end
  577.   writeAt(settingsX, settingsY - 2, "  SETTINGS  ", sTextColour, 32768)
  578.   writeAt(settingsX, settingsY + sHeight - 5, " " .. string.char(27) .. " BACK ", sTextColour, 16384)
  579.   writeAt(settingsX + 10, settingsY + sHeight - 5, " RESET ", sTextColour, 512)
  580. end
  581.  
  582. local function settings()
  583.   local sWidth = 37
  584.   local sHeight = 17
  585.   local settingsX = (w / 2) - math.floor(sWidth / 2) + 2
  586.   local settingsY = (h / 2) - math.floor(sHeight / 2) + 2
  587.   local colourConfigs = {
  588.     [settingsY + 0] = "borderColour",
  589.     [settingsY + 1] = "chartBGColour",
  590.     [settingsY + 2] = "inputChartColour",
  591.     [settingsY + 3] = "outputChartColour",
  592.     [settingsY + 4] = "batteryChargeColour",
  593.     [settingsY + 5] = "idleColour",
  594.     [settingsY + 6] = "sectionTitleColour",
  595.     [settingsY + 7] = "baseTextColour",
  596.   }
  597.   local numberConfigs = {
  598.     [settingsY + 9] = "screenRefreshRate",
  599.     [settingsY + 10] = "unitFormatCaps",
  600.   }
  601.   while true do
  602.     m.clear()
  603.     drawSettings(settingsX, settingsY, sWidth, sHeight)
  604.     local event, side, x_, y_ = os.pullEvent("monitor_touch")
  605.     if x_ >= settingsX + 18 and x_ <= settingsX + 32 then
  606.       if colourConfigs[y_] then
  607.         local optionIndex = dropdownMenu(settingsX + 32, y_, colorText)
  608.         if optionIndex ~= nil then saveConfig(colourConfigs[y_], colorDec[optionIndex]) end
  609.       elseif y_ == settingsY + 9 then
  610.         local optionIndex = dropdownMenu(settingsX + 32, y_, refreshRates.display)
  611.         if optionIndex ~= nil then saveConfig("screenRefreshRate", refreshRates.values[optionIndex]) end
  612.       elseif y_ == settingsY + 10 then
  613.         local optionIndex = dropdownMenu(settingsX + 32, y_, unitStyle.display)
  614.         if optionIndex ~= nil then saveConfig("unitFormatCaps", unitStyle.values[optionIndex]) end
  615.       end
  616.     elseif x_ >= settingsX and x_ <= settingsX + 7 and y_ == settingsY + sHeight - 5 then
  617.       return
  618.     elseif x_ >= settingsX + 10 and x_ <= settingsX + 16 and y_ == settingsY + sHeight - 5 then
  619.       local defaults = {
  620.         borderColour = 128, chartBGColour = 128, inputChartColour = 32,
  621.         outputChartColour = 16384, batteryChargeColour = 8, idleColour = 16,
  622.         sectionTitleColour = 1, baseTextColour = 1, screenRefreshRate = 0.1,
  623.         unitFormatCaps = true
  624.       }
  625.       for configName, value in pairs(defaults) do saveConfig(tostring(configName), value) end
  626.     end
  627.   end
  628. end
  629.  
  630. local redrawRequired = false
  631. while true do
  632.   if redrawRequired then
  633.     m.clear(); drawStaticElements(); drawStatistics(); redrawRequired = false
  634.   end
  635.   parallel.waitForAny(touch, display)
  636.   if y_touch == 2 and x_touch >= 87 and x_touch <= 96 then
  637.     redrawRequired = true; m.clear(); settings()
  638.   end
  639. end
  640.  
Add Comment
Please, Sign In to add comment