[terminal]关于进度条的学习
在PowerShell中隐藏光标
在pip的源码C:\Python36\Lib\site-packages\pip\utils\ui.py中发现了:
1 @contextlib.contextmanager 2 def hidden_cursor(file): 3 # The Windows terminal does not support the hide/show cursor ANSI codes, 4 # even via colorama. So don't even try. 5 if WINDOWS: 6 yield 7 # We don't want to clutter the output with control characters if we're 8 # writing to a file, or if the user is running with --quiet. 9 # See https://github.com/pypa/pip/issues/3418 10 elif not file.isatty() or logger.getEffectiveLevel() > logging.INFO: 11 yield 12 else: 13 file.write(HIDE_CURSOR) 14 try: 15 yield 16 finally: 17 file.write(SHOW_CURSOR)
本来都打算放弃了, 第二天在windows上PowerShell中一试居然可以
具体HIDE_CURSOR的代码在C:\Python36\Lib\site-packages\pip\_vendor\progress\helpers.py文件中
1 HIDE_CURSOR = '\x1b[?25l' 2 SHOW_CURSOR = '\x1b[?25h'
跟进度条的斗争, 从sys.stdout.write()和sys.stdout.flush(), 到progressbar2, 再到tqdm, 再到之后因为tqdm在jupyter notebook中好看, 于是折腾anaconda, 最终试着研究tqdm源码, 发现ascii参数, 再到研究pip源码,着实学到不少, 不过像本少这种门外少年, 还是需要时间慢慢来, 才能接受看源码的设定. 苦笑...
最后附上个人定制进度...称不上条了....
1 #!/usr/bin/env python3 2 # -*- coding: UTF-8 -*- 3 __author__ = "Sigai" 4 5 # 9733, 9734 6 from collections import deque 7 import sys 8 from time import sleep 9 10 d = deque([9733, 9734, 9733, 9734, 9733, 9734]) 11 HIDE_CURSOR = '\x1b[?25l' 12 SHOW_CURSOR = '\x1b[?25h' 13 sys.stdout.write(HIDE_CURSOR) 14 while 1: 15 sys.stdout.write(f"\033[91m{''.join(chr(i) for i in d)}\033[0m") 16 sys.stdout.flush() 17 sys.stdout.write("\r") 18 sys.stdout.flush() 19 d.rotate(1) 20 sleep(.3) 21 sys.stdout.write(SHOW_CURSOR)
最新发现: https://my.oschina.net/jcseg/blog/178047
于是更改tqdm源码 _tqdm.py 实现彩色进度条, 实现jupyter notebook中类似的进度条咯~~
1 # format bar depending on availability of unicode/ascii chars 2 if ascii: 3 bar_length, frac_bar_length = divmod( 4 int(frac * N_BARS * 10), 10) 5 6 bar = '\033[42m \033[0m' * bar_length 7 frac_bar = chr(48 + frac_bar_length) if frac_bar_length \ 8 else ' '
更改源码中第416行的'#' 为上面第6行的仿真终端代码
愿望达成~~~
想去github提交下, 不过我是win10系统, cmd, powershell都有效果, 没试过别的平台
还是以后更厉害再去贡献吧...
2017.11.06 更新----------------------------------
早上看流畅的python中提到python环境变量PYTHONIOENCODING的设置, 心想会不会可以简单实现, 可是windows设置了环境变量并没有用, 可是tqdm可以输出正确的终端控制字符.
这就让我抓狂了, 所以反正无聊, 继续翻tqdm源码, 实验过程中发现 执行一次tqdm的进度条之后, cmd和powershell就可以正确输出终端控制字符了, WTF, tqdm肯定干了什么!!!
所以看trang, 发现是tqdm的shotcut而已, 只能从头看_tqdm部分了, 最后发现它import了colorama模块而且还执行了init的函数, 这TM肯定有事儿啊!
就去这个模块的PyPi看了一下, 果然init()之后就可以执行终端控制了.....可是现在只能到这了 init里面干的事太多了, 看不懂.目前可以简单利用了
1 python -c "from colorama import init;init();print('\033[42m \033[0m')"
2017.12.14 更新---------------------------------
脚本运行完成, 发出蜂鸣提示功能
1 import winsound 2 winsound.Beep(2015, 3000)
美剧<黑客军团>第一季第一集中破解心理医生那段其实是python脚本...一段打印颜色字符的代码很不错.
1 class bcolors: 2 HEADER = '\033[95m' 3 OKBLUE = '\033[94m' 4 OKGREEN = '\033[92m' 5 WARNING = '\033[93m' 6 FAIL = '\033[91m' 7 ENDC = '\033[0m' 8 BOLD = '\033[1m' 9 UNDERLINE = '\033[4m' 10 11 prompt = bcolors.OKBLUE + 'root@elliot:' + bcolors.ENDC + ' ' 12 13 print(prompt + 'ping (222.12.154.102)')
学习一下 https://ideone.com/R8VuQp
发现控制终端输出颜色已经有固定模式了
class INFO: HEADER = '\033[95m' OKBLUE = '\033[94m' OKGREEN = '\033[92m' WARNING = '\033[93m' ARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' def __init__(self): self.SUCCESS = self.OKGREEN + self.BOLD + 'Found SUCCESS!' + self.ENDC self.FAILED = self.FAIL + self.BOLD + 'Found FAILED!' + self.ENDC
http://www.freebuf.com/articles/web/127888.html
python的colorama可实现跨平台
item2文本闪烁:
echo -e "\033[5mTitle of the Program\033[0m" echo -e "\033[33;5mTitle of the Program\033[0m" echo -e "\033[33;7mTitle of the Program\033[0m" echo -e "\033[33;5;7mTitle of the Program\033[0m"


浙公网安备 33010602011771号