Add possibly overengineered tabview

This commit is contained in:
Ben 2024-06-01 16:20:00 +02:00
parent 24124667a1
commit 6c54f462db
Signed by: ben
GPG key ID: 1353F41CF1CFF2D3
6 changed files with 204 additions and 106 deletions

View file

@ -7,5 +7,5 @@ return {
monitor_scale = 1.0,
step_sleep_time = 0.5,
round_sleep_time = 60.0,
initial_tab = 1,
initial_tab = "requests",
}

View file

@ -31,55 +31,55 @@ local function fetch(wait_for)
end
local function display(page_start_idx)
local tw,th = term.getSize()
local function displayTab(self)
local tw,th = self.window.getSize()
for i, val in pairs(orders) do
local line_num = (i-page_start_idx)*2+3
local line_num = (i-self.scrollPos)*2+1
if line_num > 2 and line_num < th-1 then
term.setTextColor(colors.lightGray)
term.setCursorPos(1,line_num)
term.write(val.id)
term.write(" ")
self.window.setTextColor(colors.lightGray)
self.window.setCursorPos(1,line_num)
self.window.write(val.id)
self.window.write(" ")
if val.pos then
term.write(val.pos.x .. "," .. val.pos.y .. "," .. val.pos.z)
term.write(" ")
self.window.write(val.pos.x .. "," .. val.pos.y .. "," .. val.pos.z)
self.window.write(" ")
end
term.setCursorPos(2,line_num+1)
term.setTextColor(colors.white)
self.window.setCursorPos(2,line_num+1)
self.window.setTextColor(colors.white)
if val.type then
term.write(val.type)
term.write(" ")
self.window.write(val.type)
self.window.write(" ")
end
if val.target then
term.write(val.target)
term.write(" ")
self.window.write(val.target)
self.window.write(" ")
end
if val.res_status then
term.setCursorPos(tw-2-#val.res_status, line_num)
term.write(val.res_status)
self.window.setCursorPos(tw-2-#val.res_status, line_num)
self.window.write(val.res_status)
else
term.setCursorPos(tw-2, line_num)
self.window.setCursorPos(tw-2, line_num)
end
term.write(" ")
self.window.write(" ")
if val.claimed then
term.setTextColor(colors.yellow)
term.write("C")
term.setTextColor(colors.white)
self.window.setTextColor(colors.yellow)
self.window.write("C")
self.window.setTextColor(colors.white)
else
term.write(" ")
self.window.write(" ")
end
term.write(" ")
self.window.write(" ")
end
end
end
local function on_touch(touch_x, touch_y)
local function onTouch(self, touch_x, touch_y)
return false
end
return {
name="orders",
fetch=fetch,
display=display,
on_touch=on_touch,
displayTab=displayTab,
onTouch=onTouch,
}

View file

@ -46,45 +46,45 @@ local function fetch(wait_for)
end
end
local function display(page_start_idx)
local tw,th = term.getSize()
local function displayTab(self)
local tw,th = self.window.getSize()
display_list = {}
for i, val in pairs(requests) do
local line_num = i-page_start_idx+3
local line_num = i-self.scrollPos+1
if line_num > 2 and line_num < th then
term.setTextColor(colors.white)
term.setCursorPos(1,line_num)
term.write(val.req.name)
term.write(" ")
self.window.setTextColor(colors.white)
self.window.setCursorPos(1,line_num)
self.window.write(val.req.name)
self.window.write(" ")
if val.answer == nil then
term.setTextColor(colors.orange)
term.write("N/A")
self.window.setTextColor(colors.orange)
self.window.write("N/A")
else
if permset.has(allowed_items, val.answer.name) then
term.setTextColor(colors.green)
self.window.setTextColor(colors.green)
else
term.setTextColor(colors.yellow)
self.window.setTextColor(colors.yellow)
end
term.write(val.answer.name)
self.window.write(val.answer.name)
display_list[line_num] = val.answer.name
term.setTextColor(colors.white)
term.setCursorPos(tw-4,line_num)
term.write(" [")
self.window.setTextColor(colors.white)
self.window.setCursorPos(tw-4,line_num)
self.window.write(" [")
if permset.has(allowed_items, val.answer.name) then
term.setTextColor(colors.green)
term.write("X")
term.setTextColor(colors.white)
self.window.setTextColor(colors.green)
self.window.write("X")
self.window.setTextColor(colors.white)
else
term.write(" ")
self.window.write(" ")
end
term.write("] ")
self.window.write("] ")
end
end
end
end
local function on_touch(touch_x, touch_y)
local tw,th = term.getSize()
local function onTouch(self, touch_x, touch_y)
local tw, th = self.window.getSize()
if touch_x > tw-5 and display_list[touch_y] ~= nil then
local item_name = display_list[touch_y]
if permset.has(allowed_items, item_name) then
@ -100,6 +100,6 @@ end
return {
name="requests",
fetch=fetch,
display=display,
on_touch=on_touch,
displayTab=displayTab,
onTouch=onTouch,
}

91
tabview.lua Normal file
View file

@ -0,0 +1,91 @@
local parent = term.current()
local allTabs = {}
local tabOrder = {}
local currentTab = nil
local function setTerm(new_term)
parent = new_term
end
local function addTab(tab, order_idx)
assert(tab.name, "tab needs a unique name")
local isCurrentTab = currentTab == nil
if isCurrentTab then
currentTab = tab
end
local tw, th = parent.getSize()
tab.window = window.create(parent, 1, 1, tw, th, isCurrentTab)
tab.scrollPos = 1
tab.pageDown = function ()
local tw, th = tab.window.getSize()
tab.scrollPos = tab.scrollPos + th
end
tab.pageUp = function ()
local tw, th = tab.window.getSize()
tab.scrollPos = math.max(1, tab.scrollPos-th)
end
allTabs[tab.name] = tab
tabOrder[order_idx] = tab.name
return tab
end
local function showTab(name)
if currentTab ~= nil then
currentTab.window.setVisible(false)
end
currentTab = allTabs[name]
if currentTab == nil then
-- backwards compatible idx lookup
currentTab = allTabs[tabOrder[name]]
end
assert(currentTab ~= nil, "tab name '" .. name .. "' was not registered")
currentTab.window.setVisible(true)
end
local function switchTab()
assert(#allTabs > 0, "tabs need to be registered in order to switch tabs")
local idx = 0
if currentTab ~= nil then
currentTab.window.setVisible(false)
idx = tabOrder[currentTab.name]
end
idx = idx + 1
if idx > #allTabs then
idx = 1
end
currentTab = allTabs[tabOrder[idx]]
currentTab.window.setVisible(true)
end
local function getCurrentTab()
return currentTab
end
local function getAllTabs()
return allTabs
end
local function updateSize()
local tw, th = parent.getSize()
for name, tab in pairs(allTabs) do
tab.window.reposition(1, 1, tw, th)
end
end
local function onTouch(touch_x, touch_y)
if currentTab ~= nil then
currentTab.onTouch(currentTab, touch_x, touch_y)
end
end
return {
setTerm=setTerm,
addTab=addTab,
showTab=showTab,
switchTab=switchTab,
updateSize=updateSize,
onTouch=onTouch,
currentTab=getCurrentTab,
allTabs=getAllTabs,
}

114
test.lua
View file

@ -1,20 +1,13 @@
local config = require "config"
local tabview = require "tabview"
local header_lines = 2
-- index of current tab
local current_tab = config.initial_tab
-- display working state
local currently_working = true
-- current start index to view requests
local page_start_idx = 1
-- schedule immediate reload
local immediate_reload = false
local tabs = {
-- to display requests and answer with an ME interface
[1]=require "requests",
-- to display colony work orders
[2]=require "orders",
}
-- window for tabview
local tabview_window = nil
local function display()
@ -28,64 +21,55 @@ local function display()
term.setTextColor(colors.black)
term.clearLine()
term.write(" " .. string.upper(tabs[current_tab].name) .. " ")
if currently_working == false then
term.setCursorPos(tw-8,1)
term.write(" " .. string.upper(tabview.currentTab().name) .. " ")
term.setCursorPos(tw-8,1)
if currently_working then
term.write(" w ")
else
term.write(" R ")
end
term.setCursorPos(tw-5,1)
term.write(" ^ v ")
term.setBackgroundColor(colors.black)
tabs[current_tab].display(page_start_idx)
if currently_working then
local cx,cy = term.getCursorPos()
term.setCursorPos(1,cy+2)
term.setCursorBlink(true)
term.setTextColor(colors.gray)
term.write("Working... ")
for name, tab in pairs(tabview.allTabs()) do
tab.displayTab(tab)
end
end
local function page_down(th)
page_start_idx = page_start_idx+th-3
end
local function page_up(th)
page_start_idx = math.max(1, page_start_idx-th+3)
end
local function on_touch(touch_x, touch_y)
local tw,th = term.getSize()
if touch_y == 1 then
if touch_x < (#tabs[current_tab].name + 2) then
current_tab = current_tab + 1
if current_tab > #tabs then
current_tab = 1
end
if touch_x > tw-3 and touch_x <= tw then
tabview.currentTab().pageDown()
return true
elseif touch_x > tw-6 and touch_x < tw-2 then
tabview.currentTab().pageUp()
return true
elseif currently_working == false and touch_x > tw-9 and touch_x < tw-5 then
-- reload button
immediate_reload = true
return true
elseif touch_x > tw-6 and touch_x < tw-2 then
page_up(th)
return true
elseif touch_x > tw-3 and touch_x <= tw then
page_down(th)
elseif touch_x < (#tabview.currentTab().name + 2) then
tabview.switchTab()
return true
end
elseif tabs[current_tab].on_touch(touch_x, touch_y) then
elseif tabview.onTouch(touch_x, touch_y) then
return true
end
return false
end
local function update_windows()
if tabview_window then
local tw, th = term.getSize()
tabview_window.reposition(1, 1+header_lines, tw, th-header_lines)
tabview.updateSize()
end
end
local function wait_for(time_secs)
display()
local timer_id = os.startTimer(time_secs)
@ -104,22 +88,44 @@ local function wait_for(time_secs)
if event_data[2] == timer_id then
break
end
elseif event_data[1] == "term_resize" then
update_windows()
display()
end
end
end
if peripheral.isPresent(config.monitor_side) then
monitor = peripheral.wrap(config.monitor_side)
monitor.setTextScale(config.monitor_scale)
term.redirect(monitor)
local function init()
if peripheral.isPresent(config.monitor_side) then
local monitor = peripheral.wrap(config.monitor_side)
monitor.setTextScale(config.monitor_scale)
term.redirect(monitor)
end
local tw, th = term.getSize()
tabview_window = window.create(term.current(), 1, 1+header_lines, tw, th-header_lines)
tabview.setTerm(tabview_window)
-- to display requests and answer with an ME interface
tabview.addTab(require "requests", 1)
-- to display colony work orders
tabview.addTab(require "orders", 2)
tabview.showTab(config.initial_tab)
end
while true do
currently_working = true
for i, tab in pairs(tabs) do
tab.fetch(wait_for)
local function main()
init()
while true do
currently_working = true
for name, tab in pairs(tabview.allTabs()) do
tab.fetch(wait_for)
end
currently_working = false
wait_for(config.round_sleep_time)
end
currently_working = false
wait_for(config.round_sleep_time)
end
main()

View file

@ -29,6 +29,7 @@ end
if update_update() then
download_file("permset.lua")
download_file("tabview.lua")
download_file("requests.lua")
download_file("orders.lua")
download_file("test.lua")