Commit b0977db7 authored by Joseph Tannhuber's avatar Joseph Tannhuber Committed by Bruno Pinto

Budspencer theme improvements

- Bookmarks and command/directory history functions implemented
- Function to edit commandline with $EDITOR
- Faster git parser
parent f73426c2
# budspencer theme # budspencer theme
Translation of zsh's prezto [budspencer theme][budspencer]. Translation of zsh's prezto [budspencer theme][budspencer]. This theme is
optimized for visibility and a fast workflow.
## Requirements
Non standard tools:
* xsel
* git
## Configuration ## Configuration
The theme behaves similar to vim's airline/powerline plugins. It needs a The theme behaves similar to vim's airline/powerline plugins. Thus, it needs a
[powerline font][font]. Although it works with emacs mode, it's more powerful [powerline font][font]. Although it works with emacs mode, it's more powerful
with vi mode. In order to enable vi mode, put the following lines into with vi mode. In order to enable vi mode, put the following lines into
`$HOME/.config/fish/config.fish` before `set fish_path $HOME/.oh-my-fish`: `$HOME/.config/fish/config.fish` before `set fish_path $HOME/.oh-my-fish`:
``` ```
set -e fish_key_bindings
set -U fish_key_bindings fish_vi_key_bindings set -U fish_key_bindings fish_vi_key_bindings
``` ```
## Left prompt segments ## Left prompt segments
- Vi mode indicator - Vi mode indicator similar to vim airline/powerline, cursor color also changes
if terminal supports it
- Git repository information - Git repository information
- Status symbols - Status symbols
* ⌘: present working directory is in bookmark list, can be toggled with `#` to
show the number of the bookmark
* V: vi is parent process * V: vi is parent process
* R: [ranger][ranger] is parent process * R: [ranger][ranger] is parent process
* ⚙: there are background jobs * ⚙: there are background jobs, can be toggled with `#` to show the amount of
background jobs
* : no write permissions in present working directory * : no write permissions in present working directory
* ✔: last command succeeded * ✔: last command succeeded, can be toggled with `#` to show the status value
* ✘: last command failed * ✘: last command failed, can be toggled with `#` to show the status value
* ⚡: superuser indicator * ⚡: superuser indicator
- Present working directory
## Right prompt segments ## Right prompt segments
- Last command's duration time - Last command's duration time
- Git status - Git status
* style can be toggled in NORMAL and in VISUAL mode with `#` between
- `symbols` (shows git status symbols, see below)
- `counts` (shows amount of files that are affected)
* symbols: * symbols:
- ↑: git repository is ahead origin - ↑: git repository is ahead origin
- ↓: git repository is behind origin - ↓: git repository is behind origin
- +: new files were added - +: changes have been added to git index
- –: files have been deleted - –: files have been deleted in work tree
- ✱: files have been modified - ✱: files have been modified in work tree
- →: files have been renamed - →: files have been renamed in git index
- ═: there are unmerged commits - ═: there are unmerged commits
- ●: there are untracked files - ●: there are untracked (new) files
- ✭: there are stashed commits - ✭: there are stashed commits
- Present working directory
* style can be toggled in NORMAL and in VISUAL mode with space bar ## Toggle prompt segments
The following shortcuts need vi-mode:
- Style of "present working directory"-segment can be toggled in NORMAL and in
VISUAL mode with space bar
* styles implemented: * styles implemented:
- `short` (show truncated path) - `short` (shows username, short hostname and truncated path; recommended in
- `long` (show full path) most cases)
- `none` (show nothing) - `long` (shows username, full hostname and full path; useful for copy-paste
* configurable by global array `$PWDSTYLE` (if not set, defaults to `short long none`) in ssh connections)
- `none` (shows nothing except small delimiter; useful for small terminals)
- If prompt is toggled with `#`, the IP address is shown instead of the
hostname.
* configurable by global list `$PWDSTYLE` (if not set, defaults to
`short long none`); put `set -g PWDSTYLE none long` into
`$HOME/.config/fish/budspencer_config.fish` if you want to toggle only between
`none` and `long`.
- Style of symbols can be toggled in NORMAL and in VISUAL mode with `#`
* styles implemented:
- `symbols` (shows status symbols)
- `numbers` (shows numbers instead of symbols in left prompt as well as amount of changes in
right git prompt)
## Quickly navigate in history of working directories
There is a function `d` that shows the history as enumerated list. Unlike
fish's builtin `dirh`, `d` does not show any duplicates. Enter a number to jump
to a directory within the list. It's also possible to give that number directly as
argument to the `d` function.
The following shortcuts need vi-mode:
- Press `H` in NORMAL mode to change present working directory to previous
working directory in history.
- Press `L` in NORMAL mode to change present working directory to next working
directory in history.
## Quickly navigate in command history
The function `c` shows the command history as enumerated list equivalently to
the `d` function. Selections are also paste into the X clipboard.
## Bookmarks
A bookmark can be created with `mark`. It can be removed with `unmark`.
Bookmarks are persistent and universal.
They are stored in `$HOME/.config/fish/budspencer_config.fish`.
A new shell automatically changes working directory on startup to newest bookmark.
`m` is a function that shows the bookmarks as enumerated list equivalently to `d`.
The following shortcuts need vi mode:
- Create a bookmark for present working directory with `m` in NORMAL mode.
- Remove a bookmark for present working directory with `M` in NORMAL mode.
## Edit commandline with your favorite editor
The function `edit-commandline` let you edit the commandline with your editor.
It makes your commandline as powerful as your editor. If `$EDITOR` is not set,
use vi.
The following shortcut needs vi mode:
- Type `.` in NORMAL mode to edit commandline in editor.
## Screenshot ## Screenshot
......
###############################################################################
#
# Prompt theme name:
# budspencer
#
# Description:
# a sophisticated airline/powerline theme
#
# Author:
# Joseph Tannhuber <sepp.tannhuber@yahoo.de>
#
# Sections:
# -> Functions
# -> Help
# -> Welcome message
#
###############################################################################
###############################################################################
# => Functions
###############################################################################
#########
# => Help
#########
function budspencer_help -d "Shows helpfile"
set -l readme_file $fish_path"/themes/budspencer/README.md"
if set -q PAGER
if test -e $readme_file
eval $PAGER $readme_file
else
set_color $fish_color_error
echo $readme_file" wasn't found."
end
else
open $readme_file
end
end
####################
# => Welcome message
####################
function fish_greeting -d "Show greeting in login shell."
if begin
test (count $bookmarks) -gt 0
and not set -q -x LOGIN
and not set -q -x RANGER_LEVEL
and not set -q -x VIM
end
echo This is oh-my-fish\'s (set_color -b 083746 268bd2)budspencer(set_color normal) theme.
echo Type (set_color -b 083746 cb4b16)»budspencer_help«(set_color normal) in order to see how you can speed up your workflow.
set_color normal
end
end
############################################################################### ###############################################################################
# #
# prompt theme name: budspencer # Prompt theme name:
# budspencer
# #
# description: a sophisticated airline/powerline theme # Description:
# a sophisticated airline/powerline theme
# #
# author: Joseph Tannhuber # Author:
# Joseph Tannhuber <sepp.tannhuber@yahoo.de>
#
# Sections:
# -> Color definitions
# -> Key bindings
# -> Files
# -> Functions
# -> Directory history
# -> Command history
# -> Bookmarks
# -> Commandline editing with $EDITOR
# -> Git segment
# -> Bind-mode segment
# -> Symbols segment
# -> Prompt initialization
# -> Left prompt
# -> Segments
# #
############################################################################### ###############################################################################
############################################################################### ###############################################################################
# Color definitions # => Color definitions
############################################################################### ###############################################################################
# define colors # Define colors
# values are: black dark_gray light_gray white yellow orange red magenta violet blue cyan green # Values are: black dark_gray light_gray white yellow orange red magenta violet blue cyan green
set -g budspencer_colors 000000 083743 445659 fdf6e3 b58900 cb4b16 dc121f af005f 6c71c4 268bd2 2aa198 859900 set -g budspencer_colors 000000 083743 445659 fdf6e3 b58900 cb4b16 dc121f af005f 6c71c4 268bd2 2aa198 859900
# cursor color changes according to vi-mode # Cursor color changes according to vi-mode
# define values for: normal_mode insert_mode visual_mode # Define values for: normal_mode insert_mode visual_mode
set -g budspencer_cursors "\033]12;#$budspencer_colors[10]\007" "\033]12;#$budspencer_colors[5]\007" "\033]12;#$budspencer_colors[8]\007" set -g budspencer_cursors "\033]12;#$budspencer_colors[10]\007" "\033]12;#$budspencer_colors[5]\007" "\033]12;#$budspencer_colors[8]\007"
# some terminals cannot change the cursor color # Some terminals cannot change the cursor color
set -l unsupported_terminals "fbterm" "st" "linux" "screen" set -l unsupported_terminals "fbterm" "st" "linux" "screen"
if contains $TERM $unsupported_terminals if contains $TERM $unsupported_terminals
set -e budspencer_cursors set -e budspencer_cursors
end end
############################################################################### ###############################################################################
# Utils # => Key bindings
###############################################################################
if test "$fish_key_bindings" = "fish_vi_key_bindings"
bind '#' fish_symbols_toggle_cm
bind -M visual '#' fish_symbols_toggle_cm
bind ' ' fish_pwd_toggle_cm
bind -M visual ' ' fish_pwd_toggle_cm
bind L fish_cd_next
bind H fish_cd_prev
bind m mark
bind M unmark
bind . edit-commandline
end
###############################################################################
# => Files
###############################################################################
# Config file
set -g budspencer_config $HOME/.config/fish/budspencer_config.fish
# Temporary file
set -g budspencer_tmpfile "/tmp/"(echo %self)".fish"
###############################################################################
# => Functions
############################################################################### ###############################################################################
function __budspencer_git_branch_name -d "Return the current branch name" ######################
set -l branch (git symbolic-ref HEAD ^/dev/null | sed -e 's|^refs/heads/||') # => Directory history
######################
function create_dir_hist -v PWD -d "Create directory history without duplicates"
if test $hist_lock = "false"
if contains $PWD $dir_hist
set -e dir_hist[(contains -i $PWD $dir_hist)]
end
set dir_hist $dir_hist "$PWD"
set -g dir_hist_val (count $dir_hist)
end
end
function fish_cd_prev -d "Change to previous directory, press H in NORMAL mode."
if test $dir_hist_val -gt 1
set dir_hist_val (math $dir_hist_val-1)
set hist_lock "true"
cd $dir_hist[$dir_hist_val]
commandline -f repaint
end
end
function fish_cd_next -d "Change to next directory, press L in NORMAL mode."
if test $dir_hist_val -lt (math (count $dir_hist))
set dir_hist_val (math $dir_hist_val+1)
set hist_lock "true"
cd $dir_hist[$dir_hist_val]
commandline -f repaint
end
end
function d -d "List directory history, jump to directory in list with d <number>"
set hist_lock "true"
set -l num_items (math (count $dir_hist)-1)
if test $num_items -eq 0
echo (set_color $fish_color_error)"Directory history is empty."
return
end
if test $argv[1] -ge 0 -a $argv[1] -lt $num_items ^ /dev/null
cd $dir_hist[(math $num_items-$argv[1])]
else
for i in (seq $num_items)
if test (math \($num_items-$i\)%2) -eq 0
set_color normal
else
set_color $budspencer_colors[4]
end
echo (math $num_items-$i)\t$dir_hist[$i] | sed "s|$HOME|~|"
end
if test $num_items -eq 1
set last_item ""
else
set last_item "-"(math $num_items-1)
end
set input_length (expr length (math $num_items-1))
read -p 'echo -n (set_color -b $budspencer_colors[2] $budspencer_colors[5])" ♻ Goto [0"$last_item"] "(set_color -b normal $budspencer_colors[2])" "(set_color $budspencer_colors[5])' -n $input_length -l dir_num
if begin
test -n $dir_num
and contains $dir_num (seq 0 (math $num_items-1))
end
cd $dir_hist[(math $num_items-$dir_num)]
end
end
end
####################
# => Command history
####################
function create_cmd_hist -e fish_prompt -d "Create command history without duplicates"
if test $hist_lock = "false"
set -l cmd $history[1]
if not begin
expr $cmd : 'cd ' > /dev/null
or expr $cmd : '[cdm] ' > /dev/null
or test $cmd = 'cd' -o $cmd = 'c' -o $cmd = 'd' -o $cmd = 'm'
end
if contains $cmd $cmd_hist
set -e cmd_hist[(contains -i $cmd $cmd_hist)]
end
set cmd_hist $cmd_hist $cmd
end
end
end
function c -d "List command history, execute command in list with c <number>"
set hist_lock "true"
set -l num_items (count $cmd_hist)
if test $num_items -eq 0
echo (set_color $fish_color_error)"Command history is empty."
return
end
if test $argv[1] -lt $num_items ^ /dev/null
commandline $cmd_hist[(math $num_items-$argv[1])]
echo $cmd_hist[(math $num_items-$argv[1])] | xsel
else
for i in (seq $num_items)
if test (math \($num_items-$i\)%2) -eq 0
set_color normal
else
set_color $budspencer_colors[4]
end
echo -n (math $num_items-$i)
set -l item (echo $cmd_hist[$i] | fish_indent | expand -t 4)
echo -n \t$item\n
end
if test $num_items -eq 1
set last_item ""
else
set last_item "-"(math $num_items-1)
end
set input_length (expr length (math $num_items-1))
read -p 'echo -n (set_color -b $budspencer_colors[2] $budspencer_colors[8])" ↩ Exec [0"$last_item"] "(set_color -b normal $budspencer_colors[2])" "(set_color $budspencer_colors[8])' -n $input_length -l cmd_num
if begin
test -n $cmd_num
and contains $cmd_num (seq 0 (math $num_items-1))
end
commandline $cmd_hist[(math $num_items-$cmd_num)]
echo $cmd_hist[(math $num_items-$cmd_num)] | xsel
end
set fish_bind_mode default
end
set hist_lock "false"
end
##############
# => Bookmarks
##############
function mark -d "Create bookmark for present working directory."
if not contains $PWD $bookmarks
set -U bookmarks $PWD $bookmarks
sed -i '/^set -U bookmarks/d' $budspencer_config ^ /dev/null
echo "set -U bookmarks $bookmarks" >> $budspencer_config
set hist_lock "true"
commandline -f repaint
end
end
function unmark -d "Remove bookmark for present working directory."
if contains $PWD $bookmarks
set -e bookmarks[(contains -i $PWD $bookmarks)]
sed -i '/^set -U bookmarks/d' $budspencer_config ^ /dev/null
echo "set -U bookmarks $bookmarks" >> $budspencer_config
set hist_lock "true"
commandline -f repaint
end
end
function m -d "List bookmarks, jump to directory in list with m <number>"
set hist_lock "true"
set -l num_items (count $bookmarks)
if test $num_items -eq 0
echo (set_color $fish_color_error)"Bookmark list is empty."
return
end
if test $argv[1] -ge 0 -a $argv[1] -lt $num_items ^ /dev/null
cd $bookmarks[(math $num_items-$argv[1])]
else
for i in (seq $num_items)
if test (math \($num_items-$i\)%2) -eq 0
set_color normal
else
set_color $budspencer_colors[4]
end
echo (math $num_items-$i)\t$bookmarks[$i] | sed "s|$HOME|~|"
end
if test $num_items -eq 1
set last_item ""
else
set last_item "-"(math $num_items-1)
end
echo -en $budspencer_cursors[1]
set input_length (expr length $num_items)
read -p 'echo -n (set_color -b $budspencer_colors[2] $budspencer_colors[10])" ⌘ Goto [0"$last_item"] "(set_color -b normal $budspencer_colors[2])" "(set_color $budspencer_colors[10])' -n $input_length -l dir_num
if begin
test -n $dir_num
and contains $dir_num (seq 0 (math $num_items-1))
end
cd $bookmarks[(math $num_items-$dir_num)]
end
end
end
#####################################
# => Commandline editing with $EDITOR
#####################################
function edit-commandline -d "Opens current commandline with your $EDITOR"
commandline > $budspencer_tmpfile
eval $EDITOR $budspencer_tmpfile
if test -s $budspencer_tmpfile
set hist_lock "true"
commandline (sed 's|^\s*||' $budspencer_tmpfile)
else
set hist_lock "true"
commandline ""
end
rm $budspencer_tmpfile
end
################
# => Git segment
################
function __budspencer_prompt_git_branch -d "Return the current branch name"
set -l branch (command git symbolic-ref HEAD ^ /dev/null | sed -e 's|^refs/heads/||')
if test (count $branch) -eq 0 if test (count $branch) -eq 0
set -l position (git describe --contains --all HEAD ^/dev/null) set -l position (command git describe --contains --all HEAD ^ /dev/null)
if test (count $position) -eq 0 if test (count $position) -eq 0
set -l commit (git rev-parse HEAD ^/dev/null | sed 's|\(^.......\).*|\1|') set -l commit (command git rev-parse HEAD ^ /dev/null | sed 's|\(^.......\).*|\1|')
echo -n (set_color -b $budspencer_colors[11])""(set_color $budspencer_colors[1])" ➦ "$commit" "(set_color $budspencer_colors[11]) echo -n (set_color -b $budspencer_colors[11])""(set_color $budspencer_colors[1])" ➦ "$commit" "(set_color $budspencer_colors[11])
else else
echo -n (set_color -b $budspencer_colors[9])""(set_color $budspencer_colors[1])"  "$position" "(set_color $budspencer_colors[9]) echo -n (set_color -b $budspencer_colors[9])""(set_color $budspencer_colors[1])"  "$position" "(set_color $budspencer_colors[9])
...@@ -45,12 +292,15 @@ function __budspencer_git_branch_name -d "Return the current branch name" ...@@ -45,12 +292,15 @@ function __budspencer_git_branch_name -d "Return the current branch name"
end end
end end
function fish_vi_prompt_cm -d "Displays the current mode" ######################
# => Bind-mode segment
######################
function __budspencer_prompt_bindmode -d "Displays the current mode"
switch $fish_bind_mode switch $fish_bind_mode
case default case default
set_color -b $budspencer_colors[10] $budspencer_colors[1] set_color -b $budspencer_colors[10] $budspencer_colors[1]
echo -en $budspencer_cursors[1] echo -en $budspencer_cursors[1]
if test "$fish_key_bindings" = "fish_vi_key_bindings" -o "$fish_key_bindings" = "my_fish_key_bindings" if test "$fish_key_bindings" = "fish_vi_key_bindings" -o "$fish_key_bindings" = "fish_user_key_bindings"
echo -n " NORMAL " echo -n " NORMAL "
else else
echo -n " EMACS " echo -n " EMACS "
...@@ -61,6 +311,10 @@ function fish_vi_prompt_cm -d "Displays the current mode" ...@@ -61,6 +311,10 @@ function fish_vi_prompt_cm -d "Displays the current mode"
echo -en $budspencer_cursors[2] echo -en $budspencer_cursors[2]
echo -n " INSERT " echo -n " INSERT "
set_color -b $budspencer_colors[1] $budspencer_colors[5] set_color -b $budspencer_colors[1] $budspencer_colors[5]
if test $hist_lock = "true"
set hist_lock "false"
create_dir_hist
end
case visual case visual
set_color -b $budspencer_colors[8] $budspencer_colors[1] set_color -b $budspencer_colors[8] $budspencer_colors[1]
echo -en $budspencer_cursors[3] echo -en $budspencer_cursors[3]
...@@ -69,38 +323,77 @@ function fish_vi_prompt_cm -d "Displays the current mode" ...@@ -69,38 +323,77 @@ function fish_vi_prompt_cm -d "Displays the current mode"
end end
end end
function fish_prompt_symbols -d "Display symbols" ####################
# => Symbols segment
####################
function __budspencer_prompt_left_symbols -d "Display symbols"
set_color -b $budspencer_colors[2] set_color -b $budspencer_colors[2]
echo -n "" echo -n ""
# indicator for vim parent process
# Bookmark indicator
if contains $PWD $bookmarks
if test $symbols_style = "symbols"
set_color -o $budspencer_colors[10]
echo -n " ⌘"
else
set_color $budspencer_colors[10]
echo -n " "(math (count $bookmarks)-(contains -i $PWD $bookmarks))
end
end
# Indicator for vim parent process
if set -q -x VIM if set -q -x VIM
set_color -o $budspencer_colors[9] set_color -o $budspencer_colors[9]
echo -n " V" echo -n " V"
end end
# indicator for ranger parent process
# Indicator for ranger parent process
if set -q -x RANGER_LEVEL if set -q -x RANGER_LEVEL
set_color -o $budspencer_colors[9] set_color -o $budspencer_colors[9]
echo -n " R" echo -n " R"
end end
# background job indicator
if [ (jobs | wc -l) -gt 0 ] # Background job indicator
set jobs (jobs | wc -l)
if [ $jobs -gt 0 ]
if test $symbols_style = "symbols"
set_color -o $budspencer_colors[11] set_color -o $budspencer_colors[11]
echo -n " ⚙" echo -n " ⚙"
else
set_color normal
set_color -b $budspencer_colors[2] $budspencer_colors[11]
echo -n " "$jobs
end end
# write protection indicator end
# Write protection indicator
if [ ! -w . ] if [ ! -w . ]
set_color -o $budspencer_colors[6] set_color -o $budspencer_colors[6]
echo -n " " echo -n " "
end end
# status indicator
# Status indicator
if [ $last_status -eq 0 ] if [ $last_status -eq 0 ]
if test $symbols_style = "symbols"
set_color -o $budspencer_colors[12] set_color -o $budspencer_colors[12]
echo -n " ✔" echo -n " ✔"
else else
set_color normal
set_color -b $budspencer_colors[2] $budspencer_colors[12]
echo -n " "$last_status
end
else
if test $symbols_style = "symbols"
set_color -o $budspencer_colors[7] set_color -o $budspencer_colors[7]
echo -n " ✘" echo -n " ✘"
else
set_color normal
set_color -b $budspencer_colors[2] $budspencer_colors[7]
echo -n " "$last_status
end end
# superuser indicator end
# Superuser indicator
if [ $USER = "root" ] if [ $USER = "root" ]
set_color -o $budspencer_colors[6] set_color -o $budspencer_colors[6]
echo -n " ⚡" echo -n " ⚡"
...@@ -110,30 +403,71 @@ function fish_prompt_symbols -d "Display symbols" ...@@ -110,30 +403,71 @@ function fish_prompt_symbols -d "Display symbols"
end end
############################################################################### ###############################################################################
# Prompt # => Prompt initialization
###############################################################################
# Initialize some global variables
set -g hist_lock false
set -g dir_hist
set -g cmd_hist
set -g CMD_DURATION 0
# Load user defined key bindings
if functions --query fish_user_key_bindings
fish_user_key_bindings
end
# Set favorite editor
if not set -q EDITOR
set -g EDITOR vi
end
# Source config file
if test -e $budspencer_config
source $budspencer_config
end
# Set PWD segment style
if set -q $PWDSTYLE
set -g PWDSTYLE short long none
end
set pwd_style $PWDSTYLE[1]
# Cd to newest bookmark if this is a login shell
if not begin
set -q -x LOGIN
or set -q -x RANGER_LEVEL
or set -q -x VIM
end ^ /dev/null
cd $bookmarks[1]
end
set -x LOGIN $USER
###############################################################################
# => Left prompt
############################################################################### ###############################################################################
function fish_prompt -d "Write out the left prompt of the budspencer theme" function fish_prompt -d "Write out the left prompt of the budspencer theme"
set -g last_status $status set -g last_status $status
############################################################################# #############
# Segments # => Segments
############################################################################# #############
# vi mode # Vi mode
set -l ps_vi "" set -l ps_vi ""
set ps_vi (fish_vi_prompt_cm) set ps_vi (__budspencer_prompt_bindmode)
# git # Git
set -l ps_git "" set -l ps_git ""
set -l git_branch_name (__budspencer_git_branch_name) set -l git_branch_name (__budspencer_prompt_git_branch)
if test -n "$git_branch_name" if test -n "$git_branch_name"
set ps_git $git_branch_name set ps_git $git_branch_name
end end
# symbols # Symbols
set -l ps_symbols (fish_prompt_symbols) set -l ps_symbols (__budspencer_prompt_left_symbols)
# left prompt # Left prompt
echo -n -s $ps_vi $ps_git $ps_symbols '' ' ' echo -n -s $ps_vi $ps_git $ps_symbols '' ' '
end end
############################################################################### ###############################################################################
# Init #
# Prompt theme name:
# budspencer
#
# Description:
# a sophisticated airline/powerline theme
#
# Author:
# Joseph Tannhuber <sepp.tannhuber@yahoo.de>
#
# Sections:
# -> Functions
# -> Toggle functions
# -> Command duration segment
# -> Git segment
# -> PWD segment
# -> Prompt
# -> Segments
#
############################################################################### ###############################################################################
set -g CMD_DURATION 0
############################################################################### ###############################################################################
# Utils # => Functions
############################################################################### ###############################################################################
function __budspencer_is_git_ahead_or_behind -d "Check if there are unpulled or unpushed commits" #####################
echo (command git rev-list --count --left-right "HEAD...@{upstream}" ^/dev/null | sed 's/[[:space:]+]/\\x1e/g') # => Toggle functions
end #####################
set symbols_style "symbols"
function __budspencer_git_status -d "Check git status" function fish_symbols_toggle_cm -d "Toggles style of symbols, press # in NORMAL or VISUAL mode"
set -l added 0 if test $symbols_style = "symbols"
set -l deleted 0 set symbols_style "numbers"
set -l modified 0
set -l renamed 0
set -l unmerged 0
set -l untracked 0
set -l git_status (command git status --porcelain ^/dev/null)
for i in (seq (count $git_status))
echo $git_status[$i] | egrep "^[ACDMT][\ MT]\ |^[ACMT]D\ " > /dev/null; and set added (math $added+1)
echo $git_status[$i] | egrep "^[\ ACMRT]D\ " > /dev/null; and set deleted (math $deleted+1)
echo $git_status[$i] | egrep "^.[MT]\ " > /dev/null; and set modified (math $modified+1)
echo $git_status[$i] | egrep "^R.\ " > /dev/null; and set renamed (math $renamed+1)
echo $git_status[$i] | egrep "^AA\ |^DD\ |^U.\ |^.U\ " > /dev/null; and set unmerged (math $unmerged+1)
echo $git_status[$i] | egrep "^\?\?\ " > /dev/null; and set untracked (math $untracked+1)
end
printf '%s\x1e%s\x1e%s\x1e%s\x1e%s\x1e%s' $added $deleted $modified $renamed $unmerged $untracked
end
function __budspencer_is_git_stashed -d "Check if there are stashed commits"
echo (command git stash list ^/dev/null | wc -l | awk '{print $1}')
end
set git_style "symbols"
function fish_git_toggle_cm -d "Toggles style of git segment, press # in NORMAL or VISUAL mode"
if test $git_style = "symbols"
set git_style "counts"
else else
set git_style "symbols" set symbols_style "symbols"
end end
set hist_lock "true"
commandline -f repaint commandline -f repaint
end end
if test "$fish_key_bindings" = "fish_vi_key_bindings" -o "$fish_key_bindings" = "my_fish_key_bindings"
bind -M default '#' fish_git_toggle_cm
bind -M visual '#' fish_git_toggle_cm
end
if set -q -x $PWDSTYLE
set -x PWDSTYLE short long none
end
set pwd_style $PWDSTYLE[1]
function fish_pwd_toggle_cm -d "Toggles style of pwd segment, press space bar in NORMAL or VISUAL mode" function fish_pwd_toggle_cm -d "Toggles style of pwd segment, press space bar in NORMAL or VISUAL mode"
for i in (seq (count $PWDSTYLE)) for i in (seq (count $PWDSTYLE))
if test $PWDSTYLE[$i] = $pwd_style if test $PWDSTYLE[$i] = $pwd_style
set pwd_style $PWDSTYLE[(math $i%(count $PWDSTYLE)+1)] set pwd_style $PWDSTYLE[(math $i%(count $PWDSTYLE)+1)]
set hist_lock "true"
commandline -f repaint commandline -f repaint
break break
end end
end end
end end
if test "$fish_key_bindings" = "fish_vi_key_bindings" -o "$fish_key_bindings" = "my_fish_key_bindings"
bind -M default ' ' fish_pwd_toggle_cm
bind -M visual ' ' fish_pwd_toggle_cm
end
#############################
# => Command duration segment
#############################
function fish_cmd_duration_cm -d "Displays the elapsed time of last command" function fish_cmd_duration_cm -d "Displays the elapsed time of last command"
set -l seconds "" set -l seconds ""
set -l minutes "" set -l minutes ""
...@@ -91,7 +77,29 @@ function fish_cmd_duration_cm -d "Displays the elapsed time of last command" ...@@ -91,7 +77,29 @@ function fish_cmd_duration_cm -d "Displays the elapsed time of last command"
end end
end end
function fish_git_prompt_cm -d "Displays the git symbols" ################
# => Git segment
################
function __budspencer_is_git_ahead_or_behind -d "Check if there are unpulled or unpushed commits"
command git rev-list --count --left-right "HEAD...@{upstream}" ^ /dev/null | sed 's|\s\+|\n|g'
end
function __budspencer_git_status -d "Check git status"
set -l git_status (command git status --porcelain ^/dev/null | cut -c 1-2)
set -l added (echo -sn $git_status\n | egrep -c "[ACDMT][ MT]|[ACMT]D")
set -l deleted (echo -sn $git_status\n | egrep -c "[ ACMRT]D")
set -l modified (echo -sn $git_status\n | egrep -c ".[MT]")
set -l renamed (echo -sn $git_status\n | egrep -c "R.")
set -l unmerged (echo -sn $git_status\n | egrep -c "AA|DD|U.|.U")
set -l untracked (echo -sn $git_status\n | egrep -c "\?\?")
echo -n $added\n$deleted\n$modified\n$renamed\n$unmerged\n$untracked
end
function __budspencer_is_git_stashed -d "Check if there are stashed commits"
command git stash list ^ /dev/null | wc -l
end
function __budspencer_prompt_git_symbols -d "Displays the git symbols"
set -l git_prompt "" set -l git_prompt ""
set -l is_repo (command git rev-parse --is-inside-work-tree ^/dev/null) set -l is_repo (command git rev-parse --is-inside-work-tree ^/dev/null)
if test $is_repo="true" if test $is_repo="true"
...@@ -99,7 +107,7 @@ function fish_git_prompt_cm -d "Displays the git symbols" ...@@ -99,7 +107,7 @@ function fish_git_prompt_cm -d "Displays the git symbols"
set -l git_ahead_behind (__budspencer_is_git_ahead_or_behind) set -l git_ahead_behind (__budspencer_is_git_ahead_or_behind)
if test (count $git_ahead_behind) -eq 2 if test (count $git_ahead_behind) -eq 2
if test $git_ahead_behind[1] -gt 0 if test $git_ahead_behind[1] -gt 0
if test $git_style = "symbols" if test $symbols_style = "symbols"
set git_prompt (set_color -o $budspencer_colors[5])" ↑" set git_prompt (set_color -o $budspencer_colors[5])" ↑"
else else
set git_prompt (set_color $budspencer_colors[5])" "$git_ahead_behind[1] set git_prompt (set_color $budspencer_colors[5])" "$git_ahead_behind[1]
...@@ -107,7 +115,7 @@ function fish_git_prompt_cm -d "Displays the git symbols" ...@@ -107,7 +115,7 @@ function fish_git_prompt_cm -d "Displays the git symbols"
end end
if test $git_ahead_behind[2] -gt 0 if test $git_ahead_behind[2] -gt 0
if test $git_style = "symbols" if test $symbols_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[5])" ↓" set git_prompt $git_prompt(set_color -o $budspencer_colors[5])" ↓"
else else
set git_prompt $git_prompt(set_color $budspencer_colors[5])" "$git_ahead_behind[2] set git_prompt $git_prompt(set_color $budspencer_colors[5])" "$git_ahead_behind[2]
...@@ -117,7 +125,7 @@ function fish_git_prompt_cm -d "Displays the git symbols" ...@@ -117,7 +125,7 @@ function fish_git_prompt_cm -d "Displays the git symbols"
set -l git_status (__budspencer_git_status) set -l git_status (__budspencer_git_status)
if test $git_status[1] -gt 0 if test $git_status[1] -gt 0
if test $git_style = "symbols" if test $symbols_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[12])" +" set git_prompt $git_prompt(set_color -o $budspencer_colors[12])" +"
else else
set git_prompt $git_prompt(set_color $budspencer_colors[12])" "$git_status[1] set git_prompt $git_prompt(set_color $budspencer_colors[12])" "$git_status[1]
...@@ -125,7 +133,7 @@ function fish_git_prompt_cm -d "Displays the git symbols" ...@@ -125,7 +133,7 @@ function fish_git_prompt_cm -d "Displays the git symbols"
end end
if test $git_status[2] -gt 0 if test $git_status[2] -gt 0
if test $git_style = "symbols" if test $symbols_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[7])" –" set git_prompt $git_prompt(set_color -o $budspencer_colors[7])" –"
else else
set git_prompt $git_prompt(set_color $budspencer_colors[7])" "$git_status[2] set git_prompt $git_prompt(set_color $budspencer_colors[7])" "$git_status[2]
...@@ -133,7 +141,7 @@ function fish_git_prompt_cm -d "Displays the git symbols" ...@@ -133,7 +141,7 @@ function fish_git_prompt_cm -d "Displays the git symbols"
end end
if test $git_status[3] -gt 0 if test $git_status[3] -gt 0
if test $git_style = "symbols" if test $symbols_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[10])" ✱" set git_prompt $git_prompt(set_color -o $budspencer_colors[10])" ✱"
else else
set git_prompt $git_prompt(set_color $budspencer_colors[10])" "$git_status[3] set git_prompt $git_prompt(set_color $budspencer_colors[10])" "$git_status[3]
...@@ -141,7 +149,7 @@ function fish_git_prompt_cm -d "Displays the git symbols" ...@@ -141,7 +149,7 @@ function fish_git_prompt_cm -d "Displays the git symbols"
end end
if test $git_status[4] -gt 0 if test $git_status[4] -gt 0
if test $git_style = "symbols" if test $symbols_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[8])" →" set git_prompt $git_prompt(set_color -o $budspencer_colors[8])" →"
else else
set git_prompt $git_prompt(set_color $budspencer_colors[8])" "$git_status[4] set git_prompt $git_prompt(set_color $budspencer_colors[8])" "$git_status[4]
...@@ -149,7 +157,7 @@ function fish_git_prompt_cm -d "Displays the git symbols" ...@@ -149,7 +157,7 @@ function fish_git_prompt_cm -d "Displays the git symbols"
end end
if test $git_status[5] -gt 0 if test $git_status[5] -gt 0
if test $git_style = "symbols" if test $symbols_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[9])" ═" set git_prompt $git_prompt(set_color -o $budspencer_colors[9])" ═"
else else
set git_prompt $git_prompt(set_color $budspencer_colors[9])" "$git_status[5] set git_prompt $git_prompt(set_color $budspencer_colors[9])" "$git_status[5]
...@@ -157,7 +165,7 @@ function fish_git_prompt_cm -d "Displays the git symbols" ...@@ -157,7 +165,7 @@ function fish_git_prompt_cm -d "Displays the git symbols"
end end
if test $git_status[6] -gt 0 if test $git_status[6] -gt 0
if test $git_style = "symbols" if test $symbols_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[4])" ●" set git_prompt $git_prompt(set_color -o $budspencer_colors[4])" ●"
else else
set git_prompt $git_prompt(set_color $budspencer_colors[4])" "$git_status[6] set git_prompt $git_prompt(set_color $budspencer_colors[4])" "$git_status[6]
...@@ -165,8 +173,8 @@ function fish_git_prompt_cm -d "Displays the git symbols" ...@@ -165,8 +173,8 @@ function fish_git_prompt_cm -d "Displays the git symbols"
end end
set -l git_stashed (__budspencer_is_git_stashed) set -l git_stashed (__budspencer_is_git_stashed)
if test git_stashed -gt 0 if test $git_stashed -gt 0
if test $git_style = "symbols" if test $symbols_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[11])" ✭" set git_prompt $git_prompt(set_color -o $budspencer_colors[11])" ✭"
else else
set git_prompt $git_prompt(set_color $budspencer_colors[11])" "$git_stashed set git_prompt $git_prompt(set_color $budspencer_colors[11])" "$git_stashed
...@@ -177,12 +185,21 @@ function fish_git_prompt_cm -d "Displays the git symbols" ...@@ -177,12 +185,21 @@ function fish_git_prompt_cm -d "Displays the git symbols"
end end
end end
function fish_pwd_prompt_cm -d "Displays the present working directory" ################
# => PWD segment
################
function __budspencer_prompt_pwd -d "Displays the present working directory"
set -l user_host " " set -l user_host " "
if test (count $SSH_CLIENT) -gt 0 if test (count $SSH_CLIENT) -gt 0
set user_host " "$USER"@"(hostname) if test $symbols_style = "symbols"
if test $pwd_style != "none" switch $pwd_style
set user_host $user_host":" case short
set user_host " "$USER"@"(hostname -s)":"
case long
set user_host " "$USER"@"(hostname -f)":"
end
else
set user_host " "$USER"@"(hostname -i)":"
end end
end end
switch $fish_bind_mode switch $fish_bind_mode
...@@ -203,40 +220,38 @@ function fish_pwd_prompt_cm -d "Displays the present working directory" ...@@ -203,40 +220,38 @@ function fish_pwd_prompt_cm -d "Displays the present working directory"
set_color -b $budspencer_colors[8] $budspencer_colors[1] set_color -b $budspencer_colors[8] $budspencer_colors[1]
end end
switch $pwd_style switch $pwd_style
case none
echo -n $user_host' '
case short case short
echo -n $user_host(prompt_pwd)' ' echo -n $user_host(prompt_pwd)' '
case long case long
echo -n $user_host$PWD' ' # | sed "s|$HOME|~|" echo -n $user_host$PWD' '
end end
set_color normal set_color normal
end end
############################################################################### ###############################################################################
# Prompt # => Prompt
############################################################################### ###############################################################################
function fish_right_prompt -d "Write out the right prompt of the budspencer theme" function fish_right_prompt -d "Write out the right prompt of the budspencer theme"
############################################################################# #############
# Segments # => Segments
############################################################################# #############
# command duration # Command duration
set ps_duration (fish_cmd_duration_cm) set ps_duration (fish_cmd_duration_cm)
# git # Git
set ps_git (fish_git_prompt_cm) set ps_git (__budspencer_prompt_git_symbols)
if test -n "$ps_git" if test -n "$ps_git"
set ps_git (set_color $budspencer_colors[3])""(set_color -b $budspencer_colors[3])""$ps_git(set_color -b $budspencer_colors[3] normal) set ps_git (set_color $budspencer_colors[3])""(set_color -b $budspencer_colors[3])""$ps_git(set_color -b $budspencer_colors[3] normal)
end end
# pwd # Pwd
set -l ps_pwd "" set -l ps_pwd ""
set ps_pwd (fish_pwd_prompt_cm) set ps_pwd (__budspencer_prompt_pwd)
# right prompt # Right prompt
echo -n $ps_duration $ps_git $ps_pwd echo -n $ps_duration $ps_git $ps_pwd
set_color normal set_color normal
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment