refactor(git): rework module around clearer Status and Repo split
This commit is contained in:
+42
-6
@@ -1,11 +1,11 @@
|
||||
local M = {}
|
||||
|
||||
---@class ow.Git.ScratchOpts
|
||||
---@class ow.Git.Util.ScratchOpts
|
||||
---@field name string?
|
||||
---@field bufhidden ("hide"|"wipe")?
|
||||
|
||||
---@param buf integer
|
||||
---@param opts ow.Git.ScratchOpts
|
||||
---@param opts ow.Git.Util.ScratchOpts
|
||||
local function setup_scratch(buf, opts)
|
||||
vim.bo[buf].buftype = "nofile"
|
||||
vim.bo[buf].bufhidden = opts.bufhidden or "wipe"
|
||||
@@ -43,10 +43,10 @@ function M.place_buf(buf, split)
|
||||
return win
|
||||
end
|
||||
|
||||
---@class ow.Git.NewScratchOpts : ow.Git.ScratchOpts
|
||||
---@class ow.Git.Util.NewScratchOpts : ow.Git.Util.ScratchOpts
|
||||
---@field split (false|"above"|"below"|"left"|"right")?
|
||||
|
||||
---@param opts ow.Git.NewScratchOpts?
|
||||
---@param opts ow.Git.Util.NewScratchOpts?
|
||||
---@return integer buf
|
||||
---@return integer win
|
||||
function M.new_scratch(opts)
|
||||
@@ -170,14 +170,14 @@ function M.debounce(fn, delay)
|
||||
}
|
||||
end
|
||||
|
||||
---@class ow.Git.ExecOpts
|
||||
---@class ow.Git.Util.ExecOpts
|
||||
---@field cwd string?
|
||||
---@field stdin string?
|
||||
---@field silent boolean?
|
||||
---@field on_done fun(stdout: string?)?
|
||||
|
||||
---@param cmd string[]
|
||||
---@param opts ow.Git.ExecOpts?
|
||||
---@param opts ow.Git.Util.ExecOpts?
|
||||
---@return string?
|
||||
function M.exec(cmd, opts)
|
||||
opts = opts or {}
|
||||
@@ -209,4 +209,40 @@ function M.exec(cmd, opts)
|
||||
return handle(vim.system(cmd, sys_opts):wait())
|
||||
end
|
||||
|
||||
---@class ow.Git.Util.Emitter<T>
|
||||
---@field private _listeners table<T, (fun(...))[]>
|
||||
local Emitter = {}
|
||||
Emitter.__index = Emitter
|
||||
|
||||
---@return ow.Git.Util.Emitter
|
||||
function Emitter.new()
|
||||
return setmetatable({ _listeners = {} }, Emitter)
|
||||
end
|
||||
|
||||
---@param event T
|
||||
---@param fn fun(...)
|
||||
---@return fun() unsubscribe
|
||||
function Emitter:on(event, fn)
|
||||
local list = self._listeners[event] or {}
|
||||
self._listeners[event] = list
|
||||
table.insert(list, fn)
|
||||
return function()
|
||||
for i, f in ipairs(list) do
|
||||
if f == fn then
|
||||
table.remove(list, i)
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param event T
|
||||
function Emitter:emit(event, ...)
|
||||
for _, fn in ipairs(self._listeners[event] or {}) do
|
||||
fn(...)
|
||||
end
|
||||
end
|
||||
|
||||
M.Emitter = Emitter
|
||||
|
||||
return M
|
||||
|
||||
Reference in New Issue
Block a user