1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-09-11 04:46:05 +03:00

updated displaying git branch in modeline

Noah Graff 2019-10-02 14:00:05 -04:00
parent f81146dde8
commit 3984833e3b

@ -113,42 +113,34 @@ hook global WinCreate .* %{
## Git branch integration
Here's another [example by lenormf](https://github.com/mawww/kakoune/issues/657) on how to add more info to the modeline, like the current git branch (status bar)
Here's an example on how to add more info to the modeline, like the current git branch.
```
##
## git-branch.kak by lenormf
## Store the current git branch that contains the buffer
##
declare-option -docstring "name of the git branch holding the current buffer" \
str modeline_git_branch
hook global WinCreate .* %{
hook window NormalIdle .* %{ evaluate-commands %sh{
branch=$(cd "$(dirname "${kak_buffile}")" && git rev-parse --abbrev-ref HEAD 2>/dev/null)
if [ -n "${branch}" ]; then
printf 'set window modeline_git_branch %%{%s}' "${branch}"
fi
} }
}
```
The git-branch.kak file is [from lenormf here](https://github.com/lenormf/kakoune-extra/blob/master/widgets/git-branch.kak). You can use option in your modelinefmt with something like the following hook
```
## customize the status bar
hook global BufOpen .*/?[^*].+ %{
%sh{
function add_hook_on_idle {
echo "hook global NormalIdle .* %{ $@ }"
}
hook global WinCreate .* %{ evaluate-commands %sh{
is_work_tree=$(cd "$(dirname "${kak_buffile}")" && git rev-parse --is-inside-work-tree 2>/dev/null)
if [ "${is_work_tree}" = 'true' ]; then
printf 'set-option window modelinefmt %%{%s}' " %opt{modeline_git_branch} ${kak_opt_modelinefmt}"
fi
}}
```
tools=(
## cursor coordinates in the buffer
"%val{cursor_line}:%val{cursor_char_column}"
## filetype of the buffer detected by kak
"%opt{filetype}"
## name of the buffer
"%val{bufname}"
)
fmt_line=""
## current git branch
if which git 1>/dev/null; then
echo "decl str modeline-git-branch"
tools[${#tools[@]}]="%opt{modeline-git-branch}"
add_hook_on_idle "%sh{
branch=\$(cd \$(readlink -e \$(dirname \${kak_bufname})) && git rev-parse --abbrev-ref HEAD 2>/dev/null)
test -n \"\${branch}\" && echo \"set global modeline-git-branch '\${branch}'\"
}"
fi
for t in "${tools[@]}"; do
test -z "${t}" && continue
test -n "${fmt_line}" && fmt_line="${t} ${fmt_line}" || fmt_line="${t}"
done
echo set global modelinefmt "'${fmt_line}'"
}
}
```
You can use this strategy to add many different custom modules to your modelinefmt easily.