#3160 PR merged: New TextPrefix function

Labels: enhancement, fixed / solved / done

jsmeix opened issue at 2024-02-20 13:41:

  • Type: Enhancement

  • Impact: Normal

  • Description of the changes in this pull request:

New TextPrefix function to
remove leading space and tab characters from input lines
and prefix each line with the argument string if specified.

The intent is to be able to properly indent multi-line message strings
in the code and output the message without the code indentation
but prefixed as needed for example like

    message="first line
             second line
             last line"
    LogPrint "Message text:$LF$( TextPrefix ' | ' <<<"$message" )"

which results the following output

Message text:
 | first line
 | second line
 | last line

jsmeix commented at 2024-02-20 13:46:

I tested it with an adapted ErrorIfDeprecated()

function ErrorIfDeprecated () {
    { (( $# >= 2 )) || BugError "Must call ErrorIfDeprecated with at least 2 arguments - feature and reason"
      local feature="$1" ; shift
      local reason="$*"

      if IsInArray "$feature" "${DISABLE_DEPRECATION_ERRORS[@]}" ; then
          LogPrint "Disabled deprecation error for '$feature'"
          return 0
      fi

      local text="Reason:
                  $reason

                  This feature is phased out in ReaR and will be eventually removed.
                  If it is indispensable, go to https://github.com/rear/rear/issues
                  and create an issue that explains why there is no alternative to it.

                  To disable this error and continue using this feature for now, set
                  DISABLE_DEPRECATION_ERRORS+=( $feature )
                 "
    } 2>>/dev/$DISPENSABLE_OUTPUT_DEV
    Error "Deprecation of '$feature'$LF$( TextPrefix '' <<<"$text" )"
}

which results for me this output

# usr/sbin/rear mkrescue
ERROR: Deprecation of 'gpt_sync_mbr'
Reason:
The 'gpt_sync_mbr' partitioning is no longer supported by SUSE since 2016
see https://github.com/rear/rear/issues/3148

This feature is phased out in ReaR and will be eventually removed.
If it is indispensable, go to https://github.com/rear/rear/issues
and create an issue that explains why there is no alternative to it.

To disable this error and continue using this feature for now, set
DISABLE_DEPRECATION_ERRORS+=( gpt_sync_mbr )

jsmeix commented at 2024-02-20 13:51:

As another test I replaced in lib/_input-output-functions.sh
some

... | sed -e 's/^/  /' | ...

with

... | TextPrefix | ...

(plain 'TextPrefix' adds two spaces indentation).

jsmeix commented at 2024-02-20 13:53:

@rear/contributors
please have a look here and provide feedback
what you think about such a TextPrefix function.

jsmeix commented at 2024-02-22 13:51:

@rear/contributors
as no news is good news
I will merge it tomorrow afternoon
unless there are severe objections.
This is only an internal helper function
so we can change it as we like if needed.

jsmeix commented at 2024-02-27 12:59:

The new TextPrefix needs to be fixed
because currently:

# function TextPrefix () { local prefix="${1-  }" ; sed -e "s/^[ \t]*/$prefix/" ; }

# (set -x ; echo foo | TextPrefix '/prefix/dir/' )
+ echo foo
+ TextPrefix /prefix/dir/
+ local prefix=/prefix/dir/
+ sed -e 's/^[ \t]*//prefix/dir//'
sed: -e expression #1, char 13: unknown option to `s'

The fix is to escape all / with / in prefix

# function TextPrefix () { local prefix="${1-  }" ; sed -e "s/^[ \t]*/${prefix//\//\\/}/" ; }

# (set -x ; echo foo | TextPrefix '/prefix/dir/' )
+ echo foo
+ TextPrefix /prefix/dir/
+ local prefix=/prefix/dir/
+ sed -e 's/^[ \t]*/\/prefix\/dir\//'
/prefix/dir/foo

The fix will be done "by the way"
in the related pull request
https://github.com/rear/rear/pull/3166
via
https://github.com/rear/rear/pull/3166/commits/1c047d93115a99023b064e8519a7d51ad2e5c36e


[Export of Github issue for rear/rear.]