Switch to bare vars in our math invocations

Using bare vars is more efficient because it makes the builtin `math`
expression cache more useful. That's because if you prefix each var with
a dollar-sign then the fish parser expands it before `math` is run.
Something like `math x + 1` can be cached since the expression is the
same each time it is run. But if you do `math $x + 1` and x==1 then you're
effectively executing `math 1 + 1`. And if x==2 the next time then you're
running `math 2 + 1`. Which makes the expression cache much less effective.
This commit is contained in:
Kurtis Rader 2017-08-24 12:38:10 -07:00
parent 56d9134534
commit bd18736ee5
12 changed files with 37 additions and 23 deletions

View File

@ -50,13 +50,14 @@ function __fish_complete_npm --description "Complete the commandline using npm's
set -lx COMP_LINE (commandline -o)
# COMP_CWORD is the index of the current word in COMP_LINE
# bash starts arrays with 0, so subtract 1
set -lx COMP_CWORD (math (count $COMP_LINE) - 1)
set -l x (count $COMP_LINE)
set -lx COMP_CWORD (math x - 1)
# COMP_POINT is the index of point/cursor when the commandline is viewed as a string
set -lx COMP_POINT (commandline -C)
# If the cursor is after the last word, the empty token will disappear in the expansion
# Readd it
if test (commandline -ct) = ""
set COMP_CWORD (math $COMP_CWORD + 1)
set COMP_CWORD (math COMP_CWORD + 1)
set COMP_LINE $COMP_LINE ""
end
command npm completion -- $COMP_LINE ^/dev/null

View File

@ -12,7 +12,7 @@ function __fish_complete_pushd_minus
# print each member of the stack, replace $HOME with ~
# Negative arguments are expected to start at "-0"
for i in (seq (count $dirstack) -1 1)
printf "%s\t%s\n" -(math $i - 1) "Rotate to "(string replace -r "^$HOME" "~" -- $dirstack[(math -$i)])
printf "%s\t%s\n" -(math i - 1) "Rotate to "(string replace -r "^$HOME" "~" -- $dirstack[(math -i)])
end
end
end

View File

@ -15,7 +15,7 @@ function __fish_complete_subcommand -d "Complete subcommand" --no-scope-shadowin
for i in $cmd
if test $skip_next -gt 0
set skip_next (math $skip_next - 1)
set skip_next (math skip_next - 1)
continue
end
@ -24,7 +24,7 @@ function __fish_complete_subcommand -d "Complete subcommand" --no-scope-shadowin
else
if contains -- $i $argv
set skip_next (math $skip_next + 1)
set skip_next (math skip_next + 1)
continue
end

View File

@ -10,7 +10,7 @@ function __fish_cursor_xterm -d 'Set cursor (xterm)'
set shape 6
end
if contains blink $argv
set shape (math $shape - 1)
set shape (math shape - 1)
end
echo -en "\e[$shape q"
end

View File

@ -478,15 +478,19 @@ function __fish_git_prompt_informative_status
set -l changedFiles (command git diff --name-status | string match -r \\w)
set -l stagedFiles (command git diff --staged --name-status | string match -r \\w)
set -l dirtystate (math (count $changedFiles) - (count (string match -r "U" -- $changedFiles)) ^/dev/null)
set -l x (count $changedFiles)
set -l y (count (string match -r "U" -- $changedFiles))
set -l dirtystate (math x - y)
set -l x(count $stagedFiles)
set -l invalidstate (count (string match -r "U" -- $stagedFiles))
set -l stagedstate (math (count $stagedFiles) - $invalidstate ^/dev/null)
set -l stagedstate (math x - invalidstate)
set -l untrackedfiles (command git ls-files --others --exclude-standard | wc -l | string trim)
set -l info
# If `math` fails for some reason, assume the state is clean - it's the simpler path
set -l state (math $dirtystate + $invalidstate + $stagedstate + $untrackedfiles ^/dev/null)
# If `math` fails for some reason, assume the state is clean - it's the simpler path.
# TBD: Can this be simplified? It should be impossible for the math command to fail here.
set -l state (math dirtystate + invalidstate + stagedstate + untrackedfiles)
if test -z "$state"
or test "$state" = 0
set info $___fish_git_prompt_color_cleanstate$___fish_git_prompt_char_cleanstate$___fish_git_prompt_color_cleanstate_done

View File

