BananaOS/test/sbin/de.lua

133 lines
3.4 KiB
Lua
Raw Normal View History

2025-02-21 00:33:21 -05:00
local component = require("component")
local computer = require("computer")
local fs = require("filesystem")
local event = require("event")
-- get GPU
local gpu = component.proxy(component.list("gpu", true)())
if gpu == nil then
return -1
end
local resolution = {}
resolution.w, resolution.h = gpu.maxResolution()
local bg_color = 0xa2cffe
local touch_listeners = {} -- {x start,y start,x end,y end,function to call, with args}
local windows = {} -- {WINDOW_NAME,X,Y,WIDTH,HEIGHT}
-- local
gpu.setResolution(resolution.w, resolution.h)
local function add_touch_listener(xs,ys,xe,ye,call,args)
table.insert(touch_listeners, {xs,ys,xe,ye,call,args})
return #touch_listeners
end
local function draw_nav()
gpu.setBackground(0xffffff)
gpu.setForeground(0xffffff)
gpu.fill(1, 1, resolution.w, 1, " ")
gpu.setForeground(0x000000)
gpu.setBackground(0xffffff)
gpu.set(2,1, "@")
local totalmem = computer.totalMemory()
local freemem = computer.freeMemory()
gpu.set(6,1, tostring(totalmem-freemem) .. " b / " .. tostring(totalmem) .. " b")
gpu.setBackground(bg_color)
end
local function draw_bg()
gpu.setBackground(bg_color)
gpu.fill(1, 2, resolution.w, resolution.h, " ")
end
local function draw_windows()
for i = 1, #windows do
gpu.setBackground(0x000000)
gpu.setForeground(0xffffff)
gpu.fill(
windows[i][2],
windows[i][3]-1,
windows[i][2]+windows[i][4],
windows[i][3]-1,
" "
)
gpu.set(windows[i][2], windows[i][3]-1, "X | " .. windows[i][1])
gpu.setBackground(0xaaaaaa)
gpu.fill(
windows[i][2],
windows[i][3],
windows[i][2]+windows[i][4],
windows[i][3]+windows[i][5],
" "
)
end
end
local function keyin()
return event.pull("key_down")
end
local function open_exec_menu(args)
gpu.setBackground(0xdadada)
gpu.fill(1,3,resolution.w, 1, " ")
gpu.setBackground(bg_color)
local grab = true
local buffer = ""
local curr_loc_x = 1
while grab do
local _,_,next_key = keyin()
if next_key == 13 then
local result = table.pack(pcall(fs.dofile, buffer))
if not result[1] then
print(result[2] .. "\n")
end
buffer = ""
curr_loc_x = 1
grab = false
elseif next_key == 8 then
if #buffer ~= 0 then
buffer = buffer:sub(1, -2)
gpu.set(curr_loc_x-1, 2, " ")
curr_loc_x = curr_loc_x - 1
end
else
buffer = buffer .. string.char(next_key)
gpu.set(curr_loc_x, 2, string.char(next_key))
curr_loc_x = curr_loc_x + 1
end
end
gpu.setBackground(bg_color)
gpu.fill(1,2,resolution.w, 3, " ")
end
draw_bg()
draw_nav()
add_touch_listener(2, 1, 2, 1, open_exec_menu, {})
while true do
local event,uuid,x,y = computer.pullSignal(0.3)
if event == "touch" then
for i = 1, #touch_listeners do
if touch_listeners[i][1] <= x and x <= touch_listeners[i][3] then
if touch_listeners[i][2] <= y and y <= touch_listeners[i][4] then
touch_listeners[i][5](touch_listeners[i][6])
end
end
end
end
draw_nav()
draw_windows()
end