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, monitor_scale = 1.0,
step_sleep_time = 0.5, step_sleep_time = 0.5,
round_sleep_time = 60.0, round_sleep_time = 60.0,
initial_tab = 1, initial_tab = "requests",
} }

View file

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

View file

@ -46,45 +46,45 @@ local function fetch(wait_for)
end end
end end
local function display(page_start_idx) local function displayTab(self)
local tw,th = term.getSize() local tw,th = self.window.getSize()
display_list = {} display_list = {}
for i, val in pairs(requests) do 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 if line_num > 2 and line_num < th then
term.setTextColor(colors.white) self.window.setTextColor(colors.white)
term.setCursorPos(1,line_num) self.window.setCursorPos(1,line_num)
term.write(val.req.name) self.window.write(val.req.name)
term.write(" ") self.window.write(" ")
if val.answer == nil then if val.answer == nil then
term.setTextColor(colors.orange) self.window.setTextColor(colors.orange)
term.write("N/A") self.window.write("N/A")
else else
if permset.has(allowed_items, val.answer.name) then if permset.has(allowed_items, val.answer.name) then
term.setTextColor(colors.green) self.window.setTextColor(colors.green)
else else
term.setTextColor(colors.yellow) self.window.setTextColor(colors.yellow)
end end
term.write(val.answer.name) self.window.write(val.answer.name)
display_list[line_num] = val.answer.name display_list[line_num] = val.answer.name
term.setTextColor(colors.white) self.window.setTextColor(colors.white)
term.setCursorPos(tw-4,line_num) self.window.setCursorPos(tw-4,line_num)
term.write(" [") self.window.write(" [")
if permset.has(allowed_items, val.answer.name) then if permset.has(allowed_items, val.answer.name) then
term.setTextColor(colors.green) self.window.setTextColor(colors.green)
term.write("X") self.window.write("X")
term.setTextColor(colors.white) self.window.setTextColor(colors.white)
else else
term.write(" ") self.window.write(" ")
end end
term.write("] ") self.window.write("] ")
end end
end end
end end
end end
local function on_touch(touch_x, touch_y) local function onTouch(self, touch_x, touch_y)
local tw,th = term.getSize() local tw, th = self.window.getSize()
if touch_x > tw-5 and display_list[touch_y] ~= nil then if touch_x > tw-5 and display_list[touch_y] ~= nil then
local item_name = display_list[touch_y] local item_name = display_list[touch_y]
if permset.has(allowed_items, item_name) then if permset.has(allowed_items, item_name) then
@ -100,6 +100,6 @@ end
return { return {
name="requests", name="requests",
fetch=fetch, fetch=fetch,
display=display, displayTab=displayTab,
on_touch=on_touch, 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 config = require "config"
local tabview = require "tabview"
local header_lines = 2
-- index of current tab
local current_tab = config.initial_tab
-- display working state -- display working state
local currently_working = true local currently_working = true
-- current start index to view requests
local page_start_idx = 1
-- schedule immediate reload -- schedule immediate reload
local immediate_reload = false local immediate_reload = false
-- window for tabview
local tabs = { local tabview_window = nil
-- to display requests and answer with an ME interface
[1]=require "requests",
-- to display colony work orders
[2]=require "orders",
}
local function display() local function display()
@ -28,64 +21,55 @@ local function display()
term.setTextColor(colors.black) term.setTextColor(colors.black)
term.clearLine() term.clearLine()
term.write(" " .. string.upper(tabs[current_tab].name) .. " ") term.write(" " .. string.upper(tabview.currentTab().name) .. " ")
if currently_working == false then term.setCursorPos(tw-8,1)
term.setCursorPos(tw-8,1) if currently_working then
term.write(" w ")
else
term.write(" R ") term.write(" R ")
end end
term.setCursorPos(tw-5,1)
term.write(" ^ v ") term.write(" ^ v ")
term.setBackgroundColor(colors.black) term.setBackgroundColor(colors.black)
tabs[current_tab].display(page_start_idx) for name, tab in pairs(tabview.allTabs()) do
tab.displayTab(tab)
if currently_working then
local cx,cy = term.getCursorPos()
term.setCursorPos(1,cy+2)
term.setCursorBlink(true)
term.setTextColor(colors.gray)
term.write("Working... ")
end end
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 function on_touch(touch_x, touch_y)
local tw,th = term.getSize() local tw,th = term.getSize()
if touch_y == 1 then if touch_y == 1 then
if touch_x < (#tabs[current_tab].name + 2) then if touch_x > tw-3 and touch_x <= tw then
current_tab = current_tab + 1 tabview.currentTab().pageDown()
if current_tab > #tabs then return true
current_tab = 1 elseif touch_x > tw-6 and touch_x < tw-2 then
end tabview.currentTab().pageUp()
return true return true
elseif currently_working == false and touch_x > tw-9 and touch_x < tw-5 then elseif currently_working == false and touch_x > tw-9 and touch_x < tw-5 then
-- reload button -- reload button
immediate_reload = true immediate_reload = true
return true return true
elseif touch_x > tw-6 and touch_x < tw-2 then elseif touch_x < (#tabview.currentTab().name + 2) then
page_up(th) tabview.switchTab()
return true
elseif touch_x > tw-3 and touch_x <= tw then
page_down(th)
return true return true
end end
elseif tabs[current_tab].on_touch(touch_x, touch_y) then elseif tabview.onTouch(touch_x, touch_y) then
return true return true
end end
return false return false
end 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) local function wait_for(time_secs)
display() display()
local timer_id = os.startTimer(time_secs) local timer_id = os.startTimer(time_secs)
@ -104,22 +88,44 @@ local function wait_for(time_secs)
if event_data[2] == timer_id then if event_data[2] == timer_id then
break break
end end
elseif event_data[1] == "term_resize" then
update_windows()
display()
end end
end end
end end
if peripheral.isPresent(config.monitor_side) then local function init()
monitor = peripheral.wrap(config.monitor_side) if peripheral.isPresent(config.monitor_side) then
monitor.setTextScale(config.monitor_scale) local monitor = peripheral.wrap(config.monitor_side)
term.redirect(monitor) 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 end
while true do
currently_working = true local function main()
for i, tab in pairs(tabs) do init()
tab.fetch(wait_for) 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 end
currently_working = false
wait_for(config.round_sleep_time)
end end
main()

View file

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