Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local term = require("term")
- local gpu = require("component").gpu
- local event = require("event")
- local tui = {
- w = 80,
- h = 25,
- dirty = true
- }
- function tui.updateResolution()
- tui.w, tui.h = gpu.getResolution()
- end
- function tui.clear()
- gpu.setBackground(0x000000)
- gpu.setForeground(0xFFFFFF)
- term.clear()
- end
- function tui.markDirty()
- tui.dirty = true
- end
- function tui.label(x, y, text)
- gpu.set(x, y, text)
- end
- function tui.centerText(y, text)
- local x = math.floor((tui.w - #text) / 2)
- gpu.set(x, y, text)
- end
- function tui.tabBar(tabs, current)
- gpu.setBackground(0x444444)
- gpu.setForeground(0xFFFFFF)
- term.setCursor(1, 1)
- local pos = 1
- for i, tab in ipairs(tabs) do
- local name = " " .. tab.name .. " "
- gpu.set(pos, 1, name)
- if i == current then
- gpu.setBackground(0xAAAAAA)
- gpu.setForeground(0x000000)
- gpu.set(pos, 1, name)
- gpu.setBackground(0x444444)
- gpu.setForeground(0xFFFFFF)
- end
- pos = pos + #name
- end
- for i = pos, tui.w do
- gpu.set(i, 1, " ")
- end
- end
- function tui.box(x, y, x2, y2, title)
- gpu.setBackground(0x222222)
- gpu.setForeground(0xFFFFFF)
- for i = y, y2 do
- gpu.fill(x, i, x2 - x + 1, 1, " ")
- end
- if title then
- gpu.set(x + 2, y, " " .. title .. " ")
- end
- end
- function tui.button(x, y, label, callback)
- local width = #label + 2
- gpu.setBackground(0x0066CC)
- gpu.setForeground(0xFFFFFF)
- gpu.set(x, y, " " .. label .. " ")
- table.insert(tui._buttons, {
- x = x, y = y,
- w = width, h = 1,
- cb = callback
- })
- end
- function tui.progress(x, y, width, value, max, label)
- local ratio = math.min(1, value / max)
- local filled = math.floor(width * ratio)
- local text = string.format("[%s%s] %s%% %s", string.rep("=", filled), string.rep(" ", width - filled), math.floor(ratio * 100), label or "")
- gpu.set(x, y, text)
- end
- function tui.checkbox(x, y, label, state, callback)
- local box = state and "[x]" or "[ ]"
- gpu.set(x, y, box .. " " .. label)
- table.insert(tui._buttons, {
- x = x, y = y,
- w = #box + 1 + #label,
- h = 1,
- cb = function()
- callback(not state)
- end
- })
- end
- function tui.slider(x, y, width, value, max, callback)
- local percent = math.floor((value / max) * 100)
- local bar = "[" .. string.rep("=", math.floor((width - 2) * value / max)) .. string.rep(" ", width - 2 - math.floor((width - 2) * value / max)) .. "] " .. percent .. "%"
- gpu.set(x, y, bar)
- table.insert(tui._buttons, {
- x = x, y = y,
- w = width,
- h = 1,
- cb = function()
- value = value + 10
- if value > max then value = 0 end
- callback(value)
- tui.markDirty()
- end
- })
- end
- function tui.toast(msg, duration)
- duration = duration or 1
- tui._toast = {msg = msg, until = computer.uptime() + duration}
- end
- function tui.confirm(msg, callback)
- local x = math.floor(tui.w / 2 - (#msg + 6) / 2)
- local y = math.floor(tui.h / 2)
- tui.box(x, y - 1, x + #msg + 6, y + 2, "Подтверждение")
- gpu.set(x + 2, y, msg)
- tui.button(x + 2, y + 1, "Да", function()
- tui._confirm = nil
- callback(true)
- end)
- tui.button(x + 8, y + 1, "Нет", function()
- tui._confirm = nil
- callback(false)
- end)
- tui._confirm = true
- end
- function tui.start(update, render)
- tui.updateResolution()
- tui._buttons = {}
- tui._toast = nil
- tui._confirm = nil
- tui.clear()
- render()
- tui.dirty = false
- while true do
- if tui.dirty then
- tui._buttons = {}
- tui.clear()
- render()
- if tui._toast and computer.uptime() > tui._toast.until then
- tui._toast = nil
- end
- if tui._toast then
- tui.centerText(tui.h, tui._toast.msg)
- end
- tui.dirty = false
- end
- update()
- local ev = {event.pull(0.05)}
- if ev[1] == "touch" then
- local _, _, tx, ty = table.unpack(ev)
- for _, btn in ipairs(tui._buttons) do
- if tx >= btn.x and tx <= btn.x + btn.w - 1 and ty == btn.y then
- btn.cb()
- break
- end
- end
- elseif ev[1] == "key_down" and ev[4] == 211 then
- os.exit()
- end
- end
- end
- return tui
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement