From e541b2d745e0ceb1a6a2c6e00e808d88dd7da2a5 Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Mon, 27 Dec 2021 11:57:49 +0530 Subject: [PATCH] fish: Refactor fish shell configuration - Drop starship. This also helps us to get rid of ttf-font-nerd install which is a dependency of starship. - Source fnm env only once on login. This makes fish shell/terminal load much much faster. Add an alias to invoke fnm use easily. - Set path or environment variables only when required path or binary exists. - Introduce a function to profile fish easily. - Borrow the fish prompt logic from Alexis King's configuration. - Factor out key binding related settings. - Make su launch fish. Prompt and other ideas for clean up taken from https://github.com/lexi-lambda/dotfiles/tree/master/fish --- fish/.config/fish/conf.d/fnm.fish | 1 - fish/.config/fish/conf.d/setup.fish | 52 +++++++++++++ fish/.config/fish/conf.d/starship.fish | 1 - fish/.config/fish/conf.d/sway.fish | 9 +-- fish/.config/fish/config.fish | 34 +++----- .../.config/fish/functions/fish_greeting.fish | 2 + fish/.config/fish/functions/fish_profile.fish | 7 ++ fish/.config/fish/functions/fish_prompt.fish | 78 +++++++++++++++++++ .../functions/fish_user_key_bindings.fish | 5 ++ fish/.config/fish/functions/fu.fish | 3 + fish/.config/fish/functions/su.fish | 4 + 11 files changed, 162 insertions(+), 34 deletions(-) delete mode 100644 fish/.config/fish/conf.d/fnm.fish create mode 100644 fish/.config/fish/conf.d/setup.fish delete mode 100644 fish/.config/fish/conf.d/starship.fish create mode 100644 fish/.config/fish/functions/fish_greeting.fish create mode 100644 fish/.config/fish/functions/fish_profile.fish create mode 100644 fish/.config/fish/functions/fish_prompt.fish create mode 100644 fish/.config/fish/functions/fish_user_key_bindings.fish create mode 100644 fish/.config/fish/functions/fu.fish create mode 100644 fish/.config/fish/functions/su.fish diff --git a/fish/.config/fish/conf.d/fnm.fish b/fish/.config/fish/conf.d/fnm.fish deleted file mode 100644 index d767908..0000000 --- a/fish/.config/fish/conf.d/fnm.fish +++ /dev/null @@ -1 +0,0 @@ -fnm env --use-on-cd --log-level error | source diff --git a/fish/.config/fish/conf.d/setup.fish b/fish/.config/fish/conf.d/setup.fish new file mode 100644 index 0000000..b9315e6 --- /dev/null +++ b/fish/.config/fish/conf.d/setup.fish @@ -0,0 +1,52 @@ +function __fish_setup_on_tty_login --description "Set up environment on tty login" + for extra_path in ~/.{local,nix-profile,cargo,cabal,ghcup}/bin + if test -d "$extra_path" + set -gxp PATH "$extra_path" + end + end + + if type -qf nvim + set -gx EDITOR nvim + set -gx VISUAL nvim + set -gx MANPAGER 'nvim +Man!' + else + if type -qf vim + set -gx EDITOR vim + set -gx VISUAL vim + end + end + + if test -d {$HOME}/.nix-defexpr/channels + set -gx NIX_PATH {$HOME}/.nix-defexpr/channels + set -gx NIX_SSL_CERT_FILE '/etc/ssl/certs/ca-certificates.crt' + end + + if test -d /var/lib/flatpak + # https://github.com/fish-shell/fish-shell/issues/7485#issuecomment-728984689 + # https://wiki.archlinux.org/title/Flatpak#Adding_Flatpak_.desktop_files_to_your_menu + set -l xdg_data_home $XDG_DATA_HOME ~/.local/share + set -gx --path XDG_DATA_DIRS $xdg_data_home[1]/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share + + for flatpakdir in ~/.local/share/flatpak/exports/bin /var/lib/flatpak/exports/bin + if test -d $flatpakdir + contains $flatpakdir $PATH; or set -a PATH $flatpakdir + end + end + end + + if type -qf kitty + set -gx TERM xterm-kitty + end + + if type -qf fnm + fnm env | source + end + + set -gx CLICOLOR 1 + set -gx LSCOLORS 'gxfxcxdxbxegedabaggxgx' +end + +# Instead of tty check, we would have liked to do this using status is-login +# check but for some reason that does not work? +set --local TTY1 (tty) +[ "$TTY1" = "/dev/tty1" ] && __fish_setup_on_tty_login diff --git a/fish/.config/fish/conf.d/starship.fish b/fish/.config/fish/conf.d/starship.fish deleted file mode 100644 index c179c03..0000000 --- a/fish/.config/fish/conf.d/starship.fish +++ /dev/null @@ -1 +0,0 @@ -starship init fish | source diff --git a/fish/.config/fish/conf.d/sway.fish b/fish/.config/fish/conf.d/sway.fish index 118bf48..0f61f7a 100644 --- a/fish/.config/fish/conf.d/sway.fish +++ b/fish/.config/fish/conf.d/sway.fish @@ -1,8 +1,3 @@ # If running from tty1 start sway -set TTY1 (tty) -if test -z "$DISPLAY"; and test $TTY1 = "/dev/tty1" - exec sway -end - -set -e BROWSER -set -Ux BROWSER firefox-wayland +set --local TTY1 (tty) +[ "$TTY1" = "/dev/tty1" ] && exec sway diff --git a/fish/.config/fish/config.fish b/fish/.config/fish/config.fish index 081295f..5123ff2 100644 --- a/fish/.config/fish/config.fish +++ b/fish/.config/fish/config.fish @@ -1,25 +1,9 @@ -set PATH {$HOME}/.nix-profile/bin {$HOME}/.cargo/bin {$HOME}/.cabal/bin {$HOME}/.local/bin $PATH -set -gx TERM xterm-kitty -set -gx NIX_PATH {$HOME}/.nix-defexpr/channels - -# https://github.com/fish-shell/fish-shell/issues/5593 -bind \cd true - -export MANPAGER='nvim +Man!' -export NIX_SSL_CERT_FILE='/etc/ssl/certs/ca-certificates.crt' - -export VISUAL="nvim" -export EDITOR="nvim" - -fzf_key_bindings - -# https://github.com/fish-shell/fish-shell/issues/7485#issuecomment-728984689 -# https://wiki.archlinux.org/title/Flatpak#Adding_Flatpak_.desktop_files_to_your_menu -set -l xdg_data_home $XDG_DATA_HOME ~/.local/share -set -gx --path XDG_DATA_DIRS $xdg_data_home[1]/flatpak/exports/share:/var/lib/flatpak/exports/share:/usr/local/share:/usr/share - -for flatpakdir in ~/.local/share/flatpak/exports/bin /var/lib/flatpak/exports/bin - if test -d $flatpakdir - contains $flatpakdir $PATH; or set -a PATH $flatpakdir - end -end +set -g fish_term24bit 1 +set -g fish_color_command cyan +set -g fish_color_end red +set -g fish_color_error red --bold +set -g fish_color_escape yellow +set -g fish_color_operator blue +set -g fish_color_param normal +set -g fish_color_quote green +set -g fish_color_redirection red diff --git a/fish/.config/fish/functions/fish_greeting.fish b/fish/.config/fish/functions/fish_greeting.fish new file mode 100644 index 0000000..018520b --- /dev/null +++ b/fish/.config/fish/functions/fish_greeting.fish @@ -0,0 +1,2 @@ +function fish_greeting +end diff --git a/fish/.config/fish/functions/fish_profile.fish b/fish/.config/fish/functions/fish_profile.fish new file mode 100644 index 0000000..636807e --- /dev/null +++ b/fish/.config/fish/functions/fish_profile.fish @@ -0,0 +1,7 @@ +function fish_profile + if test -e /tmp/fish.profile + rm /tmp/fish.profile + end + fish --profile-startup /tmp/fish.profile -i -c exit + sort -nk2 /tmp/fish.profile +end diff --git a/fish/.config/fish/functions/fish_prompt.fish b/fish/.config/fish/functions/fish_prompt.fish new file mode 100644 index 0000000..8386754 --- /dev/null +++ b/fish/.config/fish/functions/fish_prompt.fish @@ -0,0 +1,78 @@ +set --path -a NESTED_FISH_LABELS + +function fish_prompt + set -l command_status $status + set -l question_mark_in_circle '?'\u20dd + set_color red; echo -n '# ' + + # Print nesting + if test (count $NESTED_FISH_LABELS) -gt 0 + echo -ns '[' (set_color yellow) \ + (string join (set_color red)/(set_color yellow) $NESTED_FISH_LABELS) \ + (set_color red) '] ' + end + + # Print current location + if [ "$PWD" = "$HOME" ] + set_color green; echo -n '~' + else + set -l parent_dir (dirname "$PWD") + set_color blue + if [ "$parent_dir" = "$HOME" ] + echo -n '~' + else + echo -n (basename "$parent_dir") + end + echo -n ' → ' + set_color green; echo -n (basename "$PWD") + end + echo -n ' ' + + # Show some pretty git information if we're in a git repo + set -l is_git_worktree (git rev-parse --is-inside-work-tree 2>/dev/null) + if test "$is_git_worktree" = true + if git rev-parse '@{u}' >/dev/null 2>&1 + # If an upstream is set, check how far ahead/behind the branch is + set -l git_commits_ahead (git rev-list '@{u}..HEAD' | wc -l | awk '{print $1}') + set -l git_commits_behind (git rev-list 'HEAD..@{u}' | wc -l | awk '{print $1}') + if [ \( "$git_commits_ahead" -eq 0 \) -a \( "$git_commits_behind" -eq 0 \) ] + set_color blue; echo -n '⦿' + else + if [ "$git_commits_behind" -gt 0 ] + set_color red; echo -n "↓$git_commits_behind" + end + if [ "$git_commits_ahead" -gt 0 ] + set_color brred; echo -n "↑$git_commits_ahead" + end + end + else + # Otherwise, indicate that an upstream is unknown + set_color brred; echo -n "$question_mark_in_circle" + end + + set -l git_branch (git branch 2>/dev/null | sed -n '/\* /s///p') + if test -n "$git_branch" + set_color blue; echo -n ' [' + # Color the branch name differently if the working tree is dirty + if [ (count (git status --porcelain)) -gt 0 ] + set_color brred + else + set_color yellow + end + echo -n "$git_branch" + set_color blue; echo -n '] ' + end + end + + if [ "$command_status" -eq 0 ] + set_color brcyan; echo -n 'λ: ' + else + set_color red; echo -n 'λ! ' + end + set_color normal + + # Color the hostname in the prompt dynamically whenever connected through SSH + if set -q SSH_TTY + set -g fish_color_host brred + end +end diff --git a/fish/.config/fish/functions/fish_user_key_bindings.fish b/fish/.config/fish/functions/fish_user_key_bindings.fish new file mode 100644 index 0000000..f7cc218 --- /dev/null +++ b/fish/.config/fish/functions/fish_user_key_bindings.fish @@ -0,0 +1,5 @@ +function fish_user_key_bindings + # https://github.com/fish-shell/fish-shell/issues/5593 + bind \cd true + fzf_key_bindings +end diff --git a/fish/.config/fish/functions/fu.fish b/fish/.config/fish/functions/fu.fish new file mode 100644 index 0000000..0c6f067 --- /dev/null +++ b/fish/.config/fish/functions/fu.fish @@ -0,0 +1,3 @@ +function fu + fnm use +end diff --git a/fish/.config/fish/functions/su.fish b/fish/.config/fish/functions/su.fish new file mode 100644 index 0000000..84cc0cf --- /dev/null +++ b/fish/.config/fish/functions/su.fish @@ -0,0 +1,4 @@ +# https://wiki.archlinux.org/title/fish#Make_su_launch_fish +function su + command su --shell=/usr/bin/fish $argv +end