Make the terminal go to new line if the width of the term is greater than the art width

This commit is contained in:
Nathan Byrd 2023-08-25 14:24:47 -05:00
parent 0b157ddd1b
commit ccaaa71e23
2 changed files with 16 additions and 16 deletions

View File

@ -37,6 +37,10 @@ function ANSIEscapeParser(options) {
this.mciReplaceChar = miscUtil.valueWithDefault(options.mciReplaceChar, ''); this.mciReplaceChar = miscUtil.valueWithDefault(options.mciReplaceChar, '');
this.termHeight = miscUtil.valueWithDefault(options.termHeight, 25); this.termHeight = miscUtil.valueWithDefault(options.termHeight, 25);
this.termWidth = miscUtil.valueWithDefault(options.termWidth, 80); this.termWidth = miscUtil.valueWithDefault(options.termWidth, 80);
this.breakWidth = this.termWidth;
if(!(_.isNil(options.artWidth)) && options.artWidth > 0 && options.artWidth < this.breakWidth) {
this.breakWidth = options.artWidth;
}
this.trailingLF = miscUtil.valueWithDefault(options.trailingLF, 'default'); this.trailingLF = miscUtil.valueWithDefault(options.trailingLF, 'default');
this.row = Math.min(options?.startRow ?? 1, this.termHeight); this.row = Math.min(options?.startRow ?? 1, this.termHeight);
@ -90,8 +94,8 @@ function ANSIEscapeParser(options) {
switch (charCode) { switch (charCode) {
case CR: case CR:
self.emit('literal', text.slice(start, pos)); self.emit('literal', text.slice(start, pos + 1));
start = pos; start = pos + 1;
self.column = 1; self.column = 1;
@ -105,8 +109,8 @@ function ANSIEscapeParser(options) {
self.column = 1; self.column = 1;
} }
self.emit('literal', text.slice(start, pos)); self.emit('literal', text.slice(start, pos + 1));
start = pos; start = pos + 1;
self.row += 1; self.row += 1;
@ -114,13 +118,16 @@ function ANSIEscapeParser(options) {
break; break;
default: default:
if (self.column === self.termWidth) { if (self.column === self.breakWidth) {
self.emit('literal', text.slice(start, pos + 1)); self.emit('literal', text.slice(start, pos + 1));
start = pos + 1; start = pos + 1;
// If the art is terminal, then we need to force the terminal to go to the next line.
if(self.column < self.termWidth) {
self.emit('literal', '\r\n');
}
self.column = 1; self.column = 1;
self.row += 1; self.row += 1;
self.positionUpdated(); self.positionUpdated();
} else { } else {
self.column += 1; self.column += 1;
@ -135,7 +142,7 @@ function ANSIEscapeParser(options) {
// //
// Finalize this chunk // Finalize this chunk
// //
if (self.column > self.termWidth) { if (self.column > self.breakWidth) {
self.column = 1; self.column = 1;
self.row += 1; self.row += 1;

View File

@ -280,18 +280,11 @@ function display(client, art, options, cb) {
} }
} }
// Use width from SAUCE if available - if the width is less than the term width,
// we need to set that as the width for the parser so that wide terminals
// display correctly
let parseWidth = getWidthFromSAUCE(options.sauce);
if(_.isNil(parseWidth) || parseWidth > client.term.termWidth) {
parseWidth = client.term.termWidth;
}
const ansiParser = new aep.ANSIEscapeParser({ const ansiParser = new aep.ANSIEscapeParser({
mciReplaceChar: options.mciReplaceChar, mciReplaceChar: options.mciReplaceChar,
termHeight: client.term.termHeight, termHeight: client.term.termHeight,
termWidth: parseWidth, termWidth: client.term.termWidth,
artWidth: getWidthFromSAUCE(options.sauce),
trailingLF: options.trailingLF, trailingLF: options.trailingLF,
startRow: options.startRow, startRow: options.startRow,
}); });