diff --git a/data/core/command.lua b/data/core/command.lua index 2cf851da..07b30fb8 100644 --- a/data/core/command.lua +++ b/data/core/command.lua @@ -64,7 +64,7 @@ end function command.add_defaults() local reg = { "core", "root", "command", "doc", "findreplace", - "files", "drawwhitespace", "dialog" + "files", "drawwhitespace", "dialog", "log" } for _, name in ipairs(reg) do require("core.commands." .. name) diff --git a/data/core/commands/log.lua b/data/core/commands/log.lua new file mode 100644 index 00000000..8a5b7f3d --- /dev/null +++ b/data/core/commands/log.lua @@ -0,0 +1,16 @@ +local core = require "core" +local command = require "core.command" + + +command.add(nil, { + ["log:open-as-doc"] = function() + local doc = core.open_doc("logs.txt") + core.root_view:open_doc(doc) + doc:insert(1, 1, core.get_log()) + doc.new_file = false + doc:clean() + end, + ["log:copy-to-clipboard"] = function() + system.set_clipboard(core.get_log()) + end +}) diff --git a/data/core/init.lua b/data/core/init.lua index 446d6b20..91fc59e8 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -1036,15 +1036,21 @@ function core.get_views_referencing_doc(doc) end -local function log(icon, icon_color, fmt, ...) +local function log(level, show, fmt, ...) local text = string.format(fmt, ...) - if icon then - core.status_view:show_message(icon, icon_color, text) + if show then + local s = style.log[level] + core.status_view:show_message(s.icon, s.color, text) end local info = debug.getinfo(2, "Sl") local at = string.format("%s:%d", info.short_src, info.currentline) - local item = { text = text, time = os.time(), at = at } + local item = { + level = level, + text = text, + time = os.time(), + at = at + } table.insert(core.log_items, item) if #core.log_items > config.max_log_items then table.remove(core.log_items, 1) @@ -1054,17 +1060,17 @@ end function core.log(...) - return log("i", style.text, ...) + return log("INFO", true, ...) end function core.log_quiet(...) - return log(nil, nil, ...) + return log("INFO", false, ...) end function core.error(...) - return log("!", style.accent, ...) + return log("ERROR", true, ...) end @@ -1077,7 +1083,7 @@ function core.get_log(i) return table.concat(r, "\n") end local item = type(i) == "number" and core.log_items[i] or i - local text = string.format("[%s] %s at %s", os.date(nil, item.time), item.text, item.at) + local text = string.format("%s [%s] %s at %s", os.date(nil, item.time), item.level, item.text, item.at) if item.info then text = string.format("%s\n%s\n", text, item.info) end diff --git a/data/core/logview.lua b/data/core/logview.lua index 1ea0e43e..aa1ad84a 100644 --- a/data/core/logview.lua +++ b/data/core/logview.lua @@ -1,5 +1,6 @@ local core = require "core" local common = require "core.common" +local keymap = require "core.keymap" local style = require "core.style" local View = require "core.view" @@ -36,12 +37,15 @@ local LogView = View:extend() LogView.context = "session" + function LogView:new() LogView.super.new(self) self.last_item = core.log_items[#core.log_items] self.expanding = {} self.scrollable = true self.yoffset = 0 + + core.status_view:show_message("i", style.text, "ctrl+click to copy entry") end @@ -77,25 +81,30 @@ function LogView:each_item() end -function LogView:on_mouse_moved(px, py, ...) - LogView.super.on_mouse_moved(self, px, py, ...) - local hovered = false - for _, item, x, y, w, h in self:each_item() do +function LogView:on_mouse_pressed(button, px, py, clicks) + if LogView.super.on_mouse_pressed(self, button, px, py, clicks) then + return true + end + + local index, selected + for i, item, x, y, w, h in self:each_item() do if px >= x and py >= y and px < x + w and py < y + h then - hovered = true - self.hovered_item = item + index = i + selected = item break end end - if not hovered then self.hovered_item = nil end -end - -function LogView:on_mouse_pressed(button, mx, my, clicks) - if LogView.super.on_mouse_pressed(self, button, mx, my, clicks) then return end - if self.hovered_item then - self:expand_item(self.hovered_item) + if selected then + if keymap.modkeys["ctrl"] then + system.set_clipboard(core.get_log(selected)) + core.status_view:show_message("i", style.text, "copied entry #"..index.." to clipboard.") + else + self:expand_item(selected) + end end + + return true end @@ -131,21 +140,37 @@ local function draw_text_multiline(font, text, x, y, color) return resx, y end - +-- this is just to get a date string that's consistent +local datestr = os.date() function LogView:draw() self:draw_background(style.background) local th = style.font:get_height() local lh = th + style.padding.y -- for one line - for _, item, x, y, w in self:each_item() do + local iw = math.max( + style.icon_font:get_width(style.log.ERROR.icon), + style.icon_font:get_width(style.log.INFO.icon) + ) + + local tw = style.font:get_width(datestr) + for _, item, x, y, w, h in self:each_item() do + core.push_clip_rect(x, y, w, h) x = x + style.padding.x + x = common.draw_text( + style.icon_font, + style.log[item.level].color, + style.log[item.level].icon, + "center", + x, y, iw, lh + ) + x = x + style.padding.x + + -- timestamps are always 15% of the width local time = os.date(nil, item.time) - x = common.draw_text(style.font, style.dim, time, "left", x, y, w, lh) - x = x + style.padding.x + common.draw_text(style.font, style.dim, time, "left", x, y, tw, lh) + x = x + tw + style.padding.x - x = common.draw_text(style.code_font, style.dim, is_expanded(item) and "-" or "+", "left", x, y, w, lh) - x = x + style.padding.x w = w - (x - self:get_content_offset()) if is_expanded(item) then @@ -165,6 +190,8 @@ function LogView:draw() end _, y = common.draw_text(style.font, style.text, line, "left", x, y, w, lh) end + + core.pop_clip_rect() end end diff --git a/data/core/style.lua b/data/core/style.lua index 79ef47e1..470a0904 100644 --- a/data/core/style.lua +++ b/data/core/style.lua @@ -72,4 +72,9 @@ style.syntax["function"] = { common.color "#93DDFA" } style.syntax_fonts = {} -- style.syntax_fonts["comment"] = renderer.font.load(path_to_font, size_of_font, rendering_options) +style.log = { + INFO = { icon = "i", color = style.text }, + ERROR = { icon = "!", color = style.error } +} + return style diff --git a/data/plugins/contextmenu.lua b/data/plugins/contextmenu.lua index 4b34dfd5..017846ae 100644 --- a/data/plugins/contextmenu.lua +++ b/data/plugins/contextmenu.lua @@ -42,24 +42,6 @@ keymap.add { ["menu"] = "context:show" } -local function copy_log() - local item = core.active_view.hovered_item - if item then - system.set_clipboard(core.get_log(item)) - end -end - -local function open_as_doc() - local doc = core.open_doc("logs.txt") - core.root_view:open_doc(doc) - doc:insert(1, 1, core.get_log()) -end - -menu:register("core.logview", { - { text = "Copy entry", command = copy_log }, - { text = "Open as file", command = open_as_doc } -}) - if require("plugins.scale") then menu:register("core.docview", { { text = "Cut", command = "doc:cut" },