Skip to content
Advertisement

Drawing a line that’s always as wide as its parent BoxElement?

I’m using a BoxElement from blessed to display a chat history.

Sentences are added using pushLine. For clarity, days are divided by lines (another string added using pushLine). Each line is as wide as the parent BoxElement.

If the TUI is resized however, the line no longer fits.

I have 2 questions:

  1. How can that line adapt to its new width?
  2. (bonus points) How can I center text in the middle of that line?

An example of the issue is shown below:

JavaScript

When the code is run ts-node ./Example.js it renders this:

JavaScript

Resizing the terminal gets this result:

JavaScript

Advertisement

Answer

It seems blessed doesn’t implement something like a separator, but we can simply implement them by ourselves with a simple class which stores the line index of each separator and change them on resize event. Something like:

JavaScript

About the bonus point, now it should be quite easy to achieve it; the hard part is to center a line longer than the box width: if we split it in more lines to center, all the line indexes (next to split centered line) will changes and could become harder to keep track of them.

A possible compromise could be to accept to center only lines shorter than box width, left padding them with the right amount of spaces.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement