diff --git a/docs/item.rst b/docs/item.rst index dd834bae60..eb974d4b3d 100644 --- a/docs/item.rst +++ b/docs/item.rst @@ -136,7 +136,7 @@ commandline interface with ``dfhack.run_script()`` or via the API functions defined in :source-scripts:`item.lua`, available from the return value of ``reqscript('item')``: -* ``execute(action, conditions, options)`` +* ``execute(action, conditions, options [, return_items])`` Performs ``action`` (``forbid``, ``melt``, etc.) on all items satisfying ``conditions`` (a table containing functions from item to boolean). ``options`` @@ -146,7 +146,7 @@ and ``owned`` which correspond to the (filter) options described above. The function ``execute`` performs no output, but returns three values: 1. the number of matching items -2. a table containing all matched items, if the action is ``count`` +2. a table containing all matched items, if ``return_items`` is provided and true. 3. a table containing a mapping from numeric item types to their occurrence count, if ``options.bytype=true`` diff --git a/item.lua b/item.lua index 3742271edd..3145db3d4a 100644 --- a/item.lua +++ b/item.lua @@ -200,8 +200,9 @@ end --- @param action "melt"|"unmelt"|"forbid"|"unforbid"|"dump"|"undump"|"count"|"hide"|"unhide" --- @param conditions conditions --- @param options { help : boolean, artifact : boolean, dryrun : boolean, bytype : boolean, owned : boolean } +--- @param return_items boolean|nil --- @return number, item[], table -function execute(action, conditions, options) +function execute(action, conditions, options, return_items) local count = 0 local items = {} local types = {} @@ -269,9 +270,10 @@ function execute(action, conditions, options) item.flags.hidden = true elseif action == "unhide" and not options.dryrun then item.flags.hidden = false - elseif action == "count" then - table.insert(items,item) end + + if return_items then table.insert(items, item) end + :: skipitem :: end @@ -297,7 +299,7 @@ function executeWithPrinting (action, conditions, options) end table.sort(sorted, function(a, b) return a.count > b.count end) print(("\n%-14s %5s\n"):format("TYPE", "COUNT")) - for _, t in pairs(sorted) do + for _, t in ipairs(sorted) do print(("%-14s %5s"):format(df.item_type[t.type], t.count)) end print()