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
This commit is contained in:
parent
f47f68cee6
commit
ec47bb0447
|
@ -7,6 +7,8 @@ local highlight = require"lualine.highlight"
|
||||||
|
|
||||||
-- variable to store git diff stats
|
-- variable to store git diff stats
|
||||||
local git_diff = nil
|
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}
|
-- process diff data and update git_diff{ added, removed, modified}
|
||||||
local function process_diff(data)
|
local function process_diff(data)
|
||||||
|
@ -37,8 +39,6 @@ end
|
||||||
|
|
||||||
-- variable to store git_diff getter async function
|
-- variable to store git_diff getter async function
|
||||||
local get_git_diff = nil
|
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
|
-- Updates the async function for current file
|
||||||
local function update_git_diff_getter()
|
local function update_git_diff_getter()
|
||||||
|
@ -51,22 +51,20 @@ local function update_git_diff_getter()
|
||||||
,vim.fn.expand('%:h'), vim.fn.expand('%:t')),
|
,vim.fn.expand('%:h'), vim.fn.expand('%:t')),
|
||||||
on_stdout = function(_, data)
|
on_stdout = function(_, data)
|
||||||
if data then
|
if data then
|
||||||
process_diff(data)
|
diff_data = diff_data .. data
|
||||||
updated = true
|
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
on_stderr = function (_, data)
|
on_stderr = function (_, data)
|
||||||
if data then
|
if data then
|
||||||
git_diff = nil
|
git_diff = nil
|
||||||
updated = true
|
diff_data = ''
|
||||||
end
|
end
|
||||||
end,
|
end,
|
||||||
on_exit = function()
|
on_exit = function()
|
||||||
if not updated then
|
if diff_data ~= '' then
|
||||||
-- updated not set means git exited without emmiting anything on stdout
|
process_diff(diff_data)
|
||||||
-- or stderr means file is unchanged
|
else
|
||||||
git_diff = {0, 0, 0}
|
git_diff = {0, 0, 0}
|
||||||
updated = true
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
})
|
})
|
||||||
|
@ -76,7 +74,7 @@ end
|
||||||
local function update_git_diff()
|
local function update_git_diff()
|
||||||
vim.schedule_wrap(function()
|
vim.schedule_wrap(function()
|
||||||
if get_git_diff then
|
if get_git_diff then
|
||||||
updated = false
|
diff_data = ''
|
||||||
get_git_diff:start()
|
get_git_diff:start()
|
||||||
end
|
end
|
||||||
end)()
|
end)()
|
||||||
|
|
Loading…
Reference in New Issue