又一个终端旋转器
yaspin的Python项目详细描述
yaspin:yeta另一个终端spinner用于python
Yaspin提供了一个功能齐全的终端微调器,用于显示长挂起操作期间的进度。
将其用作context manager,很容易集成到现有的代码库中。 或者作为函数decorator:
importtimefromyaspinimportyaspin# Context manager:withyaspin():time.sleep(3)# time consuming code# Function decorator:@yaspin(text="Loading...")defsome_operations():time.sleep(3)# time consuming codesome_operations()
yaspin还提供了直观而强大的api。例如,你可以很容易地召唤鲨鱼:
importtimefromyaspinimportyaspinwithyaspin().white.bold.shark.on_blueassp:sp.text="White bold shark in a blue sea"time.sleep(5)
功能
- 无外部依赖关系
- 在所有主要的cpython版本(2.7,3.4,3.5,3.6,3.7),pypy和pypy3
- 支持来自cli-spinners 的所有(60+)微调器
- 支持所有colors,highlights,attributes及其来自termcolorlibrary的混合
- 易于与其他命令行库组合,例如prompt-toolkit
- 灵活的API,易于与现有代码 集成
- 用于处理posix的用户友好api signals
- 安全的管道和重定向:
$ python script_that_uses_yaspin.py > script.log
$ python script_that_uses_yaspin.py | grep ERROR
安装
从PyPI使用pippackage manager:
pip install --upgrade yaspin
或者从github安装最新的源代码:
pip install https://github.com/pavdmyt/yaspin/archive/master.zip
使用量
基本示例
# -*- coding: utf-8 -*-importtimefromrandomimportrandintfromyaspinimportyaspinwithyaspin(text="Loading",color="yellow")asspinner:time.sleep(2)# time consuming codesuccess=randint(0,1)ifsuccess:spinner.ok("✅ ")else:spinner.fail("? ")
也可以手动控制微调器:
# -*- coding: utf-8 -*-importtimefromyaspinimportyaspinspinner=yaspin()spinner.start()time.sleep(3)# time consuming tasksspinner.stop()
从cli-spinners运行任何微调器
# -*- coding: utf-8 -*-importtimefromyaspinimportyaspinfromyaspin.spinnersimportSpinnerswithyaspin(Spinners.earth,text="Earth")assp:time.sleep(2)# time consuming code# change spinnersp.spinner=Spinners.moonsp.text="Moon"time.sleep(2)# time consuming code
任何你喜欢的颜色?
# -*- coding: utf-8 -*-importtimefromyaspinimportyaspinwithyaspin(text="Colors!")assp:# Support all basic termcolor text colorscolors=("red","green","yellow","blue","magenta","cyan","white")forcolorincolors:sp.color,sp.text=color,colortime.sleep(1)
高级颜色用法
# -*- coding: utf-8 -*-importtimefromyaspinimportyaspinfromyaspin.spinnersimportSpinnerstext="Bold blink magenta spinner on cyan color"withyaspin().bold.blink.magenta.bouncingBall.on_cyanassp:sp.text=texttime.sleep(3)# The same result can be achieved by passing arguments directlywithyaspin(Spinners.bouncingBall,color="magenta",on_color="on_cyan",attrs=["bold","blink"],)assp:sp.text=texttime.sleep(3)
运行所需的任何微调器
# -*- coding: utf-8 -*-importtimefromyaspinimportyaspin,Spinner# Compose new spinners with custom frame sequence and interval valuesp=Spinner(["?","?","?","?","?","?","?","?","?"],200)withyaspin(sp,text="Cat!"):time.sleep(3)# cat consuming code :)
动态更改微调器属性
# -*- coding: utf-8 -*-importtimefromyaspinimportyaspinfromyaspin.spinnersimportSpinnerswithyaspin(Spinners.noise,text="Noise spinner")assp:time.sleep(2)sp.spinner=Spinners.arc# spinner typesp.text="Arc spinner"# text along with spinnersp.color="green"# spinner colorsp.side="right"# put spinner to the rightsp.reversal=True# reverse spin directiontime.sleep(2)
写入消息
当微调器打开时,不应使用print在终端中编写任何消息。 要在终端中写入消息而不与yaspin微调器发生任何冲突,将提供一个.write()方法:
# -*- coding: utf-8 -*-importtimefromyaspinimportyaspinwithyaspin(text="Downloading images",color="cyan")assp:# task 1time.sleep(1)sp.write("> image 1 download complete")# task 2time.sleep(2)sp.write("> image 2 download complete")# finalizesp.ok("✔")
与其他库的集成
利用hide和show方法,可以切换 微调器,以便调用写入终端的自定义方法。这是 有助于在其他框架(如prompt-toolkit)中轻松使用。 使用强大的print_formatted_text函数甚至可以应用 输出的HTML格式和CSS样式:
# -*- coding: utf-8 -*-from__future__importprint_functionimportsysimporttimefromyaspinimportyaspinfromprompt_toolkitimportHTML,print_formatted_textfromprompt_toolkit.stylesimportStyle# override print with feature-rich ``print_formatted_text`` from prompt_toolkitprint=print_formatted_text# build a basic prompt_toolkit style for styling the HTML wrapped textstyle=Style.from_dict({'msg':'#4caf50 bold','sub-msg':'#616161 italic'})withyaspin(text='Downloading images')assp:# task 1time.sleep(1)sp.hide()print(HTML(u'<b>></b> <msg>image 1</msg> <sub-msg>download complete</sub-msg>'),style=style)sp.show()# task 2time.sleep(2)sp.hide()print(HTML(u'<b>></b> <msg>image 2</msg> <sub-msg>download complete</sub-msg>'),style=style)sp.show()# finalizesp.ok()
处理posix signals
处理键盘中断(按control-c):
# -*- coding: utf-8 -*-importtimefromyaspinimportkbi_safe_yaspinwithkbi_safe_yaspin(text="Press Control+C to send SIGINT (Keyboard Interrupt) signal"):time.sleep(5)# time consuming code
处理其他类型的信号:
# -*- coding: utf-8 -*-importosimporttimefromsignalimportSIGTERM,SIGUSR1fromyaspinimportyaspinfromyaspin.signal_handlersimportdefault_handler,fancy_handlersigmap={SIGUSR1:default_handler,SIGTERM:fancy_handler}withyaspin(sigmap=sigmap,text="Handling SIGUSR1 and SIGTERM signals")assp:sp.write("Send signals using `kill` command")sp.write("E.g. $ kill -USR1 {0}".format(os.getpid()))time.sleep(20)# time consuming code
更多examples。
发展
克隆存储库:
git clone https://github.com/pavdmyt/yaspin.git
安装开发依赖项:
pipenv install --dev
lint代码:
make lint
格式代码:
make black-fmt
运行测试:
make test
贡献
- 叉开!
- 创建功能分支:git checkout -bmy-new-feature
- 提交更改:git commit -m 'Add some feature'
- 推到分支:git push origin my-new-feature
- 提交拉取请求
- 确保测试通过
许可证
- mit-pavlo dmytrenko;https://twitter.com/pavdmyt
- 包含termcolor软件包:麻省理工学院许可证,版权所有(c)2008-2011 Volvox开发团队
- 包含来自cli-spinners:麻省理工学院许可证,版权所有(c)sindre sorhus sindresorhus@gmail.com(sindresorhus.com)