logview.lua 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. local core = require "core"
  2. local common = require "core.common"
  3. local config = require "core.config"
  4. local style = require "core.style"
  5. local View = require "core.view"
  6. local LogView = View:extend()
  7. function LogView:new()
  8. LogView.super.new(self)
  9. self.last_item = 0
  10. self.scrollable = true
  11. self.yoffset = 0
  12. end
  13. function LogView:get_name()
  14. return "Log"
  15. end
  16. function LogView:update()
  17. local item = core.log_items[#core.log_items]
  18. if self.last_item ~= item then
  19. self.last_item = item
  20. self.scroll.to.y = 0
  21. self.yoffset = -(style.font:get_height() + style.padding.y)
  22. end
  23. self.scroll.to.y = math.max(0, self.scroll.to.y)
  24. self:move_towards("yoffset", 0)
  25. LogView.super.update(self)
  26. end
  27. function LogView:draw()
  28. self:draw_background(style.background)
  29. local ox, oy = self:get_content_offset()
  30. local th = style.font:get_height()
  31. local y = oy + style.padding.y + self.yoffset
  32. for i = #core.log_items, 1, -1 do
  33. local x = ox + style.padding.x
  34. local item = core.log_items[i]
  35. local time = os.date(nil, item.time)
  36. x = renderer.draw_text(style.font, time, x, y, style.dim)
  37. x = x + style.padding.x
  38. local subx = x
  39. x = renderer.draw_text(style.font, item.text, x, y, style.text)
  40. x = renderer.draw_text(style.font, " in " .. item.view, x, y, style.dim)
  41. y = y + th
  42. if item.info then
  43. for line in item.info:gmatch("[^\n]+") do
  44. renderer.draw_text(style.font, line, subx, y, style.dim)
  45. y = y + th
  46. end
  47. end
  48. y = y + style.padding.y
  49. end
  50. end
  51. return LogView