Package: term-printer
term-printerPrint 'text color' and 'text format' on Term with Python※ It may not work depending on the OS and shell used.PIP$pipinstallterm-printerimportfromterm_printerimportColor,Color256,ColorRGB,StdText,cprintIf you want to override bultinprintfunctionfromterm_printerimportColor,Color256,ColorRGB,StdText,cprintasprintUsage1. Attrs printApplies to all characters.You can specifyFormat,Color,Color256, andColorRGB.Able to specify more than one.sourcefromterm_printerimportColor,Color256,ColorRGB,Format,cprint# defaultcprint("this is a default pen")# boldcprint("this is a bold pen",attrs=[Format.BOLD])# italiccprint("this is a italic pen",attrs=[Format.ITALIC])# red colorcprint("this is a red pen",attrs=[Color.RED])# bright red colorcprint("this is a bright red pen",attrs=[Color.BRIGHT_RED])# background magenta colorcprint("this is a bg magenta pen",attrs=[Color.BG_MAGENTA])# background bright magenta colorcprint("this is a bg bright magenta pen",attrs=[Color.BG_BRIGHT_MAGENTA])# magenta color & italiccprint("this is a magenta italic pen",attrs=[Color.MAGENTA,Format.ITALIC])# bold & italiccprint("this is a bold italic pen",attrs=[Format.BOLD,Format.ITALIC])# cyan color & bold & italiccprint("this is a cyan bold italic pen",attrs=[Color.CYAN,Format.BOLD,Format.ITALIC])# 8-bit color 154cprint("this is a 8-bit 154 pen",attrs=[Color256(154)])# 8-bit bg color 154 and magenta colorcprint("this is a bg 8-bit 154 pen",attrs=[Color256(154,is_bg=True),Color.MAGENTA])# rgb(100, 255, 255) colorcprint("this is a rgb(100, 255, 255) pen",attrs=[ColorRGB(100,255,255)])# bg rgb(100, 255, 255) color and black colorcprint("this is a bg rgb(100, 255, 255) pen",attrs=[ColorRGB(100,255,255,is_bg=True),Color.BLACK],)result2. StdText printsourcefromterm_printerimportColor,Format,StdText,cprint# defaultcprint("this is a default pen")# boldcprint(f"this is a{StdText('bold',Format.BOLD)}pen")# italiccprint(f"this is a{StdText('italic',Format.ITALIC)}pen")# reversecprint(f"this is a{StdText('reverse',Format.REVERSE)}pen")# red colorcprint(f"this is a{StdText('red',Color.RED)}pen")# background magenta colorcprint(f"this is a{StdText('bg magenta',Color.BG_MAGENTA)}pen")# magenta color & italiccprint(f"this is a{StdText('magenta',Color.MAGENTA)}{StdText('italic',Format.ITALIC)}pen")# bold & italiccprint(f"this is a{StdText('bold',Format.BOLD)}{StdText('italic',Format.ITALIC)}pen")# cyan color & bold & italiccprint(f"this is a{StdText('cyan',Color.CYAN)}{StdText('bold',Format.BOLD)}{StdText('italic',Format.ITALIC)}pen")resultColorclassColor(Enum)Enum class.Examplefromterm_printerimportColorColor.RED# RED foreground colorColor.BG_RED# RED background colorColor.BLUE# BLUE foreground colorColor.BG_BLUE# BLUE background colorsourceDefinition is3-bit and 4-bit colorsColor256classColor256(n:int,is_bg:bool=False)First argument takesint(0 - 255).Second argument takesbool(default: False).
False: change foreground color
True: change background colorExamplefromterm_printerimportColor256Color256(9)# RED foreground colorColor256(9,True)# RED background colorColor256(12)# BLUE foreground colorColor256(12,True)# BLUE background colorDefinition is8-bit 256 colorsColorRGBclassColorRGB(r:int,g:int,b:int,is_bg:bool=False)Three arguments takesint(0 - 255).Fourth argument takesbool(default: False).
False: change foreground color
True: change background colorExamplefromterm_printerimportColorRGBColorRGB(255,0,0)# RED foreground colorColorRGB(255,0,0,True)# RED background colorColorRGB(0,0,255)# BLUE foreground colorColorRGB(0,0,255,True)# BLUE background colorFormatclassFormat(Enum):BOLD=1FAINT=2ITALIC=3UNDERLINE=4BLINK=5FAST_BLINK=6REVERSE=7CONCEAL=8STRIKE=9Enum class.Examplefromterm_printerimportFormatFormat.BOLD# BOLD fontFormat.FAINT# FAINT fontFormat.ITALIC# ITALIC fontFormat.UNDERLINE# UNDERLINE fontDefinition isSGR