From ec47bb04478e2b6e69b308a1651cd8cebccda50d Mon Sep 17 00:00:00 2001 From: Shadman Date: Wed, 24 Feb 2021 23:03:24 +0600 Subject: [PATCH] fix: wrong data displayed by diff component (#117) Sometimes async process outputs partial data. Previously that data was processed imediatly causing wrong results. Now the data is processed after async processes finishes --- lua/lualine/components/diff.lua | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lua/lualine/components/diff.lua b/lua/lualine/components/diff.lua index ee5b9fa..00fe0e5 100644 --- a/lua/lualine/components/diff.lua +++ b/lua/lualine/components/diff.lua @@ -7,6 +7,8 @@ local highlight = require"lualine.highlight" -- variable to store git diff stats local git_diff = nil +-- accumulates async output to process in the end +local diff_data = '' -- process diff data and update git_diff{ added, removed, modified} local function process_diff(data) @@ -37,8 +39,6 @@ end -- variable to store git_diff getter async function local get_git_diff = nil --- flag to see if async job exited before updating git_diff -local updated = false -- Updates the async function for current file local function update_git_diff_getter() @@ -51,22 +51,20 @@ local function update_git_diff_getter() ,vim.fn.expand('%:h'), vim.fn.expand('%:t')), on_stdout = function(_, data) if data then - process_diff(data) - updated = true + diff_data = diff_data .. data end end, on_stderr = function (_, data) if data then git_diff = nil - updated = true + diff_data = '' end end, on_exit = function() - if not updated then - -- updated not set means git exited without emmiting anything on stdout - -- or stderr means file is unchanged + if diff_data ~= '' then + process_diff(diff_data) + else git_diff = {0, 0, 0} - updated = true end end }) @@ -76,7 +74,7 @@ end local function update_git_diff() vim.schedule_wrap(function() if get_git_diff then - updated = false + diff_data = '' get_git_diff:start() end end)()