Skip to content
Browse files

a smarter right prompt

  • Loading branch information...
1 parent 3faae4f commit 010ddf33c51bcd8b7877fc83f64673b190758131 @orefalo committed Nov 23, 2013
Showing with 87 additions and 143 deletions.
  1. +87 −0 fish/right_prompt.fish
  2. +0 −143 fish/test.fish
View
87 fish/right_prompt.fish
@@ -0,0 +1,87 @@
+function _common_section
+ printf $c1
+ printf $argv[1]
+ printf $c0
+ printf ":"
+ printf $c2
+ printf $argv[2]
+ printf $argv[3]
+ printf $c0
+ printf ", "
+end
+
+function section
+ _common_section $argv[1] $c3 $argv[2] $ce
+end
+
+function error
+ _common_section $argv[1] $ce $argv[2] $ce
+end
+
+function fish_prompt
+ # $status gets nuked as soon as something else is run, e.g. set_color
+ # so it has to be saved asap.
+ set -l last_status $status
+
+ # c0 to c4 progress from dark to bright
+ # ce is the error colour
+ set -g c0 (set_color 005284)
+ set -g c1 (set_color 0075cd)
+ set -g c2 (set_color 009eff)
+ set -g c3 (set_color 6dc7ff)
+ set -g c4 (set_color ffffff)
+ set -g ce (set_color $fish_color_error)
+
+ # Clear the line because fish seems to emit the prompt twice. The initial
+ # display, then when you press enter.
+ printf "\033[K"
+
+ # Current time
+ printf (date "+$c2%H$c0:$c2%M$c0:$c2%S, ")
+ if [ $last_status -ne 0 ]
+ error last $last_status
+ set -ge status
+ end
+
+ # Track the last non-empty command. It's a bit of a hack to make sure
+ # execution time and last command is tracked correctly.
+ set -l cmd_line (commandline)
+ if test -n "$cmd_line"
+ set -g last_cmd_line $cmd_line
+ set -ge new_prompt
+ else
+ set -g new_prompt true
+ end
+
+ # Show last execution time and growl notify if it took long enough
+ set -l now (date +%s)
+ if test $last_exec_timestamp
+ set -l taken (math $now - $last_exec_timestamp)
+ if test $taken -gt 10 -a -n "$new_prompt"
+ error taken $taken
+ # Clear the last_cmd_line so pressing enter doesn't repeat
+ set -ge last_cmd_line
+ end
+ end
+ set -g last_exec_timestamp $now
+
+ # Show loadavg when too high
+ set -l load1m (uptime | grep -o '[0-9]\+\.[0-9]\+' | head -n1)
+ set -l ncpu 1
+ if not set ncpu (sysctl hw.ncpu | cut -f2 -d' ')
+ if not set ncpu (grep -c ^processor /proc/cpuinfo)
+ set ncpu 1
+ end
+ end
+ set -l load1m_test (math $load1m \* 100 / $ncpu)
+ if test $load1m_test -gt 100
+ error load $load1m
+ end
+
+ # Show disk usage when low
+ set -l du (df / | tail -n1 | sed "s/ */ /g" | cut -d' ' -f 5 | cut -d'%' -f1)
+ if test $du -gt 80
+ error du $du%%
+ end
+
+end
View
143 fish/test.fish
@@ -1,143 +0,0 @@
-#### PARSE STATUS
-set -l untracked 0
-set -l added 0
-set -l modified 0
-
-#set IFS '\n'
-set -l git_status (command git status --porcelain ^/dev/null)
-
-for line in $git_status
- set -l x (echo $line | cut -c 1)
- set -l y (echo $line | cut -c 2)
-
- if test $x = '?'
- set untracked (math $untracked + 1)
- else
- test $x != ' '; and set added (math $added + 1)
- test $y != ' '; and set modified (math $modified + 1)
- end
-end
-
-echo $untracked $added $modified
-
-
-
-function __g2_getBranchOp
-
- set -l git_dir (command git rev-parse --git-dir 2>/dev/null)
- test ! -d $git_dir; and return 1
-
- ### Returns the branch name
-
-
- # endgures the state of the repo
- set -l op ''
- set -l branch ''
-
- command git ls-tree -quiet HEAD 2>/dev/null
-
- if test $status -eq 128
- set op 'init'
- else
-
- set -l step 0
- set -l total 0
-
- if test -d "$git_dir/rebase-merge"
-
- set step (cat "$git_dir/rebase-merge/msgnum")
- set total (cat "$git_dir/rebase-merge/end")
- set branch (cat "$git_dir/rebase-merge/head-name")" $step/$total"
-
- if test -f "$git_dir/rebase-merge/interactive"
- set op "rebase -i"
- else
- set op "rebase -m"
- end
-
- else
-
- if test -d "$git_dir/rebase-apply"
-
- set step (cat "$git_dir/rebase-apply/next")
- set total (cat "$git_dir/rebase-apply/last")
-
- if test -f "$git_dir/rebase-apply/rebasing"
- set op "rebase"
- else
- if test -f "$git_dir/rebase-apply/applying"
- set op "am"
- else
- set op "am/rebase"
- end
- end
- else
- if test -f "$git_dir/MERGE_HEAD"
- set op "merge"
- else
- if test -f "$git_dir/CHERRY_PICK_HEAD"
- set op "cherrypick"
- else
- if test -f "$git_dir/REVERT_HEAD"
- set op "revert"
- else
- if test -f "$git_dir/BISECT_LOG"
- set op "bisect"
- end
- end
- end
- end
- end
-
-
- if not set branch (command git symbolic-ref HEAD 2>/dev/null)
-
- test ! "$op"; and set op "detached"
-
- if not set branch (command git describe --tags --exact-match HEAD 2>/dev/null)
- if not set branch (cut -c 1-7 "$git_dir/HEAD" 2>/dev/null)'...'
- set branch "unknown"
- end
- end
-
- if test -n "$step" -a -n "$total"
- set branch "[$branch $step/$total]"
- else
- set branch "[$branch]"
- end
-
- end
- end
-
- end
-
- echo $op
- echo $branch | sed 's/refs\/heads\///g'
-end
-
-set v (__g2_getBranchOp)
-echo branch:$v[1]
-echo op:$v[2]
-
-
-#### GET GIT HEX-REVISION
-set -l rawhex (command git rev-parse --short HEAD)
-echo $rawhex
-
-
-### AHEAD BEHIND
-function __g2_prompt_aheadbehind --argument-names local
-
- set -l remote (__g2_getremote)
-
- set -l cnt (command git rev-list --left-right --count $local...$remote -- ^/dev/null |tr \t \n)
- if test $cnt[1] -gt 0 -a $cnt[2] -gt 0
- echo -n '±'
- else
- test $cnt[1] -gt 0; and echo -n '+'
- test $cnt[2] -gt 0; and echo -n '-'
- end
-end
-
-__g2_prompt_aheadbehind master
-echo

0 comments on commit 010ddf3

Please sign in to comment.
Something went wrong with that request. Please try again.