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:
- How can that line adapt to its new width?
- (bonus points) How can I center text in the middle of that line?
An example of the issue is shown below:
/**
* Example.ts
*/
import * as blessed from 'blessed';
const screen = blessed.screen({
smartCSR: true,
title: 'Chatr',
dockBorders: true
});
const chatBox = blessed.box({
parent: screen,
title: 'Chatbox',
top: 'top',
left: 'center',
height: '100%',
width: '100%',
border: {
type: 'line'
},
});
screen.append(chatBox);
screen.render();
chatBox.pushLine("This is the first line");
// This is the separator - and will not resize with the terminal
chatBox.pushLine("_".repeat(chatBox.width as number - 2));
chatBox.pushLine("This is a second line");
screen.render();
When the code is run ts-node ./Example.js it renders this:
┌────────────────────────────────────────────────────────────────────────────────────────┐
│This is a line │
│________________________________________________________________________________________│
│This is a second line │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└────────────────────────────────────────────────────────────────────────────────────────┘
Resizing the terminal gets this result:
┌──────────────────────────────────────────────────────────┐
│This is a line │
│__________________________________________________________│
│______________________________ │
│This is a second line │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
│ │
└──────────────────────────────────────────────────────────┘