Package: escprint
escprintLibrary for formatting and styling terminal outputs via ANSI escape sequences.InstallationpipinstallescprintUsagefromescprintimportescesc.printThis is useful for a basic print statement. The first argument is the string to print, while the rest are styles/colors to format the string with.*Note that all styles are cleared after the print statement is executedAll of the keyword arguments valid for python'sprintfunction are valid, as well as several others, shown in examples below.A list of valid formatting strings can be found at the bottom of this document.esc.print("Hello World!","red","underline",end="")Different Escape Arguments can be deliminated by a "/"esc.print("Hello World!","red/underline",end="")Keyword ArgsKeywordDescriptionbgsets background color, either color code (int) or R,G,B (tuple)fgsets foreground color , either color code (int) or R,G,B (tuple)prefixsets prefix to prepend to stringprecalla function to call before print statementpostcalla function to call after print statementTo get more specific with color preferences, you can use thefgorbgkeyword argument to plug in either a color code value ranging from 0 to 255, or an (R,G,B) tuple.fgsets the foreground color, whilebgsets the background.These color codes can also be found at the bottom of this document.esc.print("Print white color",fg=255)esc.print("Print white color, with black background.",fg=(255,255,255),bg=(0,0,0))Prefixes are also validesc.print("World!","red/underline",prefix="Hello ")precall&postcallkeyword arguments can be passed as well. These are functions that will be called before and after the print statement, respectively.esc.print("World","red",precall=lambda:print("Hello"),postcall=lambda:print("!"))esc.printfSometimes, we want different characters/words to have different styles.esc.printf("This is normal, ",("but this is red and underlined, ","red/underline"),("and this is blue and italic","blue/italic"))We can also set a default style as well, with the default keyword argument.esc.printf("This is red ",("This is blue","blue"),"This is still red",default="red")All otherkeyword argumentsvalid foresc.printare valid for this function as well.esc.printf("These are separated by newlines",["and this is red and underlined, ","red/underline"],end="\n")esc.create_fnIf we are constantly printing the same styles over and over, we might want to create a printing function to do it for us.print_yellow_italic=esc.create_fn("yellow","italic")print_yellow_italic("This will be yellow and italic",end="")Note that thekeyword argumentsallowed foresc.printare valid when creating the function.print_yellow_italic=esc.create_fn("yellow","italic",precall=print("'Ello Govna"))esc.inputWe also might want to format our inputs as well.Here, the prompt is "What is your name? ". The prompts style is set via thepromptkeyword argument. The input style will be set via theinputkeyword argument.user_input=esc.input("What is your name? ",prompt="yellow/italic",input="red/underline",end="")esc.setThis is used to set the terminal outputs to a style indefinitely.esc.set("cyan","underline","italic")# OResc.set("cyan/underline/italic")The keyword argumentsbg,fg, are valid keywords as well.esc.set(fg=(122,56,255))esc.clearThis is used to reset all terminal output styles to their default.esc.set("red")print("This is red")esc.clear()print("This is normal")esc.cursor_upThis moves the cursor position up by an integern, which defaults to 1.esc.cursor_up(2)esc.cursor_downThis moves the cursor position down by an integern, which defaults to 1.esc.cursor_down(2)esc.cursor_leftThis moves the cursor position left by an integern, which defaults to 1.esc.cursor_left(2)esc.cursor_rightThis moves the cursor position right by an integern, which defaults to 1.esc.cursor_right(2)esc.erase_to_endlnThis erases everything from the current position of your cursor to the end of the line.esc.erase_to_endln()esc.erase_screenThis erases everything on the screen.esc.erase_screen()esc.erase_lineThis erases everything on the line the cursor is on.esc.erase_line()esc.erase_prevThis moves the cursor up 1, and erases everything on that line.esc.erase_prev()esc.hide_cursorThis hides the cursor.esc.hide_cursor()esc.show_cursorThis unhides, or shows the cursor.esc.show_cursor()esc.enable_alt_bufferThis enables an alternative buffer.esc.enable_alt_buffer()esc.disable_alt_bufferThis disables the alternative buffer.esc.disable_alt_buffer()esc.save_cursorThis saves the cursor position.esc.save_cursor()esc.restore_cursorThis restores the cursor position to the previously saved one.esc.restore_cursor()esc.save_screenThis saves the current screen.esc.save_screen()esc.restore_screenThis restores the saved screen.esc.restore_screen()esc.fg_codeSets the foreground color to a color code.esc.fg_code(255)esc.bg_codeSets the background color to a color code.esc.fg_code(255)esc.fg_codeSets the foreground color to a color code.esc.fg_code(255)esc.bg_codeSets the background color to a color code.esc.bg_code(255)esc.fg_rgbSets the foreground color to an rgb color code.esc.fg_rgb(255,142,34)esc.bg_codeSets the background color to an rgb color code.esc.bg_rgb(255,142,34)esc.terminal_sizeReturns an (x,y) pair representing the width and height of the screen in terms of columns/lines.size=esc.terminal_size()n_cols=size.xn_lines=size.y# move cursor halfway up the screen, and halfway to the rightesc.cursor_up(n_lines/2)esc.cursor_right(n_cols/2)esc.cursor_to_topSets the cursor position to the top of the screen.esc.cursor_to_top()esc.cursor_homeSets the cursor position to the home position (0,0).esc.cursor_home()Formatting StringsText DecorationStringDescriptionresetreset formatting to defaultboldmake text bolddimmake text dimblinkmake text blinkitalicmake text italiciitalic shorthandunderlineunderline textuunderline shorthandColorStringDescriptionblackset foreground color to blackwhiteset foreground color to whiteredset foreground color to redgreenset foreground color to greenyellowset foreground color to yellowblueset foreground color to bluemagentaset foreground color to magentacyanset foreground color to cyanBlackset foreground color to bright blackWhiteset foreground color to bright whiteRedset foreground color to bright redGreenset foreground color to bright greenYellowset foreground color to bright yellowBlueset foreground color to bright blueMagentaset foreground color to bright magentaCyanset foreground color to bright cyanbblackset background color to blackbwhiteset background color to whitebredset background color to redbgreenset background color to greenbyellowset background color to yellowbblueset background color to bluebmagentaset background color to magentabcyanset background color to cyanbBlackset background color to bright blackbWhiteset background color to bright whitebRedset background color to bright redbGreenset background color to bright greenbYellowset background color to bright yellowbBlueset background color to bright bluebMagentaset background color to bright magentabCyanset background color to bright cyanOtherStringDescriptionreversereverse foreground & backgroundhiddenhide textstrikestrikethrough textstrikethroughstrikethrough textdeletedelete characterhomeset cursor to home positionrequestrequest cursor positionsavecursorsave cursor positionrestorecursorrestore cursor positionColor CodesResources•Christian Petersen - ANSI Escape SequencesMIT LicenseCopyright (c) 2023 Michael Schilling MunsonPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.