Say we get the current columns and rows of a terminal with node.js:
console.log('rows:', process.stdout.rows);
console.log('columns:', process.stdout.columns);
is there a way to calculate the number of bytes that can fit in the terminal window? I mean I would guess that it's rows*columns, but I really have no idea.
My guess is that rows*columns
is the max number of bytes that can fit, but in reality, it's probably less, it wouldn't be exact.
The maximum number depends on the nominal size of the window (rows times columns) as well as the way the character cells are encoded. The node application assumes everything is encoded as UTF-8, so that means each cell could be 4 bytes (see this answer for example).
Besides that, you have to allow for a newline at the end of each row (unless you're relying upon line-wrapping the whole time). A newline is a single byte.
So...
(1 + columns) * rows * 4
as a first approximation.
If you take combining characters into account, that could increase the estimate, but (see this answer) the limit on that is not well defined. In practice, those are rarely used in European characters, but are used in some Asian characters (ymmv).