Skip to content

Commit

Permalink
#134: added support for terminal Save/Restore Position command (s/u)
Browse files Browse the repository at this point in the history
  • Loading branch information
bugy committed Apr 7, 2019
1 parent c0c55ca commit 8b258cb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
27 changes: 23 additions & 4 deletions web-src/js/components/terminal/terminal_model.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export class TerminalModel {
this.listeners = [];

this.clear();

this.changedLines = [];
}

addListener(listener) {
Expand Down Expand Up @@ -140,6 +138,10 @@ export class TerminalModel {

this.currentStyle = null;

this.changedLines = [];

this.savedCursorPosition = null;

for (const listener of this.listeners) {
listener.cleared();
}
Expand Down Expand Up @@ -504,6 +506,23 @@ class MoveCursorToPositionHandler extends CommandHandler {
}
}

class SaveCursorPositionHandler extends CommandHandler {
handle(args, terminal) {
terminal.savedCursorPosition = [terminal.currentLine, terminal.currentPosition];
}
}

class RestoreCursorPositionHandler extends CommandHandler {
handle(args, terminal) {
if (isNull(terminal.savedCursorPosition)) {
console.log('WARN! trying to restore cursor position, but nothing is saved');
return;
}
const [line, position] = terminal.savedCursorPosition;
terminal.setCursorPosition(line, position);
}
}

class ClearLineHandler extends CommandHandler {
constructor() {
super();
Expand Down Expand Up @@ -534,8 +553,8 @@ COMMAND_HANDLERS.set('A', new MoveCursorVerticallyHandler(true));
COMMAND_HANDLERS.set('B', new MoveCursorVerticallyHandler(false));
COMMAND_HANDLERS.set('C', new MoveCursorHorizontallyHandler(true));
COMMAND_HANDLERS.set('D', new MoveCursorHorizontallyHandler(false));
COMMAND_HANDLERS.set('s', null);
COMMAND_HANDLERS.set('u', null);
COMMAND_HANDLERS.set('s', new SaveCursorPositionHandler());
COMMAND_HANDLERS.set('u', new RestoreCursorPositionHandler());

const TEXT_COLOR_DICT = new Map();
TEXT_COLOR_DICT.set(39, null);
Expand Down
49 changes: 48 additions & 1 deletion web-src/tests/terminal_model_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ import {
moveCursorLeft,
moveCursorRight,
moveCursorUp,
moveToPosition
moveToPosition,
restorePosition,
savePosition
} from './terminal_test_utils';

chaiConfig.truncateThreshold = 0;
Expand Down Expand Up @@ -970,4 +972,49 @@ describe('Test terminal model', function () {
assert.deepEqual([[0, 1, 2], [2]], this.changedLines);
});
});

describe('Save/restore cursor position', function () {
beforeEach(function () {
sinon.stub(console, 'log').returns(void 0);
});

afterEach(function () {
console.log.restore();
});

it('Test restore in the first line', function () {
this.model.write('1234' + savePosition() + '5678' + restorePosition() + 'abc');

assert.deepEqual(['1234abc8'], this.model.lines);
});

it('Test restore in the first line, when 3 lines', function () {
this.model.write('123' + savePosition() + '45\n678\n90' + restorePosition() + 'abc');

assert.deepEqual(['123abc', '678', '90'], this.model.lines);
});

it('Test restore in the last line, when 3 lines', function () {
this.model.write('123\n4567\n8' + savePosition() + '90' + moveCursorUp(2) + 'X'
+ restorePosition() + 'abc');

assert.deepEqual(['123X', '4567', '8abc'], this.model.lines);
});

it('Test restore position without save', function () {
this.model.write('1234' + restorePosition() + 'abc');

assert.deepEqual(['1234abc'], this.model.lines);
expect(console.log.args[0][0]).to.equal('WARN! trying to restore cursor position, but nothing is saved');
});

it('Test restore position after clear', function () {
this.model.write('1234' + savePosition());
this.model.clear();
this.model.write('abc' + restorePosition() + 'XYZ');

assert.deepEqual(['abcXYZ'], this.model.lines);
expect(console.log.args[0][0]).to.equal('WARN! trying to restore cursor position, but nothing is saved');
});
})
});
8 changes: 8 additions & 0 deletions web-src/tests/terminal_test_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export function moveToPosition(line, column, command = 'H') {
return escapePrefix + line + ';' + column + command;
}

export function savePosition() {
return escapePrefix + 's';
}

export function restorePosition() {
return escapePrefix + 'u';
}

export function clearLineToRight() {
return clearLine('');
}
Expand Down

0 comments on commit 8b258cb

Please sign in to comment.