From ab4ecd515bb69a4cfacf06a2af9fbcf375827ec5 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Sat, 18 Dec 2021 10:51:44 +0800 Subject: [PATCH 01/12] multiple improvements to logging - added style.log table - removed contextmenu - use ctrl+click to copy individual log entries - use icon instead of + or - for log items in logview --- data/core/command.lua | 2 +- data/core/commands/log.lua | 26 ++++++++++++++++++++++++++ data/core/init.lua | 22 ++++++++++++++-------- data/core/keymap.lua | 3 ++- data/core/logview.lua | 17 ++++++++--------- data/core/style.lua | 5 +++++ data/plugins/contextmenu.lua | 18 ------------------ 7 files changed, 56 insertions(+), 37 deletions(-) create mode 100644 data/core/commands/log.lua 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..940ffca4 --- /dev/null +++ b/data/core/commands/log.lua @@ -0,0 +1,26 @@ +local core = require "core" +local command = require "core.command" + +command.add("core.logview", { + ["log:expand-item"] = function() + if core.active_view.hovered_item then + core.active_view:expand_item(core.active_view.hovered_item) + end + end, + ["log:copy-entry"] = function() + if core.active_view.hovered_item then + system.set_clipboard(core.get_log(core.active_view.hovered_item)) + end + end +}) + +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()) + 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 aadccc66..14408393 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -1034,15 +1034,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) @@ -1052,17 +1058,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 @@ -1075,7 +1081,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/keymap.lua b/data/core/keymap.lua index b076629b..440e518d 100644 --- a/data/core/keymap.lua +++ b/data/core/keymap.lua @@ -235,7 +235,8 @@ keymap.add_direct { ["pagedown"] = "doc:move-to-next-page", ["shift+1lclick"] = "doc:select-to-cursor", - ["ctrl+1lclick"] = "doc:split-cursor", + ["ctrl+1lclick"] = { "doc:split-cursor", "log:copy-entry" }, + ["lclick"] = "log:expand-item", ["1lclick"] = "doc:set-cursor", ["2lclick"] = "doc:set-cursor-word", ["3lclick"] = "doc:set-cursor-line", diff --git a/data/core/logview.lua b/data/core/logview.lua index 1ea0e43e..5be2aaca 100644 --- a/data/core/logview.lua +++ b/data/core/logview.lua @@ -36,6 +36,7 @@ local LogView = View:extend() LogView.context = "session" + function LogView:new() LogView.super.new(self) self.last_item = core.log_items[#core.log_items] @@ -91,14 +92,6 @@ function LogView:on_mouse_moved(px, py, ...) 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) - end -end - - function LogView:update() local item = core.log_items[#core.log_items] if self.last_item ~= item then @@ -144,7 +137,13 @@ function LogView:draw() x = common.draw_text(style.font, style.dim, time, "left", x, y, w, lh) x = x + style.padding.x - x = common.draw_text(style.code_font, style.dim, is_expanded(item) and "-" or "+", "left", x, y, w, lh) + x = common.draw_text( + style.icon_font, + style.log[item.level].color, + style.log[item.level].icon, + "left", + x, y, w, lh + ) x = x + style.padding.x w = w - (x - self:get_content_offset()) diff --git a/data/core/style.lua b/data/core/style.lua index 3b0d9e35..86f88414 100644 --- a/data/core/style.lua +++ b/data/core/style.lua @@ -66,4 +66,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.accent } +} + 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" }, From b5dff196f6440d542b4b7c92ffe278e7ce559762 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Sat, 18 Dec 2021 20:10:25 +0800 Subject: [PATCH 02/12] make error icon red --- data/core/style.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/core/style.lua b/data/core/style.lua index 86f88414..c988b2ef 100644 --- a/data/core/style.lua +++ b/data/core/style.lua @@ -68,7 +68,7 @@ style.syntax_fonts = {} style.log = { INFO = { icon = "i", color = style.text }, - ERROR = { icon = "!", color = style.accent } + ERROR = { icon = "!", color = { common.color "#ff6961" } } } return style From fd3b4334ce20163edb9279a74c7068cbffab42c0 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Sat, 18 Dec 2021 20:11:24 +0800 Subject: [PATCH 03/12] add clipping to drawing log items --- data/core/logview.lua | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/data/core/logview.lua b/data/core/logview.lua index 5be2aaca..9f45a160 100644 --- a/data/core/logview.lua +++ b/data/core/logview.lua @@ -130,11 +130,8 @@ function LogView:draw() 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 - x = x + style.padding.x - - local time = os.date(nil, item.time) - x = common.draw_text(style.font, style.dim, time, "left", x, y, w, lh) + 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( @@ -164,6 +161,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 From 31df408d93c057eaaa3dfae002db64f1430a6447 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Sat, 18 Dec 2021 20:11:50 +0800 Subject: [PATCH 04/12] make timestamp fix sized --- data/core/logview.lua | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/data/core/logview.lua b/data/core/logview.lua index 9f45a160..90ce564f 100644 --- a/data/core/logview.lua +++ b/data/core/logview.lua @@ -142,6 +142,13 @@ function LogView:draw() x, y, w, lh ) x = x + style.padding.x + + -- timestamps are always 15% of the width + local tw = w * 15 / 100 + local time = os.date(nil, item.time) + common.draw_text(style.font, style.dim, time, "center", x, y, tw, lh) + x = x + tw + style.padding.x + w = w - (x - self:get_content_offset()) if is_expanded(item) then From becdb99222192a71db2b9c63569f2a635420fbfd Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Sat, 18 Dec 2021 22:42:33 +0800 Subject: [PATCH 05/12] center icons to accomodate for size difference --- data/core/logview.lua | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/data/core/logview.lua b/data/core/logview.lua index 90ce564f..6485c2f6 100644 --- a/data/core/logview.lua +++ b/data/core/logview.lua @@ -130,6 +130,10 @@ function LogView:draw() local th = style.font:get_height() local lh = th + style.padding.y -- for one line + local iw = math.max( + style.icon_font:get_width(style.log.ERROR.icon), + style.icon_font:get_width(style.log.INFO.icon) + ) for _, item, x, y, w, h in self:each_item() do core.push_clip_rect(x, y, w, h) x = x + style.padding.x @@ -138,8 +142,8 @@ function LogView:draw() style.icon_font, style.log[item.level].color, style.log[item.level].icon, - "left", - x, y, w, lh + "center", + x, y, iw, lh ) x = x + style.padding.x From 1526cd176c6c119b7bb738f1779a86d1addd0f88 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Sun, 19 Dec 2021 09:36:24 +0800 Subject: [PATCH 06/12] move selection logic to mouse click --- data/core/commands/log.lua | 12 ------------ data/core/keymap.lua | 3 +-- data/core/logview.lua | 28 +++++++++++++++++++++------- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/data/core/commands/log.lua b/data/core/commands/log.lua index 940ffca4..c0111b57 100644 --- a/data/core/commands/log.lua +++ b/data/core/commands/log.lua @@ -1,18 +1,6 @@ local core = require "core" local command = require "core.command" -command.add("core.logview", { - ["log:expand-item"] = function() - if core.active_view.hovered_item then - core.active_view:expand_item(core.active_view.hovered_item) - end - end, - ["log:copy-entry"] = function() - if core.active_view.hovered_item then - system.set_clipboard(core.get_log(core.active_view.hovered_item)) - end - end -}) command.add(nil, { ["log:open-as-doc"] = function() diff --git a/data/core/keymap.lua b/data/core/keymap.lua index 440e518d..b076629b 100644 --- a/data/core/keymap.lua +++ b/data/core/keymap.lua @@ -235,8 +235,7 @@ keymap.add_direct { ["pagedown"] = "doc:move-to-next-page", ["shift+1lclick"] = "doc:select-to-cursor", - ["ctrl+1lclick"] = { "doc:split-cursor", "log:copy-entry" }, - ["lclick"] = "log:expand-item", + ["ctrl+1lclick"] = "doc:split-cursor", ["1lclick"] = "doc:set-cursor", ["2lclick"] = "doc:set-cursor-word", ["3lclick"] = "doc:set-cursor-line", diff --git a/data/core/logview.lua b/data/core/logview.lua index 6485c2f6..d7b07e33 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" @@ -78,17 +79,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 + + 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 From 695c7bf78163e3e554533f25a65c0b5d57b0f211 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Sun, 19 Dec 2021 09:36:43 +0800 Subject: [PATCH 07/12] add instruction when logview is open --- data/core/logview.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data/core/logview.lua b/data/core/logview.lua index d7b07e33..18299ac5 100644 --- a/data/core/logview.lua +++ b/data/core/logview.lua @@ -44,6 +44,8 @@ function LogView:new() self.expanding = {} self.scrollable = true self.yoffset = 0 + + core.status_view:show_message("i", style.text, "ctrl+click to copy entry") end From 8d7867d118689098003ce844d96728366bbba341 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Tue, 21 Dec 2021 17:36:32 +0800 Subject: [PATCH 08/12] adapt style.good and style.error --- data/core/style.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data/core/style.lua b/data/core/style.lua index c988b2ef..acda297e 100644 --- a/data/core/style.lua +++ b/data/core/style.lua @@ -67,8 +67,8 @@ 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 = { common.color "#ff6961" } } + INFO = { icon = "i", color = style.good }, + ERROR = { icon = "!", color = style.error } } return style From 54f6579e9dbb448cd6b71fae67925cd99792afce Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Tue, 21 Dec 2021 17:38:25 +0800 Subject: [PATCH 09/12] change INFO to use style.text --- data/core/style.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data/core/style.lua b/data/core/style.lua index 94af2a54..470a0904 100644 --- a/data/core/style.lua +++ b/data/core/style.lua @@ -73,7 +73,7 @@ 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.good }, + INFO = { icon = "i", color = style.text }, ERROR = { icon = "!", color = style.error } } From 8f06ef9b8100a0f59aa1fca706dcb84c91316833 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Wed, 22 Dec 2021 09:55:47 +0800 Subject: [PATCH 10/12] ensure date is rendered properly --- data/core/logview.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data/core/logview.lua b/data/core/logview.lua index 18299ac5..99ab0a93 100644 --- a/data/core/logview.lua +++ b/data/core/logview.lua @@ -150,6 +150,8 @@ function LogView:draw() style.icon_font:get_width(style.log.ERROR.icon), style.icon_font:get_width(style.log.INFO.icon) ) + + local tw = style.font:get_width(os.date()) for _, item, x, y, w, h in self:each_item() do core.push_clip_rect(x, y, w, h) x = x + style.padding.x @@ -164,9 +166,8 @@ function LogView:draw() x = x + style.padding.x -- timestamps are always 15% of the width - local tw = w * 15 / 100 local time = os.date(nil, item.time) - common.draw_text(style.font, style.dim, time, "center", x, y, tw, lh) + common.draw_text(style.font, style.dim, time, "left", x, y, tw, lh) x = x + tw + style.padding.x w = w - (x - self:get_content_offset()) From 2f65d5a26ff7a3da24ef37e4f312c3d665b70c4c Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Wed, 22 Dec 2021 10:53:53 +0800 Subject: [PATCH 11/12] make the date field consistent --- data/core/logview.lua | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/data/core/logview.lua b/data/core/logview.lua index 99ab0a93..aa1ad84a 100644 --- a/data/core/logview.lua +++ b/data/core/logview.lua @@ -140,7 +140,8 @@ 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) @@ -151,7 +152,7 @@ function LogView:draw() style.icon_font:get_width(style.log.INFO.icon) ) - local tw = style.font:get_width(os.date()) + 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 From 00e2e281d3181055c435e1cdb9ed5d141976d5f8 Mon Sep 17 00:00:00 2001 From: takase1121 <20792268+takase1121@users.noreply.github.com> Date: Wed, 22 Dec 2021 10:54:25 +0800 Subject: [PATCH 12/12] remove unsaved flag from log.txt --- data/core/commands/log.lua | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data/core/commands/log.lua b/data/core/commands/log.lua index c0111b57..8a5b7f3d 100644 --- a/data/core/commands/log.lua +++ b/data/core/commands/log.lua @@ -7,6 +7,8 @@ command.add(nil, { 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())