From 424f5deebb5497452f3e63bebdbe9a1aae8b3135 Mon Sep 17 00:00:00 2001 From: osmarks Date: Sun, 12 Aug 2018 16:59:21 +0100 Subject: [PATCH] Begin working on withdrawing from system --- client.lua | 34 ++++++++++++++++++++++++++++++---- lib.lua | 15 +++++++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/client.lua b/client.lua index d6905c0..b736c99 100644 --- a/client.lua +++ b/client.lua @@ -1,6 +1,10 @@ local w = require "lib" local d = require "luadash" +local conf = w.load_config({ + "network_name" +}) + local function split_at_spaces(s) local t = {} for i in string.gmatch(s, "%S+") do @@ -14,14 +18,35 @@ local function first_letter(s) end local usage = [[ - Welcome to the Wyvern CLI Client, "Because Gollark Was Lazy". - All commands listed below can also be accessed using single-letter shortcuts for convenience. +Welcome to the Wyvern CLI Client, "Because Gollark Was Lazy". +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 = { - 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" @@ -41,5 +66,6 @@ while true do print("Command", command, "not found.") end - fn(table.unpack(args)) + local ok, result = pcall(fn(table.unpack(args))) + if result then textutils.pagedPrint(result) end end \ No newline at end of file diff --git a/lib.lua b/lib.lua index 08b2d62..63ce895 100644 --- a/lib.lua +++ b/lib.lua @@ -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) 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 }