From 9fd5cf781ad3bc47890ecd70b14d49bb493ace64 Mon Sep 17 00:00:00 2001 From: Eli Ribble Date: Wed, 12 Mar 2025 09:25:31 -0700 Subject: [PATCH] Add custom fish shell that shows whether I'm in nix-shell --- nalai/etc/nixos/configuration.nix | 1 + .../config/fish/functions/fish_prompt.fish | 151 ++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 nalai/etc/nixos/home/eliribble/config/fish/functions/fish_prompt.fish diff --git a/nalai/etc/nixos/configuration.nix b/nalai/etc/nixos/configuration.nix index a6c529a..c94d4bf 100644 --- a/nalai/etc/nixos/configuration.nix +++ b/nalai/etc/nixos/configuration.nix @@ -112,6 +112,7 @@ # The home.stateVersion option does not have a default and must be set home.stateVersion = "24.11"; home.username = "eliribble"; + home.file.".config/fish/functions/fish_prompt.fish".source = ./home/eliribble/config/fish/functions/fish_prompt.fish; home.file.".config/nvim/after/ftplugin/html.vim".source = ./home/eliribble/config/nvim/after/ftplugin/html.vim; home.file.".config/nvim/after/ftplugin/go.vim".source = ./home/eliribble/config/nvim/after/ftplugin/go.vim; home.file.".config/tmux/tmux.conf".source = ./home/eliribble/config/tmux/tmux.conf; diff --git a/nalai/etc/nixos/home/eliribble/config/fish/functions/fish_prompt.fish b/nalai/etc/nixos/home/eliribble/config/fish/functions/fish_prompt.fish new file mode 100644 index 0000000..1bcbff8 --- /dev/null +++ b/nalai/etc/nixos/home/eliribble/config/fish/functions/fish_prompt.fish @@ -0,0 +1,151 @@ +set -l nix_shell_info ( +if test -n "$IN_NIX_SHELL" +echo -n " " +end +) + +# Based on the 'nim' prompt from +# https://github.com/fish-shell/fish-shell/blob/master/share/tools/web_config/sample_prompts/nim.fish +function fish_prompt + # This prompt shows: + # - green lines if the last return command is OK, red otherwise + # - your user name, in red if root or yellow otherwise + # - your hostname, in cyan if ssh or blue otherwise + # - the current path (with prompt_pwd) + # - date +%X + # - the current virtual environment, if any + # - the current git status, if any, with fish_git_prompt + # - the current battery state, if any, and if your power cable is unplugged, and if you have "acpi" + # - current background jobs, if any + + # It goes from: + # ┬─[nim@Hattori:~]─[11:39:00] + # ╰─>$ echo here + + # To: + # ┬─[nim@Hattori:~/w/dashboard]─[11:37:14]─[V:django20]─[G:master↑1|●1✚1…1]─[B:85%, 05:41:42 remaining] + # │ 2 15054 0% arrêtée sleep 100000 + # │ 1 15048 0% arrêtée sleep 100000 + # ╰─>$ echo there + + set -l retc red + test $status = 0; and set retc green + + set -q __fish_git_prompt_showupstream + or set -g __fish_git_prompt_showupstream auto + + function _nim_prompt_wrapper + set retc $argv[1] + set -l field_name $argv[2] + set -l field_value $argv[3] + + set_color normal + set_color $retc + echo -n '─' + set_color -o green + echo -n '[' + set_color normal + test -n $field_name + and echo -n $field_name: + set_color $retc + echo -n $field_value + set_color -o green + echo -n ']' + end + + set_color $retc + echo -n '┬─' + set_color -o green + echo -n [ + + if functions -q fish_is_root_user; and fish_is_root_user + set_color -o red + else + set_color -o yellow + end + + echo -n $USER + set_color -o white + echo -n @ + + if test -z "$SSH_CLIENT" + set_color -o blue + else + set_color -o cyan + end + + echo -n (prompt_hostname) + set_color -o white + echo -n :(prompt_pwd) + set_color -o green + echo -n ']' + + # Date + _nim_prompt_wrapper $retc '' (date +%X) + + # Vi-mode + # The default mode prompt would be prefixed, which ruins our alignment. + function fish_mode_prompt + end + + if test "$fish_key_bindings" = fish_vi_key_bindings + or test "$fish_key_bindings" = fish_hybrid_key_bindings + set -l mode + switch $fish_bind_mode + case default + set mode (set_color --bold red)N + case insert + set mode (set_color --bold green)I + case replace_one + set mode (set_color --bold green)R + echo '[R]' + case replace + set mode (set_color --bold cyan)R + case visual + set mode (set_color --bold magenta)V + end + set mode $mode(set_color normal) + _nim_prompt_wrapper $retc '' $mode + end + + # Virtual Environment + set -q VIRTUAL_ENV_DISABLE_PROMPT + or set -g VIRTUAL_ENV_DISABLE_PROMPT true + set -q VIRTUAL_ENV + and _nim_prompt_wrapper $retc V (path basename "$VIRTUAL_ENV") + + # nix-shell + if test -n "$IN_NIX_SHELL" + echo -n " " + end + + # git + set -l prompt_git (fish_git_prompt '%s') + test -n "$prompt_git" + and _nim_prompt_wrapper $retc G $prompt_git + + # Battery status + type -q acpi + and acpi -a 2>/dev/null | string match -rq off + and _nim_prompt_wrapper $retc B (acpi -b | cut -d' ' -f 4-) + + # New line + echo + + # Background jobs + set_color normal + + for job in (jobs) + set_color $retc + echo -n '│ ' + set_color brown + echo $job + end + + set_color normal + set_color $retc + echo -n '╰─>' + set_color -o red + echo -n '$ ' + set_color normal +end