commit: f1e5f74bc86f99f138ad4fc34ce6bc794514f971 Author: Ulrich Müller <ulm <AT> gentoo <DOT> org> AuthorDate: Fri Oct 24 05:11:03 2025 +0000 Commit: Ulrich Müller <ulm <AT> gentoo <DOT> org> CommitDate: Fri Oct 24 05:11:03 2025 +0000 URL: https://gitweb.gentoo.org/proj/eselect.git/commit/?id=f1e5f74b
Don't fork new processes to apply text highlighting * libs/output.bash.in (apply_text_highlights): Modify the "text" variable by side effect. This avoids forking a new process for command substitution. (write_list_start, write_kv_list_entry) (write_numbered_list_entry): Update all callers. Bug 965038. Bug: https://bugs.gentoo.org/965038 Signed-off-by: Ulrich Müller <ulm <AT> gentoo.org> ChangeLog | 8 ++++++ libs/output.bash.in | 75 ++++++++++++++++++++++++++++------------------------- 2 files changed, 47 insertions(+), 36 deletions(-) diff --git a/ChangeLog b/ChangeLog index 7b47ff6..57707c5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,11 @@ +2025-10-24 Ulrich Müller <[email protected]> + + * libs/output.bash.in (apply_text_highlights): Modify the "text" + variable by side effect. This avoids forking a new process for + command substitution. + (write_list_start, write_kv_list_entry) + (write_numbered_list_entry): Update all callers. Bug 965038. + 2025-10-22 Ulrich Müller <[email protected]> * modules/locale.eselect (find_targets): List only C, POSIX and diff --git a/libs/output.bash.in b/libs/output.bash.in index f600ba6..91767de 100644 --- a/libs/output.bash.in +++ b/libs/output.bash.in @@ -1,5 +1,5 @@ # -*-eselect-*- vim: ft=eselect -# Copyright (c) 2005-2020 Gentoo Authors +# Copyright (c) 2005-2025 Gentoo Authors # # This file is part of the 'eselect' tools framework. # @@ -76,10 +76,9 @@ write_list_start() { colour=; normal= shift fi - echo -n -e "${colour}" - echo -n -e "$(apply_text_highlights "${colour}" "$*")" - echo -n -e "${normal}" - echo + local text="$*" + apply_text_highlights "${colour}" + echo -e "${colour}${text}${normal}" } # write_kv_list_entry PUBLIC @@ -87,7 +86,7 @@ write_list_start() { # Args may include text highlighting. If -p is passed, use 'plain' # highlighting rather than bold. write_kv_list_entry() { - local n text key val lindent rindent + local n text tlen key val lindent rindent local left=${COLOUR_LIST_LEFT} right=${COLOUR_LIST_RIGHT} local normal=${COLOUR_NORMAL} local cols=${COLUMNS:-80} @@ -103,15 +102,15 @@ write_kv_list_entry() { key=${1##*([[:space:]])} val=${2##*([[:space:]])} - echo -n -e " ${lindent}${left}" - echo -n -e "$(apply_text_highlights "${left}" "${key}")" - echo -n -e "${normal}" + tlen=${key//\%%%??%%%/} + n=$(( 26 + ${#rindent} - ${#lindent} - ${#tlen} )) - text=${key//\%%%??%%%/} - n=$(( 26 + ${#rindent} - ${#lindent} - ${#text} )) + text=${key} + apply_text_highlights "${left}" + echo -n -e " ${lindent}${left}${text}${normal}" - text=${val//\%%%??%%%/} - if [[ -z ${text} ]]; then + tlen=${val//\%%%??%%%/} + if [[ -z ${tlen} ]]; then # empty ${val}: end the line and be done echo return @@ -130,30 +129,35 @@ write_kv_list_entry() { fi fi - echo -n -e "$(space ${n})${right}" + space ${n} + echo -n -e "${right}" n=$(( 28 + ${#rindent} )) + text=${val} + apply_text_highlights "${right}" + # only loop if it doesn't fit on the same line - if [[ $(( ${n} + ${#text} )) -ge ${cols} ]] && ! is_output_mode brief; then - local i=0 spc="" - rindent=$(space ${n}) - local cwords=( $(apply_text_highlights "${right}" "${val}") ) - for text in ${val}; do - text=${text//\%%%??%%%/} + if [[ $(( ${n} + ${#tlen} )) -ge ${cols} ]] && ! is_output_mode brief; then + local i spc="" nind=${n} words cwords + read -r -d '' -a words <<< "${val}" + read -r -d '' -a cwords <<< "${text}" + for (( i = 0; i < ${#words[@]}; i++ )); do + tlen=${words[i]//\%%%??%%%/} # put the word on the same line if it fits - if [[ $(( ${n} + ${#spc} + ${#text} )) -lt ${cols} ]]; then + if [[ $(( ${n} + ${#spc} + ${#tlen} )) -lt ${cols} ]]; then echo -n -e "${spc}${cwords[i]}" - n=$(( ${n} + ${#spc} + ${#text} )) + n=$(( ${n} + ${#spc} + ${#tlen} )) # otherwise, start a new line and indent else - echo -n -e "\n${rindent}${cwords[i]}" - n=$(( ${#rindent} + ${#text} )) + echo + space ${nind} + echo -n -e "${cwords[i]}" + n=$(( ${nind} + ${#tlen} )) fi - (( i++ )) spc=" " done else - echo -n -e "$(apply_text_highlights "${right}" "${val}")" + echo -n -e "${text}" fi echo -e "${normal}" } @@ -171,15 +175,15 @@ write_numbered_list_entry() { fi if ! is_output_mode brief; then - echo -n -e " ${left}" - echo -n -e "[$(apply_text_highlights "${left}" "$1")]" - echo -n -e "${normal}" + local text=$1 + apply_text_highlights "${left}" + echo -n -e " ${left}[${text}]${normal}" space $(( 4 - ${#1} )) fi - echo -n -e "${right}" - echo -n -e "$(apply_text_highlights "${right}" "$2")" - echo -e "${normal}" + local text=$2 + apply_text_highlights "${right}" + echo -e "${right}${text}${normal}" } # write_numbered_list PUBLIC @@ -213,14 +217,13 @@ write_numbered_list() { } # apply_text_highlights INTERNAL -# Apply text highlights. First arg is the 'restore' colour, second arg -# is the text. +# Apply text highlights. Arg is the "restore" colour; +# variable "text" is modified by side effect. apply_text_highlights() { - local restore=${1:-${COLOUR_NORMAL}} text=$2 + local restore=${1:-${COLOUR_NORMAL}} text="${text//?%%HI%%%/${COLOUR_HI}}" text="${text//?%%WA%%%/${COLOUR_WARN}}" text="${text//?%%RE%%%/${restore}}" - echo -n "${text}" } # highlight PUBLIC
