mirror of
https://github.com/oh-my-fish/theme-bobthefish.git
synced 2026-10-07 11:31:20 +00:00
Improve git prompt performance
This commit is contained in:
@@ -65,35 +65,7 @@ function __bobthefish_version_at_least -S -a required -a actual -d 'Compare dott
|
||||
return 0
|
||||
end
|
||||
|
||||
function __bobthefish_git_branch -S -d 'Get the current git branch (or commitish)'
|
||||
set -l tag (command git describe --tags --exact-match 2>/dev/null)
|
||||
and echo "$tag_glyph $tag "
|
||||
|
||||
set -l branch (command git symbolic-ref --quiet --short HEAD 2>/dev/null)
|
||||
and begin
|
||||
[ -n "$theme_git_default_branches" ]
|
||||
or set -l theme_git_default_branches master main (command git config init.defaultBranch 2>/dev/null)
|
||||
|
||||
[ "$theme_display_git_master_branch" != yes -a "$theme_display_git_default_branch" != yes ]
|
||||
and contains $branch $theme_git_default_branches
|
||||
and echo $branch_glyph
|
||||
and return
|
||||
|
||||
# truncate the middle of the branch name, but only if it's 25+ characters
|
||||
set -l truncname $branch
|
||||
[ "$theme_use_abbreviated_branch_name" = yes ]
|
||||
and set truncname (string replace -r '^(.{17}).{3,}(.{5})$' "\$1…\$2" $branch)
|
||||
|
||||
echo $branch_glyph $truncname
|
||||
and return
|
||||
end
|
||||
|
||||
# If we've already shown a tag we don't need to show a detached branch
|
||||
if [ -z "$tag" ]
|
||||
set -l branch (command git rev-parse --short HEAD 2>/dev/null)
|
||||
echo "$detached_glyph $branch"
|
||||
end
|
||||
end
|
||||
__bobthefish_git_async_init
|
||||
|
||||
function __bobthefish_fossil_branch -S -d 'Get the current fossil branch'
|
||||
set -l branch (command fossil branch 2>/dev/null | string trim --left --chars=' *')
|
||||
@@ -271,96 +243,6 @@ function __bobthefish_project_pwd -S -a project_root_dir -a real_pwd -d 'Print t
|
||||
string replace -ar '(\.?[^/]{'"$theme_project_dir_length"'})[^/]*/' '$1/' $project_dir
|
||||
end
|
||||
|
||||
function __bobthefish_git_ahead -S -d 'Print the ahead/behind state for the current branch'
|
||||
if [ "$theme_display_git_ahead_verbose" = yes ]
|
||||
__bobthefish_git_ahead_verbose
|
||||
return
|
||||
end
|
||||
|
||||
set -l ahead 0
|
||||
set -l behind 0
|
||||
for line in (command git rev-list --left-right '@{upstream}...HEAD' 2>/dev/null)
|
||||
switch "$line"
|
||||
case '>*'
|
||||
if [ $behind -eq 1 ]
|
||||
echo '±'
|
||||
return
|
||||
end
|
||||
set ahead 1
|
||||
case '<*'
|
||||
if [ $ahead -eq 1 ]
|
||||
echo "$git_plus_minus_glyph"
|
||||
return
|
||||
end
|
||||
set behind 1
|
||||
end
|
||||
end
|
||||
|
||||
if [ $ahead -eq 1 ]
|
||||
echo "$git_plus_glyph"
|
||||
else if [ $behind -eq 1 ]
|
||||
echo "$git_minus_glyph"
|
||||
end
|
||||
end
|
||||
|
||||
function __bobthefish_git_ahead_verbose -S -d 'Print a more verbose ahead/behind state for the current branch'
|
||||
set -l commits (command git rev-list --left-right '@{upstream}...HEAD' 2>/dev/null)
|
||||
or return
|
||||
|
||||
set -l behind 0
|
||||
set -l ahead 0
|
||||
for commit in $commits
|
||||
switch "$commit"
|
||||
case '<*'
|
||||
set behind (math $behind + 1)
|
||||
case '>*'
|
||||
set ahead (math $ahead + 1)
|
||||
end
|
||||
end
|
||||
|
||||
switch "$ahead $behind"
|
||||
case '' # no upstream
|
||||
case '0 0' # equal to upstream
|
||||
return
|
||||
case '* 0' # ahead of upstream
|
||||
echo "$git_ahead_glyph$ahead"
|
||||
case '0 *' # behind upstream
|
||||
echo "$git_behind_glyph$behind"
|
||||
case '*' # diverged from upstream
|
||||
echo "$git_ahead_glyph$ahead$git_behind_glyph$behind"
|
||||
end
|
||||
end
|
||||
|
||||
function __bobthefish_git_dirty_verbose -S -d 'Print a more verbose dirty state for the current working tree'
|
||||
set -l diff_stats (command git diff --numstat)
|
||||
or return
|
||||
|
||||
set -l added 0
|
||||
set -l removed 0
|
||||
for line in $diff_stats
|
||||
set -l fields (string split \t -- $line)
|
||||
string match -rq '^\d+$' -- $fields[1]
|
||||
and set added (math $added + $fields[1])
|
||||
string match -rq '^\d+$' -- $fields[2]
|
||||
and set removed (math $removed + $fields[2])
|
||||
end
|
||||
|
||||
set -l changes "+$added/-$removed"
|
||||
echo "$changes " | string replace -r '(\+0/(-0)?|/-0)' ''
|
||||
end
|
||||
|
||||
function __bobthefish_git_stashed -S -d 'Print the stashed state for the current branch'
|
||||
if [ "$theme_display_git_stashed_verbose" = yes ]
|
||||
set -l stashed (command git rev-list --walk-reflogs --count refs/stash 2>/dev/null)
|
||||
or return
|
||||
|
||||
echo -n "$git_stashed_glyph$stashed"
|
||||
else
|
||||
command git rev-parse --verify --quiet refs/stash >/dev/null
|
||||
and echo -n "$git_stashed_glyph"
|
||||
end
|
||||
end
|
||||
|
||||
# ==============================
|
||||
# Segment functions
|
||||
# ==============================
|
||||
@@ -1179,123 +1061,11 @@ function __bobthefish_prompt_screen -S -d 'Display the screen name'
|
||||
set_color normal
|
||||
end
|
||||
|
||||
function __bobthefish_prompt_git -S -a git_root_dir -a real_pwd -d 'Display the actual git state'
|
||||
set -l dirty ''
|
||||
if [ "$theme_display_git_dirty" != no ]
|
||||
set -l show_dirty (command git config --bool bash.showDirtyState 2>/dev/null)
|
||||
if [ "$show_dirty" != false ]
|
||||
set dirty (command git diff --no-ext-diff --quiet --exit-code 2>/dev/null; or echo -n "$git_dirty_glyph")
|
||||
if [ "$dirty" -a "$theme_display_git_dirty_verbose" = yes ]
|
||||
set dirty "$dirty"(__bobthefish_git_dirty_verbose)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
set -l staged (command git diff --cached --no-ext-diff --quiet --exit-code 2>/dev/null; or echo -n "$git_staged_glyph")
|
||||
set -l stashed (__bobthefish_git_stashed)
|
||||
set -l ahead (__bobthefish_git_ahead)
|
||||
|
||||
set -l new ''
|
||||
if [ "$theme_display_git_untracked" != no ]
|
||||
set -l show_untracked (command git config --bool bash.showUntrackedFiles 2>/dev/null)
|
||||
if [ "$show_untracked" != false ]
|
||||
set new (command git ls-files --other --exclude-standard --directory --no-empty-directory "$git_root_dir" 2>/dev/null)
|
||||
if [ "$new" ]
|
||||
set new "$git_untracked_glyph"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
set -l flags "$dirty$staged$stashed$ahead$new"
|
||||
|
||||
[ "$flags" ]
|
||||
and set flags " $flags"
|
||||
|
||||
set -l flag_colors $color_repo
|
||||
if [ "$dirty" ]
|
||||
set flag_colors $color_repo_dirty
|
||||
else if [ "$staged" ]
|
||||
set flag_colors $color_repo_staged
|
||||
end
|
||||
|
||||
__bobthefish_path_segment $git_root_dir project
|
||||
|
||||
__bobthefish_start_segment $flag_colors
|
||||
echo -ns (__bobthefish_git_branch) $flags ' '
|
||||
set_color normal
|
||||
|
||||
if [ "$theme_git_worktree_support" != yes ]
|
||||
set -l project_pwd (__bobthefish_project_pwd $git_root_dir $real_pwd)
|
||||
if [ "$project_pwd" ]
|
||||
if [ -w "$real_pwd" ]
|
||||
__bobthefish_start_segment $color_path
|
||||
else
|
||||
__bobthefish_start_segment $color_path_nowrite
|
||||
end
|
||||
|
||||
echo -ns $project_pwd ' '
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
set -l project_pwd (command git rev-parse --show-prefix 2>/dev/null | string trim --right --chars=/)
|
||||
set -l work_dir (command git rev-parse --show-toplevel 2>/dev/null)
|
||||
|
||||
# only show work dir if it's a parent…
|
||||
if [ "$work_dir" ]
|
||||
switch $real_pwd/
|
||||
case $work_dir/\*
|
||||
string match "$git_root_dir*" $work_dir >/dev/null
|
||||
and set work_dir (string sub -s (math 1 + (string length $git_root_dir)) $work_dir)
|
||||
case \*
|
||||
set -e work_dir
|
||||
end
|
||||
end
|
||||
|
||||
if [ "$project_pwd" -o "$work_dir" ]
|
||||
set -l colors $color_path
|
||||
if not [ -w "$real_pwd" ]
|
||||
set colors $color_path_nowrite
|
||||
end
|
||||
|
||||
__bobthefish_start_segment $colors
|
||||
|
||||
# handle work_dir != project dir
|
||||
if [ "$work_dir" ]
|
||||
set -l work_parent (__bobthefish_dirname $work_dir)
|
||||
if [ "$work_parent" ]
|
||||
echo -n "$work_parent/"
|
||||
end
|
||||
|
||||
set_color normal
|
||||
set_color -b $color_repo_work_tree
|
||||
echo -n (__bobthefish_basename $work_dir)
|
||||
|
||||
set_color normal
|
||||
set_color -b $colors
|
||||
[ "$project_pwd" ]
|
||||
and echo -n /
|
||||
end
|
||||
|
||||
echo -ns $project_pwd ' '
|
||||
function __bobthefish_prompt_git -S -a git_root_dir -a real_pwd -a repaint -d 'Display the actual git state'
|
||||
if __bobthefish_git_async_enabled
|
||||
__bobthefish_prompt_git_async $git_root_dir $real_pwd $repaint
|
||||
else
|
||||
set project_pwd $real_pwd
|
||||
|
||||
string match "$git_root_dir*" $project_pwd >/dev/null
|
||||
and set project_pwd (string sub -s (math 1 + (string length $git_root_dir)) $project_pwd)
|
||||
|
||||
set project_pwd (string trim --left --chars=/ -- $project_pwd)
|
||||
|
||||
if [ "$project_pwd" ]
|
||||
set -l colors $color_path
|
||||
if not [ -w "$real_pwd" ]
|
||||
set colors $color_path_nowrite
|
||||
end
|
||||
|
||||
__bobthefish_start_segment $colors
|
||||
|
||||
echo -ns $project_pwd ' '
|
||||
end
|
||||
__bobthefish_prompt_git_sync $git_root_dir $real_pwd
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1328,6 +1098,9 @@ end
|
||||
function fish_prompt -d 'bobthefish, a fish theme optimized for awesome'
|
||||
# Save the last status for later (do this before anything else)
|
||||
set -l last_status $status
|
||||
set -l git_async_repaint
|
||||
set -e __bobthefish_git_async_repaint_pending
|
||||
and set git_async_repaint 1
|
||||
|
||||
# Use a simple prompt on dumb terminals.
|
||||
if [ "$TERM" = dumb ]
|
||||
@@ -1382,7 +1155,7 @@ function fish_prompt -d 'bobthefish, a fish theme optimized for awesome'
|
||||
case ''
|
||||
__bobthefish_prompt_dir $real_pwd
|
||||
case "$git_root_dir"
|
||||
__bobthefish_prompt_git $git_root_dir $real_pwd
|
||||
__bobthefish_prompt_git $git_root_dir $real_pwd $git_async_repaint
|
||||
case "$hg_root_dir"
|
||||
__bobthefish_prompt_hg $hg_root_dir $real_pwd
|
||||
case "$fossil_root_dir"
|
||||
|
||||
Reference in New Issue
Block a user