文字列を行に分割し、出力フレーム

文字列を行に分割し、出力フレーム

私は文字列を取得し、最大文字幅を設定し、限界に近い、または限界の行を単語で分割し、内容全体を整理する方法を研究してきました。

printf配列とループを使用してこれを実行しましたが、for迷惑です。たとえ初心者ですが、*毎回すべての作業を行う必要があります。

ここに画像の説明を入力してください。

関数を作成し、文字列をパラメータとして渡したいと思います。私は実際にこれを行う方法の例を見つけましたが、私がコードを作成するのに十分な理解を持っていないので、コードを使用するのは怖いです。私が見つけたものは、次のパラメータでは機能しないので必須です。プロセスリダイレクトとコマンドの組み合わせですfmt。私は無限に試しましたが、成功しませんでした。その後、foldマニュアルページで見つかった参照を試しました。fmt

ここにあるドキュメントを使ってファイルにリダイレクトしてからfmt整理できると思います。しかし、ファイルシステムを使用するprintfことはtrap何らかの方法で避けたいと思います。

printMESSAGEBOXこれが私が得たものです。コードの関数です。

#!/usr/bin/env bash

# FORMATING: BOX DRAWING
boxp="                                                                      " ; boxP="                                                                           " ; boxt="      ╭────────────────────────────────────────────────────────────────────────╮" ; boxb="      ╰────────────────────────────────────────────────────────────────────────╯" ; boxT="╭─────────────────────────────────────────────────────────────────────────────╮" ; boxB="╰─────────────────────────────────────────────────────────────────────────────╯" ; boxl="      │ " ; boxL="│ " ; boxr=" │"
# FORMATING: FOREGROUND/TEXT
ñbla () { tput setaf 0;} ; ñred () { tput setaf 1;} ; ñgre () { tput setaf 2;} ; ñyel () { tput setaf 3;} ; ñblu () { tput setaf 4;} ; ñmag () { tput setaf 5;} ; ñcya () { tput setaf 6;} ; ñwhi () { tput setaf 7;}
# FORMATING: BACKGROUND/HIGHLIGHTING
ññbla () { tput setab 0;} ; ññred () { tput setab 1;} ; ññgre () { tput setab 2;} ; ññyel () { tput setab 3;} ; ññblu () { tput setab 4;} ; ññmag () { tput setab 5;} ; ññcya () { tput setab 6;} ; ññwhi () { tput setab 7;}
# FORMATING: MISC (HIGHLIGHT-STANDOUT-ON/HIGHLIGHT-STANDOUT-OFF/RM-STYLE-BOLD-OFF/BOLD-ON/UNDERLINE-ON/UNDERLINE-OFF)
ñH () { tput smso;} ; ñh () { tput rmso;} ; ñ0 () { tput sgr0;} ; alias ñb="ñ0" ; ñB () { tput bold;} ; ñU () { tput smul;} ; ñu () { tput rmul;}
# EXIT CLEANUP
trap ñ0 SIGINT
# MESSAGE PRINTING - REVISED CODE
printBOXHEADER() {
  # SORT OUT INPUT
  while [ "$1" != "" ]; do
    case "$1" in
      --type) shift; type=$1 ;;
      --t0) type=0 ;; # don't print
      --t1) type=1 ;; # green checkmark
      --t2) type=2 ;; # red crossmark
      --t3) type=3 ;; # custom icon
      --t4) type=4 ;; # custom icon, custom color
      --ic) shift; ic=$1 ;;
      --ic-red) ic=red ;; # only obeyed with types 4
      --ic-gre) ic=gre ;; # only obeyed with types 4
      --ic-yel) ic=yel ;; # only obeyed with types 4
      --ic-blu) ic=blu ;; # only obeyed with types 4
      --ic-mag) ic=mag ;; # only obeyed with types 4
      --ic-cya) ic=cya ;; # only obeyed with types 4
      --ic-whi) ic=whi ;; # only obeyed with types 4
      --icon) shift; icon=$1 ;; # single character/emoji, # only obeyed with types 3,4
      --header) shift; header="$1" ;; # optional
    esac
    shift
  done
  # DO THAT THING YOU DO
  if [[ $type -eq 0 ]]; then
    return 0
  elif [[ $type -eq 1 ]]; then
    echo "[$(ñgre) ✔︎ $(ñ0)] $header"
  elif [[ $type -eq 2 ]]; then
    echo "[$(ñred) ✖︎ $(ñ0)] $header"
  elif [[ $type -eq 3 ]]; then
    echo "[ $icon ] $header"
  elif [[ $type -eq 4 ]]; then
    echo "[$(ñ$ic) $icon $(ñ0)] $header"
  fi
}
# MESSAGE PRINTING - HAPHAZARD CODE
msgprint() {
  printf "%s\n" "$boxt"
  for line in "${msg[@]}"; do
    printf "%s%s%s%s\n" "$boxl" "$line" "${boxp:${#line}}" "$boxr"
  done
  printf "%s\n" "$boxb"
}
msgprintw() {
  printf "%s\n" "$boxT"
  for line in "${msg[@]}"; do
    printf "%s%s%s%s\n" "$boxL" "$line" "${boxP:${#line}}" "$boxr"
  done
  printf "%s\n" "$boxB"
}
printMESSAGEBOX() {
  if [[ $widerbox =~ y ]]; then
    msgprintw
  elif [[ $widerbox =~ n ]]; then
    msgprint
  elif [[ $widerbox =~ x ]]; then
    return
  fi
}

