January 24, 2017

P4 Command Line Tool in Colors

Flexible Workflows

A few years ago, I posted a blog article on how you can add colors to the P4 command line. This involved a lot of hacking, administrator access and a specialised P4 executable for Windows to work, as well as a modified journal file. Not for the faint of heart or, as the post stated: Here be dragons.

The desire to add colors to the command line tool still had merit, because it generally makes the output more readable. You can witness this in the evolution of the Unix command line (generally bash) and all its tools, which these days have colors by default.

The good news is the P4 2016.2 has added the ability to colorize the output across platforms. The even better news is that this does not require a database change or server update and is completely transparent. The ability to add color to your output is undocumented (which is why I’m writing this post), so take this new feature as an Easter-egg with the usual caution. That said, I have been using an unofficial version of this feature for a year now without any problems.

Have a look at this command line output from my machine:

Thumbnail

As you can see, the individual objects within a response - such as the depot path, client path or revision numbers - are colored, as is the warning at the bottom of the output. All you need to do is to

·      Download and install the latest P4 executable

·      Define an environment or configuration variable called P4COLORS.

This is where things get interesting. My P4COLORS variable looks like this:

P4COLORS=@info=0:@error=31;1:@warning=33;1:action=36:how:36:change=33:depotFile=32:path=32:location=32:rev=31:depotRev=31

If you know how to set colors for ls under Linux, the format should look familiar: blocks of name=ASCII color separated by a colon (‘:’). ASCII colors are 1 to 3 values separated by a semicolon: foreground, background and attribute. Foreground and background color should be pretty straightforward, whereas text attributes are special properties like bold, underscore, etc. Here are the tables of potentially usable values:

Thumbnail

The names are the field names in tagged output you should be familiar with by now. This gives you full flexibility to spice up your output any way you like. You can also use the asterisk (‘*’) wildcard for matching, for example: I said “potentially usable” because not all may work. For example, in my Mac terminal windows only the 1 (bold) and 4 (underscore) attribute actually work. Your mileage may vary.

                  *File matches depotFile and clientFile

The default text color for each message level is controlled by setting the @info, @warning and @error variables.

You might be worried that adding colors would mess up your scripts, but you do not need to be concerned. P4 will check whether you are invoking the command from a terminal or a script and enable colors only if the former is the case.

So, liven up your terminal to your heart’s content. Why don’t you check out all the other new features from the 16.2 release in the release notes? I’ll be writing about ‘p4 undo’ and the subprotections soon.

Happy hacking!