3-5x faster basename and dirname.

This commit is contained in:
Justin Hileman 2017-12-23 18:26:29 -08:00
parent 3d3690eb40
commit 61325754bb

View File

@ -44,6 +44,14 @@
# Helper methods # Helper methods
# ============================== # ==============================
function __bobthefish_basename -d 'basically basename, but faster'
string replace -r '^.*/' '' -- $argv
end
function __bobthefish_dirname -d 'basically dirname, but faster'
string replace -r '/[^/]+/?$' '' -- $argv
end
function __bobthefish_git_branch -S -d 'Get the current git branch (or commitish)' function __bobthefish_git_branch -S -d 'Get the current git branch (or commitish)'
set -l ref (command git symbolic-ref HEAD ^/dev/null) set -l ref (command git symbolic-ref HEAD ^/dev/null)
and string replace 'refs/heads/' "$__bobthefish_branch_glyph " $ref and string replace 'refs/heads/' "$__bobthefish_branch_glyph " $ref
@ -69,9 +77,10 @@ function __bobthefish_pretty_parent -S -a current_dir -d 'Print a parent directo
# Replace $HOME with ~ # Replace $HOME with ~
set -l real_home ~ set -l real_home ~
set -l parent_dir (string replace -r '^'"$real_home"'($|/)' '~$1' (dirname $current_dir)) set -l parent_dir (string replace -r '^'"$real_home"'($|/)' '~$1' (__bobthefish_dirname $current_dir))
if [ $parent_dir = "/" ] # Must check whether `$parent_dir = /` if using native dirname
if [ -z "$parent_dir" ]
echo -n / echo -n /
return return
end end
@ -128,7 +137,7 @@ function __bobthefish_git_project_dir -S -d 'Print the current git project base
return return
end end
set -l project_dir (dirname $git_dir) set -l project_dir (__bobthefish_dirname $git_dir)
switch $PWD/ switch $PWD/
case $project_dir/\* case $project_dir/\*
@ -151,12 +160,13 @@ function __bobthefish_hg_project_dir -S -d 'Print the current hg project base di
and return and return
set -l d $PWD set -l d $PWD
while not [ $d = / ] # Must check whether `$d = /` if using native dirname
while not [ -z "$d" ]
if [ -e $d/.hg ] if [ -e $d/.hg ]
command hg root --cwd "$d" ^/dev/null command hg root --cwd "$d" ^/dev/null
return return
end end
set d (dirname $d) set d (__bobthefish_dirname $d)
end end
end end
@ -278,7 +288,7 @@ function __bobthefish_path_segment -S -a current_dir -d 'Display a shortened for
set directory '~' set directory '~'
case '*' case '*'
set parent (__bobthefish_pretty_parent "$current_dir") set parent (__bobthefish_pretty_parent "$current_dir")
set directory (basename "$current_dir") set directory (__bobthefish_basename "$current_dir")
end end
echo -n $parent echo -n $parent
@ -737,13 +747,13 @@ function __bobthefish_prompt_git -S -a current_dir -d 'Display the actual git st
# handle work_dir != project dir # handle work_dir != project dir
if [ "$work_dir" ] if [ "$work_dir" ]
set -l work_parent (dirname $work_dir | string replace -r '^/' '') set -l work_parent (__bobthefish_dirname $work_dir)
if [ "$work_parent" ] if [ "$work_parent" ]
echo -n "$work_parent/" echo -n "$work_parent/"
end end
set_color normal set_color normal
set_color -b $__color_repo_work_tree set_color -b $__color_repo_work_tree
echo -n (basename $work_dir) echo -n (__bobthefish_basename $work_dir)
set_color normal set_color normal
set_color -b $colors set_color -b $colors
[ "$project_pwd" ] [ "$project_pwd" ]