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