Package: pycolor-term
PycolorInstallation.Example Usage.Configuration.Formatting Strings.Color Formatting.Field Formatting.Group Formatting.Alignment.Truncate.Debugging and Creating Profiles.Debug Colors.Creating Profiles.Limitations.Known Issues.A Python program that executes commands to perform real-time terminal output coloring using ANSI color codes.
Color formatting can be added to program output using JSON configuration files and regular expressions to improve readability of the output.Designed for Unix, but works in Windows.Installationpipinstallpycolor-termExample UsagePycolor can be used explicitly on the command line:Before:After:Sample df configuration file.Pycolor can also be aliased in~/.bashrclike so:aliasrsync='pycolor rsync'Before:After:Note pycolor omitted lines with trailing slashes in addition to coloring output for better readability.Sample rsync configuration file.Sample rclone copy output:Before:After:ConfigurationPycolor will first try to load configuration from~/.pycolor.jsonbefore loading files found in~/.pycolor.d/in filename order.When looking for a profile to use, pycolor will select the last matching profile based on thename/command,name_expression/command_expression, orwhichproperty.Matching patterns are applied first-to-last for each profile.JSON schema files that describe the config format can be found in/config/schema/.Sample config files can be foundhereand will be automatically copied to~/.pycolor.d/when run, if it does not exist.Formatting StringsUse formatting strings to color/manipulate the program output in real-time.
These are valid formats:%(<name>:<value>)%<code>(<value>)%<code><value>CodeNameDescriptionCcolorColor formatterFfieldField (separator) formatterGgroupRegex group formatterHcolorctxContext-aware field/group color aliasalignAlignment formattertruncTruncation formatterFormatting strings can written like%(color:red),%C(red), or%Cr.%C(red)helloformats the stringhelloin red.A literal%can be used in a format string by using%%.
E.g. the format stringThe total is %C(red)15%%will becomeThe total is 15%, with the15%part in red.Valid format value characters are upper/lowercase letters and numbers, unless the argument is encapsulated in parentheses, then everything in the parenthesis pair is used.Checkthe sample configfor examples of formatting strings being used for actual programs.ColorsTo colorize output through a replace pattern use%(color:<color value>),%C(<color value>)or%C<color value>.Recognized Attributes and Colors:Color ValueAliasesANSI CodeDescriptionresetnormal, res, nor, z\e[0mResets all ANSI color formattingboldbright, bol, bri\e[1mBold charactersdim\e[2mDim charactersitalicita\e[3mItalicize charactersunderlineunderlined, ul, und\e[4mUnderline charactersblinkbli\e[5mBlink charactersinvertreverse, inv, rev\e[7mInvert background and foreground colorshiddenconceal, hid, con\e[8mHide charactersstrikethroughstrike, str, crossedout, crossed, cro\e[9mStrikethrough charactersblackk\e[30mBlack colorredr\e[31mRed colorgreeng\e[32mGreen coloryellowy\e[33mYellow colorblueb\e[34mBlue colormagentam\e[35mMagenta colorcyanc\e[36mCyan colorgreygray, e\e[37mGrey colordefault\e[39mDefault coloroverlineoverlined, ol, ove\e[53mOverline charactersdarkgreydarkgray, de, lk\e[90mDark grey colorlightredlr\e[91mLight red colorlightgreenlg\e[92mLight green colorlightyellowly\e[93mLight yellow colorlightbluelb\e[94mLight blue colorlightmagentalm\e[95mLight magenta colorlightcyanlc\e[96mLight cyan colorwhitelightgrey, le, w\e[97mWhite colorClick here for a list of all attributes and color codes.Modifier CharactersYou can select multiple color values by separating them with;(must be wrapped in parentheses). e.g.%(color:bold;red)or%C(bold;red).If^is added before a color (e.g.%C(^red)), it will set the background color instead.
The color formatting for bold, red-on-yellow text can be written as%C(bold;red;^yellow)hello%C(z), or%C(bol;r;^y)hello%Cz, which will produce\e[1;31;43mhello\e[0m.If^is added before a style (e.g.%C(^italic)produces\e[23m), then the style is turned off.Note that turning off bold (%C(^bold)i.e.\e[21m) instead turns on double underline for some terminals.Special Colors8-bit ColorIf a color value is just a number (e.g.%C130), then it will use the 8-bit color set (in this case, a brown color:\e[38;5;130m).
This also works for background colors as well (e.g.%C(^130)produces\e[48;5;130m).Click here to see the 8-bit color table.24-bit Color24-bit color is also supported by using hex codes (%C(0xffaa00)or%C(0xfa0)will produce orange:\e[38;2;255;170;0m).Raw ANSI CodesIf for some reason you would like to use raw color codes,%C(raw1;3;36)will produce bold, italic, cyan (\e[1;3;36m).FieldsIf the pattern'sseparatorproperty is set, then the fields and their separators can be referenced in the format string.To get a field's text, use%F<field number>.To get the field separator string, use%Fs<field number>. This will return the field separator string that precedes that field number.
(e.g.%Fs3will get the field separator string that comes before the third field%F3).Ifseparatoris set to#+, and the output line isa#b##c###d##e#f, the field format values will be:Field FormatValue%F1a%Fs2#%F2b%Fs3##%F3c%Fs4###%F4d%Fs5##%F5e%Fs6#%F6fNegative IndexingField formats support negative indexing, so%F(-1)_%F(-2)will format tof_ein the previous example.Field RangesGiving a range of fields is possible using the*character, in the format%F(<start range>*<end range>).Using the same example as above:Field FormatValue%F(*3)a#b##c%F(1*3)a#b##c%F(2*3)b##c%F(2*4)b##c###d%F(4*)d##e#fSeparator replacementIf you want to format using field ranges, but want to override the separator used to be a constant-length string, use%F(<start range>*<end range>,<separator>).
Using the previous input as an example,%F(*4,_)formats toa_b_c_d.replace-fieldsChanging only the matched fields is possible using pattern'sreplace-fieldsproperty.
Any fields not explicitly set will be left alone.Use%Fcto specify the current field that applies to the index/string.It can be a list of format strings, where the format string at indexiis applied to field numberi + 1.'replace_fields': ["%Cg%Fc%Cz", "%Cr%Fc%Cz", "%Cg%Fc%Cz"]It can also be an object with the keys as field numbers and the value as the format value.
Keys may be comma-separated numbers, or even ranges ("<start>*<end>"or"<start>*<end>*<step>").'replace_fields': {
    "1,3": "%Cg%Fc%Cz",
    "2": "%Cr%Fc%Cz",
    "4*6": "%Cb%Fc%Cz"
}GroupsRegex groups can be referenced with the format:%G<group number or name>.%G0is the entire matching text fromexpression.%G1is the first matching group,%G2is the second, etc.
If the regex group is named, it can also be referenced by name (e.g.%G(myregexgroup)).Group IncrementorInstead of using groups explicitly in order (e.g.%G1, %G2: %G3), using the special incrementor instead of numbers,%Gn, %Gn: %Gn, will result in the same format output.Note that if a named groupnis defined in the expression, then the special incrementor will be overridden.replace-groupsChanging only the matched regex groups is possible using pattern'sreplace-groupsproperty.
Any groups not explicitly set will be left alone.Use%Gcto specify the current group that applies to the index/strin.It can be a list of format strings, where the format string at indexiis applied to group numberi + 1.'replace_groups': ["%Cg%Gc%Cz", "%Cr%Gc%Cz", "%Cg%Gc%Cz"]It can also be an object with the keys as group numbers and the value as the format value.
Keys may be comma-separated numbers, or even ranges ("<start>*<end>"or"<start>*<end>*<step>").'replace_groups': {
    "1,3": "%Cg%Gc%Cz",
    "2": "%Cr%Gc%Cz",
    "4*6": "%Cb%Fc%Cz"
}Context-Aware Color Alias FormatYou may find yourself coloring groups or fields often using%Cg%Fc%Czor%Cg%Gc%Cz, which is why a context-aware color alias format is available:%H(<color value>)is an alias for%C(<color value>)%Fc%Czor%C(<color value>)%Gc%Cz.Now%Cg%Gc%Czcan be replaced with the shorter alias%Hg.
If%His used inreplace_groups, it will be an alias for coloring%Gc, and if%His used inreplace_fields, it will be an alias for coloring%Fc.AlignmentAlign strings to a certain width with%(align:<width>,<position>,<pad character>)<string>%(end).ValueDescriptionwidthalignstringto this widthpositionalignment position:left,middle, orright(defaultleft)pad characterpad character used to pad the string towidth(default)The text will be padded to the width specified.
If the width is shorter than the text, then no padding or alignment is done.For example, left-aligning group 1 with a 12 character width can be done with the format string%(align:12)%G1%(end).Right-aligning can be done with%(align:12,right)%G1%(end).TruncateTruncate strings to a certain length using%(trunc:<length>,<location>,<replace>,<hard length>)<string>%(end), where:ValueDescriptionlengthtruncatestringto this lengthlocationwhere to truncatestring:left,middle, orrightreplaceinsert this string at the truncation (e.g...) (default empty)hard lengthindicates if length ofreplaceis counted as part oflength:yesorno(defaultyes)Truncate SamplesTruncate the value of group 1 to 8 chars, adding...if necessary:%(trunc:8,right,...,no)%G1%(end).StringResultTestingTestingLongStringLongStri...Truncate the value of a path in field 1 to 16 chars, inserting...if necessary:%(trunc:16,middle,...)%F1%(end).StringResult/root//root//path/to/a/certain/file/path/...in/fileDebugging and Creating ProfilesDebug ColorsTo check the supported color codes of your terminal, runpycolor --debug-colorto print all the available ANSI color codes.
Using--debug-colorwill show the results of the text styles, 16-color output, 8-bit color support, and 24-bit color support.Output ofpycolor --debug-colorrun on Windows PowerShell (24-bit colors are not shown in this image for brevity):Note: bold off (\e[21m) is actually bold underline in PowerShellCreating ProfilesPycolor has a debug mode when running commands that shows the raw output of the command.
Debug mode is turned on using-vor--verboseand can be used multiple times to increase the debug level.Debug levels (previous levels are applied to the current):LevelCommandDescription1-vPrint received output2-vvPrint final output written3-vvvPrint each matching pattern index and its output4-vvvvPrint the output line numberUsing debug level 3 onfree -hwith a profile loaded:LimitationsPrograms that expect interactive standard input may not work properly.Programs that rewrite parts of the screen may cause unexpected behavior.Known IssuesA fatal Python error can occur when attempting to acquire a lock for stdout at interpreter shutdown, caused when the command pycolor is running terminates, but output has not yet fully been written to the screen.It has only been observed when running pycolor inside of tmux while in copy-mode.