From 6aa10f9bb6e2bebb39ad67ac7a0748b2b923eb8a Mon Sep 17 00:00:00 2001 From: Kjetil Orbekk Date: Fri, 13 May 2016 13:04:38 -0400 Subject: Add fish configuration. --- bin/kj-setup.sh | 12 ++- config/fish/config.fish | 1 + config/fish/functions/__format_time.fish | 27 ++++++ config/fish/functions/__parse_current_folder.fish | 3 + config/fish/functions/__parse_git_branch.fish | 4 + config/fish/functions/fish_prompt.fish | 110 ++++++++++++++++++++++ config/fish/functions/insert_cmd.fish | 3 + 7 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 config/fish/config.fish create mode 100644 config/fish/functions/__format_time.fish create mode 100644 config/fish/functions/__parse_current_folder.fish create mode 100644 config/fish/functions/__parse_git_branch.fish create mode 100644 config/fish/functions/fish_prompt.fish create mode 100644 config/fish/functions/insert_cmd.fish diff --git a/bin/kj-setup.sh b/bin/kj-setup.sh index a5a91ad..2ce62bf 100755 --- a/bin/kj-setup.sh +++ b/bin/kj-setup.sh @@ -56,11 +56,13 @@ for binary in dotfiles/bin/*; do create_symlink "../dotfiles/bin/${binary}" "bin/${binary}" done -if [[ "${SHELL}" = *zsh* ]]; then - create_symlink dotfiles/zshrc .zshrc -else - printf "${orange}[SKIPPED]${none} shell is not zsh :-(.\n" -fi +mkdir -p .config/fish/functions +create_symlink ../../dotfiles/config/fish/config.fish .config/fish/config.fish +for f in dotfiles/config/fish/functions/*.fish; do + create_symlink ../../../$f .config/fish/functions/$(basename $f) +done + +create_symlink dotfiles/zshrc .zshrc mkdir -p .ssh create_symlink ../dotfiles/ssh/config .ssh/config diff --git a/config/fish/config.fish b/config/fish/config.fish new file mode 100644 index 0000000..a02b905 --- /dev/null +++ b/config/fish/config.fish @@ -0,0 +1 @@ +bind \ec insert_cmd diff --git a/config/fish/functions/__format_time.fish b/config/fish/functions/__format_time.fish new file mode 100644 index 0000000..298e4cb --- /dev/null +++ b/config/fish/functions/__format_time.fish @@ -0,0 +1,27 @@ +function __format_time -d "Format milliseconds to a human readable format" + set -l milliseconds $argv[1] + set -l seconds (math "$milliseconds / 1000 % 60") + set -l minutes (math "$milliseconds / 60000 % 60") + set -l hours (math "$milliseconds / 3600000 % 24") + set -l days (math "$milliseconds / 86400000") + set -l time + set -l threshold 5 + + if test $days -gt 0 + set time (command printf "$time%sd " $days) + end + + if test $hours -gt 0 + set time (command printf "$time%sh " $hours) + end + + if test $minutes -gt 0 + set time (command printf "$time%sm " $minutes) + end + + if test $seconds -gt $threshold + set time (command printf "$time%ss " $seconds) + end + + echo -e $time +end diff --git a/config/fish/functions/__parse_current_folder.fish b/config/fish/functions/__parse_current_folder.fish new file mode 100644 index 0000000..031b53e --- /dev/null +++ b/config/fish/functions/__parse_current_folder.fish @@ -0,0 +1,3 @@ +function __parse_current_folder -d "Replace '$HOME' with '~'" + pwd | sed "s|$HOME|~|" +end diff --git a/config/fish/functions/__parse_git_branch.fish b/config/fish/functions/__parse_git_branch.fish new file mode 100644 index 0000000..42b46fb --- /dev/null +++ b/config/fish/functions/__parse_git_branch.fish @@ -0,0 +1,4 @@ +function __parse_git_branch -d "Parse current Git branch name" + command git symbolic-ref --short HEAD ^/dev/null; + or command git show-ref --head -s --abbrev | head -n1 ^/dev/null +end diff --git a/config/fish/functions/fish_prompt.fish b/config/fish/functions/fish_prompt.fish new file mode 100644 index 0000000..c45066c --- /dev/null +++ b/config/fish/functions/fish_prompt.fish @@ -0,0 +1,110 @@ +# vim: set ft=sh: + +# Pure +# by Rafael Rinaldi +# https://github.com/rafaelrinaldi/pure +# MIT License + +# Whether or not is a fresh session +set -g fresh_session 1 + +# Symbols + +set -g symbol_prompt "❯" +set -g symbol_git_down_arrow "⇣" +set -g symbol_git_up_arrow "⇡" +set -g symbol_git_dirty "*" +set -g symbol_horizontal_bar "—" + +# Colors + +set -g color_red (set_color red) +set -g color_green (set_color green) +set -g color_blue (set_color blue) +set -g color_yellow (set_color yellow) +set -g color_cyan (set_color cyan) +set -g color_gray (set_color 93A1A1) +set -g color_normal (set_color normal) + +function fish_prompt + # Save previous exit code + set -l exit_code $status + + # Set default color symbol to green meaning it's all good! + set -l color_symbol $color_green + + # Template + + set -l current_folder (__parse_current_folder) + set -l git_branch_name "" + set -l git_dirty "" + set -l git_arrows "" + set -l command_duration "" + set -l prompt "" + + # Do not add a line break to a brand new session + if test $fresh_session -eq 0 + set prompt $prompt "\n" + end + + # Prompt failed command execution duration + set command_duration (__format_time $CMD_DURATION) + + # Format current folder on prompt output + set prompt $prompt "$color_yellow$command_duration$color_blue$current_folder$color_normal " + + # Handle previous failed command + if test $exit_code -ne 0 + # Symbol color is red when previous command fails + set color_symbol $color_red + + set prompt $prompt "$color_yellow$command_duration$color_normal" + end + + # Exit with code 1 if git is not available + if not which git >/dev/null + return 1 + end + + # Check if is on a Git repository + set -l is_git_repository (command git rev-parse --is-inside-work-tree ^/dev/null) + + if test -n "$is_git_repository" + set git_branch_name (__parse_git_branch) + + # Check if there are files to commit + set -l is_git_dirty (command git status --porcelain --ignore-submodules ^/dev/null) + + if test -n "$is_git_dirty" + set git_dirty $symbol_git_dirty + end + + # Check if there is an upstream configured + command git rev-parse --abbrev-ref '@{upstream}' >/dev/null ^&1; and set -l has_upstream + if set -q has_upstream + set -l git_status (command git rev-list --left-right --count 'HEAD...@{upstream}' | sed "s/[[:blank:]]/ /" ^/dev/null) + + # Resolve Git arrows by treating `git_status` as an array + set -l git_arrow_left (command echo $git_status | cut -c 1 ^/dev/null) + set -l git_arrow_right (command echo $git_status | cut -c 3 ^/dev/null) + + # If arrow is not "0", it means it's dirty + if test $git_arrow_left -ne "0" + set git_arrows $symbol_git_up_arrow + end + + if test $git_arrow_right -ne "0" + set git_arrows $git_arrows$symbol_git_down_arrow + end + end + + # Format Git prompt output + set prompt $prompt "$color_gray$git_branch_name$git_dirty$color_normal\t$color_cyan$git_arrows$color_normal" + end + + set prompt $prompt "\n$color_symbol$symbol_prompt$color_normal " + + echo -e -s $prompt + + set fresh_session 0 +end diff --git a/config/fish/functions/insert_cmd.fish b/config/fish/functions/insert_cmd.fish new file mode 100644 index 0000000..b167734 --- /dev/null +++ b/config/fish/functions/insert_cmd.fish @@ -0,0 +1,3 @@ +function insert_cmd + commandline -i (pwd) +end -- cgit v1.2.3