From 7fcaa2502fdef153b2215bbf35a65c2ae76fa05b Mon Sep 17 00:00:00 2001 From: Joshua Minor Date: Sat, 20 Nov 2021 18:38:43 -0800 Subject: Add support for submodules & per-file color in the treeview based on git status. --- plugins/gitstatus.lua | 45 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/plugins/gitstatus.lua b/plugins/gitstatus.lua index bb16025..4d0cb87 100644 --- a/plugins/gitstatus.lua +++ b/plugins/gitstatus.lua @@ -1,7 +1,9 @@ -- mod-version:2 -- lite-xl 2.0 local core = require "core" +local common = require "core.common" local config = require "core.config" local style = require "core.style" +local _, TreeView = pcall(require, "plugins.treeview") local StatusView = require "core.statusview" local scan_rate = config.project_scan_rate or 5 @@ -13,6 +15,14 @@ local git = { } +config.gitstatus = { + recurse_submodules = true +} +style.gitstatus_addition = {common.color "#587c0c"} +style.gitstatus_modification = {common.color "#0c7d9d"} +style.gitstatus_deletion = {common.color "#94151b"} + + local function exec(cmd, wait) local proc = process.start(cmd) proc:wait(wait * 1000) @@ -27,10 +37,39 @@ core.add_thread(function() -- get branch name git.branch = exec({"git", "rev-parse", "--abbrev-ref", "HEAD"}, 1):match("[^\n]*") + if TreeView then + TreeView:clear_all_color_overrides() + end + + local inserts = 0 + local deletes = 0 + -- get diff - local line = exec({"git", "diff", "--stat"}, 1):match("[^\n]*%s*$") - git.inserts = tonumber(line:match("(%d+) ins")) or 0 - git.deletes = tonumber(line:match("(%d+) del")) or 0 + local diff = exec({"git", "diff", "--numstat"}, 1) + if config.gitstatus.recurse_submodules and system.get_file_info(".gitmodules") then + diff = diff .. exec({"git", "submodule", "foreach", "git diff --numstat --stat"}, 1) + end + + local root = "" + for line in string.gmatch(diff, "[^\n]+") do + local submodule = line:match("^Entering '(.+)'$") + if submodule then + root = submodule.."/" + else + local ins, dels, path = line:match("(%d+)%s+(%d+)%s+(.+)") + if path then + inserts = inserts + (tonumber(ins) or 0) + deletes = deletes + (tonumber(dels) or 0) + local abs_path = core.project_dir.."/"..root..path + if TreeView then + TreeView:set_color_override(abs_path, style.gitstatus_modification) + end + end + end + end + + git.inserts = inserts + git.deletes = deletes else git.branch = nil -- cgit v1.2.3 From 5fe0ad00084c8caaeda8a2c9bcb82da32fdf3a86 Mon Sep 17 00:00:00 2001 From: Joshua Minor Date: Sat, 20 Nov 2021 22:26:57 -0800 Subject: Double check that TreeView has the color override feature. --- plugins/gitstatus.lua | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/plugins/gitstatus.lua b/plugins/gitstatus.lua index 4d0cb87..eb925b0 100644 --- a/plugins/gitstatus.lua +++ b/plugins/gitstatus.lua @@ -8,6 +8,14 @@ local StatusView = require "core.statusview" local scan_rate = config.project_scan_rate or 5 +if TreeView then + if not (TreeView["set_color_override"] and TreeView["clear_all_color_overrides"]) then + -- TreeView doesn't have the color override feature we rely on, so skip it. + TreeView = nil + end +end + + local git = { branch = nil, inserts = 0, -- cgit v1.2.3 From 73faa10b4acc18228accfcf20e22d36228eb0cb3 Mon Sep 17 00:00:00 2001 From: Joshua Minor Date: Sat, 20 Nov 2021 23:09:02 -0800 Subject: Color every parent folder of each changed file. --- plugins/gitstatus.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/plugins/gitstatus.lua b/plugins/gitstatus.lua index eb925b0..54bd6e8 100644 --- a/plugins/gitstatus.lua +++ b/plugins/gitstatus.lua @@ -70,7 +70,13 @@ core.add_thread(function() deletes = deletes + (tonumber(dels) or 0) local abs_path = core.project_dir.."/"..root..path if TreeView then - TreeView:set_color_override(abs_path, style.gitstatus_modification) + -- Color this file, and each parent folder, + -- so you can see at a glance which folders + -- have modified files in them. + while abs_path do + TreeView:set_color_override(abs_path, style.gitstatus_modification) + abs_path = common.dirname(abs_path) + end end end end -- cgit v1.2.3 From a5f06db1bbaa1b315ae88d157a2135b0d6631ad6 Mon Sep 17 00:00:00 2001 From: Joshua Minor Date: Sat, 20 Nov 2021 23:09:40 -0800 Subject: Huge performance improvement using yield instread of proc:wait() --- plugins/gitstatus.lua | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/plugins/gitstatus.lua b/plugins/gitstatus.lua index 54bd6e8..4de2267 100644 --- a/plugins/gitstatus.lua +++ b/plugins/gitstatus.lua @@ -31,11 +31,16 @@ style.gitstatus_modification = {common.color "#0c7d9d"} style.gitstatus_deletion = {common.color "#94151b"} -local function exec(cmd, wait) +local function exec(cmd) local proc = process.start(cmd) - proc:wait(wait * 1000) - local res = proc:read_stdout() - return res + -- Don't use proc:wait() here - that will freeze the app. + -- Instead, rely on the fact that this is only called within + -- a coroutine, and yield for a fraction of a second, allowing + -- other stuff to happen while we wait for the process to complete. + while proc:running() do + coroutine.yield(0.1) + end + return proc:read_stdout() end @@ -43,19 +48,20 @@ core.add_thread(function() while true do if system.get_file_info(".git") then -- get branch name - git.branch = exec({"git", "rev-parse", "--abbrev-ref", "HEAD"}, 1):match("[^\n]*") - - if TreeView then - TreeView:clear_all_color_overrides() - end + git.branch = exec({"git", "rev-parse", "--abbrev-ref", "HEAD"}):match("[^\n]*") local inserts = 0 local deletes = 0 -- get diff - local diff = exec({"git", "diff", "--numstat"}, 1) + local diff = exec({"git", "diff", "--numstat"}) if config.gitstatus.recurse_submodules and system.get_file_info(".gitmodules") then - diff = diff .. exec({"git", "submodule", "foreach", "git diff --numstat --stat"}, 1) + local diff2 = exec({"git", "submodule", "foreach", "git diff --numstat --stat"}) + diff = diff .. diff2 + end + + if TreeView then + TreeView:clear_all_color_overrides() end local root = "" -- cgit v1.2.3 From c916e5cda7df66b9affae73b707435a614d7f4b3 Mon Sep 17 00:00:00 2001 From: Joshua Minor Date: Sun, 21 Nov 2021 15:25:45 -0800 Subject: Switched to TreeView:color_for_item(abs_path) --- plugins/gitstatus.lua | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/plugins/gitstatus.lua b/plugins/gitstatus.lua index 4de2267..bb1882c 100644 --- a/plugins/gitstatus.lua +++ b/plugins/gitstatus.lua @@ -3,16 +3,18 @@ local core = require "core" local common = require "core.common" local config = require "core.config" local style = require "core.style" -local _, TreeView = pcall(require, "plugins.treeview") local StatusView = require "core.statusview" +local TreeView = require "plugins.treeview" + local scan_rate = config.project_scan_rate or 5 +local cached_color_for_item = {} -if TreeView then - if not (TreeView["set_color_override"] and TreeView["clear_all_color_overrides"]) then - -- TreeView doesn't have the color override feature we rely on, so skip it. - TreeView = nil - end +-- Override TreeView's color_for_item, but first +-- stash the old one (using [] in case it is not there at all) +local old_color_for_item = TreeView["color_for_item"] +function TreeView:color_for_item(abs_path) + return cached_color_for_item[abs_path] or old_color_for_item(abs_path) end @@ -60,29 +62,26 @@ core.add_thread(function() diff = diff .. diff2 end - if TreeView then - TreeView:clear_all_color_overrides() - end + -- forget the old state + cached_color_for_item = {} - local root = "" + local folder = core.project_dir for line in string.gmatch(diff, "[^\n]+") do local submodule = line:match("^Entering '(.+)'$") if submodule then - root = submodule.."/" + folder = core.project_dir .. PATHSEP .. submodule else local ins, dels, path = line:match("(%d+)%s+(%d+)%s+(.+)") if path then inserts = inserts + (tonumber(ins) or 0) deletes = deletes + (tonumber(dels) or 0) - local abs_path = core.project_dir.."/"..root..path - if TreeView then - -- Color this file, and each parent folder, - -- so you can see at a glance which folders - -- have modified files in them. - while abs_path do - TreeView:set_color_override(abs_path, style.gitstatus_modification) - abs_path = common.dirname(abs_path) - end + local abs_path = folder .. PATHSEP .. path + -- Color this file, and each parent folder, + -- so you can see at a glance which folders + -- have modified files in them. + while abs_path do + cached_color_for_item[abs_path] = style.gitstatus_modification + abs_path = common.dirname(abs_path) end end end -- cgit v1.2.3 From 1cfa26ca4c455218f399fa87669c33d2ab87af33 Mon Sep 17 00:00:00 2001 From: Joshua Minor Date: Sun, 21 Nov 2021 15:40:45 -0800 Subject: Removed redundant --stat option. --- plugins/gitstatus.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/gitstatus.lua b/plugins/gitstatus.lua index bb1882c..2578862 100644 --- a/plugins/gitstatus.lua +++ b/plugins/gitstatus.lua @@ -58,7 +58,7 @@ core.add_thread(function() -- get diff local diff = exec({"git", "diff", "--numstat"}) if config.gitstatus.recurse_submodules and system.get_file_info(".gitmodules") then - local diff2 = exec({"git", "submodule", "foreach", "git diff --numstat --stat"}) + local diff2 = exec({"git", "submodule", "foreach", "git diff --numstat"}) diff = diff .. diff2 end -- cgit v1.2.3 From 0cbdc9d6deb298faa7503e5caa10ed4b4d95866e Mon Sep 17 00:00:00 2001 From: Joshua Minor Date: Sun, 21 Nov 2021 15:45:22 -0800 Subject: Guard against nil response. --- plugins/gitstatus.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/gitstatus.lua b/plugins/gitstatus.lua index 2578862..de5c74b 100644 --- a/plugins/gitstatus.lua +++ b/plugins/gitstatus.lua @@ -42,7 +42,7 @@ local function exec(cmd) while proc:running() do coroutine.yield(0.1) end - return proc:read_stdout() + return proc:read_stdout() or "" end -- cgit v1.2.3