4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

シェルスクリプトで待ち時間にスピナーを表示する

Last updated at Posted at 2020-12-06

関数

Bash

ESC=$(printf '\033')
ERASE_LINE="${ESC}[2K"
NEXT_LINE="${ESC}[1E"
PREVIOUS_LINE="${ESC}[1F"

function spinner() {
	local i=0
	local spin='⠧⠏⠛⠹⠼⠶'
	local n=${#spin}
	while true; do
		sleep 0.1
		printf "%s%s" "${NEXT_LINE}" "${ERASE_LINE}"
		printf "%s %s" "${spin:i++%n:1}" "$*"
		printf "%s\r" "${PREVIOUS_LINE}"
	done
}

説明

  • n=${#spin}: spinという変数の文字列の長さを取得しています
  • printf: ここではecho -en と同等。エスケープシーケンスを解釈して最後に改行はしない
  • ${<文字列>:n:m}: 文字列のn文字目からm文字分の長さの部分文字列 (※ Bash依存)
  • i++%n: iの数値をnで割ったあまりを返してiに1を加えます
  • $*: spinner関数に渡された引数を全て出力します
  • \r: キャリッジリターン。カーソル位置を行頭に戻します

zsh

ESC=$(printf '\033')
ERASE_LINE="${ESC}[2K"
NEXT_LINE="${ESC}[1E"
PREVIOUS_LINE="${ESC}[1F"

function spinner() {
  local idx
  local spin='⠧⠏⠛⠹⠼⠶'
  local n=${#spin}
  while true; do
    sleep 0.1
    printf "%s%s" "$NEXT_LINE" "$ERASE_LINE"
    printf "%s %s" "${spin[idx++ % n + 1]}" "$*"
    printf "%s\r" "$PREVIOUS_LINE"
  done
}

使用例

spinner proccessing... & pid=$!
sleep 5
kill $pid
wait $pid 2>/dev/null
printf "%s%s%s\n" "$NEXT_LINE" "$ERASE_LINE" 'Done!!'

参考

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?