Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactored the Mercurial prompt code to be more efficient. #1834

Merged
merged 1 commit into from
Sep 13, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions vendor/clink.lua
Original file line number Diff line number Diff line change
Expand Up @@ -285,31 +285,40 @@ end

local function hg_prompt_filter()

-- Colors for mercurial status
local colors = {
clean = "\x1b[1;37;40m",
dirty = "\x1b[31;1m",
}

if get_hg_dir() then
-- if we're inside of mercurial repo then try to detect current branch
local branch = get_hg_branch()
local color
if branch then
-- Has branch => therefore it is a mercurial folder, now figure out status
if get_hg_status() then
color = colors.clean
else
color = colors.dirty
local result = ""

local hg_dir = get_hg_dir()
if hg_dir then
-- Colors for mercurial status
local colors = {
clean = "\x1b[1;37;40m",
dirty = "\x1b[31;1m",
}

-- 'hg id' gives us BOTH the branch name AND an indicator that there
-- are uncommitted changes, in one fast(er) call
local pipe = io.popen("hg id 2>&1")
local output = pipe:read('*all')
local rc = { pipe:close() }

if output ~= nil and
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

clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", color.."("..branch..")")
return false
-- 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

-- No mercurial present or not in mercurial file
clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", "")
clink.prompt.value = string.gsub(clink.prompt.value, "{hg}", result)
return false
end

Expand Down