Skip to content

Commit

Permalink
added initial async support (#20)
Browse files Browse the repository at this point in the history
* added inital async setup

* update README.md
  • Loading branch information
hoob3rt committed Jan 4, 2021
1 parent 3ab7066 commit f62c0d4
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 7 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ EOF
## TODO's
Please create an issue/ pr if you want to see more functionality implemented
- General
- [x] async support
- [ ] additional settings for components
- [ ] create doc file
- Components
- [x] branch component
Expand Down
75 changes: 75 additions & 0 deletions lua/lualine/async.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
local M = {}

function M:new(args)
args = args or {}
for index, arg in pairs(args) do
self[index] = arg
end
setmetatable(args, self)
self.__index = self
return args
end

local function close_pipe(pipe)
if pipe ~= nil and not pipe:is_closing() then
pipe:close()
end
end

function M.close_all()
close_pipe(M.stdin)
close_pipe(M.stderr)
close_pipe(M.stdout)
close_pipe(M.handle)
end

function M.init_options()
local options = {}
local args = vim.fn.split(M.cmd, ' ')
M.stdin = vim.loop.new_pipe(false)
M.stdout = vim.loop.new_pipe(false)
M.stderr = vim.loop.new_pipe(false)
options.command = table.remove(args, 1)
options.args = args
options.stdio = {
M.stdin,
M.stdout,
M.stderr
}
if M.cwd then
options.cwd = M.cwd
end
if M.env then
options.env = M.env
end
if M.detach then
options.detach = M.detach
end
return options
end

function M.start()
local options = M.init_options()
M.handle = vim.loop.spawn(options.command, options, vim.schedule_wrap(M.stop))
if M.on_stdout then
M.stdout:read_start(vim.schedule_wrap(M.on_stdout))
end
if M.on_stderr then
M.stderr:read_start(vim.schedule_wrap(M.on_stderr))
end
end

function M.stop(code, signal)
if M.on_exit then
M.on_exit(code, signal)
end
if M.on_stdout then
M.stdout:read_stop()
end
if M.on_stderr then
M.stderr:read_stop()
end
M.close_all()
end

return M
26 changes: 19 additions & 7 deletions lua/lualine/components/branch.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
local function Branch()
local branch = vim.fn.systemlist(
'cd '..vim.fn.expand('%:p:h:S')..' 2>/dev/null && git status --porcelain -b 2>/dev/null')[1]
if not branch or #branch == 0 then
return ''
local async = require('lualine.async')

local branch

local git_branch = async:new({
cmd = 'git branch --show-current',
on_stdout = function(_, data)
if data then
branch = data:gsub('\n', '')
end
end
branch = branch:gsub([[^## No commits yet on (%w+)$]], '%1')
branch = branch:gsub([[^##%s+(%w+).*$]], '%1')
})

local timer = vim.loop.new_timer()
timer:start(0, 1000, vim.schedule_wrap(function()
git_branch:start()
end))

local function Branch()
if not branch or #branch == 0 then return '' end
local ok,devicons = pcall(require,'nvim-web-devicons')
if ok then
local icon = devicons.get_icon('git')
Expand Down

0 comments on commit f62c0d4

Please sign in to comment.