Merge pull request #193 from tannhuber/master

Budspencer theme
This commit is contained in:
Bruno 2014-08-14 17:56:03 -03:00
commit 37e9f4a476
3 changed files with 447 additions and 0 deletions

View File

@ -0,0 +1,66 @@
# budspencer theme
Translation of zsh's prezto [budspencer theme][budspencer].
## Configuration
The theme behaves similar to vim's airline/powerline plugins. It needs a
[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
`$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
```
## Left prompt segments
- Vi mode indicator
- Git repository information
- Status symbols
* V: vi is parent process
* R: [ranger][ranger] is parent process
* ⚙: there are background jobs
* : no write permissions in present working directory
* ✔: last command succeeded
* ✘: last command failed
* ⚡: superuser indicator
## Right prompt segments
- Last command's duration time
- 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:
- ↑: git repository is ahead origin
- ↓: git repository is behind origin
- +: new files were added
- : files have been deleted
- ✱: files have been modified
- →: files have been renamed
- ═: there are unmerged commits
- ●: there are untracked files
- ✭: there are stashed commits
- Present working directory
* style can be toggled in NORMAL and in VISUAL mode with space bar
* styles implemented:
- `short` (show truncated path)
- `long` (show full path)
- `none` (show nothing)
* configurable by global array `$PWDSTYLE` (if not set, defaults to `short long none`)
## Screenshot
![budspencer theme][screenshot]
## TODO
- vi REPLACE mode, as soon as REPLACE mode is implemented within fish
[budspencer]: https://github.com/tannhuber/prezto
[font]: https://github.com/Lokaltog/powerline-fonts
[ranger]: http://ranger.nongnu.org/
[screenshot]: https://raw.githubusercontent.com/tannhuber/prezto/master/screenshots/budspencer.png

View File

@ -0,0 +1,139 @@
###############################################################################
#
# prompt theme name: budspencer
#
# description: a sophisticated airline/powerline theme
#
# author: Joseph Tannhuber
#
###############################################################################
###############################################################################
# Color definitions
###############################################################################
# define colors
# 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
# cursor color changes according to vi-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"
# some terminals cannot change the cursor color
set -l unsupported_terminals "fbterm" "st" "linux" "screen"
if contains $TERM $unsupported_terminals
set -e budspencer_cursors
end
###############################################################################
# Utils
###############################################################################
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/||')
if test (count $branch) -eq 0
set -l position (git describe --contains --all HEAD ^/dev/null)
if test (count $position) -eq 0
set -l commit (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])
else
echo -n (set_color -b $budspencer_colors[9])""(set_color $budspencer_colors[1])"  "$position" "(set_color $budspencer_colors[9])
end
else
echo -n (set_color -b $budspencer_colors[3])""(set_color $budspencer_colors[1])"  "$branch" "(set_color $budspencer_colors[3])
end
end
function fish_vi_prompt_cm -d "Displays the current mode"
switch $fish_bind_mode
case default
set_color -b $budspencer_colors[10] $budspencer_colors[1]
echo -en $budspencer_cursors[1]
if test "$fish_key_bindings" = "fish_vi_key_bindings" -o "$fish_key_bindings" = "my_fish_key_bindings"
echo -n " NORMAL "
else
echo -n " EMACS "
end
set_color -b $budspencer_colors[1] $budspencer_colors[10]
case insert
set_color -b $budspencer_colors[5] $budspencer_colors[1]
echo -en $budspencer_cursors[2]
echo -n " INSERT "
set_color -b $budspencer_colors[1] $budspencer_colors[5]
case visual
set_color -b $budspencer_colors[8] $budspencer_colors[1]
echo -en $budspencer_cursors[3]
echo -n " VISUAL "
set_color -b $budspencer_colors[1] $budspencer_colors[8]
end
end
function fish_prompt_symbols -d "Display symbols"
set_color -b $budspencer_colors[2]
echo -n ""
# indicator for vim parent process
if set -q -x VIM
set_color -o $budspencer_colors[9]
echo -n " V"
end
# indicator for ranger parent process
if set -q -x RANGER_LEVEL
set_color -o $budspencer_colors[9]
echo -n " R"
end
# background job indicator
if [ (jobs | wc -l) -gt 0 ]
set_color -o $budspencer_colors[11]
echo -n " ⚙"
end
# write protection indicator
if [ ! -w . ]
set_color -o $budspencer_colors[6]
echo -n " "
end
# status indicator
if [ $last_status -eq 0 ]
set_color -o $budspencer_colors[12]
echo -n " ✔"
else
set_color -o $budspencer_colors[7]
echo -n " ✘"
end
# superuser indicator
if [ $USER = "root" ]
set_color -o $budspencer_colors[6]
echo -n " ⚡"
end
echo -n " "
set_color -b normal $budspencer_colors[2]
end
###############################################################################
# Prompt
###############################################################################
function fish_prompt -d "Write out the left prompt of the budspencer theme"
set -g last_status $status
#############################################################################
# Segments
#############################################################################
# vi mode
set -l ps_vi ""
set ps_vi (fish_vi_prompt_cm)
# git
set -l ps_git ""
set -l git_branch_name (__budspencer_git_branch_name)
if test -n "$git_branch_name"
set ps_git $git_branch_name
end
# symbols
set -l ps_symbols (fish_prompt_symbols)
# left prompt
echo -n -s $ps_vi $ps_git $ps_symbols '' ' '
end

View File

@ -0,0 +1,242 @@
###############################################################################
# Init
###############################################################################
set -g CMD_DURATION 0
###############################################################################
# Utils
###############################################################################
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')
end
function __budspencer_git_status -d "Check git status"
set -l added 0
set -l deleted 0
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
set git_style "symbols"
end
commandline -f repaint
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"
for i in (seq (count $PWDSTYLE))
if test $PWDSTYLE[$i] = $pwd_style
set pwd_style $PWDSTYLE[(math $i%(count $PWDSTYLE)+1)]
commandline -f repaint
break
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
function fish_cmd_duration_cm -d "Displays the elapsed time of last command"
set -l seconds ""
set -l minutes ""
set -l hours ""
set -l days ""
set -l cmd_duration (math $CMD_DURATION/1000)
if test $cmd_duration -gt 0
set seconds (math $cmd_duration%68400%3600%60)"s"
if test $cmd_duration -ge 60
set minutes (math $cmd_duration%68400%3600/60)"m"
if test $cmd_duration -ge 3600
set hours (math $cmd_duration%68400/3600)"h"
if test $cmd_duration -ge 68400
set days (math $cmd_duration/68400)"d"
end
end
end
if test $last_status -ne 0
echo -n (set_color $budspencer_colors[2])""(set_color -b $budspencer_colors[2] $budspencer_colors[7])" "$days$hours$minutes$seconds
else
echo -n (set_color $budspencer_colors[2])""(set_color -b $budspencer_colors[2] $budspencer_colors[12])" "$days$hours$minutes$seconds
end
end
end
function fish_git_prompt_cm -d "Displays the git symbols"
set -l git_prompt ""
set -l is_repo (command git rev-parse --is-inside-work-tree ^/dev/null)
if test $is_repo="true"
set -l git_ahead_behind (__budspencer_is_git_ahead_or_behind)
if test (count $git_ahead_behind) -eq 2
if test $git_ahead_behind[1] -gt 0
if test $git_style = "symbols"
set git_prompt (set_color -o $budspencer_colors[5])" ↑"
else
set git_prompt (set_color $budspencer_colors[5])" "$git_ahead_behind[1]
end
end
if test $git_ahead_behind[2] -gt 0
if test $git_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[5])" ↓"
else
set git_prompt $git_prompt(set_color $budspencer_colors[5])" "$git_ahead_behind[2]
end
end
end
set -l git_status (__budspencer_git_status)
if test $git_status[1] -gt 0
if test $git_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[12])" +"
else
set git_prompt $git_prompt(set_color $budspencer_colors[12])" "$git_status[1]
end
end
if test $git_status[2] -gt 0
if test $git_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[7])" "
else
set git_prompt $git_prompt(set_color $budspencer_colors[7])" "$git_status[2]
end
end
if test $git_status[3] -gt 0
if test $git_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[10])" ✱"
else
set git_prompt $git_prompt(set_color $budspencer_colors[10])" "$git_status[3]
end
end
if test $git_status[4] -gt 0
if test $git_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[8])" →"
else
set git_prompt $git_prompt(set_color $budspencer_colors[8])" "$git_status[4]
end
end
if test $git_status[5] -gt 0
if test $git_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[9])" ═"
else
set git_prompt $git_prompt(set_color $budspencer_colors[9])" "$git_status[5]
end
end
if test $git_status[6] -gt 0
if test $git_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[4])" ●"
else
set git_prompt $git_prompt(set_color $budspencer_colors[4])" "$git_status[6]
end
end
set -l git_stashed (__budspencer_is_git_stashed)
if test git_stashed -gt 0
if test $git_style = "symbols"
set git_prompt $git_prompt(set_color -o $budspencer_colors[11])" ✭"
else
set git_prompt $git_prompt(set_color $budspencer_colors[11])" "$git_stashed
end
end
echo -n $git_prompt
end
end
function fish_pwd_prompt_cm -d "Displays the present working directory"
set -l user_host " "
if test (count $SSH_CLIENT) -gt 0
set user_host " "$USER"@"(hostname)
if test $pwd_style != "none"
set user_host $user_host":"
end
end
switch $fish_bind_mode
case default
set_color $budspencer_colors[10]
echo -n ""
set_color normal
set_color -b $budspencer_colors[10] $budspencer_colors[1]
case insert
set_color $budspencer_colors[5]
echo -n ""
set_color normal
set_color -b $budspencer_colors[5] $budspencer_colors[1]
case visual
set_color $budspencer_colors[8]
echo -n ""
set_color normal
set_color -b $budspencer_colors[8] $budspencer_colors[1]
end
switch $pwd_style
case none
echo -n $user_host' '
case short
echo -n $user_host(prompt_pwd)' '
case long
echo -n $user_host$PWD' ' # | sed "s|$HOME|~|"
end
set_color normal
end
###############################################################################
# Prompt
###############################################################################
function fish_right_prompt -d "Write out the right prompt of the budspencer theme"
#############################################################################
# Segments
#############################################################################
# command duration
set ps_duration (fish_cmd_duration_cm)
# git
set ps_git (fish_git_prompt_cm)
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)
end
# pwd
set -l ps_pwd ""
set ps_pwd (fish_pwd_prompt_cm)
# right prompt
echo -n $ps_duration $ps_git $ps_pwd
set_color normal
end