refactor: use a table for status codes (#79)

Co-authored-by: PFiS <pfis1737@gmail.com>
This commit is contained in:
三咲雅 · Misaki Masa
2025-03-04 14:33:25 +08:00
committed by GitHub
parent 3d0c7c6be9
commit a1b678dfac

View File

@@ -1,27 +1,37 @@
--- @since 25.2.7
local WIN = ya.target_family() == "windows"
local PATS = {
{ "[MT]", 6 }, -- Modified
{ "[AC]", 5 }, -- Added
{ "?$", 4 }, -- Untracked
{ "!$", 3 }, -- Ignored
{ "D", 2 }, -- Deleted
{ "U", 1 }, -- Updated
{ "[AD][AD]", 1 }, -- Updated
local WINDOWS = ya.target_family() == "windows"
local CODES = {
modified = 6,
added = 5,
untracked = 4,
ignored = 3, -- ignored file
excluded = 30, -- ignored directory
deleted = 2,
updated = 1,
unknown = 0,
}
local PATTERNS = {
{ "[MT]", CODES.modified },
{ "[AC]", CODES.added },
{ "?$", CODES.untracked },
{ "!$", CODES.ignored },
{ "D", CODES.deleted },
{ "U", CODES.updated },
{ "[AD][AD]", CODES.updated },
}
local function match(line)
local signs = line:sub(1, 2)
for _, p in ipairs(PATS) do
for _, p in ipairs(PATTERNS) do
local path
if signs:find(p[1]) then
path = line:sub(4, 4) == '"' and line:sub(5, -2) or line:sub(4)
path = WIN and path:gsub("/", "\\") or path
path = WINDOWS and path:gsub("/", "\\") or path
end
if not path then
elseif path:find("[/\\]$") then
return p[2] == 3 and 30 or p[2], path:sub(1, -2)
return p[2] == CODES.ignored and CODES.excluded or p[2], path:sub(1, -2)
else
return p[2], path
end
@@ -51,11 +61,11 @@ end
local function bubble_up(changed)
local new, empty = {}, Url("")
for k, v in pairs(changed) do
if v ~= 3 and v ~= 30 then
if v ~= CODES.ignored and v ~= CODES.excluded then
local url = Url(k):parent()
while url and url ~= empty do
local s = tostring(url)
new[s] = (new[s] or 0) > v and new[s] or v
new[s] = (new[s] or CODES.unknown) > v and new[s] or v
url = url:parent()
end
end
@@ -66,11 +76,11 @@ end
local function propagate_down(ignored, cwd, repo)
local new, rel = {}, cwd:strip_prefix(repo)
for k, v in pairs(ignored) do
if v == 30 then
if v == CODES.excluded then
if rel:starts_with(k) then
new[tostring(repo:join(rel))] = 30
new[tostring(repo:join(rel))] = CODES.excluded
elseif cwd == repo:join(k):parent() then
new[k] = 3
new[k] = CODES.ignored
end
end
end
@@ -81,9 +91,9 @@ local add = ya.sync(function(st, cwd, repo, changed)
st.dirs[cwd] = repo
st.repos[repo] = st.repos[repo] or {}
for k, v in pairs(changed) do
if v == 0 then
if v == CODES.unknown then
st.repos[repo][k] = nil
elseif v == 30 then
elseif v == CODES.excluded then
st.dirs[k] = ""
else
st.repos[repo][k] = v
@@ -122,20 +132,20 @@ local function setup(st, opts)
-- Chosen by ChatGPT fairly, PRs are welcome to adjust them
local t = THEME.git or {}
local styles = {
[6] = t.modified and ui.Style(t.modified) or ui.Style():fg("#ffa500"),
[5] = t.added and ui.Style(t.added) or ui.Style():fg("#32cd32"),
[4] = t.untracked and ui.Style(t.untracked) or ui.Style():fg("#a9a9a9"),
[3] = t.ignored and ui.Style(t.ignored) or ui.Style():fg("#696969"),
[2] = t.deleted and ui.Style(t.deleted) or ui.Style():fg("#ff4500"),
[1] = t.updated and ui.Style(t.updated) or ui.Style():fg("#1e90ff"),
[CODES.modified] = t.modified and ui.Style(t.modified) or ui.Style():fg("#ffa500"),
[CODES.added] = t.added and ui.Style(t.added) or ui.Style():fg("#32cd32"),
[CODES.untracked] = t.untracked and ui.Style(t.untracked) or ui.Style():fg("#a9a9a9"),
[CODES.ignored] = t.ignored and ui.Style(t.ignored) or ui.Style():fg("#696969"),
[CODES.deleted] = t.deleted and ui.Style(t.deleted) or ui.Style():fg("#ff4500"),
[CODES.updated] = t.updated and ui.Style(t.updated) or ui.Style():fg("#1e90ff"),
}
local signs = {
[6] = t.modified_sign and t.modified_sign or "",
[5] = t.added_sign and t.added_sign or "",
[4] = t.untracked_sign and t.untracked_sign or "",
[3] = t.ignored_sign and t.ignored_sign or "",
[2] = t.deleted_sign and t.deleted_sign or "",
[1] = t.updated_sign and t.updated_sign or "U",
[CODES.modified] = t.modified_sign or "",
[CODES.added] = t.added_sign or "",
[CODES.untracked] = t.untracked_sign or "",
[CODES.ignored] = t.ignored_sign or "",
[CODES.deleted] = t.deleted_sign or "",
[CODES.updated] = t.updated_sign or "U",
}
Linemode:children_add(function(self)
@@ -143,7 +153,7 @@ local function setup(st, opts)
local dir = st.dirs[tostring(url:parent())]
local change
if dir then
change = dir == "" and 3 or st.repos[dir][tostring(url):sub(#dir + 2)]
change = dir == "" and CODES.ignored or st.repos[dir][tostring(url):sub(#dir + 2)]
end
if not change or signs[change] == "" then
@@ -183,7 +193,7 @@ local function fetch(_, job)
local changed, ignored = {}, {}
for line in output.stdout:gmatch("[^\r\n]+") do
local sign, path = match(line)
if sign == 30 then
if sign == CODES.excluded then
ignored[path] = sign
else
changed[path] = sign
@@ -199,7 +209,7 @@ local function fetch(_, job)
for _, p in ipairs(paths) do
local s = p:sub(#repo + 2)
changed[s] = changed[s] or 0
changed[s] = changed[s] or CODES.unknown
end
add(tostring(cwd), repo, changed)