パディング中心のソートを使用して変数を印刷するには?

パディング中心のソートを使用して変数を印刷するには?

$myvarパディングを端末の中央に、両側が画面の=端にくるように印刷するにはどうすればよいですか?

答え1

私はこの可能な答えを見つけるのに役立つ2つの情報をstackexchangeネットワークで見つけました。

しかし、この答えのコードは私のコードです。

詳細については、編集履歴をご覧ください。ナンセンスと「ステップバイステップ」の両方を削除しました。


最良の方法は次のとおりです。

center() {
  termwidth="$(tput cols)"
  padding="$(printf '%0.1s' ={1..500})"
  printf '%*.*s %s %*.*s\n' 0 "$(((termwidth-2-${#1})/2))" "$padding" "$1" 0 "$(((termwidth-1-${#1})/2))" "$padding"
}
center "Something I want to print"

端子80列出力:

========================== Something I want to print ===========================

パディングは単一文字である必要はなく、単一文字でもかまいません。実際、padding変数はそうではありません。上記のコードでは、長さは500文字です。行のみを変更して他の種類のパディングを使用できますpadding

padding="$(printf '%0.2s' ^v{1..500})"

結果:

^v^v^v^v^v^v^v^v^v^v^v^v^v Something I want to print ^v^v^v^v^v^v^v^v^v^v^v^v^v^

別の便利な用途は次のとおりです。

clear && center "This is my header"

答え2

そしてzsh

$ var='some text'                                                              |
$ print -r - ${(l[COLUMNS/2][=]r[COLUMNS-COLUMNS/2][=])var}                    |
===================================some text===================================|

左パッドと右パッドの列数は=端子の半分です。このmフラグを使用すると、幅がゼロまたは幅が2つの文字がある場合は、パディングの文字幅を考慮します$var

bashGNUを使用すると、wc同じことができます。

var='some text'
width=$(wc -L <<< "$var")
printf -v pad "%$(( (COLUMNS - width) / 2 ))s"
pad=${pad// /=}
printf '%s%.*s\n' "$pad$var$pad" "$(((COLUMNS-width)%2))" =

wc -L私たちはディスプレイの幅を得るためにGNUを使います$varwidth=${#var}これは、文字列に単一の幅文字しか含まれていない場合に機能します。

いずれにせよ、$varこれらの仮定には制御文字(TAB、NL、CR、カラーエスケープシーケンスなどを含む)は含まれません。

答え3

この提案は実用的に見えますが、端末がterminfo関数、、、、およびcolscfをサポートしていることを意味します。hpaechcufcud1出力(1)、用語情報(5)、情報CMP(1m)。

#!/bin/bash

# Function "center_text": center the text with a surrounding border

# first argument: text to center
# second argument: glyph which forms the border
# third argument: width of the padding

center_text()
{
    local terminal_width=$(tput cols)    # query the Terminfo database: number of columns
    local text="${1:?}"                  # text to center
    local glyph="${2:-=}"                # glyph to compose the border
    local padding="${3:-2}"              # spacing around the text

    local border=                        # shape of the border
    local text_width=${#text}

    # the border is as wide as the screen
    for ((i=0; i<terminal_width; i++))
    do
        border+="${glyph}"
    done

    printf "$border"

    # width of the text area (text and spacing)
    local area_width=$(( text_width + (padding * 2) ))

    # horizontal position of the cursor: column numbering starts at 0
    local hpc=$(( (terminal_width - area_width) / 2 ))

    tput hpa $hpc                       # move the cursor to the beginning of the area

    tput ech $area_width                # erase the border inside the area without moving the cursor
    tput cuf $padding                   # move the cursor after the spacing (create padding)

    printf "$text"                      # print the text inside the area

    tput cud1                           # move the cursor on the next line
}

center_text "Something I want to print" "~"
center_text "Something I want to print" "=" 6

次の提案は、@より強力でスケーラブルで明確です。ワイルドカード~の解決策

#!/bin/bash

# Function "center_text": center the text with a surrounding border

# first argument: text to center
# second argument: glyph which forms the border
# third argument: width of the padding

center_text()
{

    local terminal_width=$(tput cols)     # query the Terminfo database: number of columns
    local text="${1:?}"                   # text to center
    local glyph="${2:-=}"                 # glyph to compose the border
    local padding="${3:-2}"               # spacing around the text

    local text_width=${#text}             

    local border_width=$(( (terminal_width - (padding * 2) - text_width) / 2 ))

    local border=                         # shape of the border

    # create the border (left side or right side)
    for ((i=0; i<border_width; i++))
    do
        border+="${glyph}"
    done

    # a side of the border may be longer (e.g. the right border)
    if (( ( terminal_width - ( padding * 2 ) - text_width ) % 2 == 0 ))
    then
        # the left and right borders have the same width
        local left_border=$border
        local right_border=$left_border
    else
        # the right border has one more character than the left border
        # the text is aligned leftmost
        local left_border=$border
        local right_border="${border}${glyph}"
    fi

    # space between the text and borders
    local spacing=

    for ((i=0; i<$padding; i++))
    do
        spacing+=" "
    done

    # displays the text in the center of the screen, surrounded by borders.
    printf "${left_border}${spacing}${text}${spacing}${right_border}\n"
}

center_text "Something I want to print" "~"
center_text "Something I want to print" "=" 6

答え4

あなたはこれを行うことができます

WIDTH=$(tput cols)
WIDTHDIVIDED=$(($WIDTH/2)) # Solves "tput cols divided by 2"
clear
tput cup 0 $WIDTHDIVIDED # Puts the start of the PS1 at column 0 and the terminal width divided by two

お役に立てば幸いです。

関連情報