Message
| Try this variation:
<aliases>
<alias
match="inv"
enabled="y"
group="inventory"
send_to="12"
sequence="100"
>
<send>
require "wait"
wait.make (function () -- coroutine starts here
local win = GetPluginID () .. ":inventory"
local font = "f"
if not WindowInfo (win, 1) then
WindowCreate (win, 0, 0, 0, 0, 6, 0, 0)
WindowFont (win, font, "Lucida Console", 6)
end -- if
-- request inventory
Send "inventory"
-- wait for inventory to start
local x = wait.match ("Inventory:", 10, trigger_flag.OmitFromOutput)
if not x then
ColourNote ("white", "blue", "No inventory received within 10 seconds")
return
end -- if
local inv = {}
local max_width = WindowTextWidth (win, font, "Inventory")
default_category = "Other"
inventory_type = {
Containers = { "pack", "bag", "sack", "backpack", "box", "chest", },
Weapons = { "scepter", "sword", "dirk", "dagger", },
Ammo = { "arrows", "bullets", "darts", },
Jewelry = { "necklace", "bangle", "bracelet", "ring", },
Resources = { "bundle", "salt", },
Armor = { "cuirass", "chestplate", "armor", },
[default_category] = { }, -- default
} -- end of inventory_type
category_count = 0
for name in pairs (inventory_type) do
category_count = category_count + 1
end -- for
-- for duplicates
existing_items = { }
max_quantity = 1
-- loop until end of inventory
while true do
local line, wildcards, styles = wait.match ("*", 0, trigger_flag.OmitFromOutput)
-- see if end of inventory
if not string.match (line, "^ ?an?") then
if not string.match (line, "^ ?some") then
if not string.match (line, "^$") then
break
end -- if
end -- if
end -- if
if styles [1] then
line = Trim (line)
styles.line = line
styles.weight = tonumber (string.match (line, "%%(([0-9.]+) lbs?%%)"))
styles [1].text = string.gsub (styles [1].text, "^ ", "")
for category, keywords in pairs (inventory_type) do
if styles.category then
break
end -- if
for _, item in ipairs (keywords) do
if string.find (line, item, 1, true) then
styles.category = category
break
end -- if found
end -- for each item
end -- for each category
if not styles.category then
styles.category = default_category
end -- if
-- look for a duplicate
item_number = existing_items [line]
-- if found, increase its quantity
if item_number then
inv [item_number].quantity = inv [item_number].quantity + 1
max_quantity = math.max (max_quantity, inv [item_number].quantity)
else
styles.quantity = 1 -- start with one of them
existing_items [line] = #inv + 1 -- remember its new position
-- save inventory line
inv [#inv + 1] = styles
-- work out max width
max_width = math.max (max_width, WindowTextWidth (win, font, line))
end -- if
end -- if at least one style
end -- while loop
max_quantity_digits = math.floor (math.log10 (max_quantity)) + 1
if max_quantity > 1 then
max_width = max_width + WindowTextWidth (win, font, string.format (" x%s", string.rep ("9", max_quantity_digits)))
end -- if
local font_height = WindowFontInfo (win, font, 1)
local window_width = max_width + 10
local window_height = font_height * (#inv + (category_count * 2) + 2) + 10
-- make window correct size
WindowCreate (win, 0, 0, window_width, window_height, 6, 0, ColourNameToRGB "#373737")
WindowRectOp (win, 5, 0, 0, 0, 0, 5, 15 + 0x1000)
-- heading line
WindowText (win, font, "Inventory", 5, 5, 0, 0, ColourNameToRGB "yellow")
ColourNote ("yellow", "black", "Inventory:")
-- draw each inventory line
local y = font_height * 2 + 5
-- sort them
function my_sort_function (a, b)
-- category less than?
if a.category < b.category then return true end
-- category greater than?
if a.category > b.category then return false end
-- name less than?
if a.line < b.line then return true end
-- name greater than?
if a.line > b.line then return false end
-- names the same, compare weights ...
-- weight less than
if a.weight and b.weight then
if a.weight < b.weight then return true end
end -- if
-- not less than
return false
end -- function
table.sort (inv, my_sort_function)
old_category = nil
for i, styles in ipairs (inv) do
if styles.category ~= old_category then
y = y + font_height
WindowText (win, font, styles.category, 5, y, 0, 0, ColourNameToRGB "lightgray")
ColourNote ("lightgray", "", (styles.category))
old_category = styles.category
y = y + font_height
end -- if new category
local x = 5
for _, style in ipairs (styles) do
x = x + WindowText (win, font, style.text, x, y, 0, 0, style.textcolour)
ColourTell (style.textcolour, style.backcolour, style.text)
end -- for
if styles.quantity > 1 then
WindowText (win, font, string.format (" x%i", styles.quantity), x, y, 0, 0, ColourNameToRGB "lightgreen")
ColourTell ("lightgreen", "", string.format (" x%i", styles.quantity))
end -- if
print "" -- finish off line
y = y + font_height
end -- for each inventory item
WindowShow (win, true)
end) -- end of coroutine
</send>
</alias>
</aliases>
You were only doing a ColourNote if the quantity was > 1 so naturally the other lines did not appear. I've put a ColourTell into the styles loop, which will preserve the style of the original (ie. the colour). I don't know about that zero, but it probably came from this:
styles.line .. " " .. styles.weight
The weight would have been zero (ie. not bold, underlined, etc.) |
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|