mirror of
https://github.com/fish-shell/fish-shell.git
synced 2026-05-27 08:43:09 -03:00
Optimize fish_config theme choose
Just following basic shellscript optimization: - Remove a useless use of cat (`status get-file` takes microseconds, `status get-file | cat` is on the order of a millisecond - slower with bigger $PATH) - Pipe, don't run in a loop - Filter early This reduces the time taken from 12ms to 6ms on one of my systems, and 6.5ms to 4.5ms on another. This is paid on every single shell startup, including non-interactively, so it's worth it. There's more to investigate, but this is a good first pass.
This commit is contained in:
@@ -6,15 +6,16 @@ function __fish_theme_cat -a theme_name
|
||||
echo >&2 Searched (__fish_theme_dir) "and `status list-files themes`"
|
||||
return 1
|
||||
end
|
||||
set -l theme_data (__fish_config_with_file $theme_path cat)
|
||||
set -l theme_data (if string match -q '/*' -- $theme_path; cat $theme_path; else status get-file $theme_path; end)
|
||||
set -l allowed_lines \
|
||||
'\s*' \
|
||||
'\s*#.*' \
|
||||
'\[(dark|light|unknown)\]' \
|
||||
(__fish_theme_variable_filter)
|
||||
set allowed_lines "^($(string join -- '|' $allowed_lines))\$"
|
||||
for line in $theme_data
|
||||
string match -rq -- $allowed_lines $line
|
||||
printf '%s\n' $theme_data | string match -rvq -- $allowed_lines
|
||||
and for line in $theme_data
|
||||
string match -rq -- $allowed_lines $theme_data
|
||||
or printf >&2 "error: unsupported line in theme '%s': %s\n" $theme_name $line
|
||||
end
|
||||
string join \n $theme_data
|
||||
|
||||
@@ -287,7 +287,7 @@ function __fish_config_theme_choose
|
||||
|
||||
set -l color_theme
|
||||
__fish_config_theme_canonicalize
|
||||
set -l theme_data (type -q cat && __fish_theme_cat $theme_name)
|
||||
set -l theme_data (__fish_theme_cat $theme_name)
|
||||
or return
|
||||
set -l color_themes dark light unknown
|
||||
set -l theme_is_color_theme_aware false
|
||||
@@ -340,7 +340,7 @@ function __fish_config_theme_choose
|
||||
end
|
||||
|
||||
set -l color_theme
|
||||
string join \n -- $theme_data |
|
||||
string match -re -- (__fish_theme_variable_filter)'|^\[.*\]$' $theme_data |
|
||||
while read -lat toks
|
||||
if $theme_is_color_theme_aware
|
||||
for ct in $color_themes
|
||||
@@ -354,8 +354,8 @@ function __fish_config_theme_choose
|
||||
end
|
||||
end
|
||||
set -l varname $toks[1]
|
||||
string match -rq -- (__fish_theme_variable_filter) "$varname"
|
||||
or continue
|
||||
string match -q '[*' -- $varname
|
||||
and continue
|
||||
# If we're supposed to set universally, remove any shadowing globals
|
||||
# so the change takes effect immediately (and there's no warning).
|
||||
if test $scope = -U; and set -qg $varname
|
||||
|
||||
Reference in New Issue
Block a user