端末の右下に文字列を出力

端末の右下に文字列を出力

端末の右下に文字列を出力するには?

ベストアンサー1

string=whatever
stty size | {
  read y x
  tput sc # save cursor position
  tput cup "$((y - 1))" "$((x - ${#string}))" # position cursor
  printf %s "$string"
  tput rc # restore cursor.
}

すべての文字が$string1つであると仮定セル広い($string制御文字(改行、タブなど)を含まない)。

もしあなたのひも幅にゼロ(結合文字など)または二重幅文字を含めることができる場合は、ksh93の書式printf指定子%Lsを使用して文字の幅に基づいて書式設定できます。

string='whatéver'
# aka string=$'\uFF57\uFF48\uFF41\uFF54\uFF45\u0301\uFF56\uFF45\uFF52'
stty size | {
  read y x
  tput sc # save cursor position
  tput cup "$((y - 1))" 0 # position cursor
  printf "%${x}Ls" "$string"
  tput rc # restore cursor.
}

ただし、これにより最後の行の先頭が削除されます。

おすすめ記事