Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Console Library
- local console = {};
- local term = require("term");
- local component = require("component");
- local thread = require("thread");
- local event = require("event");
- local serialization = require("serialization");
- local computer = require("computer");
- local network = require("network");
- local gpu = component.gpu;
- local width, height = gpu.getResolution();
- local shouldRun = true;
- local loggedMessages = {};
- local tPrint;
- local testInt = 0;
- local drawLoopThread, inputThread;
- console.colors = {};
- console.topBarInfo = {};
- console.topBarInfo.status = network.STATUS_FLAG_DISCONNECTED;
- console.colors["0"] = 0xF0F0F0; --White
- console.colors["1"] = 0xF2B233; --Orange
- console.colors["2"] = 0xE57FD8; --Magenta
- console.colors["3"] = 0x99B2F2; --Light Blue
- console.colors["4"] = 0xDEDE6C; --Yellow
- console.colors["5"] = 0x7FCC19; --Lime
- console.colors["6"] = 0xF2B2CC; --Pink
- console.colors["7"] = 0x4C4C4C; --Gray
- console.colors["8"] = 0x999999; --Light Gray
- console.colors["9"] = 0x4C99B2; --Cyan
- console.colors["a"] = 0xB266E5; --Purple
- console.colors["b"] = 0x3366CC; --Blue
- console.colors["c"] = 0x7f664c; --Brown
- console.colors["d"] = 0x57A64E; --Green
- console.colors["e"] = 0xCC4C4C; --Red
- console.colors["f"] = 0x000000; --Black
- function console.init()
- local drawLoopThread;
- console.setupDraw();
- -- drawLoopThread = thread.create(console.drawLoop)
- -- table.insert(threadList, drawLoopThread);
- inputThread = thread.create(console.input)
- table.insert(threadList, consoleThread);
- io.stdout = nil;
- tprint = print;
- print = console.print;
- -- return drawLoopThread, inputThread;
- end
- function console.setupDraw()
- term.clear();
- gpu.fill(1,1, width, 1, "=");
- gpu.fill(1,3, width, 1, "=");
- gpu.fill(1, height, width, 1, "=");
- gpu.fill(1, height - 2, width, 1, "=");
- gpu.set(2, height - 1, ">");
- end
- function console.log(message, color)
- local colorizedMessage = console.convertToColor(message, color);
- table.insert(loggedMessages, colorizedMessage);
- if #loggedMessages > height - 6 then
- table.remove(loggedMessages, 1);
- end
- return colorizedMessage[#colorizedMessage]["color"];
- end
- function console.formatTime(time) --Format seconds into readable time
- local days = math.floor(time / 86400);
- local hours = math.floor((time % 86400)/3600)
- local minutes = math.floor((time % 3600)/60)
- local seconds = math.floor((time % 60))
- if days == 0 then
- return string.format("%02d:%02d:%02d", hours,minutes,seconds)
- end
- return string.format("%02d:%02d:%02d:%02d",days,hours,minutes,seconds)
- end
- function drawLines()
- if #loggedMessages ~= 0 then
- for i=1, #loggedMessages do
- local currentChar = 1;
- for t=1, #loggedMessages[#loggedMessages - i + 1] do
- gpu.setForeground(loggedMessages[#loggedMessages - i + 1][t]["color"]);
- gpu.set(1 + currentChar, (height - 2) - i, loggedMessages[#loggedMessages - i + 1][t]["text"]);
- currentChar = currentChar + #loggedMessages[#loggedMessages - i + 1][t]["text"];
- end
- gpu.set(1 + currentChar, (height - 2) - i, string.rep(" ", width));
- gpu.setForeground(console.colors["0"]);
- end
- end
- end
- function console.print(message, showTime)
- if showTime == nil then
- showTime = true;
- end
- if showTime then
- message = "[&3" .. console.formatTime(computer.uptime()) .. "&0] " .. message;
- end
- local maxWidth = width - 2
- local continueLoop = true;
- local lastColor = nil;
- while continueLoop do
- local currentChar = 1;
- local lastSpace = 0;
- local addNextCharacter = false;
- local nextCharacterColor = true;
- if #message <= maxWidth then
- console.log(message, lastColor)
- break;
- end
- for i=1, #message do
- local char = message:sub(i,i);
- local addLetter = true;
- if char == " " then
- lastSpace = i;
- elseif char == "-" then
- if i < #message then
- if message:sub(i+1, i+1) == "&" then
- addLetter = false;
- addNextCharacter = true;
- end
- end
- elseif char == "&" then
- if addNextCharacter then
- addNextCharacter = false;
- else
- addLetter = false;
- nextCharacterColor = true;
- end
- elseif nextCharacterColor then
- addLetter = false;
- nextCharacterColor = false;
- end
- if addLetter then
- currentChar = currentChar + 1;
- end
- if currentChar == maxWidth then
- if lastSpace > 0 then
- lastColor = console.log(message:sub(1, lastSpace), lastColor);
- message = message:sub(lastSpace + 1, #message);
- currentChar = 1;
- else
- lastColor = console.log(message:sub(1, i), lastColor);
- message = message:sub(i + 1, #message);
- end
- break;
- end
- end
- end
- drawLines();
- end
- function console.getColor(color)
- if console.colors[color] == nil then
- return console.color["0"]
- else
- return console.colors[color]
- end
- end
- function console.updateTopData(data)
- console.topBarInfo = data;
- end
- function console.convertToColor(message, color)
- local continueLoop = true;
- local currentColor;
- if color == nil then
- color = console.getColor("0")
- end
- if message:sub(1, 1) ~= "&" then
- currentColor = color;
- else
- currentColor = console.getColor(message:sub(2, 2));
- end
- local outputLog = {};
- outputLog[1] = {};
- outputLog[1]["color"] = currentColor;
- outputLog[1]["text"] = "";
- while continueLoop do
- local addNextCharacter = false;
- local nextCharacterColor = false;
- for i=1, #message do
- local addThisCharacter = true;
- local char = message:sub(i, i);
- if char == "-" then
- if i < #message then
- if message:sub(i+1, i+1) == "&" then
- addNextCharacter = true;
- addThisCharacter = false;
- end
- end
- elseif char == "&" then
- if addNextCharacter then
- addNextCharacter = false;
- else
- addThisCharacter = false;
- nextCharacterColor = true;
- end
- elseif nextCharacterColor == true then
- outputLog[#outputLog + 1] = {};
- outputLog[#outputLog]["color"] = console.getColor(char);
- currentColor = console.getColor(char);
- outputLog[#outputLog]["text"] = "";
- message = message:sub(i + 1, #message);
- break;
- end
- if addThisCharacter then
- outputLog[#outputLog]["text"] = outputLog[#outputLog]["text"] .. char;
- end
- if i == #message then
- continueLoop = false;
- end
- end
- end
- return outputLog;
- end
- function console.input()
- while(shouldRun) do
- term.setCursor(4, height - 1);
- local command = term.read({nowrap=true});
- command = string.sub(command, 1, #command - 1)
- gpu.fill(4, height - 1, width, 1, " ");
- local commandLog = "";
- local addSubtract = true;
- for i=1, #command do
- local char = command:sub(i, i);
- if char == "-" then
- if i ~= #command then
- if command:sub(i + 1, i + 1) == "&" then
- addSubtract = false;
- end
- end
- elseif char == "&" then
- if addSubtract then
- commandLog = commandLog .. "-";
- else
- addSubtract = true;
- end
- end
- commandLog = commandLog .. char;
- end
- console.print("[&2User&0] " .. commandLog);
- event.push("command", command);
- end
- end
- function console.clearLog()
- loggedMessages = {};
- gpu.fill(1, 4, width, height - 6, " ")
- end
- function calculateBattery()
- local output, color;
- local percentage = (math.floor(tonumber(network.robotData["energy"])) / math.floor(tonumber(network.robotData["max-energy"]))) * 100;
- if percentage < 25 then
- output = "▌ ";
- color = 0xCC4C4C;
- elseif percentage >= 25 and percentage <= 50 then
- output = "▌▌ ";
- color = 0xF2B233;
- elseif percentage >= 50 and percentage <= 75 then
- output = "▌▌▌ ";
- color = 0x57A64E;
- elseif percentage >= 75 then
- output = "▌▌▌▌";
- color = 0x57A64E;
- end
- return output, color;
- end
- function console.drawTopBar()
- if network.status == network.STATUS_FLAG_DISCONNECTED or network.status == nil then
- gpu.setForeground(0xCC4C4C);
- gpu.set(1, 2, string.rep(" ", width / 2 - 7))
- gpu.set(width - (width / 2) - 6, 2, "Disconnected");
- gpu.set(width - (width / 2) + 7, 2, string.rep(" ", width / 2 - 6))
- else
- gpu.set(1, 2, string.rep(" ", width - 8))
- gpu.setForeground(0xFFFFFF);
- gpu.set(width - 7, 2, "[");
- gpu.set(width - 2, 2, "]");
- local battery, color = calculateBattery();
- gpu.setForeground(color);
- gpu.set(width - 6, 2, battery);
- end
- gpu.setForeground(0xFFFFFF);
- end
- function console.drawLoop()
- while(shouldRun) do
- -- drawLines();
- console.drawTopBar();
- console.setupDraw();
- os.sleep(1);
- end
- end
- function console.kill()
- print = tprint;
- inputThread:kill();
- shouldRun = false;
- term.clear();
- end
- return console;
Advertisement
Add Comment
Please, Sign In to add comment