Skip to content

Commit

Permalink
[config.fish] Refactor and consolidate loading files.
Browse files Browse the repository at this point in the history
Impact on loading time (measured using `hyperfine 'fish --interactive -c "echo hi"'`) remains negligible.
  • Loading branch information
lgarron committed Nov 17, 2024
1 parent 8dbfeb2 commit 8d0dcd4
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 86 deletions.
9 changes: 8 additions & 1 deletion dotfiles/fish/.config/fish/abbreviations.fish
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
# For this config file, `rc` will also force redefining abbreviations. See
# the "Setup" section above.
abbr -a rc "source $HOME/.config/fish/config.fish"
# TODO: why is this needed? It seems that `$_DOTFILES_FOLDER` is reset by something else between its original definition and here, when running in a codespace?

set -l _DOTFILES_FOLDER ( \
realpath ~/.config/fish/config.fish \
| sed 's#dotfiles/fish/\.config/fish/config\.fish$##' \
)
if [ "$CODESPACES" = "true" ]
set _DOTFILES_FOLDER "/workspaces/.codespaces/.persistedshare/dotfiles/"
end

abbr -a rcu "git -C \"$_DOTFILES_FOLDER\" pull ; and source $HOME/.config/fish/config.fish"
if [ "$CODESPACES" = "true" ]
Expand Down
3 changes: 3 additions & 0 deletions dotfiles/fish/.config/fish/bindings.fish
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,6 @@
end
bind ctrl-alt-r _add_LATEST_CD_DIR_PATH
bind alt-r _add_LATEST_CD_DIR_PATH

# Restore the functionality of using `⌥_` to enter `—` on macOS.
bind alt-_ "commandline -i —"
101 changes: 16 additions & 85 deletions dotfiles/fish/.config/fish/config.fish
Original file line number Diff line number Diff line change
Expand Up @@ -4,105 +4,46 @@

# Only print if we're a TTY.
# This prevents `protocol error: bad line length character` in git.
if isatty
if not status is-interactive
echo "WARNING: Loaded `config.fish` outside an interactive shell. This may be due to a script shebang: https://github.com/fish-shell/fish-shell/issues/5394"
end
function _echo
echo $argv
end
else
function _echo; end
if not status is-interactive
exit 0
end

# fish 4.0 experimentation for token nav

if status is-interactive
echo -n -e "\r"
echo "🐟🧪 version: "$FISH_VERSION
end

# Main reload message

if [ "$_FISH_MANUAL_RELOAD" = "true" ]
_echo -n "🐟🔄 Reloading "
set_color --bold
_echo -n "fish"
set_color normal
_echo " files."
end

# Paths

function add_to_path
set NEW_PATH_COMPONENT $argv[1]
fish_add_path --append $NEW_PATH_COMPONENT
end

set -x "GOPATH" "$HOME/Code/gopath"
echo -n -e "\r"
echo "🐟🧪 version: "$FISH_VERSION

if [ "$_FISH_MANUAL_RELOAD" = "true" -o "$_FISH_USER_PATHS_HAS_BEEN_SET_UP" != "true" ]
if [ "$_FISH_MANUAL_RELOAD" = "true" ]
_echo ""
end
set -e fish_user_paths

add_to_path "$HOME/.cache/cargo/bin" # For Rust
add_to_path /opt/homebrew/bin # macOS (Apple Silicon)
add_to_path /home/linuxbrew/.linuxbrew/bin # for codespaces
add_to_path /home/linuxbrew/.linuxbrew/sbin # for codespaces
add_to_path "$GOPATH/bin"
add_to_path $HOME/.cache/.bun/bin # For zig (for building Bun) https://bun.sh/docs/project/development
add_to_path "/usr/local/bin"

set_color --bold; _echo -n "\$fish_user_paths"; set_color normal
if [ (count $fish_user_paths) -gt 0 ]
_echo " has been set to the following order:"
for path in $fish_user_paths
_echo "↪ 📂$path"
end
else
_echo " has been reset, and contains no paths."
end
_echo ""
# Path

set -U _FISH_USER_PATHS_HAS_BEEN_SET_UP true
end
source $HOME/.config/fish/path.fish

