diff --git a/API.md b/API.md index de20c7d..b61ead1 100644 --- a/API.md +++ b/API.md @@ -231,6 +231,33 @@ A map of search filters, indexed by name. --- +### Item compression + +`i3` is capable of reducing the item list size by compressing a group of items. + +#### `i3.compress(item, def)` + +Adds a new group of items to compress. + +- `item` is the item that serve as stereotype for the group of compressed items. +- `def` is a table specifying the substring replace patterns to be used. + +Example: + +```Lua +i3.compress("default:diamondblock", { + replace = "diamond", + by = {"bronze", "copper", "gold", "steel", "tin"} +}) + +``` + +#### `i3.get_compress_groups()` + +Returns a map of all compressed item groups, indexed by stereotypes. + +--- + ### Miscellaneous #### `i3.get_recipes(item)` diff --git a/etc/api.lua b/etc/api.lua index f261c97..45edbf3 100644 --- a/etc/api.lua +++ b/etc/api.lua @@ -285,4 +285,38 @@ i3.add_search_filter("groups", function(item, groups) return has_groups end) +function i3.compress(item, def) + if not true_str(item) then + return err "i3.compress: item name missing" + end + + if not is_table(def) then + return err "i3.compress: replace definition missing" + end + + if not true_str(def.replace) then + return err "i3.compress: replace string missing" + end + + if not is_table(def.by) then + return err "i3.compress: replace substrings missing" + end + + local t = {} + i3.compress_groups[item] = i3.compress_groups[item] or {} + + for _, str in ipairs(def.by) do + local it = item:gsub(def.replace, str) + + insert(t, it) + insert(i3.compress_groups[item], it) + + i3.compressed[it] = true + end +end + +function i3.get_compress_groups() + return i3.compress_groups +end + return set_fs, i3.set_tab diff --git a/init.lua b/init.lua index 1fe015e..7d50377 100644 --- a/init.lua +++ b/init.lua @@ -291,4 +291,5 @@ if i3.progressive_mode then end --dofile(modpath .. "/tests/test_tabs.lua") +--dofile(modpath .. "/tests/test_compression.lua") --dofile(modpath .. "/tests/test_custom_recipes.lua") diff --git a/tests/test_compression.lua b/tests/test_compression.lua new file mode 100644 index 0000000..7e5bb73 --- /dev/null +++ b/tests/test_compression.lua @@ -0,0 +1,4 @@ +i3.compress("default:diamondblock", { + replace = "diamond", + by = {"bronze", "copper", "gold", "steel", "tin"} +})