Corbinhol

Console Library

Jul 30th, 2024 (edited)
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.20 KB | None | 0 0
  1. --Console Library
  2. local console = {};
  3. local term = require("term");
  4. local component = require("component");
  5. local thread = require("thread");
  6. local event = require("event");
  7. local serialization = require("serialization");
  8. local computer = require("computer");
  9. local network = require("network");
  10. local gpu = component.gpu;
  11. local width, height = gpu.getResolution();
  12.  
  13. local shouldRun = true;
  14. local loggedMessages = {};
  15. local tPrint;
  16.  
  17. local testInt = 0;
  18.  
  19. local drawLoopThread, inputThread;
  20. console.colors = {};
  21.  
  22. console.topBarInfo = {};
  23. console.topBarInfo.status = network.STATUS_FLAG_DISCONNECTED;
  24.  
  25. console.colors["0"] = 0xF0F0F0; --White
  26. console.colors["1"] = 0xF2B233; --Orange
  27. console.colors["2"] = 0xE57FD8; --Magenta
  28. console.colors["3"] = 0x99B2F2; --Light Blue
  29. console.colors["4"] = 0xDEDE6C; --Yellow
  30. console.colors["5"] = 0x7FCC19; --Lime
  31. console.colors["6"] = 0xF2B2CC; --Pink
  32. console.colors["7"] = 0x4C4C4C; --Gray
  33. console.colors["8"] = 0x999999; --Light Gray
  34. console.colors["9"] = 0x4C99B2; --Cyan
  35. console.colors["a"] = 0xB266E5; --Purple
  36. console.colors["b"] = 0x3366CC; --Blue
  37. console.colors["c"] = 0x7f664c; --Brown
  38. console.colors["d"] = 0x57A64E; --Green
  39. console.colors["e"] = 0xCC4C4C; --Red
  40. console.colors["f"] = 0x000000; --Black
  41.  
  42. function console.init()
  43.     local drawLoopThread;
  44.     console.setupDraw();
  45.     -- drawLoopThread = thread.create(console.drawLoop)
  46.     -- table.insert(threadList, drawLoopThread);
  47.     inputThread = thread.create(console.input)
  48.     table.insert(threadList, consoleThread);
  49.     io.stdout = nil;
  50.     tprint = print;
  51.     print = console.print;
  52.     -- return drawLoopThread, inputThread;
  53. end
  54.  
  55. function console.setupDraw()
  56.     term.clear();
  57.     gpu.fill(1,1, width, 1, "=");
  58.     gpu.fill(1,3, width, 1, "=");
  59.     gpu.fill(1, height, width, 1, "=");
  60.     gpu.fill(1, height - 2, width, 1, "=");
  61.     gpu.set(2, height - 1, ">");
  62. end
  63.  
  64. function console.log(message, color)
  65.     local colorizedMessage = console.convertToColor(message, color);
  66.     table.insert(loggedMessages, colorizedMessage);
  67.     if #loggedMessages > height - 6 then
  68.         table.remove(loggedMessages, 1);
  69.     end
  70.     return colorizedMessage[#colorizedMessage]["color"];
  71. end
  72.  
  73. function console.formatTime(time) --Format seconds into readable time
  74.     local days = math.floor(time / 86400);
  75.     local hours = math.floor((time % 86400)/3600)
  76.     local minutes = math.floor((time % 3600)/60)
  77.     local seconds = math.floor((time % 60))
  78.     if days == 0 then
  79.         return string.format("%02d:%02d:%02d", hours,minutes,seconds)
  80.     end
  81.     return string.format("%02d:%02d:%02d:%02d",days,hours,minutes,seconds)
  82. end
  83.  
  84. function drawLines()
  85.     if #loggedMessages ~= 0 then
  86.         for i=1, #loggedMessages do
  87.             local currentChar = 1;
  88.             for t=1, #loggedMessages[#loggedMessages - i + 1] do
  89.                 gpu.setForeground(loggedMessages[#loggedMessages - i + 1][t]["color"]);
  90.                 gpu.set(1 + currentChar, (height - 2) - i, loggedMessages[#loggedMessages - i + 1][t]["text"]);
  91.                 currentChar = currentChar + #loggedMessages[#loggedMessages - i + 1][t]["text"];
  92.             end
  93.             gpu.set(1 + currentChar, (height - 2) - i, string.rep(" ", width));
  94.             gpu.setForeground(console.colors["0"]);
  95.         end
  96.     end
  97. end
  98.  
  99. function console.print(message, showTime)
  100.     if showTime == nil then
  101.         showTime = true;
  102.     end
  103.     if showTime then
  104.         message = "[&3" .. console.formatTime(computer.uptime()) .. "&0] " .. message;
  105.     end
  106.     local maxWidth = width - 2
  107.     local continueLoop = true;
  108.     local lastColor = nil;
  109.    
  110.     while continueLoop do
  111.         local currentChar = 1;
  112.         local lastSpace = 0;
  113.         local addNextCharacter = false;
  114.         local nextCharacterColor = true;
  115.  
  116.         if #message <= maxWidth then
  117.             console.log(message, lastColor)
  118.             break;
  119.         end
  120.        
  121.         for i=1, #message do
  122.             local char = message:sub(i,i);
  123.             local addLetter = true;
  124.  
  125.             if char == " " then
  126.                 lastSpace = i;
  127.             elseif char == "-" then
  128.                 if i < #message then
  129.                     if message:sub(i+1, i+1) == "&" then
  130.                         addLetter = false;
  131.                         addNextCharacter = true;
  132.                     end
  133.                 end
  134.             elseif char == "&" then
  135.                 if addNextCharacter then
  136.                     addNextCharacter = false;
  137.                 else
  138.                     addLetter = false;
  139.                     nextCharacterColor = true;
  140.                 end
  141.             elseif nextCharacterColor then
  142.                 addLetter = false;
  143.                 nextCharacterColor = false;
  144.             end
  145.  
  146.             if addLetter then
  147.                 currentChar = currentChar + 1;
  148.             end
  149.            
  150.             if currentChar == maxWidth then
  151.                 if lastSpace > 0 then
  152.                     lastColor = console.log(message:sub(1, lastSpace), lastColor);
  153.                     message = message:sub(lastSpace + 1, #message);
  154.                     currentChar = 1;
  155.                 else
  156.                     lastColor = console.log(message:sub(1, i), lastColor);
  157.                     message = message:sub(i + 1, #message);
  158.                 end
  159.                 break;
  160.             end
  161.         end
  162.     end
  163.     drawLines();
  164. end
  165.  
  166. function console.getColor(color)
  167.     if console.colors[color] == nil then
  168.         return console.color["0"]
  169.     else
  170.         return console.colors[color]
  171.     end
  172. end
  173.  
  174. function console.updateTopData(data)
  175.     console.topBarInfo = data;
  176. end
  177.  
  178. function console.convertToColor(message, color)
  179.     local continueLoop = true;
  180.     local currentColor;
  181.  
  182.     if color == nil then
  183.         color = console.getColor("0")
  184.     end
  185.  
  186.     if message:sub(1, 1) ~= "&" then
  187.         currentColor = color;
  188.     else
  189.         currentColor = console.getColor(message:sub(2, 2));
  190.     end
  191.  
  192.     local outputLog = {};
  193.     outputLog[1] = {};
  194.     outputLog[1]["color"] = currentColor;
  195.     outputLog[1]["text"] = "";
  196.  
  197.     while continueLoop do
  198.         local addNextCharacter = false;
  199.         local nextCharacterColor = false;
  200.         for i=1, #message do
  201.             local addThisCharacter = true;
  202.             local char = message:sub(i, i);
  203.             if char == "-" then
  204.                 if i < #message then
  205.                     if message:sub(i+1, i+1) == "&" then
  206.                         addNextCharacter = true;
  207.                         addThisCharacter = false;
  208.                     end
  209.                 end
  210.             elseif char == "&" then
  211.                 if addNextCharacter then
  212.                     addNextCharacter = false;
  213.                 else
  214.                     addThisCharacter = false;
  215.                     nextCharacterColor = true;
  216.                 end
  217.             elseif nextCharacterColor == true then
  218.                 outputLog[#outputLog + 1] = {};
  219.                 outputLog[#outputLog]["color"] = console.getColor(char);
  220.                 currentColor = console.getColor(char);
  221.                 outputLog[#outputLog]["text"] = "";
  222.                 message = message:sub(i + 1, #message);
  223.                 break;
  224.             end
  225.  
  226.             if addThisCharacter then
  227.                 outputLog[#outputLog]["text"] = outputLog[#outputLog]["text"] .. char;
  228.             end
  229.             if i == #message then
  230.                 continueLoop = false;
  231.             end
  232.         end
  233.     end
  234.     return outputLog;
  235. end
  236.  
  237. function console.input()
  238.     while(shouldRun) do
  239.         term.setCursor(4, height - 1);
  240.         local command = term.read({nowrap=true});
  241.         command = string.sub(command, 1, #command - 1)
  242.         gpu.fill(4, height - 1, width, 1, " ");
  243.         local commandLog = "";
  244.         local addSubtract = true;
  245.         for i=1, #command do
  246.             local char = command:sub(i, i);
  247.             if char == "-" then
  248.                 if i ~= #command then
  249.                     if command:sub(i + 1, i + 1) == "&" then
  250.                         addSubtract = false;
  251.                     end
  252.                 end
  253.             elseif char == "&" then
  254.                 if addSubtract then
  255.                     commandLog = commandLog .. "-";
  256.                 else
  257.                     addSubtract = true;
  258.                 end
  259.             end
  260.  
  261.             commandLog = commandLog .. char;
  262.         end
  263.         console.print("[&2User&0] " .. commandLog);
  264.         event.push("command", command);
  265.     end
  266. end
  267.  
  268. function console.clearLog()
  269.     loggedMessages = {};
  270.     gpu.fill(1, 4, width, height - 6, " ")
  271. end
  272.  
  273. function calculateBattery()
  274.     local output, color;
  275.     local percentage = (math.floor(tonumber(network.robotData["energy"])) / math.floor(tonumber(network.robotData["max-energy"]))) * 100;
  276.     if percentage < 25 then
  277.         output = "▌   ";
  278.         color = 0xCC4C4C;
  279.     elseif percentage >= 25 and percentage <= 50 then
  280.         output = "▌▌  ";
  281.         color = 0xF2B233;
  282.     elseif percentage >= 50 and percentage <= 75 then
  283.         output = "▌▌▌ ";
  284.         color = 0x57A64E;
  285.     elseif percentage >= 75 then
  286.         output = "▌▌▌▌";
  287.         color = 0x57A64E;
  288.     end
  289.     return output, color;
  290. end
  291.  
  292. function console.drawTopBar()
  293.     if network.status == network.STATUS_FLAG_DISCONNECTED or network.status == nil then
  294.         gpu.setForeground(0xCC4C4C);
  295.         gpu.set(1, 2, string.rep(" ", width / 2 - 7))
  296.         gpu.set(width - (width / 2) - 6, 2, "Disconnected");
  297.         gpu.set(width - (width / 2) + 7, 2, string.rep(" ", width / 2 - 6))
  298.     else
  299.         gpu.set(1, 2, string.rep(" ", width - 8))
  300.         gpu.setForeground(0xFFFFFF);
  301.         gpu.set(width - 7, 2, "[");
  302.         gpu.set(width - 2, 2, "]");
  303.         local battery, color = calculateBattery();
  304.         gpu.setForeground(color);
  305.         gpu.set(width - 6, 2, battery);
  306.     end
  307.     gpu.setForeground(0xFFFFFF);
  308. end
  309.  
  310.  
  311.  
  312. function console.drawLoop()
  313.     while(shouldRun) do
  314.         -- drawLines();
  315.         console.drawTopBar();
  316.         console.setupDraw();
  317.         os.sleep(1);
  318.     end
  319. end
  320.  
  321. function console.kill()
  322.     print = tprint;
  323.     inputThread:kill();
  324.     shouldRun = false;
  325.     term.clear();
  326. end
  327.  
  328. return console;
Advertisement
Add Comment
Please, Sign In to add comment