@ -34,7 +34,7 @@ function __fish_print_help --description "Print help message for the specified f
end 3<&1
end
if test -n "$cols"
set cols (math $cols - 4) # leave a bit of space on the right
set cols (math cols - 4) # leave a bit of space on the right
set rLL -rLL=$cols[1]n
end
set -lx GROFF_TMAC_PATH $__fish_datadir/groff

View File

@ -40,7 +40,9 @@ function __fish_print_packages
set cache_file $XDG_CACHE_HOME/.pac-cache.$USER
if test -f $cache_file
cat $cache_file
set age (math (date +%s) - (stat -c '%Y' $cache_file))
set -l x (date +%s)
set -l y (stat -c '%Y' $cache_file)
set age (math x - y)
set max_age 250
if test $age -lt $max_age
return
@ -65,7 +67,9 @@ function __fish_print_packages
set -l cache_file $XDG_CACHE_HOME/.zypper-cache.$USER
if test -f $cache_file
cat $cache_file
set -l age (math (date +%s) - (stat -c '%Y' $cache_file))
set -l x (date +%s)
set -l y (stat -c '%Y' $cache_file)
set -l age (math x - y)
set -l max_age 300
if test $age -lt $max_age
return
@ -85,7 +89,9 @@ function __fish_print_packages
set cache_file $XDG_CACHE_HOME/.yum-cache.$USER
if test -f $cache_file
cat $cache_file
set age (math (date +%s) - (stat -c '%Y' $cache_file))
set -l x (date +%s)
set -l y (stat -c '%Y' $cache_file)
set age (math x - y)
set max_age 21600
if test $age -lt $max_age
return
@ -107,7 +113,9 @@ function __fish_print_packages
set cache_file $XDG_CACHE_HOME/.rpm-cache.$USER
if test -f $cache_file
cat $cache_file
set age (math (date +%s) - (stat -c '%Y' $cache_file))
set -l x (date +%s)
set -l y (stat -c '%Y' $cache_file)
set age (math x - y)
set max_age 250
if test $age -lt $max_age
return

View File

@ -132,7 +132,7 @@ function __fish_svn_prompt --description "Prompt function for svn"
# the default separator is empty
set -l prompt_separator ""
for index in (seq (math "$col - $last_column"))
for index in (seq (math col - last_column))
# the prompt separator variable has to be updated with the number of separators needed to represent empty status columns (eg: if a file has the status "A +" then it should display as "A|||+" in the prompt)
set prompt_separator $prompt_separator$__fish_svn_prompt_char_separator
end

View File

@ -23,7 +23,7 @@ function contains_seq --description 'Return true if array contains a sequence'
end
if test "$s" = "$pattern[$i]"
set -e nomatch[1]
set i (math $i + 1)
set i (math i + 1)
if not set -q pattern[$i]
if set -q printnext[1]
set printnext[2] 1

View File

@ -14,9 +14,9 @@ function fish_breakpoint_prompt --description "A right prompt to be used when `b
set prompt "$prompt > "
# Make sure the prompt doesn't consume more than half the terminal width.
set -l max_len (math "$COLUMNS / 2")
set -l max_len (math COLUMNS / 2)
if test (string length -- $prompt) -gt $max_len
set prompt ...(string sub -s -(math $max_len - 3) -- $prompt)
set prompt ...(string sub -s -(math max_len - 3) -- $prompt)
end
echo -ns (set_color $fish_color_status) "BP $prompt" (set_color normal) ' '

View File

@ -53,7 +53,8 @@ function pushd --description 'Push directory to stack'
return 1
end
set rot_l (math (count $stack) - 1 - $rot_r)
set -l x (count $stack)
set rot_l (math x - 1 - rot_r)
end
# check the rotation in range
@ -63,7 +64,7 @@ function pushd --description 'Push directory to stack'
else
# rotate stack unless rot_l is 0
if test $rot_l -gt 0
set stack $stack[(math $rot_l + 1)..(count $stack)] $stack[1..$rot_l]
set stack $stack[(math rot_l + 1)..(count $stack)] $stack[1..$rot_l]
end
# now reconstruct dirstack and change directory

View File

@ -98,10 +98,10 @@ function __fish_umask_parse
set val 4
end
if string match -q '*w*' $perms
set val (math $val + 2)
set val (math val + 2)
end
if string match -q '*x*' $perms
set val (math $val + 1)
set val (math val + 1)
end
for j in $scopes_to_modify