Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion clients/crafter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ interfaceLUT = {
return
end
changeState(STATES.BUSY)
print("Too add a crafting recipe, place the recipe in the turtle's inventory.")
print("To add a crafting recipe, place the recipe in the turtle's inventory.")
print("Then enter 1 for shaped, 2 for unshaped, or anything else to cancel")
local shapeSelection = read()
if shapeSelection == "1" then
Expand Down
135 changes: 90 additions & 45 deletions clients/terminal.lua
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ if not turtleMode then
pollFrequency = 3
end

local function eventTurtleInventory()
local function processTurtleInventory()
while true do
os.pullEvent("turtle_inventory")
os.pullEvent("do_turtle_transfers")
for i = importStart, importEnd do
if turtle.getItemDetail(i) then
lib.pullItems(true, inventory, i, nil, nil, nil, { optimal = false })
Expand All @@ -147,6 +147,22 @@ local function eventTurtleInventory()
end
end

local function debounceTurtleInventory()
local dropExportTimer
while true do
local e, id = os.pullEvent()
if e == "turtle_inventory" then
if dropExportTimer then
os.cancelTimer(dropExportTimer)
end
dropExportTimer = os.startTimer(0.1)
elseif e == "timer" and id == dropExportTimer then
dropExportTimer = nil
os.queueEvent("do_turtle_transfers")
end
end
end

local function inventoryPoll()
local slotActive = false
local slotActiveTicks = 0
Expand Down Expand Up @@ -503,14 +519,15 @@ local filter = ""
---Handle creating and drawing a searchable menu
---@param drawer fun(filter: string, selected: integer, sifted: any[])
---@param listProvider fun(): any[]
---@param onSelect fun(selected: any)
---@param onSelect fun(selected: any, index: integer)
---@param event nil|fun(e: any[]): boolean?
---@param sort nil|fun(a: any, b: any): boolean
---@param match nil|fun(val: any, pattern: string): boolean
---@param tab function? mode to switch to upon pushing tab
local function searchableMenu(drawer, listProvider, onSelect, event, sort, match, tab)
---@param selected integer?
local function searchableMenu(drawer, listProvider, onSelect, event, sort, match, tab, selected)
local sifted = filterList(listProvider(), filter, sort, match)
local selected = 1
selected = selected or 1
local controlHeld = false
while true do
draw(drawer, filter, selected, sifted)
Expand All @@ -535,7 +552,7 @@ local function searchableMenu(drawer, listProvider, onSelect, event, sort, match
selected = math.min(selected + 1, #sifted)
elseif isEnter(key) then
if selected > 0 and sifted[selected] then
return onSelect(sifted[selected])
return onSelect(sifted[selected], selected)
end
elseif key == keys.tab and tab then
return tab()
Expand All @@ -553,7 +570,7 @@ local function searchableMenu(drawer, listProvider, onSelect, event, sort, match
elseif e[1] == "mouse_click" and e[4] > 3 then
selected = math.max(math.min(getFirstItemOnScreen(selected, sifted) + e[4] - 4, #sifted), 1)
if sifted[selected] then
return onSelect(sifted[selected])
return onSelect(sifted[selected], selected)
end
elseif e[1] == "mouse_click" then
local nm = handleClicks(e[3], e[4])
Expand All @@ -566,7 +583,7 @@ local function searchableMenu(drawer, listProvider, onSelect, event, sort, match
end
end


local search_selected = 1
function SEARCH()
local list = lib.list()
mode = "SEARCH"
Expand All @@ -588,7 +605,8 @@ function SEARCH()
return a.name < b.name
end

local onSelect = function(selected)
local onSelect = function(selected, index)
search_selected = index
return INFO(selected)
end

Expand All @@ -599,7 +617,7 @@ function SEARCH()
end
end

return searchableMenu(drawer, function() return list end, onSelect, event, sort, match, CRAFT)
return searchableMenu(drawer, function() return list end, onSelect, event, sort, match, CRAFT, search_selected)
end

function CRAFT()
Expand All @@ -619,45 +637,71 @@ function CRAFT()
return searchableMenu(drawer, function() return craftables end, onSelect, nil, nil, nil, CONFIG)
end

function INFO(item)
mode = "INFO"
local itemAmount = tostring(math.min(item.maxCount, item.count))
---Read some number from the user
---@param input number|string
---@return number
local function scroll_read(input)
---@type string
input = tostring(input)
local shiftHeld = false
local x, y = term.getCursorPos()
while true do
draw(function()
setColors(headerFg, headerBg)
clearLine(2)
text(1, 2, ("%u x %s"):format(item.count, item.displayName))
setColors(mainFg, mainBg)
text(1, 3, item.name)
text(1, 4, item.nbt)
if item.enchantments then
text(1, 5, "Enchantments")
for k, v in ipairs(item.enchantments) do
text(1, 5 + k, v.displayName or v.name)
end
end
text(1, h, ("Withdraw: %s"):format(itemAmount))
display.setCursorBlink(true)
display.setCursorPos(itemAmount:len() + 11, h)
end)
local e = { os.pullEvent() }
if e[1] == "char" then
local ch = e[2]
if ch >= '0' and ch <= '9' then
itemAmount = itemAmount .. ch
term.setCursorPos(x, y)
term.write((" "):rep(#input + 1))
term.setCursorPos(x, y)
term.write(input)
local e, char = os.pullEvent()
if e == "char" and tonumber(char) then
input = input .. char
elseif e == "key" then
if char == keys.backspace then
input = input:sub(1, -2)
elseif char == keys.enter then
return tonumber(input) or 0
elseif char == keys.leftShift then
shiftHeld = true
end
elseif e[1] == "key" then
local key = e[2]
if key == keys.backspace then
itemAmount = itemAmount:sub(1, -2)
elseif isEnter(key) then
requestItem(false, item, tonumber(itemAmount or 0))
return SEARCH()
elseif e == "key_up" and char == keys.leftShift then
shiftHeld = false
elseif e == "mouse_scroll" then
local scrollAmount = ((shiftHeld and 8) or 1) * -char
local newValue = math.max(scrollAmount + (tonumber(input) or 0), 0)
newValue = math.ceil(newValue / scrollAmount) * scrollAmount
input = tostring(newValue)
elseif e == "mouse_click" then
if char == 1 then
return tonumber(input) or 0
elseif char == 2 then
return 0
end
end
end
end

function INFO(item)
mode = "INFO"
local itemAmount = math.min(item.maxCount, item.count)
draw(function()
setColors(headerFg, headerBg)
clearLine(2)
text(1, 2, ("%u x %s"):format(item.count, item.displayName))
setColors(mainFg, mainBg)
text(1, 3, item.name)
text(1, 4, item.nbt)
if item.enchantments then
text(1, 5, "Enchantments")
for k, v in ipairs(item.enchantments) do
text(1, 5 + k, v.displayName or v.name)
end
end
text(1, h, "Withdraw: ")
display.setCursorBlink(true)
end)
itemAmount = scroll_read(itemAmount)
requestItem(false, item, tonumber(itemAmount or 0))
return SEARCH()
end

function REQUEST(item)
mode = "REQUEST"
if not item then
Expand All @@ -673,8 +717,8 @@ function REQUEST(item)
display.setCursorPos(1, 3)
display.clearLine()
end)
local input = read()
if input == "" then
local input = scroll_read(1)
if input == 0 then
return CRAFT()
end
local num_input = tonumber(input)
Expand Down Expand Up @@ -1136,7 +1180,8 @@ modeLookup = { SEARCH = SEARCH, CRAFT = CRAFT, CONFIG = CONFIG, SYSINFO = SYSINF
local funcs = { lib.subscribe, SEARCH }

if turtleMode then
funcs[#funcs + 1] = eventTurtleInventory
funcs[#funcs + 1] = debounceTurtleInventory
funcs[#funcs + 1] = processTurtleInventory
else
funcs[#funcs + 1] = inventoryPoll
end
Expand Down
20 changes: 17 additions & 3 deletions clients/usageMonitor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,29 @@ if not settings.get("misc.monitor") then
settings.set("misc.monitor", monitorSide)
settings.save()
end
local wirelessMode = fs.exists("websocketLib.lua")
if wirelessMode and not settings.get("misc.websocketURL") then
settings.define("misc.websocketURL",{ description = "URL of the websocket to use for wireless communication", type = "string" })
print("Enter the URL of the websocket relay service you would like to use.")
settings.set("misc.websocketURL", read())
settings.save()
end
local textScale = 0.5

settings.load()
monitorSide = settings.get("misc.monitor")
local monitor = assert(peripheral.wrap(monitorSide), "Invalid monitor")

local lib = require("modemLib")
local modem = peripheral.getName(peripheral.find("modem"))
lib.connect(modem)
local lib
if not wirelessMode then
lib = require("modemLib")
local modem = peripheral.getName(peripheral.find("modem"))
lib.connect(modem)
else
lib = require("websocketLib")
local websocket = settings.get("misc.websocketURL")
lib.connect(websocket)
end

local labelFG = colors.black
local labelBG = colors.white
Expand Down
10 changes: 10 additions & 0 deletions installer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ local introspectionTermInstall = {
}
}


local crafterInstall = {
name = "Crafter Turtle",
files = {
Expand All @@ -120,12 +121,21 @@ local monitorInstall = {
}
}

local introspectionMonInstall = {
name = "Usage Monitor (Introspection)",
files = {
["startup.lua"] = fromRepository "clients/usageMonitor.lua",
["websocketLib.lua"] = fromRepository "clients/websocketLib.lua"
}
}

local clientInstallOptions = {
name = "Client installation options",
t = terminalInstall,
i = introspectionTermInstall,
c = crafterInstall,
m = monitorInstall,
w = introspectionMonInstall,
}

local installOptions = {
Expand Down