From b9ceda00064d7b93ee05702c2a1fa6858b079c07 Mon Sep 17 00:00:00 2001 From: Matt Hunter Date: Wed, 6 May 2026 03:55:17 -0400 Subject: Remove outer grep in prepare_element for speedup The initial grep in prepare_element serves as a guard to prevent evaluation of element outputs which _arent_ present in the user' format string. However, if in the assumed general case where most (if not all) of the elements will be enabled *somewhere* in the prompt string, this check becomes entirely redundant with the main sed statement which runs at the end of the script. It's not a matter of if an element is present, but where - which is what the sed substitutions address. The downside of this change of course is that the prompt doesn't necessarily benefit from speedups just by omiting elements from the format string. However, the appropriate feature option (eg --no-status) can be used in this case to effectively turn the corresponding element into a no-op. In return, the "general" case (with all features enabled) is a bit faster. Signed-off-by: Matt Hunter --- git-sonar | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/git-sonar b/git-sonar index 9fd3264..66648f3 100755 --- a/git-sonar +++ b/git-sonar @@ -230,19 +230,15 @@ element_status() { # Main functions -IF_PRE="%\{([^%{}]{1,}:){0,1}" -IF_POST="(:[^%{}]{1,}){0,1}\}" SED_PRE="%{\(\([^%^{^}]*\)\:\)\{0,1\}" SED_POST="\(\:\([^%^{^}]*\)\)\{0,1\}}" prepare_element() { - if echo "$PROMPT_FORMAT" | grep -qE "${IF_PRE}${1}${IF_POST}"; then - result="$($2 | sed -e 's/\//\\\//g')" - if [ -n "$result" ]; then - printf '%b' "s/${SED_PRE}${1}${SED_POST}/\\\\2${result}\\\\4/" - else - printf '%b' "s/${SED_PRE}${1}${SED_POST}//" - fi + result="$($2 | sed 's/\//\\\//g')" + if [ -n "$result" ]; then + printf '%b' "s/${SED_PRE}${1}${SED_POST}/\\\\2${result}\\\\4/" + else + printf '%b' "s/${SED_PRE}${1}${SED_POST}//" fi } -- cgit v1.2.3