From ebbee7fb7c8d76b1c1055445ca2ee1ca9344b59c Mon Sep 17 00:00:00 2001 From: speedwaystar Date: Mon, 9 Jan 2023 22:11:10 +0800 Subject: [PATCH] helper functions: enum(t), dict(t), tinvert(t), switch(case), printd(...), printf(f,...), dump(t, startkey) --- SmartBuff.globals.lua | 153 +++++++++++++++++++++++++++++++++++------- 1 file changed, 128 insertions(+), 25 deletions(-) diff --git a/SmartBuff.globals.lua b/SmartBuff.globals.lua index 7512d1c..c5d3ef2 100644 --- a/SmartBuff.globals.lua +++ b/SmartBuff.globals.lua @@ -21,34 +21,137 @@ SMARTBUFF_CONST_AUTOSOUND = "Deathbind Sound"; --SMARTBUFF_CONST_AUTOSOUND = "GLUECREATECHARACTERBUTTON"; --[[ -SystemFont -GameFontNormal -GameFontNormalSmall -GameFontNormalLarge -GameFontHighlight -GameFontHighlightSmall -GameFontHighlightSmallOutline -GameFontHighlightLarge -GameFontDisable -GameFontDisableSmall -GameFontDisableLarge -GameFontGreen -GameFontGreenSmall -GameFontGreenLarge -GameFontRed -GameFontRedSmall -GameFontRedLarge -GameFontWhite -GameFontDarkGraySmall -NumberFontNormalYellow -NumberFontNormalSmallGray -QuestFontNormalSmall +SystemFont +GameFontNormal +GameFontNormalSmall +GameFontNormalLarge +GameFontHighlight +GameFontHighlightSmall +GameFontHighlightSmallOutline +GameFontHighlightLarge +GameFontDisable +GameFontDisableSmall +GameFontDisableLarge +GameFontGreen +GameFontGreenSmall +GameFontGreenLarge +GameFontRed +GameFontRedSmall +GameFontRedLarge +GameFontWhite +GameFontDarkGraySmall +NumberFontNormalYellow +NumberFontNormalSmallGray +QuestFontNormalSmall DialogButtonHighlightText -ErrorFont -TextStatusBarText -CombatLogFont +ErrorFont +TextStatusBarText +CombatLogFont NumberFontNormalLarge NumberFontNormalHuge ]]-- +---------------------------------------------------------------------------- +-- Creates a table `t` indexed by both sequentially numbered `keys` _and_ `values`, +-- thus supporting reverse lookup. Assumes numeric 'keys' and alphanumeric 'valuee'. +-- ## Example +-- ```lua +-- t = enum( "foo", "bar" ); +-- print(t.hello) -- prints the integer 1 +-- print(t[1]) -- prints the string "foo" +-- ``` +---@param t table +---@return table +function enum(t) + for i = 1, #t do + local v = t[i] + --t[i] = nil + t[v] = i + end + return t +end + +-- Creates a table `t` of self-indexed values +-- ## Example +-- ```lua +-- t = dict( "foo", "bar") +-- print(t.foo) -- prints the string "foo" +-- ``` +---@param list table +---@return table +function dict(list) + for k, v in pairs(list) do + list[k] = nil + list[v] = v + end + return list +end + +-- Returns a copy of `list` with `keys` and `values` inverted +-- ## Example +---``` +---t = { "foo" = 1, "bar" = 2}; +---s = tinvert(t); +---print(t.foo); -- prints the number 1 +---print(s[1]); -- prints the string "foo"; +---``` +---@generic T +---@param list T[] +---@return T out +function table.invert(list) + local out = {} + for k, v in pairs(list) do + out[v] = k + end + return out +end + +local Default, Nil = {}, function () end -- for uniqueness +---@param case any +---@return any +-- Implements a `switch` statement in Lua. +-- ## Example +-- ``` +-- switch(case) = { +-- [1] = function() print("one") end, +-- [2] = print, +-- [3] = math.sin, +-- default = function() print("default") end, +-- } +-- ``` +function switch (case) + return setmetatable({ case }, { + __call = function (t, cases) + local item = #t == 0 and Nil or t[1] + return (cases[item] or cases[Default] or Nil) + end + }) +end + +-- Prints debuggin information using a formatted version of its variable +-- number of arguments following the description given in its first argument. +--- +---[View documents](command:extension.lua.doc?["en-us/51/manual.html/pdf-string.format"]) +---@param s any +---@param ... any +function printf(s, ...) + print(" ",addonName,"::",string.format(s, ...)) +end + +-- Prints debug information to `stdout`. Receives any number of arguments, +-- converting each argument to a string following the same rules of +-- [tostring](command:extension.lua.doc?["en-us/51/manual.html/pdf-tostring"]). +--- +--- [View documents](command:extension.lua.doc?["en-us/51/manual.html/pdf-print"]) +--- +function printd(...) + print(" ",addonName,"::",...) +end + +--- Prints the value of any global variable, table value, frame, function result, or any valid Lua expression. Output is color coded for easier reading. Tables display up to 30 values, the rest are skipped and a message is shown. +---@param t any +---@param startkey? any +function dump(t, startkey) + DevTools_Dump(t, startkey) +end