一个高层次的跨平台tty库

teletype的Python项目详细描述


pypilicencecode style black

电传打字机

teletype是一个与Python 2.7和3+兼容的高级跨平台tty库它通过构建在terminfomsvcrt之上,在终端和cmd.exe之间提供一致的接口,并且没有依赖关系。

安装

$ pip install teletype

I/O实用程序(teletype.I O)

读取按键行程

您可以使用get_key从stdin读取击键常规键以单个字符的字符串形式返回,例如"a""1""&"等,而特殊键和键组合则以字符串描述形式返回,例如"space""f12""page-up""ctrl-c"等。支持的键组合列表列在^{}模块中。

fromteletype.ioimportget_keyprint("Delete C:/ Drive? [y/n]")selection=""whileselection.lower()notin("y","n"):selection=get_key()ifselectionin("ctrl-c","ctrl-z","escape"):selection="n"ifselection=="y":print("Deleting C:/ drive...")delete_c_drive()else:print("Leaving C:/ drive alone")

样式输出

可以使用style_format为字符串设置颜色和效果样式。样式可以作为空格分隔的字符串或在集合(例如元组、集合、列表等)中传递。传递的text字符串随后被包装在适当的ascii转义序列中并返回。当printed时,将应用适当的样式。

或者,您可以将这些相同的参数传递给style_print,然后一下子完成。style_print采用与常规打印函数相同的参数,并且可以在适当的位置使用。在python3中,您甚至可以将style_print导入为print并使用它。为了实现python2的这种兼容性,调用时必须明确指定style参数,例如style_print("yolo", style="yellow")

最后,可以使用strip_format清除先前应用过的任何转义序列的字符串。

fromteletype.ioimportstyle_format,style_print,strip_format# All of these will do the same thing, that is print the message in red and boldprint(style_format("I want to stand out!","bold red"))print(style_format("I want to stand out!",("red","bold")))style_print("I want to stand out!",style=["red","bold"])# Styles are cleared afterwards so everything else gets printed normallyprint("I want to be boring")# If for whatever reason you want to unstyle text, thats a thing tootext=style_format("I don't actually want too be styled",("red","bold"))print(strip_format(text))

光标操作

这个包包含很多帮助函数,可以在屏幕上移动光标。其中包括erase_lineserase_screenhide_cursorshow_cursormove_cursor;所有这些都是相当自我解释的唯一的注意事项是记住在Python解释器退出后,它的状态将持续下去,从而重新设置游标可见性。

组件(电传打字机组件)

该软件包还包括组件和更高级别的ui类,这些ui类由i/o函数组成,可以很容易地合并到任何cli应用程序中。

选择One

In[1]:fromteletype.componentsimportSelectOne...:...:picker=SelectOne(choices=["dog","bird","cat","monkey","gorilla"])...:print("Your Favourite Animal?")...:choice=picker.prompt()...:print("Your choice: "+choice)
Your Favourite Animal?
 ❱ dog
   bird
   cat
   monkey
   gorilla
Your choice: dog

选择多个

In[2]:fromteletype.componentsimportSelectMany...:...:picker=SelectMany(choices=["dog","bird","cat","monkey","gorilla"])...:print("Your Favourite Animals?")...:choices=picker.prompt()...:print("Your choices: "+", ".join(choices))
Your Favourite Animals?
❱● dog
 ○ bird
 ○ cat
 ○ monkey
 ○ gorilla
Your choices: dog

进度条

In[3]:fromtimeimportsleep...:fromteletype.componentsimportProgressBar...:...:iterations=15...:...:defiterable():...:for_inrange(iterations):...:sleep(0.2)...:yield...:...:ProgressBar("Progress Bar").process(iterable(),iterations)
Progress Bar: 15/15▐████████████████████████████████████████████████▌100%

选项助手

尽管不是组件本身,ChoiceHelper可以帮助您包装对象,以充分利用诸如SelectOneSelectManySelectApproval之类的组件。这是完全可选的——通常它们只是使用对象的字符串表示来显示,例如,只是打印字符串选项或调用它们的底层__str__方法。

从标签中分离值

有时这不是一个选项,或者您可能希望将对象的标签与其值分离。ChoiceHelper允许显式指定这些字段。你也可以应用样式。

In[4]:fromteletype.componentsimportSelectOne,ChoiceHelper...:...:choices=[...:ChoiceHelper(["corgi","greyhound","bulldog"],label="dog",style="blue"),...:ChoiceHelper(["siamese","chartreux","ragdoll"],label="cat",style="red"),...:ChoiceHelper(["zebra","beta","gold"],"fish",style="green")...:]...:...:print("favourite pet")...:pet=SelectOne(choices).prompt()...:...:print("favourite breed")...:breed=SelectOne(pet).prompt()
favourite pet
 ❱ dog
   cat
   fish

favourite breed
 ❱ corgi
   greyhound
   bulldog

助记符

另一件很酷的事情是使用助记符。可以使用单个字符(在这种情况下,这些字符有下划线)指定,也可以使用方括号中的单个字符(在这种情况下,这些字符将使用方括号表示)(例如,为了与哑终端兼容)

这在引擎盖下用于SelectApprovaly快速选择“是”,按n快速选择“否”

设置组件样式(teletype.components.config)

可以使用io.style_format设置组件样式。

fromteletype.ioimportstyle_format,style_printfromteletype.componentsimportProgressBar,SelectManystyle="red bold"arrow=io.style_format(CHARS_DEFAULT["arrow"],style)choices=["dogs","cats","fish"]io.style_print("\nSelect One",style=style)SelectOne(choices,arrow=arrow).prompt()

您还可以使用set_char(key, value)更改字符集,其中value是您要使用的Unicode字符,key是:

  • arrow
  • block
  • left-edge
  • right-edge
  • selected
  • unselected

许可证

麻省理工学院。有关详细信息,请参见license.txt。

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

推荐PyPI第三方库


热门话题
java FloatingAction按钮与RecyclerView中的CardView重叠   java如何计算CardLayout中的卡数   从远程系统上传MySQL数据库并访问Java应用程序   java调用堆栈如何处理带或不带返回类型的递归?   Springboot中的java组计数聚集   java如何在javafx textarea中使用richtextfx   获取与Mockito相关的错误时出现Java问题   java如何将JaxRS响应转换为Wiremock响应   Hadoop集群java。net ConnectionException:连接被拒绝错误   java如何加载文件私有文件类型是pem   java在元空间中的提升和加载的类   如何将系统属性传递给从HTML启动的Java小程序   java如何从网页中获取值并在主类中使用它?安卓应用   java在春天,advisor和aspect之间有什么区别?   java如何检测文件是否已重命名?   java消息驱动Bean何时使用