From bd18736ee55e28651db0bb2b9c9630e8a57ec200 Mon Sep 17 00:00:00 2001 From: Kurtis Rader Date: Thu, 24 Aug 2017 12:38:10 -0700 Subject: [PATCH] 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. --- share/completions/npm.fish | 5 +++-- share/completions/pushd.fish | 2 +- share/functions/__fish_complete_subcommand.fish | 4 ++-- share/functions/__fish_cursor_xterm.fish | 2 +- share/functions/__fish_git_prompt.fish | 12 ++++++++---- share/functions/__fish_print_help.fish | 2 +- share/functions/__fish_print_packages.fish | 16 ++++++++++++---- share/functions/__fish_svn_prompt.fish | 2 +- share/functions/contains_seq.fish | 2 +- share/functions/fish_breakpoint_prompt.fish | 4 ++-- share/functions/pushd.fish | 5 +++-- share/functions/umask.fish | 4 ++-- 12 files changed, 37 insertions(+), 23 deletions(-) diff --git a/share/completions/npm.fish b/share/completions/npm.fish index 0c57814a4..cd91e81fb 100644 --- a/share/completions/npm.fish +++ b/share/completions/npm.fish @@ -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 diff --git a/share/completions/pushd.fish b/share/completions/pushd.fish index d38dc298c..5a819021a 100644 --- a/share/completions/pushd.fish +++ b/share/completions/pushd.fish @@ -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 diff --git a/share/functions/__fish_complete_subcommand.fish b/share/functions/__fish_complete_subcommand.fish index 9f5ac1d59..0a6f39abe 100644 --- a/share/functions/__fish_complete_subcommand.fish +++ b/share/functions/__fish_complete_subcommand.fish @@ -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 diff --git a/share/functions/__fish_cursor_xterm.fish b/share/functions/__fish_cursor_xterm.fish index 7a964ba99..5d99e9ec2 100644 --- a/share/functions/__fish_cursor_xterm.fish +++ b/share/functions/__fish_cursor_xterm.fish @@ -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 diff --git a/share/functions/__fish_git_prompt.fish b/share/functions/__fish_git_prompt.fish index 8416806f8..e624b5977 100644 --- a/share/functions/__fish_git_prompt.fish +++ b/share/functions/__fish_git_prompt.fish @@ -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 diff --git a/share/functions/__fish_print_help.fish b/share/functions/__fish_print_help.fish index 5c9604a7b..498eba170 100644 --- a/share/functions/__fish_print_help.fish +++ b/share/functions/__fish_print_help.fish @@ -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 diff --git a/share/functions/__fish_print_packages.fish b/share/functions/__fish_print_packages.fish index 6809039fb..514b4e505 100644 --- a/share/functions/__fish_print_packages.fish +++ b/share/functions/__fish_print_packages.fish @@ -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 diff --git a/share/functions/__fish_svn_prompt.fish b/share/functions/__fish_svn_prompt.fish index 5c649c2cd..46161d88e 100644 --- a/share/functions/__fish_svn_prompt.fish +++ b/share/functions/__fish_svn_prompt.fish @@ -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 diff --git a/share/functions/contains_seq.fish b/share/functions/contains_seq.fish index d3a20e1c8..d99dd9603 100644 --- a/share/functions/contains_seq.fish +++ b/share/functions/contains_seq.fish @@ -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 diff --git a/share/functions/fish_breakpoint_prompt.fish b/share/functions/fish_breakpoint_prompt.fish index 24d27d6d9..950658b39 100644 --- a/share/functions/fish_breakpoint_prompt.fish +++ b/share/functions/fish_breakpoint_prompt.fish @@ -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) ' ' diff --git a/share/functions/pushd.fish b/share/functions/pushd.fish index 52af5c466..e90e55de1 100644 --- a/share/functions/pushd.fish +++ b/share/functions/pushd.fish @@ -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 diff --git a/share/functions/umask.fish b/share/functions/umask.fish index d6147d680..1249d308b 100644 --- a/share/functions/umask.fish +++ b/share/functions/umask.fish @@ -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