New in Xdebug 2.2: Colours on the command line
This is the first article in a series about new features in Xdebug 2.2. Besides support for PHP 5.4, there are a few that might be of interest.
Xdebug has overloaded var_dump() with xdebug_var_dump() for a long time and the overloaded function could be configured with a few configuration settings. There is xdebug.var_display_max_data to configure how much of a string should be shown; xdebug.var_display_max_children to configure how many children in an array should be shown and xdebug.var_display_max_depth to configure how many levels "deep" the var_dump() should go on for. This functionality was available for when PHP had its html_errors setting on as is usually the case in a web environment1.
Xdebug 2.2 adds this same functionality to the non-HTML environment: the command line. Now the overloaded var_dump() and native xdebug_var_dump() functions also accept the three aforementioned settings:
derick@whisky:~$ php \
-dxdebug.var_display_max_data=8 \
-dxdebug.var_display_max_children=4 \
-r 'var_dump( "a longer string", array( 1, 2, 3, 4, 5, 6, 7 ) );'
outputs:
string(15) "a longer"...
array(7) {
[0] =>
int(1)
[1] =>
int(2)
[2] =>
int(3)
[3] =>
int(4)
(more elements)...
}
Now, to be fair. This is all a side effect; and merely an add-on to a patch by Michael Maclean. He wrote a patch that adds colours to the output on the command line by using ANSI escape codes. This patch also made the overloaded var_dump() listen to the limiting settings for variable display. After his patch, the following was the behaviour on the command line as long as stdout is a tty and xdebug.cli_color is set to 1.
I have extended this so that the settings regarding data display also work without xdebug.cli_color set to 1. Further more, in Xdebug 2.2.0RC1, setting xdebug.cli_color to 2 forces the colours from being shown, even if stdout is not a tty.
Initially, the colour coding of errors and var_dump() output, would only work on a Unix system where ANSI escape codes are commonly supported. After the release of Xdebug 2.2.0RC1, Chris Jones submitted a bug report suggesting that this functionality could also be available on the Windows console. I wasn't aware that Windows could do this any more since they dropped ANSI.SYS but apparently there is a tool, ANSICON, that reimplements this. From the next release, Xdebug 2.2.0RC2, Xdebug will now also check whether the ANSICON environment variable is set, just like Xdebug would check whether stdout is a tty on a Unix platform. As a result, the equivalent console output as shown before looks like the following on Windows (providing ANSICON is installed):
Now the only thing left is adding complete documentation for this feature ;-)
As always, if you think Xdebug is a valuable tool, have a look at http://xdebug.org/buy-support.php.
Comments
would you accept a patch to add a parameter to xdebug_var_dump to always emit ANSI codes even in a webserver SAPI? This could be extemely handy for curl-based debugging of APIs.
If so, I would look into making this happen (with the disclaimer that my C is very, very rusty)
@Philip; Sadly, we can't do that as var_dump() and xdebug_var_dump() take one or more variables as arguments to dump.
-
1
: Except for in PHP 5.3, where this sadly was turned off by default.
Life Line
Merged pull request #1048
Fixed issue #2386: Crashes when running context_get in an exception t…
It's PHP 8.5 release week !
I'm giving two talks on what's new in it, first on Wednesday evening in Leeds: https://www.meetup.com/leedsphp/events/311677834/
And then on Thursday evening in Rotterdam: https://eventy.io/events/q8lmw0v4Will I see you there?
@robinince Loving the new series of the Infinite Monkey Cage so far! #bbc
Updated a clothes shop and a restaurant; Deleted a dentist; Confirmed an estate_agent shop, a dentist, and 2 other objects
📷 Low Thames
🚩 Ranelagh Gardens, London Borough of Hammersmith and Fulham, United Kingdom
Updated a cafe
Created 2 gates; Updated a gate
Updated a restaurant
Updated a restaurant; Confirmed a cafe
📷 Shiny Translucent Mushroom
🚩 De Steeg, Nederland
I walked 1.7km in 18m14s
My personal domain name needs renewal. Gandi is charging £27 for this. I'll be moving it to @beasts, which charge £11, and actively reply to my silly questions here on Mastodon.
I walked 6.9km in 1h9m01s
📷 Public Footpath/Bridleway
🚩 West Parkside, Chelsham, United Kingdom
Update shops and extend of Londis on Harrow Road (and realign some odd alignments of buildings)
Created an interior_decoration shop, an entrance, and an alcohol shop; Updated a caterer, an architect office, and 3 other objects; Confirmed a bakery shop, a gallery, and 2 other objects
I walked 7.2km in 1h33m59s
I walked 1.2km in 10m20s





Shortlink
This article has a short URL available: https://drck.me/clicolor-9cr