feat: make the git status signs more intuitive, closes #18

This commit is contained in:
sxyazi
2024-09-01 10:01:46 +08:00
parent 2b001d2cf3
commit 0c32c6eefd

View File

@@ -1,25 +1,29 @@
local PATS = { local PATS = {
{ "!$", "." }, -- Ignored
{ "?$", "?" }, -- Untracked
{ "U", "U" }, -- Updated
{ "[AD][AD]", "U" }, -- Updated
{ "[MT]", "M" }, -- Modified { "[MT]", "M" }, -- Modified
{ "[AC]", "A" }, -- Added { "[AC]", "A" }, -- Added
{ "?$", "?" }, -- Untracked
{ "D", "D" }, -- Deleted
{ "U", "U" }, -- Updated
{ "[AD][AD]", "U" }, -- Updated
} }
local PRIOS = { local WEIGHTS = {
["M"] = 6,
["A"] = 5,
["?"] = 4,
["D"] = 3,
["U"] = 2,
[""] = 1, [""] = 1,
["."] = 2,
["?"] = 3,
["U"] = 4,
["M"] = 5,
["A"] = 6,
} }
local function match(s) local function match(line)
local signs = line:sub(1, 2)
for _, p in ipairs(PATS) do for _, p in ipairs(PATS) do
if s:find(p[1]) then if not signs:find(p[1]) then
return p[2] elseif line:sub(4, 4) == '"' then
return p[2], line:sub(5, -2)
else
return p[2], line:sub(4)
end end
end end
end end
@@ -45,11 +49,11 @@ end)
local function setup(st, opts) local function setup(st, opts)
st.states = {} st.states = {}
local styles = { local styles = {
["."] = THEME.git_ignored and ui.Style(THEME.git_ignored) or ui.Style():fg("gray"), ["M"] = THEME.git_modified and ui.Style(THEME.git_modified) or ui.Style():fg("blue"),
["?"] = THEME.git_staged and ui.Style(THEME.git_staged) or ui.Style():fg("yellow"), ["A"] = THEME.git_added and ui.Style(THEME.git_added) or ui.Style():fg("green"),
["U"] = THEME.git_untracked and ui.Style(THEME.git_untracked) or ui.Style():fg("blue"), ["?"] = THEME.git_untracked and ui.Style(THEME.git_untracked) or ui.Style():fg("yellow"),
["M"] = THEME.git_modified and ui.Style(THEME.git_modified) or ui.Style():fg("red"), ["D"] = THEME.git_deleted and ui.Style(THEME.git_deleted) or ui.Style():fg("red"),
["A"] = THEME.git_deleted and ui.Style(THEME.git_deleted) or ui.Style():fg("green"), ["U"] = THEME.git_updated and ui.Style(THEME.git_updated) or ui.Style():fg("blue"),
} }
Linemode:children_add(function(self) Linemode:children_add(function(self)
@@ -71,7 +75,7 @@ local function fetch(self)
local cwd = self.files[1].url:parent() local cwd = self.files[1].url:parent()
local output, err = Command("git") local output, err = Command("git")
:cwd(tostring(cwd)) :cwd(tostring(cwd))
:args({ "-c", "core.quotePath=", "status", "--porcelain", "-unormal", "--no-renames", "--ignored=matching" }) :args({ "-c", "core.quotePath=", "status", "--porcelain", "-unormal", "--no-renames" })
:args(paths) :args(paths)
:stdout(Command.PIPED) :stdout(Command.PIPED)
:output() :output()
@@ -87,11 +91,12 @@ local function fetch(self)
local states = {} local states = {}
for line in output.stdout:gmatch("[^\r\n]+") do for line in output.stdout:gmatch("[^\r\n]+") do
local s = match(line:sub(1, 2)) local sign, path = match(line)
if s and line:find("[/\\]$") then if not sign then
states[prefix .. line:sub(4, -2)] = s elseif path:find("[/\\]$") then
states[prefix .. path:sub(1, -2)] = sign
else else
states[prefix .. line:sub(4)] = s states[prefix .. path] = sign
end end
end end
@@ -102,7 +107,7 @@ local function fetch(self)
local url = Url(k):parent() local url = Url(k):parent()
while url and url ~= prefix do while url and url ~= prefix do
local s = tostring(url) local s = tostring(url)
parents[s] = (PRIOS[parents[s]] or 0) > PRIOS[v] and parents[s] or v parents[s] = (WEIGHTS[parents[s]] or 0) > WEIGHTS[v] and parents[s] or v
url = url:parent() url = url:parent()
end end
end end