Begin working on withdrawing from system

This commit is contained in:
osmarks 2018-08-12 16:59:21 +01:00
parent 29d2bd9798
commit 424f5deebb
2 changed files with 45 additions and 4 deletions

View File

@ -1,6 +1,10 @@
local w = require "lib" local w = require "lib"
local d = require "luadash" local d = require "luadash"
local conf = w.load_config({
"network_name"
})
local function split_at_spaces(s) local function split_at_spaces(s)
local t = {} local t = {}
for i in string.gmatch(s, "%S+") do for i in string.gmatch(s, "%S+") do
@ -14,14 +18,35 @@ local function first_letter(s)
end end
local usage = [[ local usage = [[
Welcome to the Wyvern CLI Client, "Because Gollark Was Lazy". Welcome to the Wyvern CLI Client, "Because Gollark Was Lazy".
All commands listed below can also be accessed using single-letter shortcuts for convenience. All commands listed below can also be accessed using single-letter shortcuts for convenience.
withdraw [quantity] [name] - withdraw [quantity] items with display names close to [name] from storage
withdraw [items] - as above but withdraws all available matching items
]] ]]
local commands = { local commands = {
help = function() print(usage) end help = function() return usage end,
withdraw = function(number, ...)
local query_tokens = {...}
local quantity = math.huge
if tonumber(number) ~= nil then
quantity = tonumber(number)
else
table.insert(query_tokens, 1, numbr)
end
local query = table.concat(query_tokens, " ") -- unsplit query
local items = w.query_by_type("storage", {
type = "search",
query = query
})
end
} }
w.init()
if not turtle then error "Wyvern CLI must be run on a turtle." end
print "Wyvern CLI Client" print "Wyvern CLI Client"
@ -41,5 +66,6 @@ while true do
print("Command", command, "not found.") print("Command", command, "not found.")
end end
fn(table.unpack(args)) local ok, result = pcall(fn(table.unpack(args)))
if result then textutils.pagedPrint(result) end
end end

15
lib.lua
View File

@ -246,4 +246,19 @@ local function init()
d.map(find_peripherals(function(type, name, wrapped) return type == "modem" end), function(p) rednet.open(p.name) end) d.map(find_peripherals(function(type, name, wrapped) return type == "modem" end), function(p) rednet.open(p.name) end)
end end
-- Rust-style unwrap. If x is a response type, will take out its contents and return them - if error, will crash and print it, with msg if provided
local function unwrap(x, msg)
if not x or type(x) ~= "table" or not x.type then x = errors.make(errors.INTERNAL, "Error/response object is invalid. This is probably a problem with the node being contacted.") end
if x.type == "error" then
local text = "An error occured"
if msg then text .. " " .. msg
else text = text .. "!" end
text = text .. ".\nDetails: " .. errors.format(x.error)
error(text)
elseif x.type == "response" then
return x.response
end
end
return { errors = errors, serve = serve, query_by_ID = query_by_ID, query_by_type = query_by_type, get_internal_identifier = get_internal_identifier, load_config = load_config, find_peripherals = find_peripherals, init = init, collate = collate, satisfied = satisfied, collate_stacks = collate_stacks } return { errors = errors, serve = serve, query_by_ID = query_by_ID, query_by_type = query_by_type, get_internal_identifier = get_internal_identifier, load_config = load_config, find_peripherals = find_peripherals, init = init, collate = collate, satisfied = satisfied, collate_stacks = collate_stacks }