Add variable caching

This commit is contained in:
Ilan Cosman 2021-08-21 15:05:08 -07:00
parent 5c2a6c2e03
commit 9c0ed1e4ce
7 changed files with 48 additions and 33 deletions

View File

@ -0,0 +1,18 @@
function _tide_cache_variables
# pwd
set -gx _tide_color_anchors (set_color -o $tide_pwd_color_anchors)
set -gx _tide_color_truncated_dirs (set_color $tide_pwd_color_truncated_dirs || echo)
set -gx _tide_reset_to_color_dirs (set_color normal -b $tide_pwd_bg_color; set_color $tide_pwd_color_dirs)
# git
contains git $tide_left_prompt_items $tide_right_prompt_items &&
set -gx _tide_location_color (set_color $tide_git_color_branch || echo)
# two line prompt
contains newline $tide_left_prompt_items &&
set -gx _tide_prompt_and_frame_color (set_color $tide_prompt_color_frame_and_connection -b normal)
# newline before
set -e _tide_add_newline
test "$tide_prompt_add_newline_before" = true && set -g _tide_add_newline ''
end

View File

@ -1,13 +1,12 @@
function _tide_item_git
set -l location_color (set_color $tide_git_color_branch || echo)
set -l location $location_color(git branch --show-current 2>/dev/null) || return
set -l location $_tide_location_color(git branch --show-current 2>/dev/null) || return
# --quiet = don't error if there are no commits
git rev-parse --quiet --git-dir --is-inside-git-dir --short HEAD |
read --local --line git_dir is_inside_git_dir sha
if test -z "$location" # Default to branch, then tag, then sha
set location '#'$location_color(git tag --points-at HEAD)[1] # get the first tag
test -z "$location" && set location '@'$location_color$sha
set location '#'$_tide_location_color(git tag --points-at HEAD)[1] # get the first tag
test -z "$location" && set location '@'$_tide_location_color$sha
end
# Operation
@ -61,7 +60,7 @@ function _tide_item_git
set -g tide_git_bg_color $tide_git_bg_color_unstable
end
_tide_print_item git $location_color $tide_git_icon' ' (set_color white; printf %s $location
_tide_print_item git $_tide_location_color $tide_git_icon' ' (set_color white; printf %s $location
set_color $tide_git_color_operation; printf %s ' '$tide_git_operation ' '$tide_git_step/$tide_git_total_steps
set_color $tide_git_color_upstream; printf %s ' ⇣'$upstream_behind ' ⇡'$upstream_ahead
set_color $tide_git_color_stash; printf %s ' *'$stash

View File

@ -1,20 +1,19 @@
function _tide_prompt
left_prompt=(_tide_left_prompt) right_prompt=(_tide_right_prompt) if set -q left_prompt[2] # If prompt is two lines
set -l prompt_and_frame_color (set_color $tide_prompt_color_frame_and_connection -b normal)
if test "$tide_left_prompt_frame_enabled" = true
set left_prompt[1] $prompt_and_frame_color╭─"$left_prompt[1]"
set left_prompt[2] $prompt_and_frame_color╰─"$left_prompt[2]"
set left_prompt[1] $_tide_prompt_and_frame_color╭─"$left_prompt[1]"
set left_prompt[2] $_tide_prompt_and_frame_color╰─"$left_prompt[2]"
end
if test "$tide_right_prompt_frame_enabled" = true
set right_prompt[1] "$right_prompt[1]"$prompt_and_frame_color─╮
set right_prompt[2] "$right_prompt[2]"$prompt_and_frame_color─╯
set right_prompt[1] "$right_prompt[1]"$_tide_prompt_and_frame_color─╮
set right_prompt[2] "$right_prompt[2]"$_tide_prompt_and_frame_color─╯
end
echo $right_prompt[2]
set -lx dist_btwn_sides (math $COLUMNS + 5 - ( # Regex removes color. 5 = @PWD@ length which will be replaced
string replace -ar '\e(\[[\d;]*|\(B\e\[)m(\co)?' '' "$left_prompt[1]""$right_prompt[1]" | string length))
printf '%s' (string replace @PWD@ (_tide_pwd) "$left_prompt[1]") $prompt_and_frame_color
printf '%s' (string replace @PWD@ (_tide_pwd) "$left_prompt[1]") $_tide_prompt_and_frame_color
string repeat --no-newline --max (math max 0, $dist_btwn_sides - $pwd_length) $tide_prompt_icon_connection
printf '%s' $right_prompt[1] \n $left_prompt[2]' '

View File

@ -1,18 +1,14 @@
function _tide_pwd
set -l color_anchors (set_color -o $tide_pwd_color_anchors)
set -l color_truncated_dirs (set_color $tide_pwd_color_truncated_dirs || echo)
set -l reset_to_color_dirs (set_color normal -b $tide_pwd_bg_color; set_color $tide_pwd_color_dirs)
set -l split_pwd (string replace -- $HOME '~' $PWD | string split /)
set -l split_pwd_for_length $split_pwd
# Anchor first and last directories (which may be the same)
if test -n "$split_pwd[1]" # ~/foo/bar, hightlight ~
set split_pwd_for_output $color_anchors$split_pwd[1]$reset_to_color_dirs $split_pwd[2..]
set split_pwd_for_output $_tide_color_anchors$split_pwd[1]$_tide_reset_to_color_dirs $split_pwd[2..]
else # /foo/bar, hightlight foo not empty string
set split_pwd_for_output '' $color_anchors$split_pwd[2]$reset_to_color_dirs $split_pwd[3..]
set split_pwd_for_output '' $_tide_color_anchors$split_pwd[2]$_tide_reset_to_color_dirs $split_pwd[3..]
end
set split_pwd_for_output[-1] $color_anchors$split_pwd[-1]$reset_to_color_dirs
set split_pwd_for_output[-1] $_tide_color_anchors$split_pwd[-1]$_tide_reset_to_color_dirs
if not test -w $PWD
set -g tide_pwd_icon $tide_pwd_icon_unwritable' '
@ -31,18 +27,18 @@ function _tide_pwd
# Returns true if any markers exist in dir_section
if test -z false (string split --max 2 " " -- "-o -e "$parent_dir/$dir_section/$tide_pwd_markers)
set split_pwd_for_output[$i] $color_anchors$dir_section$reset_to_color_dirs
set split_pwd_for_output[$i] $_tide_color_anchors$dir_section$_tide_reset_to_color_dirs
else if test $pwd_length -gt $dist_btwn_sides
while set -l truncation_length (math $truncation_length + 1) &&
set -l truncated (string sub --length $truncation_length -- $dir_section) &&
test $truncated != $dir_section -a (count $parent_dir/$truncated*/) -gt 1
end
set split_pwd_for_length[$i] $truncated
set split_pwd_for_output[$i] $color_truncated_dirs$truncated$reset_to_color_dirs
set split_pwd_for_output[$i] $_tide_color_truncated_dirs$truncated$_tide_reset_to_color_dirs
set pwd_length (string length "$tide_pwd_icon"(string join / $split_pwd_for_length)) # Update length
end
end
printf '%s' $reset_to_color_dirs $tide_pwd_icon
printf '%s' $_tide_reset_to_color_dirs $tide_pwd_icon
string join -- / $split_pwd_for_output
end

View File

@ -4,6 +4,7 @@ end
status is-interactive || exit
_tide_remove_unusable_items
_tide_cache_variables
# The first element in $$_tide_prompt_var is right prompt
# All remaining ones are 'left' prompt (also upper right in 2-line prompts)
@ -25,8 +26,7 @@ function fish_prompt
set -g _tide_last_pid $last_pid
end
test "$tide_prompt_add_newline_before" = true && echo
string unescape $$_tide_prompt_var[1][2..]
string unescape $_tide_add_newline $$_tide_prompt_var[1][2..]
end
function fish_right_prompt

View File

@ -9,14 +9,14 @@ function finish
_tide_menu
switch $_tide_selected_option
case y
set -e _tide_selected_option # Skip through all the _next_choices
_tide_finish
set $_tide_prompt_var
clear
end
end
function _tide_finish
set -e _tide_selected_option # Skip through all the _next_choices
# Deal with prompt char/vi mode
if contains character $fake_tide_left_prompt_items
_tide_find_and_remove vi_mode fake_tide_right_prompt_items
@ -30,10 +30,13 @@ function _tide_finish
end
end
# Finally, set the real variables
# Set the real variables
for fakeVar in (set --names | string match --regex "^fake_tide.*")
set -U (string replace 'fake_' '' $fakeVar) $$fakeVar
end
_tide_remove_unusable_items
set -e $_tide_prompt_var # Make sure old prompt won't display
# Re-initialize the prompt
source (functions --details fish_prompt)
end

View File

@ -1,17 +1,17 @@
function _fake_tide_prompt
left_prompt=(_fake_tide_left_prompt) right_prompt=(_fake_tide_right_prompt) if set -q left_prompt[2] # If prompt is two lines
set -l prompt_and_frame_color (set_color $fake_tide_prompt_color_frame_and_connection -b normal || echo)
set -l _tide_prompt_and_frame_color (set_color $fake_tide_prompt_color_frame_and_connection -b normal || echo)
if test "$fake_tide_left_prompt_frame_enabled" = true
set left_prompt[1] $prompt_and_frame_color╭─"$left_prompt[1]"
set left_prompt[2] $prompt_and_frame_color╰─"$left_prompt[2]"
set left_prompt[1] $_tide_prompt_and_frame_color╭─"$left_prompt[1]"
set left_prompt[2] $_tide_prompt_and_frame_color╰─"$left_prompt[2]"
end
if test "$fake_tide_right_prompt_frame_enabled" = true
set right_prompt[1] "$right_prompt[1]"$prompt_and_frame_color─╮
set right_prompt[2] "$right_prompt[2]"$prompt_and_frame_color─╯
set right_prompt[1] "$right_prompt[1]"$_tide_prompt_and_frame_color─╮
set right_prompt[2] "$right_prompt[2]"$_tide_prompt_and_frame_color─╯
end
printf '%s' $left_prompt[1] $prompt_and_frame_color
printf '%s' $left_prompt[1] $_tide_prompt_and_frame_color
set -l dist_btwn_sides (math $fake_columns - ( # Regex removes color codes
string replace -ar '\e(\[[\d;]*|\(B\e\[)m(\co)?' '' "$left_prompt[1]""$right_prompt[1]" | string length))