-1

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.

Alexander Mills
  • 90,741
  • 139
  • 482
  • 817
  • FYI bytes ≠ characters. The number of bytes here is irrelevant. You would be able to fit the same number of `Ω` characters as you would `O` characters, even though the number of bytes is different. – Patrick Roberts Jul 13 '19 at 21:50
  • What problem are you really trying to solve? – jfriend00 Jul 13 '19 at 22:56
  • I want to know how many bytes to read from a file to display, it's sort of an optimization. – Alexander Mills Jul 13 '19 at 23:03
  • @PatrickRoberts that's right, that's why I am talking about max/min not exact, I think we can figure this out – Alexander Mills Jul 13 '19 at 23:04
  • For example, say we have 10 rows and 10 columns, so that's 100 characters max that can fit? That means that the *minimum* number of bytes that can be displayed is 100 also. Correct? Maybe there is no way to determine the maximum number of bytes. – Alexander Mills Jul 13 '19 at 23:05

1 Answers1

1

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).

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
  • this assumes that columns and rows represent the number of characters that can fit right? I think that's correct, but I am personally not sure – Alexander Mills Jul 16 '19 at 23:46