refactor(git/status): rework entries into typed variants on porcelain v2
This commit is contained in:
+17
-35
@@ -1,47 +1,29 @@
|
||||
local repo = require("git.repo")
|
||||
local status = require("git.status")
|
||||
local util = require("git.util")
|
||||
|
||||
local M = {}
|
||||
|
||||
---@class ow.Git.Statusline.Status
|
||||
---@field head string?
|
||||
---@field entries ow.Git.Status.Entry[]
|
||||
---@field unstaged boolean
|
||||
---@field staged boolean
|
||||
---@field conflict boolean
|
||||
---@field entry ow.Git.Status.Entry?
|
||||
|
||||
---@param entries ow.Git.Status.Entry[]
|
||||
---@param head string?
|
||||
---@return ow.Git.Statusline.Status
|
||||
local function build(entries, head)
|
||||
local out = {
|
||||
head = head,
|
||||
entries = entries,
|
||||
unstaged = false,
|
||||
staged = false,
|
||||
conflict = false,
|
||||
}
|
||||
for _, e in ipairs(entries) do
|
||||
if e.kind == "unstaged" or e.kind == "untracked" then
|
||||
out.unstaged = true
|
||||
elseif e.kind == "staged" then
|
||||
out.staged = true
|
||||
elseif e.kind == "unmerged" then
|
||||
out.conflict = true
|
||||
end
|
||||
end
|
||||
return out
|
||||
end
|
||||
|
||||
---@param entries ow.Git.Status.Entry[]
|
||||
---@param entry ow.Git.Status.Entry?
|
||||
---@return string
|
||||
local function render(entries)
|
||||
if #entries == 0 then
|
||||
local function render(entry)
|
||||
if not entry then
|
||||
return ""
|
||||
end
|
||||
local marks = status.marks_for(entry)
|
||||
if #marks == 0 then
|
||||
return ""
|
||||
end
|
||||
local parts = {}
|
||||
for _, e in ipairs(entries) do
|
||||
table.insert(parts, string.format("%%#%s#%s%%*", e.hl, e.char))
|
||||
for _, mark in ipairs(marks) do
|
||||
table.insert(
|
||||
parts,
|
||||
string.format("%%#%s#%s%%*", mark.hl, mark.char)
|
||||
)
|
||||
end
|
||||
return table.concat(parts, " ")
|
||||
end
|
||||
@@ -70,9 +52,9 @@ local function update_buf(buf, r)
|
||||
if not rel then
|
||||
return clear(buf)
|
||||
end
|
||||
local entries = r.status.entries[rel] or {}
|
||||
vim.b[buf].git_status = build(entries, r:head())
|
||||
vim.b[buf].git_status_string = render(entries)
|
||||
local entry = r.status.entries[rel]
|
||||
vim.b[buf].git_status = { head = r:head(), entry = entry }
|
||||
vim.b[buf].git_status_string = render(entry)
|
||||
end
|
||||
|
||||
local enabled = false
|
||||
|
||||
Reference in New Issue
Block a user