Fish, simplify functions, move n alias call

Simplifies the git_ family of functions to make fish_right_prompt more
readable. git_prompt function is added, which prints the relevant git
prompt information. The git_ family of functions is moved into the
git_prompt function, and fish_right_prompt calls git_prompt

Going forward I'm trying to have modules in charge of the data for
other modules, so I moved the nnn alias call from fish to n, and added a
handy "extraFunctions" config option to fish. Maintaining the fish
module's control of the functions, but allowing others to add stuff in
This commit is contained in:
Julia Lange 2024-04-21 01:19:38 -07:00
parent 0336466d64
commit 9978fb7921
13 changed files with 96 additions and 130 deletions

View file

@ -0,0 +1,69 @@
function git_is_repo
test -d .git
or begin
set -l info (command git rev-parse --git-dir --is-bare-repository 2>/dev/null)
and test $info[2] = false
end
end
function git_is_worktree
git_is_repo
and test (command git rev-parse --is-inside-git-dir) = false
end
function git_is_dirty
git_is_worktree; and not command git diff --no-ext-diff --quiet --exit-code
end
function git_is_staged
git_is_repo; and begin
not command git diff --cached --no-ext-diff --quiet --exit-code
end
end
function git_branch_name
git_is_repo; and begin
command git symbolic-ref --short HEAD 2> /dev/null;
or command git show-ref --head -s --abbrev | head -n1 2> /dev/null
end
end
function git_is_touched
git_is_worktree; and begin
# The first checks for staged changes, the second for unstaged ones.
# We put them in this order because checking staged changes is *fast*.
not command git diff-index --cached --quiet HEAD -- >/dev/null 2>&1
or not command git diff --no-ext-diff --quiet --exit-code >/dev/null 2>&1
end
end
if git_is_repo
echo -n -s (set_color yellow) (git_branch_name) (set_color normal)
set -l git_meta ""
if test (command git ls-files --others --exclude-standard | wc -w 2> /dev/null) -gt 0
set git_meta "$git_meta?"
end
if test (command git rev-list --walk-reflogs --count refs/stash 2> /dev/null)
set git_meta "$git_meta\$"
end
if git_is_touched
git_is_dirty && set git_meta "$git_meta"
git_is_staged && set git_meta "$git_meta"
end
set -l commit_count (command git rev-list --count --left-right (git remote)/(git_branch_name)"...HEAD" 2> /dev/null)
if test $commit_count
set -l behind (echo $commit_count | cut -f 1)
set -l ahead (echo $commit_count | cut -f 2)
if test $behind -gt 0
set git_meta "$git_meta🠋"
end
if test $ahead -gt 0
set git_meta "$git_meta🠉"
end
end
if test $git_meta
echo -n -s (set_color red) " " $git_meta " " (set_color normal)
else
echo -n -s " "
end
end