Created
July 15, 2016 10:09
-
-
Save ficapy/d32a62a8d37de8518fb4f1b43b8dbff5 to your computer and use it in GitHub Desktop.
使用ANSI控制符实现多行进度条 "\033[r;cH" 参考:https://www.student.cs.uwaterloo.ca/~cs452/terminal.html 缺点:运行时无法获取当前光标行数print('\033[6n', end='')
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
import random | |
import time | |
import sys | |
if not sys.stdout.isatty() or sys.version_info[0] == 2: | |
sys.exit(2) | |
class PrograssBar(): | |
index = [0] * 100 | |
def __init__(self): | |
self._set_index() | |
self.status = 0 | |
self.done = False | |
def _set_index(self): | |
for index, i in enumerate(PrograssBar.index): | |
if i == 0: | |
self.index = index + 2 | |
PrograssBar.index[index] = 1 | |
break | |
def write(self, precent): | |
print( | |
"\033[{index};1H{:<100}{precent:3}% identifier:{identifier:3}".format('=' * precent, index=self.index, | |
precent=precent, | |
identifier=self.index - 1)) | |
def update(self, precent): | |
self.status += precent | |
self.status = min(100, self.status) | |
self.write(self.status) | |
if self.status == 100: | |
self.done = True | |
target = [PrograssBar() for i in range(10)] | |
while 1: | |
process = random.choice(target) | |
if process.done: | |
continue | |
process.update(random.randint(1, 5)) | |
if process.done and all(map(lambda x: x.done, target)): | |
print('\033[{};1H'.format(len(target) + 2), end='') | |
break | |
time.sleep(0.05) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment