John Davidson

How to get terminal size in Windows 10 (with PHP)


I know that it's possible to get the terminal size on Linux using the following :

$cols = `tput cols`;
$lines = `tput lines`;

But on windows, the only solution I found is the following: using the command mode con. The problem with this is that I can't even parse it because it's translated and depending on the language of the OS.

So my question is simple: how to get terminal size on windows programmatically with PHP?

My Answer:
You can use the `shell_exec` function in PHP to run a command in the Windows command prompt and capture the output. Here is an example of how you can get the terminal size in Windows using PHP:

$output = shell_exec('mode');
$lines = explode("\n", $output);
$cols = null;
$lines = array_filter($lines, function($line) use (&$cols) {
if (strpos($line, 'Columns') !== false) {
$cols = explode(':', $line)[1];
return true;
return false;

if ($cols) {
echo "Columns: " . trim($cols) . PHP_EOL;
} else {
echo "Unable to get terminal size." . PHP_EOL;

This code will run the `mode` command in the Windows command prompt, which will output information about the terminal size. It then parses the output to extract the number of columns in the terminal. You can modify this code to also extract the number of lines if needed.

Rate this post

3 of 5 based on 1368 votes


© 2024 - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us