##!/usr/bin/python
# -*-coding:UTF-8-*-

import sys
import time

class ShowProcess(object):
    """
    显示处理进度的类
    调用该类相关函数即可实现处理进度的显示
    """

    i = 0  # 当前的处理进度
    max_steps = 0  # 总共需要处理的次数
    max_arrow = 50  # 进度条的长度

    # 初始化函数，需要知道总共的处理次数
    def __init__(self, max_steps):
        self.max_steps = max_steps  # 总共需要处理的次数
        self.max_arrow = 50  # 进度条的长度
        self.i = 1  # 当前的处理进度

    # 显示函数，根据当前的处理进度i显示进度
    # 效果为[>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>]100.00%
    def show_process(self, i=None):
        if i is not None:
            self.i = i
        num_arrow = int((self.i * self.max_arrow) / self.max_steps)  # 计算显示多少个'>'
        num_line = self.max_arrow - num_arrow  # 计算显示多少个'-'
        percent = self.i * 100.0 / self.max_steps  # 计算完成进度，格式为xx.xx%
        process_bar = '\r' + '[' + '>' * num_arrow + '-' * num_line + ']' + '%.2f' % percent + '%'  # 带输出的字符串，'\r'表示不换行回到最左边
        sys.stdout.write(process_bar)  # 这两句打印字符到终端
        sys.stdout.flush()
        self.i += 1

    def close(self, words='done'):
        print('')
        # print(words)
        self.i = 1

if __name__ == '__main__':
    max_steps = 1000
    process_bar = ShowProcess(max_steps)

    for i in range(max_steps + 1):
        process_bar.show_process()
        time.sleep(0.02)
    process_bar.close()

# loading above process bar
# max_steps = pdf_pages
# process_bar = py_load.ShowProcess(max_steps)
# process_bar.show_process()
# sys.stdout.flush()
# time.sleep(0.02)
