Skip to content

Instantly share code, notes, and snippets.

@ronaldsuwandi
Last active July 30, 2021 07:54
Show Gist options
  • Save ronaldsuwandi/10013651 to your computer and use it in GitHub Desktop.
Save ronaldsuwandi/10013651 to your computer and use it in GitHub Desktop.
My personal fish shell config for OS X. Supports git branch (fast git checks), virtualenv (if installed) and Kubernetes current context
function ls --description 'List contents of directory'
command ls -lFG $argv
end
function subl --description 'Launches sublime text in a new window'
command subl -n $argv
end
function code --description 'Launches visual code studio in a new window'
command code -n $argv
end
function grep --description 'Colorful grep that ignores binary file and outputs line number'
command grep --color=always -I $argv
end
function gf --description 'Do a git fetch'
git fetch
end
function gdeletemergedcurrent --wraps git --description 'Delete all local branches that is already merged to current branch (excludes master and main)'
git branch --merged | grep -v "\*" | grep -v "master" | grip -v "main" | xargs -n 1 git branch -d
git remote prune origin
end
set -gx GOPATH $HOME/Go
set -gx GOROOT (go env GOROOT)
set PATH $HOME/bin $GOPATH/bin $HOME/.rbenv/shims /usr/local/bin /usr/local/sbin $PATH $GOROOT/bin ./node_modules/.bin
. $HOME/.config/fish/prompt.fish
# set -gx HOMEBREW_GITHUB_API_TOKEN #token here#
# Java
set -gx JAVA_HOME (/usr/libexec/java_home)
# Allow 256 colors in iTerm2 for pretty vim colors
set -gx CLICOLOR 1
set -gx TERM xterm-256color
# use custom function instead of fish built in git prompt due to performance
# (fish git prompt uses diff which can be slow)
function git_prompt
set -l git_status_origin (command git status -s -b 2> /dev/null)
printf ''
set -l is_repo (string join0 $git_status_origin)
if test -z $is_repo
# git status returns error (not a git repo)
printf ''
else
echo $git_status_origin | string match --regex '\[.*ahead.*\]' --quiet
set -l ahead $status
echo $git_status_origin | string match --regex '\[.*behind.*\]' --quiet
set -l behind $status
set -l branch (echo $git_status_origin | string replace -r '## ([\S]+).*' '$1' | string replace -r '(.+)\.\.\..*' '$1')
# simply check for modified/deleted/rename, match only 1
echo $git_status_origin | string match --regex '[MDR ][MDR ] .*' --quiet
set -l git_dirty $status
# simply check for ?? in the list of files, match only 1
echo $git_status_origin | string match --regex '\?\? .*' --quiet
set -l git_untracked $status
if test "$git_dirty" -eq 0
printf ' (%s%s' (set_color red)
else
printf ' (%s%s' (set_color yellow)
end
# Use branch name if available
if test -n "$branch"
printf $branch
else
# for new branch, git branch will return nothing, use branch name from git status
set -l git_status_no_commits_branch (echo $git_status_origin | string replace '## No commits yet on ' '')
printf $git_status_no_commits_branch
end
if test "$git_untracked" -eq 0
printf '%s*' (set_color purple)
end
if test -s .git/refs/stash # stash exists - check on .git/refs/stash file
printf '%s$' (set_color green)
end
# if local repo is ahead, show up arrow
if test "$ahead" -eq 0
printf '%s↑' (set_color cyan)
end
# if local repo is behind, show down arrow
if test "$behind" -eq 0
printf '%s↓' (set_color magenta)
end
printf '%s)' (set_color normal)
end
end
function kubectl_context
if test -e ~/.kube/config; and type -q kubectl
set -l current_context (kubectl config current-context)
printf ' %s[' (set_color normal)
printf '%s%s' (set_color cyan) $current_context
printf '%s]' (set_color normal)
else
printf ''
end
end
function fish_prompt
# check for iterm2 shell integration
if test -e {$HOME}/.iterm2_shell_integration.fish ; and source {$HOME}/.iterm2_shell_integration.fish
printf ' '
end
printf '%s%s%s' (set_color $fish_color_cwd) (prompt_pwd) (set_color normal)
printf '%s%s ☕️ ' (git_prompt) (kubectl_context)
set_color normal
end
@ronaldsuwandi
Copy link
Author

updated to remove all grep calls and use fish' built in string matcher which is faster than grep. Now the bottleneck is only on git status

~/w/demo (6.1.1-post*↓) ☕️ fish --profile /tmp/profile -c fish_prompt; sort -nk1 /tmp/profile | tail -n 10
72	72	----> echo $git_status_origin | string match --regex '\[.*ahead.*\]' --quiet
86	118	---> set -l is_repo (string join0 $git_status_origin)
87	87	--> set_color $fish_color_cwd
112	102796	---> set -l git_status_origin (command git status -s -b 2> /dev/null)
131	131	----> string replace -r '^'"$realhome"'($|/)' '~$1' $PWD
135	712	--> prompt_pwd
198	104113	-> printf '%s%s ☕️ ' (git_prompt) (kubectl_context)
233	246	---> source /usr/local/Cellar/fish/3.3.1/share/fish/functions/prompt_pwd.fish
265	1082	-> printf '%s%s%s' (set_color $fish_color_cwd) (prompt_pwd) (set_color normal)
102684	102684	----> command git status -s -b 2> /dev/null

@ronaldsuwandi
Copy link
Author

upgraded to latest git and it's even faster now

~/w/demo (6.1.1-post*↓) ☕️  fish --profile /tmp/profile-oldgit -c fish_prompt; sort -nk2 /tmp/profile-oldgit | tail -n 10
64        200     ---> set -l tmp (string replace -r '^'"$realhome"'($|/)' '~$1' $PWD)
274     285     ---> source /usr/local/Cellar/fish/3.3.1/share/fish/functions/prompt_pwd.fish
151     768     --> prompt_pwd
48      867     ---> if test -z $is_repo...
272     1151    -> printf '%s%s%s' (set_color $fish_color_cwd) (prompt_pwd) (set_color normal)
40040   40040   ----> command git status -s -b 2> /dev/null
111     40151   ---> set -l git_status_origin (command git status -s -b 2> /dev/null)
27      41174   --> git_prompt
200     41435   -> printf '%s%s ☕️ ' (git_prompt) (kubectl_context)
23      42659   > fish_prompt

@ronaldsuwandi
Copy link
Author

Fixed git prompt for repo without origin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment