Skip to content

Commit

Permalink
Fix height truncation when using styled text (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Apr 20, 2022
1 parent 7342453 commit 81b1229
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
5 changes: 3 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ansiEscapes from 'ansi-escapes';
import cliCursor from 'cli-cursor';
import wrapAnsi from 'wrap-ansi';
import sliceAnsi from 'slice-ansi';
import stripAnsi from 'strip-ansi';

const defaultTerminalHeight = 24;

Expand All @@ -27,8 +28,8 @@ const fitToTerminalHeight = (stream, text) => {

return sliceAnsi(
text,
lines.slice(0, toRemove).join('\n').length + 1,
text.length);
stripAnsi(lines.slice(0, toRemove).join('\n')).length + 1,
);
};

export function createLogUpdate(stream, {showCursor = false} = {}) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"ansi-escapes": "^5.0.0",
"cli-cursor": "^4.0.0",
"slice-ansi": "^5.0.0",
"strip-ansi": "^7.0.1",
"wrap-ansi": "^8.0.1"
},
"devDependencies": {
Expand Down
20 changes: 20 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {createLogUpdate} from './index.js';

const setup = options => {
const terminal = new Terminal(options);
terminal.rows = options.rows;
terminal.columns = options.columns;
terminal.state.setMode('crlf', true);
const log = createLogUpdate(terminal);
return {terminal, log};
Expand Down Expand Up @@ -42,6 +44,24 @@ test('output beyond terminal height', t => {
t.is(terminal.state.getLine(2).str, '');
});

test('output beyond terminal height with color', t => {
const {terminal, log} = setup({rows: 3, columns: 20});

log('\u001B[32m√\u001B[39mline 1\nline 2\nline 3');
t.is(terminal.state.getLine(0).str, 'line 2');
t.is(terminal.state.getLine(1).str, 'line 3');
t.is(terminal.state.getLine(2).str, '');
});

test('output beyond terminal height with multi-line color', t => {
const {terminal, log} = setup({rows: 3, columns: 20});

log('\u001B[32m√line 1\nline 2\nline 3\u001B[39m');
t.is(terminal.state.getLine(0).str, 'line 2');
t.is(terminal.state.getLine(1).str, 'line 3');
t.is(terminal.state.getLine(2).str, '');
});

test('growing output', t => {
const {terminal, log} = setup({rows: 4, columns: 20});

Expand Down

0 comments on commit 81b1229

Please sign in to comment.