Package: ltermio
ltermio - A Lightweight POSIX terminal I/O libraryThe package contains 5 modules:cursor,termkey,termouse,color256andunicon. Tested only onMacOS terminalandiTerm2, so the platform compatibility has not been well verfied yet.All functions are based onXTermspecification,CSIsequences andtermios, no additional requirements other than the standard library.Applicability: They are only forPOSIXterminal applications.References:https://en.wikipedia.org/wiki/ANSI_escape_codehttps://invisible-island.net/xterm/ctlseqs/ctlseqs.htmlhttps://www.xfree86.org/current/ctlseqs.htmlInstallation & UsageUses pip to install the package:pip3 install ltermioMore details on module usage:pydoc ltermio.module-nameHistory & WhyAs we all know, character terminal are outdated facilities, why did I still write such a package?When I was learningPythona few months ago, I decided to write a terminalTetrisgame as a practice of the language learning. Due to the learning reason, I did not want to use any third-party packages. So when I finally finished the game, there naturally formed this by-product.color256 moduleSets 256-color display attributes of character terminal.There are three pairs of functions to set or restore colors according to their literal name, and another pair to set text display attributes:set_fcolor(color:int)reset_fcolor()set_bcolor(color:int)reset_bcolor()set_color(fcolor:int,bcolor:int)reset_color()set_textattr(attr:TextAttr|int)reset_textattr(attr:TextAttr|int)All color functions use indexed color as parameter, functionrgb()can make indexed color number from given RGB parameters. Enum classColordefined constants of some common-used indexed colors and grayscales.Enum classTextAttrdefined constants of text display attributes, likeBOLD,ITALICandUNERLINED, etc.Following sample code prints gradually bright colorful greetings:fromltermioimportset_fcolor,reset_fcolorforiinrange(6):forcharin'Hello, color256!':set_fcolor(ord(char)%32+20+i*36)print(char,end='')print()reset_fcolor()Another sample to output all colors:fromltermioimportrgb,set_bcolor,reset_bcolorforredinrange(6):forgreeninrange(6):forblueinrange(6):set_bcolor(rgb(red,green,blue))print('  ',end='')print()reset_bcolor()cursor moduleWrapper functions of theCSI(Control Sequence Introducer)sequences about cursor and screen.A several of additional functions are provided for text composing:v_composing(seq:str)->strdownward_seq(text:str,cols:int)->strvert_seq(text:str)->strrect_border_seq(width:int,height:int,sym:str)->strFollowing sample code ofv_composing()displays three big colorful greetings:fromltermioimportputmsg,v_composing_P1=':{0}\x1b3hj'_P15=':{0}\x1b6l:{0}\x1b11hj'_DASH='5:{0}\x1b11hj'_NEXT_POS='5k17l'LETTERS={'E':v_composing(f'{(_DASH+_P1)*2}{_DASH}{_NEXT_POS}'),'H':v_composing(f'{_P15*2}{_DASH}{_P15*2}{_NEXT_POS}'),'L':v_composing(f'{_P1*4}{_DASH}{_NEXT_POS}'),'O':v_composing(f'{_DASH}{_P15*3}{_DASH}'),}greeting=''.join(map(LETTERS.get,'HELLO'))putmsg(3,20,greeting.format('\u2b50'))putmsg(9,14,greeting.format('\u2b55'))putmsg(15,8,greeting.format('\U0001f7e2'))print('\n')termkey moduleFunctions to read input in non-canonical mode.There are 5curseslike functions:getch(): Gets a character from stdin.ungetch(): Puts one or more characters into the key buffer.getkey(): Calls getch() and transforms character to keycode.ungetkey(): Puts a keycode into the key buffer.setparams(): Sets frenquently-used attributes of the input.And a function to support mouse tracking.mouse_handler(): Sets a function to handle mouse report.Function keycodes in common using are defined in enum classKey.A typical usage example as following:importltermiofromltermioimportKeyltermio.setparams(echo=False)key=ltermio.getkey()whilekey!=Key.ESC:...key=ltermio.getkey()ltermio.setparams()termouse moduleDetects and reports mouse events.The implementation of the module follows theXTermspecification of the mouse tracking and uses normal tracking mode, see also:https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Mouse-Trackinghttps://www.xfree86.org/current/ctlseqs.html#Mouse%20TrackingCallsltermio.mouse_tracking_on()to turn on the mouse tracking, or sets the parametermouse=Trueon the decoratorltermio.appentry_args().The mouse events are reported byltermio.getkey(), with value encoded in a 32-bits integer and larger thanKey.MOUSE_EVENT. It is simple to make difference from normal key codes bycode > Key.MOUSE_EVENT.Decodes the event codes by callingltermio.decode_mouse_event()which returns a tuple with explicit items.An example to get key and mouse inputs as following:importltermiofromltermioimportKey,MouseEventltermio.mouse_tracking_on()ltermio.setparams(echo=False)code=ltermio.getkey()whilecode!=Key.CONTROL_X:ifcode>Key.MOUSE_EVENT:event,row,col,modifiers=ltermio.decode_mouse_event(code)ifevent==MouseEvent.B1_CLICKED:...# do something with mouse eventelse:...# do something with key inputcode=ltermio.getkey()ltermio.setparams(echo=True)ltermio.mouse_tracking_off()unicon moduleCollection of some common icons in unicode character set.