View Single Post
07-20-21, 02:30 AM   #6
Zax
A Flamescale Wyrmkin
AddOn Author - Click to view addons
Join Date: Aug 2006
Posts: 147
OK, I finally solve my problem, with the following code (maybe it could be optimized):

Lua Code:
  1. function .executeCmd(cmdStr, isMacroBoolean, noPrintErrorBoolean) -- return "", or error string
  2.     local cmdStr = cmdStr or ""
  3.     local tbl = {strsplit("\n", cmdStr)}
  4.    
  5.     local function executeString(str) -- return "", or error string
  6.         local str = str or ""
  7.         local func, errorMessage = loadstring(str)
  8.         if (not func) then
  9.             return "slash command LUA error #59000."
  10.         else
  11.             local success, errorMessage = pcall(func)
  12.             if (not success) then
  13.                 return "slash command error #59001."
  14.             else
  15.                 return ""
  16.             end
  17.         end
  18.     end
  19.    
  20.     local resultStr = ""
  21.     for i, strLine in ipairs(tbl) do
  22.         if (resultStr ~= "") then break end
  23.         if (isMacroBoolean) then
  24.             --------------------------- macro ("/" needed)
  25.             if (string.sub(strLine, 1, 1) ~= "/") then strLine = "/"..strLine end
  26.             if (string.sub(strLine, 1, 5) == "/run ") then
  27.                 strLine = string.sub(strLine, 6)
  28.                 resultStr = executeString(strLine)
  29.             elseif (string.sub(strLine, 1, 8) == "/script ") then
  30.                 strLine = string.sub(strLine, 9)
  31.                 resultStr = executeString(strLine)
  32.             else
  33.                 DEFAULT_CHAT_FRAME.editBox:SetText(strLine) -- sent to Chat Box
  34.                 ChatEdit_SendText(DEFAULT_CHAT_FRAME.editBox,0)
  35.             end
  36.         else
  37.             --------------------------- command ("/" not valid)
  38.             if (string.sub(strLine, 1, 1) == "/") then strLine = string.sub(strLine, 2) end -- delete char 1
  39.             resultStr = executeString(strLine)
  40.         end
  41.     end
  42.    
  43.     if ((resultStr ~= "") and (not noPrintErrorBoolean)) then print(resultStr) end
  44.     return resultStr
  45. end
__________________
Zax - Addons List, not all maintained.
  Reply With Quote