简体   繁体   English

Bash function 与 printf Z78E6221F6393D1356681DB398F14CE6D 有时缺少

[英]Bash function with printf output missing formatting only sometimes

I have a function that takes four parameters and outputs a formatted heading.我有一个 function,它接受四个参数并输出一个格式化的标题。 Most of the time this function works, but occasionally it decides to not.大多数时候这个 function 工作,但偶尔它决定不工作。

yelB=$'\e[1;33m'
fclr=$'\e[0m'

format_title() {
    # USAGE
    #   format_title "TITLE" "{h1/h2/h3}" "${color}" "{fill_character}"
    # EXMAPLE
    #   format_title "Heading 1" "h2" "$yelB" "="
    #   ============================ Heading 1 ============================

    ftitle=$1
    heading=$2
    color=$3
    fill=$4

    total_length=100
    ftitle_spacing=" "
    ftitle_border=$'\n' # variable implemented in h1 instances only
    formatted_ftitle=""

    if [[ $heading == "h1" ]]; then
        for (( i=1; i<=$total_length; i++ )); do
            ftitle_border=$ftitle_border$fill
        done
    fi

    if [[ $heading == "h1" || $heading == "h2" ]]; then
        ftitle_spacing="          "
    fi

    ftitle_fill=$(( ( $total_length / 2 ) - ( ${#ftitle} / 2 ) - ${#ftitle_spacing} ))

    for (( i=0; i<=$ftitle_fill; i++ )); do
        formatted_ftitle=$formatted_ftitle$fill

        if (( i == $ftitle_fill - 1 )); then

            formatted_ftitle=$formatted_ftitle$ftitle_spacing$color$ftitle$fclr$ftitle_spacing$formatted_ftitle

            # Check if the fill will be even or odd; if odd, remove the last fill character
            if (( $ftitle_fill % 2 )); then
                formatted_ftitle="${formatted_ftitle::-1}"
            fi

            if [[ $heading == "h1" ]]; then
                formatted_ftitle=$ftitle_border$'\n'$formatted_ftitle$ftitle_border$'\n'
            fi

            break
        fi
    done

    printf "%s\n" "$formatted_ftitle"
}

format_title "TEST HEADING ONE WITH EVEN NUMBER CHARACTERS" "h1" "$yelB" "#"
format_title "TEST HEADING TWO WITH ODD NUMBER CHARACTERS" "h1" "$yelB" "#"
format_title "TEST HEADING ONE WITH EVEN NUMBER CHARACTERS" "h2" "$yelB" "="
format_title "TEST HEADING TWO WITH ODD NUMBER CHARACTERS" "h2" "$yelB" "="
format_title "TEST HEADING ONE WITH EVEN NUMBER CHARACTERS" "h3" "$yelB" "-"
format_title "TEST HEADING TWO WITH ODD NUMBER CHARACTERS" "h3" "$yelB" "-"

Shell Output Shell Output https://i.stack.imgur.com/aTBGV.png The idea is that all headings line up to a max of 100 characters with a fill character for titles with less than 100 characters.这个想法是,所有标题最多排列 100 个字符,少于 100 个字符的标题使用填充字符。

As you can see, some headings don't line up;如您所见,有些标题没有对齐; though trivial, it's very annoying (ignore the uncoloured heading -- a variable was misnamed when I printed the output).虽然微不足道,但很烦人(忽略未着色的标题——当我打印输出时变量命名错误)。

I can't figure out what's causing the issue of some of the headings.我无法弄清楚是什么导致了某些标题的问题。 Is it a character interpretation error with Bash when concatenating strings?连接字符串时 Bash 是否存在字符解释错误?

For h1/h2 you have ${#formatted_ftitle} = 18/19 for even/odd ${#ftitle} , respectively (ie, even=even, odd=odd).对于 h1/h2 你有${#formatted_ftitle} = 18/19对于偶数/奇数${#ftitle} ,分别(即,偶数=偶数,奇数=奇数)。

But for h3 you have ${#formatted_ftitle} = 27/28 for even/odd ${#ftitle} , respectively (ie, odd=even, even=odd).但是对于 h3 你有${#formatted_ftitle} = 27/28 for even/odd ${#ftitle} ,分别(即,奇数=偶数,偶数=奇数)。

So, it looks like you need to take into consideration not only the odd/even-ness of the filler but also the title... when determining if you should strip off a trailing character.因此,看起来您不仅需要考虑填充符的奇数/偶数,还要考虑标题......在确定是否应该删除尾随字符时。

Actually, it looks like it boils down to just having to worry about the even/odd-ness of ${#ftitle} , eg:实际上,看起来它归结为只需要担心${#ftitle}的偶数/奇数,例如:

yelB=$'\e[1;33m'
fclr=$'\e[0m'

format_title() {
    # USAGE
    #   format_title "TITLE" "{h1/h2/h3}" "${color}" "{fill_character}"
    # EXMAPLE
    #   format_title "Heading 1" "h2" "$yelB" "="
    #   ============================ Heading 1 ============================

    ftitle=$1
    heading=$2
    color=$3
    fill=$4

    total_length=100
    ftitle_spacing=" "
    ftitle_border=$'\n' # variable implemented in h1 instances only
    formatted_ftitle=""

    if [[ $heading == "h1" ]]; then
        for (( i=1; i<=$total_length; i++ )); do
            ftitle_border=$ftitle_border$fill
        done
    fi

    if [[ $heading == "h1" || $heading == "h2" ]]; then
        ftitle_spacing="          "
    fi

    ftitle_fill=$(( ( $total_length / 2 ) - ( ${#ftitle} / 2 ) - ${#ftitle_spacing} ))

    for (( i=0; i<$ftitle_fill; i++ )); do
        formatted_ftitle=$formatted_ftitle$fill
    done

    formatted_ftitle=$formatted_ftitle$ftitle_spacing$color$ftitle$fclr$ftitle_spacing$formatted_ftitle

    # if the length of ftitle is odd, remove the last fill character

    if (( ${#ftitle} % 2 )); then
        formatted_ftitle="${formatted_ftitle::-1}"
    fi


    if [[ $heading == "h1" ]]; then
        formatted_ftitle=$ftitle_border$'\n'$formatted_ftitle$ftitle_border$'\n'
    fi

    printf "%s\n" "${formatted_ftitle}"
}

format_title "TEST HEADING ONE WITH EVEN NUMBER CHARACTERS" "h1" "$yelB" "#"
format_title "TEST HEADING TWO WITH ODD NUMBER CHARACTERS" "h1" "$yelB" "#"
format_title "TEST HEADING ONE WITH EVEN NUMBER CHARACTERS" "h2" "$yelB" "="
format_title "TEST HEADING TWO WITH ODD NUMBER CHARACTERS" "h2" "$yelB" "="
format_title "TEST HEADING ONE WITH EVEN NUMBER CHARACTERS" "h3" "$yelB" "-"
format_title "TEST HEADING TWO WITH ODD NUMBER CHARACTERS" "h3" "$yelB" "-"

####################################################################################################
##################          TEST HEADING ONE WITH EVEN NUMBER CHARACTERS          ##################
####################################################################################################


####################################################################################################
###################          TEST HEADING TWO WITH ODD NUMBER CHARACTERS          ##################
####################################################################################################

==================          TEST HEADING ONE WITH EVEN NUMBER CHARACTERS          ==================
===================          TEST HEADING TWO WITH ODD NUMBER CHARACTERS          ==================
--------------------------- TEST HEADING ONE WITH EVEN NUMBER CHARACTERS ---------------------------
---------------------------- TEST HEADING TWO WITH ODD NUMBER CHARACTERS ---------------------------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM