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

@ -13,5 +13,8 @@
NNN_FIFO = "/tmp/nnn.fifo"; NNN_FIFO = "/tmp/nnn.fifo";
NNN_TRASH = lib.mkIf config.trash.enable "1"; NNN_TRASH = lib.mkIf config.trash.enable "1";
}; };
fish.extraFunctions = lib.mkIf config.fish.enable {
n = ''${builtins.readFile ./nnn_fish_function.fish}'';
};
}; };
} }

View file

@ -3,43 +3,32 @@
{ {
options.fish = { options.fish = {
enable = lib.mkEnableOption "Enables fish"; enable = lib.mkEnableOption "Enables fish";
extraFunctions = lib.mkOption {
type = with lib.types; attrsOf lines;
default = {};
};
}; };
config = lib.mkIf config.fish.enable { config = lib.mkIf config.fish.enable {
programs.fish.enable = true; programs.fish = {
enable = true;
programs.fish.functions = { functions = {
fish_greeting = ""; fish_greeting = "";
fish_prompt = '' fish_prompt = ''
${builtins.readFile ./functions/fish_prompt.fish} if test $status -eq 0
''; echo -n -s (set_color blue -o) τ " " (set_color normal)
fish_right_prompt = '' else
${builtins.readFile ./functions/fish_right_prompt.fish} echo -n -s (set_color red -o) τ " " (set_color normal)
''; end
git_branch_name = '' '';
${builtins.readFile ./functions/git_branch_name.fish} fish_right_prompt = ''
''; git_prompt
git_is_dirty = '' echo -n -s (set_color blue) (prompt_pwd) " "
${builtins.readFile ./functions/git_is_dirty.fish} echo -n -s (set_color yellow) $CMD_DURATION ms
''; echo -n -s (set_color normal)
git_is_repo = '' '';
${builtins.readFile ./functions/git_is_repo.fish} git_prompt = ''${builtins.readFile ./functions/git_prompt.fish}'';
''; } // config.fish.extraFunctions;
git_is_staged = ''
${builtins.readFile ./functions/git_is_staged.fish}
'';
git_is_touched = ''
${builtins.readFile ./functions/git_is_touched.fish}
'';
git_is_worktree = ''
${builtins.readFile ./functions/git_is_worktree.fish}
'';
ssh = ''
${builtins.readFile ./functions/ssh.fish}
'';
n = lib.mkIf config.nnn.enable ''
${builtins.readFile ./functions/n.fish}
'';
}; };
}; };
} }

View file

@ -1,13 +0,0 @@
set -l last_command_status $status
set -l symbol 'τ'
set -l normal_color (set_color normal)
set -l symbol_color (set_color blue -o)
set -l error_color (set_color red -o)
if test $last_command_status -eq 0
echo -n -s $symbol_color $symbol " " $normal_color
else
echo -n -s $error_color $symbol " " $normal_color
end

View file

@ -1,61 +0,0 @@
set -l cwd
set -l cwd_color (set_color blue)
set -l normal_color (set_color normal)
set -l branch_color (set_color yellow)
set -l meta_color (set_color red)
if git_is_repo
echo -n -s $branch_color (git_branch_name) $normal_color
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 $meta_color " " $git_meta " " $normal_color
else
echo -n -s " "
end
set root_folder (command git rev-parse --show-toplevel 2> /dev/null)
set parent_root_folder (dirname $root_folder)
set cwd (echo $PWD | sed -e "s|$parent_root_folder/||")
else
set cwd (prompt_pwd)
end
echo -n -s $cwd_color "$cwd"
set_color --dim
set -l S (math $CMD_DURATION/1000)
set -l M (math $S/60)
echo -n -s " "
if test $M -gt 1
echo -n -s $M m
else if test $S -gt 1
echo -n -s $S s
else
echo -n -s $CMD_DURATION ms
end
set_color normal

View file

@ -1,4 +0,0 @@
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

View file

@ -1 +0,0 @@
git_is_worktree; and not command git diff --no-ext-diff --quiet --exit-code

View file

@ -1,5 +0,0 @@
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

View file

@ -1,3 +0,0 @@
git_is_repo; and begin
not command git diff --cached --no-ext-diff --quiet --exit-code
end

View file

@ -1,6 +0,0 @@
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

View file

@ -1,2 +0,0 @@
git_is_repo
and test (command git rev-parse --is-inside-git-dir) = false

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

View file

@ -15,7 +15,7 @@
manpages.enable = true; manpages.enable = true;
trash.enable = true; trash.enable = true;
zsh.enable = true; fish.enable = true;
kitty.enable = true; kitty.enable = true;
kitty.font = "Cascadia Code"; kitty.font = "Cascadia Code";
git.enable = true; git.enable = true;