cc/test.lua

132 lines
3.4 KiB
Lua
Raw Normal View History

local config = require "config"
2024-06-01 14:20:00 +00:00
local tabview = require "tabview"
local header_lines = 2
2024-05-31 23:12:12 +00:00
-- display working state
local currently_working = true
-- schedule immediate reload
local immediate_reload = false
2024-06-01 14:20:00 +00:00
-- window for tabview
local tabview_window = nil
2024-05-31 23:12:12 +00:00
2024-06-01 02:39:41 +00:00
local function display()
2024-05-31 23:12:12 +00:00
local tw,th = term.getSize()
term.setCursorBlink(false)
term.setBackgroundColor(colors.black)
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(colors.yellow)
term.setTextColor(colors.black)
term.clearLine()
2024-06-01 14:20:00 +00:00
term.write(" " .. string.upper(tabview.currentTab().name) .. " ")
term.setCursorPos(tw-8,1)
if currently_working then
term.write(" w ")
else
2024-05-31 23:12:12 +00:00
term.write(" R ")
end
term.write(" ^ v ")
term.setBackgroundColor(colors.black)
2024-06-01 14:20:00 +00:00
for name, tab in pairs(tabview.allTabs()) do
tab.displayTab(tab)
2024-05-31 23:12:12 +00:00
end
end
2024-06-01 02:39:41 +00:00
local function on_touch(touch_x, touch_y)
2024-05-31 23:12:12 +00:00
local tw,th = term.getSize()
if touch_y == 1 then
2024-06-01 14:20:00 +00:00
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()
2024-05-31 23:12:12 +00:00
return true
elseif currently_working == false and touch_x > tw-9 and touch_x < tw-5 then
2024-06-01 02:39:41 +00:00
-- reload button
2024-05-31 23:12:12 +00:00
immediate_reload = true
return true
2024-06-01 14:20:00 +00:00
elseif touch_x < (#tabview.currentTab().name + 2) then
tabview.switchTab()
2024-05-31 23:12:12 +00:00
return true
end
2024-06-01 14:20:00 +00:00
elseif tabview.onTouch(touch_x, touch_y) then
2024-05-31 23:12:12 +00:00
return true
end
return false
end
2024-06-01 14:20:00 +00:00
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
2024-06-01 02:39:41 +00:00
local function wait_for(time_secs)
2024-05-31 23:12:12 +00:00
display()
local timer_id = os.startTimer(time_secs)
while true do
local event_data = {os.pullEvent()}
if event_data[1] == "monitor_touch" or (event_data[1] == "mouse_click" and event_data[2] == 1) then
if on_touch(event_data[3], event_data[4]) then
display()
end
if immediate_reload == true then
2024-06-01 02:39:41 +00:00
os.cancelTimer(timer_id)
2024-05-31 23:12:12 +00:00
immediate_reload = false
break
end
elseif event_data[1] == "timer" then
if event_data[2] == timer_id then
break
end
2024-06-01 14:20:00 +00:00
elseif event_data[1] == "term_resize" then
update_windows()
display()
2024-05-31 23:12:12 +00:00
end
end
end
2024-06-01 14:20:00 +00:00
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)
2024-05-31 23:12:12 +00:00
end
2024-06-01 14:20:00 +00:00
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)
2024-06-01 02:39:41 +00:00
end
2024-05-31 23:12:12 +00:00
end
2024-06-01 14:20:00 +00:00
main()