Merge pull request #1834 from b0bh00d/master

Refactored the Mercurial prompt code to be more efficient.
This commit is contained in:
Benjamin Staneck 2018-09-13 18:07:07 +02:00 committed by GitHub
commit 35eab7a51a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

39
vendor/clink.lua vendored
View File

@ -285,31 +285,40 @@ end
local function hg_prompt_filter() local function hg_prompt_filter()
local result = ""
local hg_dir = get_hg_dir()
if hg_dir then
-- Colors for mercurial status -- Colors for mercurial status
local colors = { local colors = {
clean = "\x1b[1;37;40m", clean = "\x1b[1;37;40m",
dirty = "\x1b[31;1m", dirty = "\x1b[31;1m",
} }
if get_hg_dir() then -- 'hg id' gives us BOTH the branch name AND an indicator that there
-- if we're inside of mercurial repo then try to detect current branch -- are uncommitted changes, in one fast(er) call
local branch = get_hg_branch() local pipe = io.popen("hg id 2>&1")
local color local output = pipe:read('*all')
if branch then local rc = { pipe:close() }
-- Has branch => therefore it is a mercurial folder, now figure out status
if get_hg_status() then
color = colors.clean
else
color = colors.dirty
end
clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", color.."("..branch..")") if output ~= nil and
return false string.sub(output,1,7) ~= "abort: " and -- not an HG working copy
string.sub(output,1,12) ~= "000000000000" and -- empty wc (needs update)
(not string.find(output, "is not recognized")) then -- 'hg' not in path
local color = colors.clean
-- split elements on space delimiter
local items = {}
for i in string.gmatch(output, "%S+") do
table.insert(items, i)
end
-- if the repo hash ends with '+', the wc has uncommitted changes
if string.sub(items[1], -1, -1) == "+" then color = colors.dirty end
-- substitute the branch in directly -- already WITH parentheses. :)
result = color .. items[2] -- string.sub(items[2], 1, string.len(items[2]) - 1)
end end
end end
-- No mercurial present or not in mercurial file clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", result)
clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", "")
return false return false
end end