Skip to content

Instantly share code, notes, and snippets.

@jonasem
Last active November 19, 2022 22:14
Show Gist options
  • Save jonasem/e8b3a6258a495946df12 to your computer and use it in GitHub Desktop.
Save jonasem/e8b3a6258a495946df12 to your computer and use it in GitHub Desktop.
git status peek in prompt for clink
---
-- Find out current branch
-- @return {false|git branch name}
---
function get_git_branch()
for line in io.popen("git branch 2>nul"):lines() do
local m = line:match("%* (.+)$")
if m then
return m
end
end
return false
end
---
-- Get the status of working dir
-- @return {bool}
---
function get_git_status()
local status = {
anyChanges = false,
indexAdded = 0,
indexModified = 0,
indexDeleted = 0,
filesAdded = 0,
filesModified = 0,
filesDeleted = 0
}
for line in io.popen("git -c color.status=false status --short 2>nul"):lines() do
if line:match("^A(.+)$") then status.indexAdded = status.indexAdded + 1 end
if line:match("^M(.+)$") then status.indexModified = status.indexModified + 1 end
if line:match("^R(.+)$") then status.indexModified = status.indexModified + 1 end
if line:match("^C(.+)$") then status.indexModified = status.indexModified + 1 end
if line:match("^D(.+)$") then status.indexDeleted = status.indexDeleted + 1 end
if line:match("^%?(.+)$") then status.filesAdded = status.filesAdded + 1 end
if line:match("^ A(.+)$") then status.filesAdded = status.filesAdded + 1 end
if line:match("^ M(.+)$") then status.filesModified = status.filesModified + 1 end
if line:match("^ R(.+)$") then status.filesModified = status.filesModified + 1 end
if line:match("^ C(.+)$") then status.filesModified = status.filesModified + 1 end
if line:match("^ D(.+)$") then status.filesDeleted = status.filesDeleted + 1 end
status.anyChanges = true
end
return status
end
function git_prompt_filter()
-- Colors for git status
local colors = {
clean = "\x1b[1;37;40m",
red = "\x1b[31;1m",
green = "\x1b[32;1m",
}
local branch = get_git_branch()
if branch then
-- Has branch => therefore it is a git folder, now figure out status
local status = get_git_status()
if not status.anyChanges then
clink.prompt.value = string.gsub(clink.prompt.value, "{git}", colors.clean.."("..branch..")")
else
-- for k,v in pairs(status) do print(k,v) end
clink.prompt.value = string.gsub(clink.prompt.value, "{git}", string.format("%s(%s %s+%d ~%d -%d%s | %s+%d ~%d -%d%s)",
colors.clean,
branch,
colors.green,
status.indexAdded,
status.indexModified,
status.indexDeleted,
colors.clean,
colors.red,
status.filesAdded,
status.filesModified,
status.filesDeleted,
colors.clean))
end
return true
end
-- No git present or not in git file
clink.prompt.value = string.gsub(clink.prompt.value, "{git}", "")
return false
end
clink.prompt.register_filter(git_prompt_filter, 50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment