Skip to content

Commit

Permalink
[item] only return matched items when requested
Browse files Browse the repository at this point in the history
  • Loading branch information
chdoc committed Jan 18, 2024
1 parent 8774e28 commit 229ce4c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions docs/item.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand All @@ -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``

Expand Down
10 changes: 6 additions & 4 deletions item.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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<number,number>
function execute(action, conditions, options)
function execute(action, conditions, options, return_items)
local count = 0
local items = {}
local types = {}
Expand Down Expand Up @@ -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

Expand All @@ -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()
Expand Down

0 comments on commit 229ce4c

Please sign in to comment.