又一个终端旋转器

yaspin的Python项目详细描述


yaspin Logo

yaspinyeta另一个终端spinner用于python

Build StatusCoverageCodacypyupblack-fmt

pypiVersionsWheelExamplesDownloads

Yaspin提供了一个功能齐全的终端微调器,用于显示长挂起操作期间的进度。

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/demo.gif

将其用作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)
https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/shark.gif

功能

  • 无外部依赖关系
  • 在所有主要的cpython版本(2.73.43.53.63.7),pypypypy3
  • 支持来自cli-spinners
  • 的所有(60+)微调器
  • 支持所有colorshighlightsattributes及其来自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

使用量

基本示例 https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/basic_example.gif
# -*- 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运行任何微调器

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/cli_spinners.gif
# -*- 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

任何你喜欢的颜色?

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/basic_colors.gif
# -*- 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)

高级颜色用法

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/advanced_colors.gif
# -*- 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)

运行所需的任何微调器

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/custom_spinners.gif
# -*- 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 :)

动态更改微调器属性

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/sp_properties.gif
# -*- 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)

写入消息

https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/write_text.gif

当微调器打开时,不应使用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("✔")

与其他库的集成 https://raw.githubusercontent.com/pavdmyt/yaspin/master/gifs/hide_show.gif

利用hideshow方法,可以切换 微调器,以便调用写入终端的自定义方法。这是 有助于在其他框架(如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

贡献

  1. 叉开!
  2. 创建功能分支:git checkout -bmy-new-feature
  3. 提交更改:git commit -m 'Add some feature'
  4. 推到分支:git push origin my-new-feature
  5. 提交拉取请求
  6. 确保测试通过

许可证

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java什么数据库最类似于Map,每个用户/id存储无限多个“键”和“值”?   java仅使用super pom进行测试   内存不足如何解析java。OutOfMemoryError:Java堆空间在增加堆大小的情况下将意味着延迟OutOfMemoryError   来自另一个类的mysql和java jdbc调用[运行时应用程序]   java通过下拉菜单更改搜索框搜索的内容   JAVAlang.ClassNotFoundException:sun。jdbc。odbc。JdbcOdbcDriver   java Selenium点击链接   JavaSpringHibernate:从唯一值列表中获取对象列表   java Bing广告与桌面身份验证问题   java如何在没有任何外部SDK的情况下从安卓打印到收据打印机?   未调用java菜单片段类   java在IDEA和PyCharm中同时为同一个项目工作   java我们如何为同一个异常提供不同的海关信息   jakarta ee中是否预定义了“请求”和“响应”变量或值?   java更好地解决“之前和之后”难题?   尝试将数据从Excel添加到Java   发送电子邮件的Java代码只适用于一个电子邮件id?   java如何从资产解析XML?