Skip to content

Instantly share code, notes, and snippets.

@maple3142
Last active September 26, 2021 14:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maple3142/e553226d697e75c34518f3ca7fabec9e to your computer and use it in GitHub Desktop.
Save maple3142/e553226d697e75c34518f3ca7fabec9e to your computer and use it in GitHub Desktop.
clink show git branch
function show_git_branch()
for line in io.popen("git branch 2>nul"):lines() do
local m = line:match("%* (.+)$")
local b = "\x1b[32;22;49m".."("..m..")".."\x1b[39;22;49m"
if m then
clink.prompt.value = clink.prompt.value:gsub(">"," "..b.." >")
break
end
end
return false
end
clink.prompt.register_filter(show_git_branch, 50)
@zodman
Copy link

zodman commented Nov 6, 2019

this need a small doc to how to install

@maple3142
Copy link
Author

@zodman Go to your clink installation dir and find clink.lua, then paste this code at the bottom.

@keocra
Copy link

keocra commented Nov 28, 2019

@zodman Nice work. For me a small modification was necessary because I was running into the following issue:

clink.lua:3435: attempt to concatenate local 'm' (a nil value)

I think it should be (at least that works for me):

function show_git_branch()
	for line in io.popen("git branch 2>nul"):lines() do
		local m = line:match("%* (.+)$")
		if m then
			local b = "\x1b[32;22;49m".."("..m..")".."\x1b[39;22;49m"
			clink.prompt.value = clink.prompt.value:gsub(">"," "..b.." >")
			break
		end
	end
	return false
end

clink.prompt.register_filter(show_git_branch, 50)

@zodman
Copy link

zodman commented Nov 28, 2019

@keocra your code works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment