bash

  1. echo -ne "\033[?25l"
  2. for i in {1..1000};do
  3. echo -ne "\033[K completed $(( $i * 100 / 1000 ))%\r"
  4. sleep 0.01
  5. done
  6. echo
  7. echo all completed!
  8. echo -ne "\033[?25h"

GIF.gif

PS:

  • -n do not output the trailing newline
  • -e enable interpretation of backslash escapes
  • \r carriage return

  • \033[?25l 关闭光标

  • \033[?25h 开启光标
  • \033[K 清除光标到行尾的内容

python

  1. import sys
  2. import time
  3. for i in range(1, 1001):
  4. sys.stdout.write('\033[K[{0}%] completed\r'.format(i * 100 / 1000))
  5. sys.stdout.flush()
  6. time.sleep(0.01)
  7. print
  8. print 'all completed!'

GIF.gif
another example

  1. import sys
  2. import time
  3. ori_line = '=' * 50
  4. for i in range(1, 1001):
  5. percent = i * 100 / 1000
  6. line = '\033[1;32m{}\033[0m{}'.format('>'*(percent/2), ori_line[percent/2:])
  7. sys.stdout.write('\033[K[{0:50}] \033[36m{1}%\033[0m\r'.format(line, percent))
  8. sys.stdout.flush()
  9. time.sleep(0.01)
  10. print
  11. print 'all completed!'

GIF.gif

python logging

  1. ...
  2. format = '\x1b[80D\x1b[1A\x1b[K[%(asctime)s \033[36m%(funcName)s\033[0m %(levelname)s] %(message)s'
  3. ...