一个高层次的跨平台tty库
teletype的Python项目详细描述
电传打字机
teletype是一个与Python 2.7和3+兼容的高级跨平台tty库它通过构建在terminfo和msvcrt之上,在终端和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转义序列中并返回。当print
ed时,将应用适当的样式。
或者,您可以将这些相同的参数传递给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_lines
、erase_screen
、hide_cursor
、show_cursor
和move_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
可以帮助您包装对象,以充分利用诸如SelectOne
、SelectMany
或SelectApproval
之类的组件。这是完全可选的——通常它们只是使用对象的字符串表示来显示,例如,只是打印字符串选项或调用它们的底层__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
助记符
另一件很酷的事情是使用助记符。可以使用单个字符(在这种情况下,这些字符有下划线)指定,也可以使用方括号中的单个字符(在这种情况下,这些字符将使用方括号表示)(例如,为了与哑终端兼容)
这在引擎盖下用于SelectApproval
按y
快速选择“是”,按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。