logger.lua 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. return function(BOXUI)
  2. local utils = BOXUI.utils
  3. local List = BOXUI.list
  4. local M = utils.newclass("logger", List)
  5. M.init = function(self, x, y, width, height)
  6. List.init(self, x, y, width, height)
  7. self.count = 0
  8. self.maxlog = 50
  9. self.icons = nil
  10. end
  11. M.format = function(i, text, count)
  12. if count then return ("%.3i:\t%s (x %i)"):format(i, text, count) end
  13. return ("%.3i:\t%s"):format(i, text)
  14. end
  15. M.add = function(self, text, level)
  16. local logitems = self.list.items
  17. local entry = {raw = text, icon = self.icons[level or "ok"]}
  18. if not logitems then
  19. self.count = self.count + 1
  20. entry.text = self.format(self.count, text)
  21. self:setItems({entry}, true)
  22. return
  23. end
  24. local len = #logitems
  25. if logitems[len].raw == text then
  26. entry = logitems[len]
  27. entry.count = (entry.count or 1) + 1
  28. entry.text = self.format(self.count, text, entry.count)
  29. table.remove(logitems)
  30. else
  31. self.count = self.count + 1
  32. entry.text = self.format(self.count, text)
  33. end
  34. if len >= self.maxlog then table.remove(logitems, 1) end
  35. table.insert(logitems, entry)
  36. self:setItems(logitems, true)
  37. end
  38. BOXUI.add(M)
  39. end