widerbox=n
msg=(
    "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do"
    "eiusmod tempor incididunt ut labore et dolore magna aliqua."
    "Pellentesque nec nam aliquam sem et."
)

printBOXHEADER "$@"
printMESSAGEBOX

出力:

zx9:Scripting v$ messageBOXREVISITING 
      ╭────────────────────────────────────────────────────────────────────────╮
      │ Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do        │
      │ eiusmod tempor incididunt ut labore et dolore magna aliqua.            │
      │ Pellentesque nec nam aliquam sem et.                                   │
      ╰────────────────────────────────────────────────────────────────────────╯
zx9:Scripting v$ 

printBOXHEADER私はそれが最初は同じ汚れた構造を持つコードの関数と同じであることを望みますprintMESSAGEBOX。先行スペースは意図的に存在し、使用されるときは整列されprintBOXHEADER、使用されないときは注意を引くために存在します。少なくともそれが私たちの意図です。

私が試したことの1つは、fmtこれまで使用してきた配列に行を分割しようとしたことですprintf。しかし、文字列全体を渡したり、各単語を分割したりするので、間違って引用したようです。もう一つの試み枠にはまっていて、自分のラインに合わせて枠にはまっていないのです。ほぼ全部受け取りました... :(

fmt何でも歓迎し、高く評価します。私のアプローチから完全に外れた場合でも同様です(Bashとほとんどのシステムで一般的に使用されている以前のコマンド(例:... []の項目)を使用して実行される限り)。/usr/bin

*: 言い換えれば、私はちょうどスクリプトを作成し始めました:)しかし、私のスキルが不足しても良いことがわかりません。

ベストアンサー1

あなたは再発明をしているようです。、ほとんどのLinuxディストリビューション(debian、ubuntuなど)用にパッケージapt-get install boxes化されています。

テキストを入力できますfmt(またはより良い方法は基準)を入力してからを入力しますboxes

$ par -w 65 < /tmp/lorem | boxes -d stone 
+----------------------------------------------------------------+
| Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed   |
| do eiusmod tempor incididunt ut labore et dolore magna aliqua. |
| Pellentesque nec nam aliquam sem et.                           |
+----------------------------------------------------------------+

注:boxesマルチバイト文字は現在構成ファイルではサポートされていません。githubの問題#72。使用できる美しいUnicode線描画文字が多いため、これは不幸なことです。この問題が最終的に解決されることを願っています。

boxesただし、入力テキストはマルチバイト入力文字をサポートします。およびANSIエスケープシーケンス(例:色)。ただし、サポートされておらず、fmt正しくサポートされていませんpar。どちらの手順も仮定します。1文字= 1バイト

~によるとウィキペディア標準ページそして1.53ポールパッチです。Parにマルチバイト文字サポートを追加、マルチバイトパッチがありますが、parまだアップストリームコードにマージされていないため、直接パッチを適用して再構築する必要があるかもしれません。

おすすめ記事