# Loading

function _echo_manual_reload
if [ "$_FISH_MANUAL_RELOAD" = "true" ]
_echo $argv[1]
echo $argv[1]
end
end

_echo_manual_reload ""(status --current-filename)

function load_or_fail
_echo_manual_reload " ↪ 🐟 $argv[1]"
source $argv[2]
end

# Dotfiles conveniences

set _DOTFILES_FOLDER ( \
realpath ~/.config/fish/config.fish \
| sed 's#dotfiles/fish/\.config/fish/config\.fish$##' \
)
if [ "$CODESPACES" = "true" ]
set _DOTFILES_FOLDER "/workspaces/.codespaces/.persistedshare/dotfiles/"
end

_echo_manual_reload -ns "🐟🔄 Reloading "(set_color --bold)"fish"(set_color normal)" files."
_echo_manual_reload ""(status --current-filename)

# XDG path configuration

load_or_fail "xdg-basedir-workarounds" $HOME/.config/fish/xdg-basedir-workarounds.fish

load_or_fail "greeting" $HOME/.config/fish/greeting.fish

# Greeting
load_or_fail "xdg-basedir-workarounds" $HOME/.config/fish/xdg-basedir-workarounds.fish
load_or_fail "abbr" $HOME/.config/fish/abbr.fish

load_or_fail "greeting" $HOME/.config/fish/greeting.fish
load_or_fail "abbreviations" $HOME/.config/fish/abbreviations.fish
load_or_fail "git" $HOME/.config/fish/git.fish
load_or_fail "dev" $HOME/.config/fish/dev.fish
load_or_fail "repo" $HOME/Code/git/github.com/lgarron/repo/repo_completions_and_abbreviations.fish # TODO: configure this load statically

# Shortcuts

Expand All @@ -113,13 +54,8 @@
cd $argv[1]
end

# Restore the functionality of using `⌥_` to enter `—` on macOS.
bind alt-_ "commandline -i —"

### Abbrevation definition helpers

load_or_fail "abbr" $HOME/.config/fish/abbr.fish

### Editors

# TODO: https://github.com/microsoft/vscode/issues/139634
Expand Down Expand Up @@ -183,11 +119,6 @@

# Includes

load_or_fail "abbreviations" $HOME/.config/fish/abbreviations.fish
load_or_fail "git" $HOME/.config/fish/git.fish
load_or_fail "dev" $HOME/.config/fish/dev.fish
load_or_fail "repo" $HOME/Code/git/github.com/lgarron/repo/repo_completions_and_abbreviations.fish # TODO: configure this load statically

_echo_manual_reload ""

# Cleanup
Expand Down
36 changes: 36 additions & 0 deletions dotfiles/fish/.config/fish/path.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Path

function add_to_path
set NEW_PATH_COMPONENT $argv[1]
fish_add_path --append $NEW_PATH_COMPONENT
end

set -x "GOPATH" "$HOME/Code/gopath"

if [ "$_FISH_MANUAL_RELOAD" = "true" -o "$_FISH_USER_PATHS_HAS_BEEN_SET_UP" != "true" ]
if [ "$_FISH_MANUAL_RELOAD" = "true" ]
echo ""
end
set -e fish_user_paths

add_to_path "$HOME/.cache/cargo/bin" # For Rust
add_to_path /opt/homebrew/bin # macOS (Apple Silicon)
add_to_path /home/linuxbrew/.linuxbrew/bin # for codespaces
add_to_path /home/linuxbrew/.linuxbrew/sbin # for codespaces
add_to_path "$GOPATH/bin"
add_to_path $HOME/.cache/.bun/bin # For zig (for building Bun) https://bun.sh/docs/project/development
add_to_path "/usr/local/bin"

set_color --bold; echo -n "\$fish_user_paths"; set_color normal
if [ (count $fish_user_paths) -gt 0 ]
echo " has been set to the following order:"
for path in $fish_user_paths
echo "↪ 📂$path"
end
else
echo " has been reset, and contains no paths."
end
echo ""

set -U _FISH_USER_PATHS_HAS_BEEN_SET_UP true
end

0 comments on commit 8d0dcd4

Please sign in to comment.