Skip to content

Instantly share code, notes, and snippets.

@sebastien
Created December 6, 2017 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastien/5e8b24cb9ce6d7e5bfac3777700046a5 to your computer and use it in GitHub Desktop.
Save sebastien/5e8b24cb9ce6d7e5bfac3777700046a5 to your computer and use it in GitHub Desktop.
Mercurial prompt tools for Fish shell
function fish_hg_prompt --description 'Prompt function for Mercurial'
# BRANCH ⑂3 ‥+-?! [RNNN|D]
# 1 2 3 4 5
#
# 1 = Current branch/bookmark
# 2 = Number of heads/branches
# 3 = Modified ‥ added + removed - untracked ? merge !
# 4 = Revision number
# 5 = Time since last commit
# SEE: https://github.com/fish-shell/fish-shell/blob/master/share/functions/__fish_hg_prompt.fish#L23
# Find an hg directory above $PWD
# without calling `hg root` because that's too slow
set -l root
set -l dir $PWD
while test $dir != "/"
if test -f $dir'/.hg/dirstate'
set root $dir"/.hg"
break
end
# Go up one directory
set -l dir (string replace -r '[^/]*/?$' '' $dir)
end
if test -z "$root"
return 0
end
# =========================================================================
# BRANCH
# =========================================================================
# Read branch and bookmark
set -l branch (cat $root/branch ^/dev/null; or echo default)
if set -l bookmark (cat $root/bookmarks.current ^/dev/null)
set branch "$branch|$bookmark"
end
set -l heads (hg heads -R $root/.. -T '{rev}\n' | wc -l)
echo -n (set_color 5EE2FF)"$branch|$heads ⑂ "
# =========================================================================
# STATUS
# =========================================================================
set -l repo_status (hg status | string sub -l 2 | sort -u)
if test -z "$repo_status"
# We don't do anything if the repo is clean
set_color normal
else
set -l hg_statuses
# Take actions for the statuses of the files in the repo
for line in $repo_status
# Add a character for each file status if we have one
switch $line
case 'A '
set hg_statuses $hg_statuses added
case 'M ' ' M'
set hg_statuses $hg_statuses modified
case 'C '
set hg_statuses $hg_statuses copied
case 'D ' ' D'
set hg_statuses $hg_statuses deleted
case '\? '
set hg_statuses $hg_statuses untracked
case 'U*' '*U' 'DD' 'AA'
set hg_statuses $hg_statuses unmerged
end
end
end
# if string match -qr '^[AMCD]' $repo_status
# set_color $fish_color_hg_modified
# else
# set_color $fish_color_hg_dirty
# end
#
#
# Sort status symbols
set -g fish_prompt_hg_status_order added modified copied deleted untracked unmerged
for i in $fish_prompt_hg_status_order
if contains -- $i $hg_statuses
switch $i
case added
echo -n "+"
case modified
echo -n "~"
case copied
echo -n "→"
case deleted
echo -n "X"
case untracked
echo -n "?"
case unmerged
echo -n "«"
end
end
end
# =========================================================================
# REV
# =========================================================================
set -l hg_rev (hg -R $root/.. id -n)
set -l hg_tip (hg -R $root/.. log -r tip -T '{rev}')
# FIXME: When it's a merge, the hg id will change to the merge id
#set -l hg_revdate (hg log -R $root/.. -r $hg_rev -T "{date}")
# If hg_rev is NNN+NNN+ it's a merge
echo -n "R$hg_rev|$hg_tip"
# set -l hg_timedelta (hg log -R $root/.. -r $hg_rev -T "{date}" | python -c 'import time,sys,datetime;t=float(sys.stdin.read());t=datetime.timedelta(seconds=time.time()-t);print "{0}d{1}h".format(t.days,t.seconds/3600)')
# echo -n "|$hg_tip @ $hg_timedelta"
set_color normal
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment