Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ExtraUtilits = {}
- ExtraUtilits.version = 1.12
- ------------------------------------------------
- local io = require("io")
- local component = require("component")
- local gpu = component.gpu
- local term = require("term")
- local event = require("event")
- local fs = require("filesystem")
- local unicode = require("unicode")
- local serialization = require("serialization")
- local computer = require("computer")
- local internet = require("internet")
- local shell = require("shell")
- ------------------------------------------------
- local function loadModule(name)
- local path = "/lib/ExtraUtilitsModules/" .. name .. ".lua"
- local func, reason = loadfile(path)
- if not func then
- local func = function()
- local file = io.open("/lib/ExtraErrLogs.txt", "a")
- file:write("[" .. computer.uptime() .. "]:" .. name .. ":" .. path .. ":\n" .. reason .. "\n")
- file:close()
- local text = "Error with loading module:"
- local text2 = "you can see error logs at /lib/ExtraErrLogs.txt."
- local length = #text
- if #text2 > length then length = #text2 end
- local width, height = length+2, 4
- local w, h = gpu.getResolution()
- gpu.setBackground(0x323232)
- gpu.setForeground(0xff0000)
- gpu.fill(w / 2 - width / 2, h / 2 - height / 2, width, height, " ")
- gpu.set(w / 2 - #text/2, h/2-1, text)
- gpu.set(w / 2 - #text2/2, h/2, text2)
- event.pull("key_down")
- end
- return function() ExtraUtilits.screenBackup(func) end
- else
- return func
- end
- end
- ------------------------------------------------
- function ExtraUtilits.getLines(path) -- Устарело
- if fs.exists(path) and not fs.isDirectory(path) then
- local lines = 0
- local file = io.open(path, "r")
- local i = 0
- repeat
- local line = file:read()
- i = i + 1
- until line == nil
- return i - 1
- elseif not fs.exists(path) then
- return nil, "File does not exists"
- elseif fs.isDirectory(path) then
- return nil, "File is directory"
- end
- end
- function ExtraUtilits.progressBar(x, y, whide, progress, total, Bcolor, Fcolor)
- local color = gpu.getBackground()
- gpu.setBackground(Bcolor)
- gpu.fill(x, y, x + whide, 1, " ")
- gpu.setBackground(Fcolor)
- gpu.fill(x, y, x + whide / total * progress, 1, "a")
- gpu.setBackground(color)
- end
- function ExtraUtilits.screenBackup(...)
- local func = table.unpack({...})
- local gpu = require("component").gpu
- local w, h = gpu.getResolution()
- local scr = {}
- for i = 1, w do
- scr[i] = {}
- for j = 1, h do
- scr[i][j] = {gpu.get(i, j)}
- end
- end
- func()
- for i = 1, w do
- for j = 1, h do
- local symbol = {gpu.get(i, j)}
- if symbol[1] ~= scr[i][j][1] or symbol[2] ~= scr[i][j][2] or symbol[3] ~= scr[i][j][3] then
- if gpu.getBackground ~= scr[i][j][3] then gpu.setBackground(scr[i][j][3]) end
- if gpu.getForeground ~= scr[i][j][2] then gpu.setForeground(scr[i][j][2]) end
- gpu.set(i, j, scr[i][j][1])
- end
- end
- end
- end
- function ExtraUtilits.buttonPress(...)
- local args = {...}
- local close = false
- while not close do
- local ev = {computer.pullSignal()}
- local _, _, x, y, key = table.unpack(ev)
- if ev[1] == "touch" or ev[1] == "drag" then
- for i = 1, #args do
- local mas = args[i]
- if mas.close == true then close = true end
- end
- if x ~= nil and y ~= nil then
- for i = 1, #args do
- local mas = args[i]
- for j = 1, #mas do
- if type(mas[i]) == "table" then
- if x >= mas[j][1] and x <= mas[j][1] + mas[j][3] - 1 and y >= mas[j][2] and y <= mas[j][2] + mas[j][4] - 1 and mas[j][9] ~= false then
- if key == 0 or mas[j][10] == nil then
- mas[j][8]()
- else if key == 1 then
- mas[j][10]() end
- end
- os.sleep(0.05)
- end
- end
- end
- end
- end
- end
- for i = 1, #args do
- if args[i].close then close = true end
- if args[i].eventFunc and ev then args[i].eventFunc(ev) end
- end
- end
- end
- function ExtraUtilits.draw(mas)
- for i = 1, #mas do
- if mas[i][9] ~= false then
- local posX = mas[i][1] + mas[i][3] / 2 - unicode.wlen(mas[i][7]) / 2
- local posY = mas[i][2] + mas[i][4] / 2
- gpu.setBackground(mas[i][5])
- gpu.setForeground(mas[i][6])
- gpu.fill(mas[i][1], mas[i][2], mas[i][3], mas[i][4], " ")
- gpu.set(posX, posY, mas[i][7])
- end
- end
- end
- function ExtraUtilits.save(var, path)
- local tp = type(var)
- if tp == "table" then
- local file = io.open(path, "w")
- file:write("--TABLE--\n" .. serialization.serialize(var))
- file:close()
- else
- local file = io.open(path, "w")
- file:write("--" .. unicode.upper(type(var)) .. "--\n" .. tostring(var))
- file:close()
- end
- end
- function ExtraUtilits.load(path)
- if not fs.exists then return nil, "File does not exists" end
- local file = io.open(path, "r")
- local tp = unicode.lower(string.sub(file:read(), 3, -3))
- if tp == "table" then
- return serialization.unserialize(file:read())
- elseif tp == "number" then
- return tonumber(file:read())
- else
- return file:read()
- end
- end
- function ExtraUtilits.list(path)
- local list = fs.list(path)
- tbl = {}
- i = 1
- w = false
- while not w do
- local n = list()
- if n then
- tbl[i] = n
- i = i + 1
- else
- w = true
- end
- end
- return tbl
- end
- function ExtraUtilits.disintegrate(text)
- local massive = {}
- for i = 1, unicode.wlen(text) do
- massive[i] = tonumber(unicode.sub(text, i, -(unicode.wlen(text) - i + 1)))
- end return table.unpack(massive)
- end
- function ExtraUtilits.stringToTable(str)
- local tbl = {}
- for i = 1, unicode.wlen(str) do
- tbl[i] = string.sub(str, i, i)
- end
- return tbl
- end
- function ExtraUtilits.replace(str, list)
- local tbl = ExtraUtilits.stringToTable(str)
- local replaced = ""
- for index = 1, #tbl do
- local symbol = nil
- for from in pairs(list) do
- if from == tbl[index] then
- symbol = list[from]
- end
- end
- if not symbol then
- symbol = tbl[index]
- end
- replaced = replaced .. symbol
- end
- return replaced
- end
- function ExtraUtilits.getFormat(str)
- local tbl = ExtraUtilits.stringToTable(str)
- local format
- for i = 1, string.len(str) do
- if tbl[i] == "." then format = string.sub(str, -(string.len(str)-i)) end
- end
- return format
- end
- function ExtraUtilits.getLine(path, num)
- if fs.exists(path) and not fs.isDirectory(path) and ExtraUtilits.getLines(path) >= num then
- local file = io.open(path, "r")
- local line
- for i = 1, num do
- if i == num then line = file:read() else file:read() end
- end
- file:close()
- return line
- elseif not fs.exists(path) then return nil, "File does not exists."
- elseif fs.isDirectory(path) then return nil, "File is directory."
- end
- end
- ------------------------------------------------
- ExtraUtilits.ReadyUtilits = {}
- function ExtraUtilits.ReadyUtilits.WriteFieldUI(...)
- local text = ""
- local args = {...}
- local function forBackup()
- local x = args[1]
- local y = args[2]
- local txt = args[3]
- if #args == 3 then
- tColor = 0xffffff
- bColor = 0x161616
- sColor = 0x161616
- cColor = 0x323232
- fColor = 0x00ff00
- fOkColor = 0x00ff00
- bOkColor = 0xffffff
- elseif #args == 10 then
- fColor = args[4]
- bColor = args[5]
- sColor = args[6]
- cColor = args[7]
- tColor = args[8]
- fOkColor = args[9]
- bOkColor = args[10]
- else
- print("args: pos x, pos y, message text, write color, write field background color, window background color, cursor color, mes color, OK button foreground, OK button background.")
- os.exit()
- end
- local time = computer.uptime()
- gpu.setBackground(sColor)
- gpu.setForeground(tColor)
- gpu.fill(x, y, 40, 3, " ")
- gpu.setBackground(sColor)
- gpu.setForeground(tColor)
- gpu.fill(x + 1, y + 1, 38, 1, " ")
- gpu.set(x, y, txt)
- local w = false
- gpu.setForeground(fOkColor)
- gpu.setBackground(bOkColor)
- gpu.fill(x + 35, y + 2, 4, 1, " ")
- gpu.set(x + 35, y + 2, "OK")
- gpu.setBackground(bColor)
- gpu.setForeground(fColor)
- gpu.fill(x + 1, y + 1, 38, 1, " ")
- while not w do
- local type, _, key, eY = event.pull(0)
- if type == "key_down" then
- time = computer.uptime()
- if key >= 33 and key <= 125 or key >= 97 and key <= 122 or key >= 1040 and key <= 1071 or key <= 1103 and key >= 1072 or key == 32 then
- if unicode.wlen(text) < 38 then
- text = text .. unicode.char(key)
- gpu.setForeground(fColor)
- gpu.setBackground(bColor)
- gpu.set(x + 1, y + 1, text)
- end
- elseif key == 13 then
- os.sleep(0.0000010) w = true
- elseif key == 8 then
- text = string.sub(text, 1, unicode.wlen(text) - 1)
- gpu.fill(x + 1, y + 1, 38, 1, " ")
- gpu.set(x + 1, y + 1, text)
- elseif key == 9 then
- w = true
- n = false
- end
- elseif type == "touch" or type == "drag" then
- if key >= x + 35 and key <= x + 39 and eY == y + 2 then
- w = true
- end
- end
- if computer.uptime() - time == 1 then
- time = computer.uptime()
- if unicode.wlen(text) <= 37 then
- gpu.setForeground(cColor)
- if gpu.get(x + 1 + unicode.wlen(text), y + 1) == " " then
- gpu.set(x + 1 + unicode.wlen(text), y + 1, "∣")
- else
- gpu.set(x + 1 + unicode.wlen(text), y + 1, " ")
- end
- gpu.setForeground(fColor)
- end
- end
- end
- end
- ExtraUtilits.screenBackup(forBackup, 200)
- return text
- end
- function ExtraUtilits.ReadyUtilits.RGBPalette(x, y, text, color)
- os.sleep(0.05)
- local symbols = {
- unicode.char(9654),
- unicode.char(9664)
- }
- local r, g, b = 0, 0, 0
- if color then r, g, b = ExtraUtilits.Color.Extract(color) end
- local function main()
- gpu.setBackground(0x161616)
- gpu.fill(x, y, 15, 5, " ")
- if text then
- gpu.setForeground(0x00ffff)
- gpu.set(x, y, text)
- end
- local ui = function() end
- local color = ExtraUtilits.Color.Pack(r, g, b)
- function setR()
- repeat
- r = tonumber(ExtraUtilits.ReadyUtilits.WriteFieldUI(60, 23, "Enter count of red color:"))
- if r then ui() end
- until r and r>=0 and r<=255
- end
- function setB()
- repeat
- b = tonumber(ExtraUtilits.ReadyUtilits.WriteFieldUI(60, 23, "Enter count of blue color:"))
- if b then ui() end
- until b and b>=0 and b<=255
- end
- function setG()
- repeat
- g = tonumber(ExtraUtilits.ReadyUtilits.WriteFieldUI(60, 23, "Enter count of green color:"))
- if g then ui() end
- until g and g>=0 and g<=255
- end
- local buttons = {
- {x+8, y+1, 1, 1, 0x161616, 0x00ffff, symbols[2], function() if r - 5 >= 0 then r = r - 5 ui() end end},
- {x+8, y+2, 1, 1, 0x161616, 0x00ffff, symbols[2], function() if g - 5 >= 0 then g = g - 5 ui() end end},
- {x+8, y+3, 1, 1, 0x161616, 0x00ffff, symbols[2], function() if b - 5 >= 0 then b = b - 5 ui() end end},
- {x+12, y+1, 1, 1, 0x161616, 0x00ffff, symbols[1], function() if r + 5 <= 255 then r = r + 5 ui() end end},
- {x+12, y+2, 1, 1, 0x161616, 0x00ffff, symbols[1], function() if g + 5 <= 255 then g = g + 5 ui() end end},
- {x+12, y+3, 1, 1, 0x161616, 0x00ffff, symbols[1], function() if b + 5 <= 255 then b = b + 5 ui() end end},
- {x+9, y+1, 3, 1, 0x161616, 0xff0000, tostring(r), setR},
- {x+9, y+2, 3, 1, 0x161616, 0x00ff00, tostring(g), setG},
- {x+9, y+3, 3, 1, 0x161616, 0x0000ff, tostring(b), setB}
- }
- function ui()
- color = ExtraUtilits.Color.Pack(r, g, b)
- gpu.setBackground(color)
- gpu.fill(x+2, y+1, 4, 3, " ")
- ExtraUtilits.draw(buttons)
- buttons[7][7] = tostring(r)
- buttons[8][7] = tostring(g)
- buttons[9][7] = tostring(b)
- ExtraUtilits.draw(buttons)
- end
- buttons.eventFunc = function(ev)
- if ev[3] == 13 and ev[1] == "key_down" then
- buttons.close = true
- elseif ev[1] == "scroll" and ev[3] <= x + 12 and ev[3] >= x + 9 then
- if ev[5] == 1 then
- if ev[4] == y+1 and r + 3 <= 255 then r = r + 3 end
- if ev[4] == y+2 and g + 3 <= 255 then g = g + 3 end
- if ev[4] == y+3 and b + 3 <= 255 then b = b + 3 end
- else
- if ev[4] == y+1 and r - 3 >= 0 then r = r - 3 end
- if ev[4] == y+2 and g - 3 >= 0 then g = g - 3 end
- if ev[4] == y+3 and b - 3 >= 0 then b = b - 3 end
- end
- ui()
- end
- end
- ui()
- ExtraUtilits.buttonPress(buttons)
- end
- ExtraUtilits.screenBackup(main)
- return ExtraUtilits.Color.Pack(r, g, b)
- end
- function ExtraUtilits.ReadyUtilits.WFUIV2(...)
- return loadModule("WriteFieldUIv2")(...)
- end
- ------------------------------------------------
- ExtraUtilits.Color = {}
- function ExtraUtilits.Color.Extract(color)
- color = color % 0x1000000
- local r = math.floor(color / 0x10000)
- local g = math.floor((color - r * 0x10000) / 0x100)
- local b = color - r * 0x10000 - g * 0x100
- return r, g, b
- end
- function ExtraUtilits.Color.Pack(r, g, b)
- if r>=0 and r<=255 and g>=0 and g<=255 and b>=0 and g<=255 then return (r *(256^2)) + (g * 256) + b end
- end
- function ExtraUtilits.Color.Invert(color)
- local r, g, b = ExtraUtilits.Color.Extract(color)
- return ExtraUtilits.Color.Pack(255 - r, 255 - g, 255 - b)
- end
- function ExtraUtilits.Color.Add(color, value)
- local r, g, b = ExtraUtilits.Color.Extract(color)
- if r + value > 255 then r = 255 elseif r + value < 0 then r = 0 else r = r - value end
- if g + value > 255 then g = 255 elseif g + value < 0 then g = 0 else g = g - value end
- if b + value > 255 then b = 255 elseif b + value < 0 then b = 0 else b = b - value end
- end
- function ExtraUtilits.Color.hideScreen(func, value)
- local function main()
- local w, h = gpu.getResolution()
- for i = 1, w do
- for j = 1, h do
- local symbol, foreground, background = gpu.get(i, j)
- local fr, fg, fb = ExtraUtilits.Color.Extract(foreground)
- local br, bg, bb = ExtraUtilits.Color.Extract(background)
- if fr - value >= 0 then fr = fr - value end
- if fg - value >= 0 then fg = fg - value end
- if fb - value >= 0 then fb = fb - value end
- if br - value >= 0 then br = br - value end
- if bg - value >= 0 then bg = bg - value end
- if bb - value >= 0 then bb = bb - value end
- foreground = ExtraUtilits.Color.Pack(fr, fg, fb)
- background = ExtraUtilits.Color.Pack(br, bg, bb)
- if gpu.getForeground ~= foreground then gpu.setForeground(ExtraUtilits.Color.Pack(fr, fg, fb)) end
- if gpu.getBackground ~= background then gpu.setBackground(ExtraUtilits.Color.Pack(br, bg, bb)) end
- gpu.set(i, j, symbol)
- end
- end
- func()
- end
- ExtraUtilits.screenBackup(main)
- end
- ------------------------------------------------
- ExtraUtilits.internet = {}
- function ExtraUtilits.internet.get(URL)
- local req = internet.request(URL)
- local text = ""
- for line in req do
- text = text .. line
- end
- return text
- end
- function ExtraUtilits.internet.downloadPaste(paste, path)
- local file, reason = io.open(path, "w")
- if not file then
- error("Failed to opening file for writing: " .. reason)
- end
- file:write(get("https://pastebin.com/raw/" .. paste))
- file:close()
- end
- function ExtraUtilits.internet.download(URL, path)
- local file = io.open(path, "w")
- file:write(ExtraUtilits.internet.get(URL))
- file:close()
- end
- ------------------------------------------------
- return ExtraUtilits
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement