2021-01-04 00:38:01 +00:00
|
|
|
local async = require('lualine.async')
|
|
|
|
|
2021-01-04 01:14:29 +00:00
|
|
|
local git_branch
|
2021-01-04 00:38:01 +00:00
|
|
|
|
2021-01-04 01:14:29 +00:00
|
|
|
local get_git_branch = async:new({
|
2021-01-04 00:38:01 +00:00
|
|
|
cmd = 'git branch --show-current',
|
|
|
|
on_stdout = function(_, data)
|
|
|
|
if data then
|
2021-01-04 01:14:29 +00:00
|
|
|
git_branch = data:gsub('\n', '')
|
2021-01-04 00:38:01 +00:00
|
|
|
end
|
2021-01-08 09:51:11 +00:00
|
|
|
end,
|
|
|
|
on_stderr = function (_, data)
|
|
|
|
if data then
|
|
|
|
if data:find("fatal: not a git repository") then
|
|
|
|
git_branch = ''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
2021-01-04 00:38:01 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
local timer = vim.loop.new_timer()
|
|
|
|
timer:start(0, 1000, vim.schedule_wrap(function()
|
2021-01-08 09:51:11 +00:00
|
|
|
local cur_dir = vim.fn.getcwd()
|
|
|
|
local buffer_working_directory = vim.fn.expand("%:p:h")
|
|
|
|
local status, _ = pcall(vim.api.nvim_set_current_dir, buffer_working_directory)
|
|
|
|
if status == true then
|
|
|
|
get_git_branch:start()
|
|
|
|
vim.api.nvim_set_current_dir(cur_dir)
|
|
|
|
end
|
2021-01-04 00:38:01 +00:00
|
|
|
end))
|
|
|
|
|
2021-01-04 01:14:29 +00:00
|
|
|
local function branch()
|
|
|
|
if not git_branch or #git_branch == 0 then return '' end
|
2020-12-30 14:48:51 +00:00
|
|
|
local ok,devicons = pcall(require,'nvim-web-devicons')
|
|
|
|
if ok then
|
|
|
|
local icon = devicons.get_icon('git')
|
|
|
|
if icon ~= nil then
|
2021-01-04 01:14:29 +00:00
|
|
|
return icon .. ' ' .. git_branch
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-01-04 01:14:29 +00:00
|
|
|
return git_branch
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-01-05 23:43:36 +00:00
|
|
|
ok = vim.fn.exists("*WebDevIconsGetFileTypeSymbol")
|
2020-12-31 03:12:40 +00:00
|
|
|
if ok ~= 0 then
|
2020-12-30 14:48:51 +00:00
|
|
|
local icon = ''
|
2021-01-04 01:14:29 +00:00
|
|
|
return icon .. ' ' .. git_branch
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
2021-01-04 01:14:29 +00:00
|
|
|
return git_branch
|
2020-12-30 14:48:51 +00:00
|
|
|
end
|
|
|
|
|
2021-01-04 01:14:29 +00:00
|
|
|
return branch
|