Package: teletype
teletypeteletypeis a high-level cross platform tty library compatible with Python 3.7+. It provides a consistent interface between the terminal and cmd.exe by building on top ofterminfoandmsvcrtand has no dependencies.Installation$ pip install teletypeI/O Utilities (teletype.io)Reading Key StrokesYou can read keystrokes from stdin usingget_key. Regular keys get returned as a string with a single character, e.g."a","1","&", etc., while special keys and key combinations are returned as a string description, e.g."space","f12","page-up","ctrl-c", etc. A listing of the supported key combinations are listed in thecodesmodule.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")Styling OutputYou can style strings with COLOURS and effects usingstyle_format. Styles can be passed in either as a space delimited string or in a collection (e.g. a tuple, set, list, etc.). The passedtextstring is then wrapped in the appropriate ASCII escape sequences and returned. Whenprinted the appropriate styles will be applied.Alternatively you can you just pass these same parameters tostyle_printand accomplish this in one fell swoop.style_printtakes the same parameters as the regular print function and can be used in place. In python3 you can even import style_print as print and use it in place. In order to pull this compatibility off for python2, thestyleargument must be specified explitly when calling, however, e.g.style_print("yolo", style="yellow").Lastly, you can usestrip_formatto clear a string of any escape sequences that have been previously applied.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))Cursor manipulationThe package includes quite a few helper functions to move the CURSOR around the screen. These includeerase_lines,erase_screen,hide_cursor,show_cursor, andmove_cursor; all of which are fairly self explanitory. The only word of caution is to remember to reset CURSOR visibility as its state will persist after the python interpreter has exited.Components (teletype.components)The package also includes components, higher level UI classes that are composed from the I/O functions and can be easily incorporated to any CLI application.SelectOneIn[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: dogSelectManyIn[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: dogProgressBarIn[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%ChoiceHelperAlthough not a component in and of itself,ChoiceHelpercan help you wrap your objects to make full use of components likeSelectOne,SelectMany, orSelectApproval. This is completely optional-- normally these just use the string representations of objects for display, e.g. just printing options which are strings or calling their underlying__str__methods.Seperate Values from LabelsSometimes this isn't an option or you might want to seperate the label of an object from its value.ChoiceHelperlets you specifiy these fields explicitly. You can apply styles, too.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
   bulldogMnemonicsAnother cool thing thatChoiceHelpers let you do is use mneumonics. These can be specified either using a single character, in which case they are underlined, or as a single character wrapped in square brackets, in which case they will be indicated using square brackets (e.g. for compatibility with dumb terminals).This is used under the hood forSelectApprovalto quickly select yes by pressingyand no by pressingn.Styling Components (teletype.components.config)You can set component styles usingio.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()You can also change character sets usingset_char(key, value)where value is the unicode character you want to use and key is one of:arrowblockleft-edgeright-edgeselectedunselectedLicenseMIT. See LICENSE.txt for details.