Skip to content

Commit e1f9cf6

Browse files
authored
fix: add error handling when calling executable (#4)
1 parent 8ba6877 commit e1f9cf6

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

lua/git-prompt-string-lualine/init.lua

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,24 @@ M.git_prompt_string_json = function()
4545
table.insert(cmd, '--color-disabled')
4646
end
4747
local stdout = ''
48+
local job_id
4849
-- replace with vim.system if/when we no longer support neovim v9
49-
local job_id = vim.fn.jobstart(cmd, {
50+
local status, result = pcall(vim.fn.jobstart, cmd, {
5051
cwd = opts.cwd,
5152
stdout_buffered = true,
5253
stderr_buffered = true,
5354
on_stdout = function(_, data)
5455
stdout = table.concat(data, '')
5556
end,
5657
})
58+
59+
if status then
60+
job_id = result
61+
else
62+
vim.notify_once('git-prompt-string-lualine.nvim: ERROR ' .. result, vim.log.levels.ERROR, {})
63+
return { error = result, color = 'error' }
64+
end
65+
5766
vim.fn.jobwait({ job_id })
5867
local json = vim.json.decode(stdout == '' and '{}' or stdout)
5968
return {

lua/lualine/components/git_prompt_string.lua

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ function M:init(options)
4545
untracked = self:create_hl(self.options.prompt_config.color_untracked, 'untracked'),
4646
no_upstream = self:create_hl(self.options.prompt_config.color_no_upstream, 'no_upstream'),
4747
merging = self:create_hl(self.options.prompt_config.color_merging, 'merging'),
48+
error = self:create_hl('ErrorMsg', 'error'),
4849
}
4950
end
5051

@@ -63,10 +64,14 @@ function M:update_status()
6364
self.options.icon_color_highlight = self.highlights[prompt.color]
6465
self.options.color_highlight = self.highlights[prompt.color]
6566
end
66-
if self.options.trim_prompt_prefix then
67+
if self.options.trim_prompt_prefix and prompt.prompt_prefix then
6768
prompt.prompt_prefix = prompt.prompt_prefix:gsub('^%s+', '')
6869
end
69-
return prompt.prompt_prefix .. prompt.branch_info .. prompt.branch_status .. prompt.prompt_suffix
70+
return (prompt.error or '')
71+
.. (prompt.prompt_prefix or '')
72+
.. (prompt.branch_info or '')
73+
.. (prompt.branch_status or '')
74+
.. (prompt.prompt_suffix or '')
7075
end
7176

7277
return M

0 commit comments

Comments
 (0)