statusview.lua 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 DocView = require "core.docview"
  6. local View = require "core.view"
  7. local StatusView = View:extend()
  8. local separator = " "
  9. local separator2 = " | "
  10. function StatusView:new()
  11. StatusView.super.new(self)
  12. self.focusable = false
  13. self.message_timeout = 0
  14. self.message = {}
  15. end
  16. function StatusView:show_message(icon, icon_color, text)
  17. self.message = {
  18. icon_color, style.icon_font, icon,
  19. style.dim, style.font, separator2, style.text, text
  20. }
  21. self.message_timeout = system.get_time() + config.message_timeout
  22. end
  23. function StatusView:update()
  24. self.size.y = style.font:get_height() + style.padding.y * 2
  25. if system.get_time() < self.message_timeout then
  26. self.scroll.to.y = self.size.y
  27. else
  28. self.scroll.to.y = 0
  29. end
  30. StatusView.super.update(self)
  31. end
  32. function StatusView:draw_items(items, right_align, yoffset)
  33. local font = style.font
  34. local color = style.text
  35. local x, y = self:get_content_offset()
  36. y = y + (yoffset or 0)
  37. local i
  38. if right_align then
  39. x = x + self.size.x - style.padding.x
  40. i = #items
  41. else
  42. x = x + style.padding.x
  43. i = 1
  44. end
  45. while items[i] do
  46. local item = items[i]
  47. if type(item) == "userdata" then
  48. font = item
  49. elseif type(item) == "table" then
  50. color = item
  51. else
  52. if right_align then
  53. x = x - font:get_width(item)
  54. common.draw_text(font, color, item, nil, x, y, 0, self.size.y)
  55. else
  56. x = common.draw_text(font, color, item, nil, x, y, 0, self.size.y)
  57. end
  58. end
  59. i = i + (right_align and -1 or 1)
  60. end
  61. end
  62. local function draw_for_doc_view(self, x, y)
  63. local dv = core.active_view
  64. local line, col = dv.doc:get_selection()
  65. local dirty = dv.doc:is_dirty()
  66. self:draw_items {
  67. dirty and style.accent or style.text, style.icon_font, "f",
  68. style.dim, style.font, separator2, style.text,
  69. dv.doc.filename and style.text or style.dim, dv.doc:get_name(),
  70. style.text,
  71. separator,
  72. "line: ", line,
  73. separator,
  74. col > config.line_limit and style.accent or style.text, "col: ", col,
  75. style.text,
  76. separator,
  77. string.format("%d%%", line / #dv.doc.lines * 100),
  78. }
  79. self:draw_items({
  80. "g", style.icon_font,
  81. style.text, separator2, style.dim, style.font,
  82. #dv.doc.lines, " lines",
  83. separator,
  84. dv.doc.crlf and "CRLF" or "LF"
  85. }, true)
  86. end
  87. function StatusView:draw()
  88. self:draw_background(style.background2)
  89. local th = style.font:get_height()
  90. local x, y = self:get_content_offset()
  91. x = x + style.padding.x
  92. y = y + (self.size.y - th) / 2
  93. if self.message then
  94. self:draw_items(self.message, false, self.size.y)
  95. end
  96. if getmetatable(core.active_view) == DocView then
  97. draw_for_doc_view(self)
  98. else
  99. self:draw_items({
  100. "g", style.icon_font,
  101. style.text, separator2, style.dim, style.font,
  102. #core.docs, " / ", style.dim,
  103. #core.project_files, " files"
  104. }, true)
  105. end
  106. end
  107. return StatusView